From 840b94fb6ee316953eea07cbb20402102260554f Mon Sep 17 00:00:00 2001 From: Swain Molster Date: Wed, 1 Jun 2022 12:28:34 -0400 Subject: [PATCH] fix!: remove schema + client publishing utils --- README.md | 71 ------------------ src/bin/__snapshots__/cli.test.ts.snap | 28 ++++---- src/bin/cli.ts | 48 ------------- src/generate-publishable-client.test.ts | 85 ---------------------- src/generate-publishable-client.ts | 49 ------------- src/generate-publishable-schema.test.ts | 96 ------------------------- src/generate-publishable-schema.ts | 28 -------- src/meta-schema.ts | 12 ---- src/types.ts | 6 -- 9 files changed, 12 insertions(+), 411 deletions(-) delete mode 100644 src/generate-publishable-client.test.ts delete mode 100644 src/generate-publishable-client.ts delete mode 100644 src/generate-publishable-schema.test.ts delete mode 100644 src/generate-publishable-schema.ts diff --git a/README.md b/README.md index f347810..8ed59a1 100644 --- a/README.md +++ b/README.md @@ -187,77 +187,6 @@ const server = new Koa() .listen(); ``` -### Distributing Schemas - -Use the `generate-publishable-schema` command in concert with the `Meta.PackageJSON` entry to generate a ready-to-publish NPM artifact containing the schema. - -```yaml -# schema.yml -Meta: - PackageJSON: - name: desired-package-name - description: A description of the package - # ... any other desired package.json values -# ... -``` - -```bash -one-schema generate-publishable-schema \ - --schema schema.yml \ - --output output-directory -``` - -The `output-directory` will have this file structure: - -``` -output-directory/ - package.json - schema.json - schema.yaml -``` - -### Distributing Clients - -Use the `generate-publishable-client` command in concert with the `Meta.PackageJSON` entry to generate a ready-to-publish NPM artifact containing a ready-to-use client. - -```yaml -# schema.yml -Meta: - PackageJSON: - name: desired-package-name - description: A description of the package - # ... any other desired package.json values -# ... -``` - -```bash -one-schema generate-publishable-client \ - --schema schema.yml \ - --output output-directory -``` - -The `output-directory` will have this file structure: - -``` -output-directory/ - package.json - schema.json - schema.yaml - index.js - index.d.ts -``` - -The generated client code is identical to the output of `generate-axios-client`: - -```typescript -import axios from 'axios'; -import { Client } from './output-directory'; - -const client = new Client(axios.create(...)) - -// use the client -``` - ### OpenAPI Spec generation Use the `generate-open-api-spec` command to generate an OpenAPI spec from a simple schema, which may be useful for interfacing with common OpenAPI tooling. diff --git a/src/bin/__snapshots__/cli.test.ts.snap b/src/bin/__snapshots__/cli.test.ts.snap index 303bf19..95b7ffe 100644 --- a/src/bin/__snapshots__/cli.test.ts.snap +++ b/src/bin/__snapshots__/cli.test.ts.snap @@ -4,14 +4,12 @@ exports[`input validation snapshots bogus command name 1`] = ` "cli.ts Commands: - cli.ts generate-axios-client Generates an Axios client using the - specified schema and options. - cli.ts generate-api-types Generates API types using the specified - schema and options. - cli.ts generate-open-api-spec Generates an OpenAPI v3.1.0 spec using the - specified schema and options. - cli.ts generate-publishable-schema Generates a publishable schema artifact. - cli.ts generate-publishable-client Generates a publishable client artifact. + cli.ts generate-axios-client Generates an Axios client using the specified + schema and options. + cli.ts generate-api-types Generates API types using the specified schema + and options. + cli.ts generate-open-api-spec Generates an OpenAPI v3.1.0 spec using the + specified schema and options. Options: --help Show help [boolean] @@ -24,14 +22,12 @@ exports[`input validation snapshots empty input 1`] = ` "cli.ts Commands: - cli.ts generate-axios-client Generates an Axios client using the - specified schema and options. - cli.ts generate-api-types Generates API types using the specified - schema and options. - cli.ts generate-open-api-spec Generates an OpenAPI v3.1.0 spec using the - specified schema and options. - cli.ts generate-publishable-schema Generates a publishable schema artifact. - cli.ts generate-publishable-client Generates a publishable client artifact. + cli.ts generate-axios-client Generates an Axios client using the specified + schema and options. + cli.ts generate-api-types Generates API types using the specified schema + and options. + cli.ts generate-open-api-spec Generates an OpenAPI v3.1.0 spec using the + specified schema and options. Options: --help Show help [boolean] diff --git a/src/bin/cli.ts b/src/bin/cli.ts index 252a359..38c849e 100644 --- a/src/bin/cli.ts +++ b/src/bin/cli.ts @@ -9,8 +9,6 @@ import { generateAxiosClient } from '../generate-axios-client'; import { generateAPITypes } from '../generate-api-types'; import { loadSchemaFromFile, SchemaAssumptions } from '../meta-schema'; import { toOpenAPISpec } from '../openapi'; -import { generatePublishableSchema } from '../generate-publishable-schema'; -import { generatePublishableClient } from '../generate-publishable-client'; const getPrettierParser = (outputFilename: string): BuiltInParserName => { const extension = path.extname(outputFilename).replace('.', ''); @@ -186,52 +184,6 @@ const program = yargs(process.argv.slice(2)) writeGeneratedFile(argv.output, output, { format: argv.format }); }, ) - .command( - 'generate-publishable-schema', - 'Generates a publishable schema artifact.', - getCommonOptions, - (argv) => { - const spec = loadSchemaFromFile( - argv.schema, - parseAssumptions(argv.assumptions), - ); - - const { files } = generatePublishableSchema({ spec }); - - for (const [filename, content] of Object.entries(files)) { - writeGeneratedFile(path.resolve(argv.output, filename), content, { - format: argv.format, - }); - } - }, - ) - .command( - 'generate-publishable-client', - 'Generates a publishable client artifact.', - (y) => - getCommonOptions(y).option('className', { - type: 'string', - description: 'The name of the generated client class.', - default: 'Client', - }), - async (argv) => { - const spec = loadSchemaFromFile( - argv.schema, - parseAssumptions(argv.assumptions), - ); - - const { files } = await generatePublishableClient({ - spec, - outputClass: argv.className, - }); - - for (const [filename, content] of Object.entries(files)) { - writeGeneratedFile(path.resolve(argv.output, filename), content, { - format: argv.format, - }); - } - }, - ) .demandCommand() .strict(); diff --git a/src/generate-publishable-client.test.ts b/src/generate-publishable-client.test.ts deleted file mode 100644 index 266cd2c..0000000 --- a/src/generate-publishable-client.test.ts +++ /dev/null @@ -1,85 +0,0 @@ -import { generateAxiosClient } from './generate-axios-client'; -import { generatePublishableClient } from './generate-publishable-client'; -import { generatePublishableSchema } from './generate-publishable-schema'; - -test('skips generating a package.json if there is no PackageJSON entry', async () => { - const result = await generatePublishableClient({ - outputClass: 'Client', - 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', async () => { - const 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: {}, - }, - }, - }; - const result = await generatePublishableClient({ - outputClass: 'Client', - spec, - }); - - expect(Object.keys(result.files)).toStrictEqual([ - 'schema.json', - 'schema.yaml', - 'package.json', - 'index.js', - 'index.d.ts', - ]); - - const schemaArtifact = generatePublishableSchema({ spec }); - - expect(result.files['schema.json']).toStrictEqual( - schemaArtifact.files['schema.json'], - ); - - expect(result.files['schema.yaml']).toStrictEqual( - schemaArtifact.files['schema.yaml'], - ); - - const client = await generateAxiosClient({ outputClass: 'Client', spec }); - - expect(result.files['index.js']).toStrictEqual(client.javascript); - - expect(result.files['index.d.ts']).toStrictEqual(client.declaration); - - expect(result.files['package.json']).toStrictEqual( - ` -{ - "name": "@lifeomic/test-service-schema", - "description": "The OneSchema for a test-service", - "testObject": { - "some": "value" - }, - "main": "index.js", - "peerDependencies": { - "axios": "*" - } -} -`.trim(), - ); -}); diff --git a/src/generate-publishable-client.ts b/src/generate-publishable-client.ts deleted file mode 100644 index 4d78aea..0000000 --- a/src/generate-publishable-client.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { OneSchemaDefinition } from '.'; -import { generateAxiosClient } from './generate-axios-client'; -import { generatePublishableSchema } from './generate-publishable-schema'; - -export type GeneratePublishableClientInput = { - spec: OneSchemaDefinition; - outputClass: string; -}; - -export type GeneratePublishableClientOutput = { - /** - * A map of filename -> file content to generate. - */ - files: Record; -}; - -export const generatePublishableClient = async ({ - spec, - outputClass, -}: GeneratePublishableClientInput): Promise => { - const { files: baseFiles } = generatePublishableSchema({ spec }); - - const { declaration, javascript } = await generateAxiosClient({ - spec, - outputClass, - }); - - const files: Record = { - ...baseFiles, - 'index.js': javascript, - 'index.d.ts': declaration, - }; - - if (files['package.json']) { - files['package.json'] = JSON.stringify( - { - ...JSON.parse(files['package.json']), - main: 'index.js', - peerDependencies: { - axios: '*', - }, - }, - null, - 2, - ); - } - - return { files }; -}; diff --git a/src/generate-publishable-schema.test.ts b/src/generate-publishable-schema.test.ts deleted file mode 100644 index cf3c9a6..0000000 --- a/src/generate-publishable-schema.test.ts +++ /dev/null @@ -1,96 +0,0 @@ -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(), - ); -}); diff --git a/src/generate-publishable-schema.ts b/src/generate-publishable-schema.ts deleted file mode 100644 index 979b1de..0000000 --- a/src/generate-publishable-schema.ts +++ /dev/null @@ -1,28 +0,0 @@ -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; -}; - -export const generatePublishableSchema = ({ - spec, -}: GeneratePublishableSchemaInput): GeneratePublishableSchemaOutput => { - const files: Record = { - '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 }; -}; diff --git a/src/meta-schema.ts b/src/meta-schema.ts index ff0bf1c..46c53d4 100644 --- a/src/meta-schema.ts +++ b/src/meta-schema.ts @@ -14,22 +14,10 @@ export const getPathParams = (name: string) => .map((part) => part.replace(':', '')); const ONE_SCHEMA_META_SCHEMA: JSONSchema4 = { - definitions: { - MetaConfig: { - type: 'object', - additionalProperties: false, - properties: { - PackageJSON: { type: 'object' }, - }, - }, - }, type: 'object', additionalProperties: false, required: ['Endpoints'], properties: { - Meta: { - $ref: '#/definitions/MetaConfig', - }, Resources: { type: 'object', patternProperties: { diff --git a/src/types.ts b/src/types.ts index 04ccfb9..4326043 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,13 +6,7 @@ export type EndpointDefinition = { Response: JSONSchema4; }; -export type OneSchemaMetaDefinition = { - PackageJSON?: Record; -}; - export type OneSchemaDefinition = { - Meta?: OneSchemaMetaDefinition; - Resources?: { [key: string]: JSONSchema4; };