-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from lifeomic/publish-schema
[Proposal] Add basic tool for generating a publishable artifact
- Loading branch information
Showing
7 changed files
with
210 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { generatePublishableSchema } from './generate-publishable-schema'; | ||
|
||
test('skips generating a package.json if there is no PackageJSON entry', () => { | ||
const result = generatePublishableSchema({ | ||
spec: { | ||
Endpoints: { | ||
'GET /posts': { | ||
Name: 'listPosts', | ||
Response: {}, | ||
Request: {}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
expect(result.files['package.json']).toBeUndefined(); | ||
}); | ||
|
||
test('generates the correct files when there is a PackageJSON entry', () => { | ||
const result = generatePublishableSchema({ | ||
spec: { | ||
Meta: { | ||
PackageJSON: { | ||
name: '@lifeomic/test-service-schema', | ||
description: 'The OneSchema for a test-service', | ||
testObject: { | ||
some: 'value', | ||
}, | ||
}, | ||
}, | ||
Endpoints: { | ||
'GET /posts': { | ||
Name: 'listPosts', | ||
Response: {}, | ||
Request: {}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
expect(Object.keys(result.files)).toStrictEqual([ | ||
'schema.json', | ||
'schema.yaml', | ||
'package.json', | ||
]); | ||
|
||
expect(result.files['schema.json']).toStrictEqual( | ||
` | ||
{ | ||
"Meta": { | ||
"PackageJSON": { | ||
"name": "@lifeomic/test-service-schema", | ||
"description": "The OneSchema for a test-service", | ||
"testObject": { | ||
"some": "value" | ||
} | ||
} | ||
}, | ||
"Endpoints": { | ||
"GET /posts": { | ||
"Name": "listPosts", | ||
"Response": {}, | ||
"Request": {} | ||
} | ||
} | ||
}`.trim(), | ||
); | ||
|
||
expect(result.files['schema.yaml']).toStrictEqual( | ||
` | ||
Meta: | ||
PackageJSON: | ||
name: '@lifeomic/test-service-schema' | ||
description: The OneSchema for a test-service | ||
testObject: | ||
some: value | ||
Endpoints: | ||
GET /posts: | ||
Name: listPosts | ||
Response: {} | ||
Request: {} | ||
`.trimStart(), | ||
); | ||
|
||
expect(result.files['package.json']).toStrictEqual( | ||
` | ||
{ | ||
"name": "@lifeomic/test-service-schema", | ||
"description": "The OneSchema for a test-service", | ||
"testObject": { | ||
"some": "value" | ||
} | ||
} | ||
`.trim(), | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as jsyaml from 'js-yaml'; | ||
import { OneSchemaDefinition } from '.'; | ||
|
||
export type GeneratePublishableSchemaInput = { | ||
spec: OneSchemaDefinition; | ||
}; | ||
|
||
export type GeneratePublishableSchemaOutput = { | ||
/** | ||
* A map of filename -> file content to generate. | ||
*/ | ||
files: Record<string, string>; | ||
}; | ||
|
||
export const generatePublishableSchema = ({ | ||
spec, | ||
}: GeneratePublishableSchemaInput): GeneratePublishableSchemaOutput => { | ||
const files: Record<string, string> = { | ||
'schema.json': JSON.stringify(spec, null, 2), | ||
'schema.yaml': jsyaml.dump(spec), | ||
}; | ||
|
||
if (spec.Meta?.PackageJSON) { | ||
files['package.json'] = JSON.stringify(spec.Meta.PackageJSON, null, 2); | ||
} | ||
|
||
return { files }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters