From 57c818a07c88a9c90cfb83e055a28269a41d06a5 Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 26 Feb 2022 16:32:58 +0000 Subject: [PATCH] Create a concept for addition of a path without injecting too many new behaviours --- .gitignore | 3 ++- _templates/compeller/add/path.ejs.t | 22 +++++++++++++++++++++ _templates/compeller/add/prompt.js | 21 ++++++++++++++++++++ _templates/compeller/add/schema.ejs.t | 10 ++++++++++ _templates/compeller/new/spec.ejs.t | 17 ++-------------- _templates/compeller/new/version-path.ejs.t | 21 ++++++++++++++++++++ _templates/compeller/new/version.ejs.t | 2 +- 7 files changed, 79 insertions(+), 17 deletions(-) create mode 100644 _templates/compeller/add/path.ejs.t create mode 100644 _templates/compeller/add/prompt.js create mode 100644 _templates/compeller/add/schema.ejs.t create mode 100644 _templates/compeller/new/version-path.ejs.t diff --git a/.gitignore b/.gitignore index db5f764..68c3e18 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ node_modules - +coverage dist +src/openapi *.tgz diff --git a/_templates/compeller/add/path.ejs.t b/_templates/compeller/add/path.ejs.t new file mode 100644 index 0000000..32347db --- /dev/null +++ b/_templates/compeller/add/path.ejs.t @@ -0,0 +1,22 @@ +--- +to: <%= directory %>/openapi/paths/<%= h.inflection.dasherize(h.inflection.underscore( name )) %>.path.ts +--- +import { <%= h.inflection.classify( name )%>Schema } from '../components/schemas/<%= h.inflection.dasherize(h.inflection.underscore( name )) %>.schema' + +export const path = { + '<%= path %>': { + <%= method %>: { + responses: { + 200: { + description: 'Get the current API version', + content: { + 'application/json': { + schema: <%= h.inflection.classify( name )%>Schema, + }, + }, + }, + }, + }, + }, +}; + diff --git a/_templates/compeller/add/prompt.js b/_templates/compeller/add/prompt.js new file mode 100644 index 0000000..4524f1e --- /dev/null +++ b/_templates/compeller/add/prompt.js @@ -0,0 +1,21 @@ +// see types of prompts: +// https://github.com/enquirer/enquirer/tree/master/examples +// +module.exports = [ + { + type: 'input', + name: 'directory', + message: 'Where to store the openapi?', + initial: 'src', + }, + { + type: 'input', + name: 'path', + message: 'Fully qualified path e.g. /v1/user', + }, + { + type: 'select', + name: 'method', + choices: ['get', 'put', 'post', 'delete'], + }, +]; diff --git a/_templates/compeller/add/schema.ejs.t b/_templates/compeller/add/schema.ejs.t new file mode 100644 index 0000000..8466734 --- /dev/null +++ b/_templates/compeller/add/schema.ejs.t @@ -0,0 +1,10 @@ +--- +to: <%= directory %>/openapi/components/schemas/<%= h.inflection.dasherize(h.inflection.underscore( name )) %>.schema.ts +unless_exists: true +--- +import { FromSchema } from 'json-schema-to-ts'; + +export const <%= h.inflection.classify( name )%>Schema = {} as const; + +export type <%= h.inflection.classify( name )%> = FromSchemaSchema>; + diff --git a/_templates/compeller/new/spec.ejs.t b/_templates/compeller/new/spec.ejs.t index dfaa8bc..5c724f0 100644 --- a/_templates/compeller/new/spec.ejs.t +++ b/_templates/compeller/new/spec.ejs.t @@ -1,7 +1,7 @@ --- to: <%= directory %>/openapi/spec.ts --- -import { VersionSchema } from './schemas/version.schema'; +import { versionPath } from './paths/version.path'; export const OpenAPISpecification = { info: { @@ -10,19 +10,6 @@ export const OpenAPISpecification = { }, openapi: '3.1.0', paths: { - 'v1/version': { - get: { - responses: { - '200': { - description: 'Get the current API version', - content: { - 'application/json': { - schema: VersionSchema, - }, - }, - }, - }, - }, - }, + ...versionPath }, }; diff --git a/_templates/compeller/new/version-path.ejs.t b/_templates/compeller/new/version-path.ejs.t new file mode 100644 index 0000000..5958099 --- /dev/null +++ b/_templates/compeller/new/version-path.ejs.t @@ -0,0 +1,21 @@ +--- +to: <%= directory %>/openapi/paths/version.path.ts +--- +import { VersionSchema } from '../components/schemas/version.schema'; + +export const versionPath = { + 'v1/version': { + get: { + responses: { + '200': { + description: 'Get the current API version', + content: { + 'application/json': { + schema: VersionSchema, + }, + }, + }, + }, + }, + }, +} diff --git a/_templates/compeller/new/version.ejs.t b/_templates/compeller/new/version.ejs.t index 469bb68..a1fe23b 100644 --- a/_templates/compeller/new/version.ejs.t +++ b/_templates/compeller/new/version.ejs.t @@ -1,5 +1,5 @@ --- -to: <%= directory %>/openapi/schemas/version.schema.ts +to: <%= directory %>/openapi/components/schemas/version.schema.ts --- import { FromSchema } from 'json-schema-to-ts';