diff --git a/README.md b/README.md index 7856538..5a07955 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,35 @@ $ npm install @geut/openapi-box @sinclair/typebox ## Usage +### CLI Flags + +The `openapi-box` CLI supports the following flags: + +| Flag | Short | Type | Default | Description | +|---------------------|-------|----------|----------------|-----------------------------------------------------------------------------| +| --output | -o | string | schema.js | Output filename | +| --header | -h | string[] | | Send headers to the request (for remote input). | +| --cjs | | boolean | false | Generate a CommonJS file | +| --remove-prefix | -r | string | | Remove prefix string from every endpoint | +| --head-template | -ht | string | | Path to a local file to override the [head-template](https://github.com/geut/openapi-box/blob/ed1258d928c90f35239bcdecb5cac180e8ccca85/src/head-template.js) | + +#### Examples + +Generate from a local file: +```bash +$ openapi-box ./openapi.json +``` + +Generate from a remote URL with a header: +```bash +$ openapi-box https://api.com/doc.json -h 'Authorization=Bearer secrettoken' +``` + +Use a custom head template: +```bash +$ openapi-box ./openapi.json --head-template ./head-template.js +``` + 1. Generate the schema from an OpenApi url (it can a be filepath too): ```bash $ openapi-box https://petstore3.swagger.io/api/v3/openapi.json diff --git a/bin/cli.js b/bin/cli.js old mode 100644 new mode 100755 index 86060e2..e69349f --- a/bin/cli.js +++ b/bin/cli.js @@ -15,11 +15,13 @@ const cli = meow(` --header, -h Send headers to the request in case is remote --cjs Generate a commonjs file --remove-prefix -r Remove prefix string from every endpoint + --head-template Path to a local head-template file Examples $ openapi-box ./openapi.json $ openapi-box https://api.com/doc.json $ openapi-box https://api.com/doc.json -h 'Authorization=Bearer secrettoken' + $ openapi-box ./openapi.json --head-template ./my-header-template.js `, { importMeta: import.meta, allowUnknownFlags: false, @@ -43,6 +45,10 @@ const cli = meow(` type: 'string', shortFlag: 'r', }, + headTemplate: { + type: 'string', + shortFlag: 'ht', + }, }, }) @@ -72,6 +78,7 @@ try { cjs: cli.flags.cjs, headers, removePrefix: cli.flags.removePrefix, + headTemplate: cli.flags.headTemplate, }) } catch (err) { spinner.fail(err.message) diff --git a/src/writer.js b/src/writer.js index f00d69e..b634d30 100644 --- a/src/writer.js +++ b/src/writer.js @@ -1,12 +1,14 @@ /** @typedef {import('@apidevtools/json-schema-ref-parser').JSONSchema} JSONSchema */ +import { pathToFileURL } from 'node:url' + import SwaggerParser from '@apidevtools/json-schema-ref-parser' import CodeBlockWriter from 'code-block-writer' import pascalcase from 'pascalcase' import * as prettier from 'prettier' import { cleanupSchema, extractSchemaOptions, kRef } from './cleanup.js' -import headTemplate from './head-template.js' +import defaultHeadTemplate from './head-template.js' import resolver from './resolver.js' const scalarTypes = { @@ -32,6 +34,7 @@ const createCodeBlockWriter = () => new CodeBlockWriter({ * cjs?: boolean * headers?: object * removePrefix?: string + * headTemplate?: string * }} [opts] * @returns {Promise} */ @@ -47,7 +50,18 @@ export const write = async (source, opts = {}) => { const requestSchemas = [] let w = createCodeBlockWriter() - w.writeLine(headTemplate(cjs)) + let headTemplateFn = defaultHeadTemplate + if (opts.headTemplate) { + const headTemplateModule = await import(pathToFileURL(opts.headTemplate).href) + if (typeof headTemplateModule.default === 'function') { + headTemplateFn = headTemplateModule.default + } else { + throw new TypeError('The head-template module must export a default function') + } + } + + w.writeLine(headTemplateFn(cjs)) + w.blankLineIfLastNot() /** @type {Map} */