From afa8718dbf28329c29ee14ef40b89bb5c38dd674 Mon Sep 17 00:00:00 2001 From: Simon Donaldson Date: Thu, 29 Jan 2026 17:46:37 +0000 Subject: [PATCH 1/6] feat: add head-template flag to overwrite the generated headTemplate --- README.md | 29 +++++++++++++++++++++++++++++ bin/cli.js | 7 +++++++ src/writer.js | 18 ++++++++++++++++-- 3 files changed, 52 insertions(+), 2 deletions(-) mode change 100644 => 100755 bin/cli.js diff --git a/README.md b/README.md index 7856538..78bebd9 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). Use multiple times if needed. Format: key=value | +| --cjs | | boolean | false | Generate a CommonJS file | +| --remove-prefix | -r | string | | Remove prefix string from every endpoint | +| --head-template | | string | | Path to a local header-template file to use instead of the built-in one | + +#### 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 header template: +```bash +$ openapi-box ./openapi.json --head-template ./my-header-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..26ac664 --- 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 header-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 --header-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} */ From 314685ae4bfa4d5c05fafed659a73023445035fa Mon Sep 17 00:00:00 2001 From: Simon Donaldson Date: Thu, 29 Jan 2026 17:48:52 +0000 Subject: [PATCH 2/6] Update cli.js --- bin/cli.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/cli.js b/bin/cli.js index 26ac664..b7e8fb4 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -15,7 +15,7 @@ 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 header-template file + --head-template Path to a local head-template file Examples $ openapi-box ./openapi.json From 0c123adf1c62b1429b34fbf347e0547ab2d293ad Mon Sep 17 00:00:00 2001 From: Simon Donaldson Date: Thu, 29 Jan 2026 17:49:17 +0000 Subject: [PATCH 3/6] Update cli.js --- bin/cli.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/cli.js b/bin/cli.js index b7e8fb4..e69349f 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -21,7 +21,7 @@ const cli = meow(` $ 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 --header-template ./my-header-template.js + $ openapi-box ./openapi.json --head-template ./my-header-template.js `, { importMeta: import.meta, allowUnknownFlags: false, From 6e812672dd6df0468f1dc48a7acee5e8a46331ec Mon Sep 17 00:00:00 2001 From: Simon Donaldson Date: Thu, 29 Jan 2026 17:50:36 +0000 Subject: [PATCH 4/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 78bebd9..a8471b9 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ The `openapi-box` CLI supports the following flags: | --header | -h | string[] | | Send headers to the request (for remote input). Use multiple times if needed. Format: key=value | | --cjs | | boolean | false | Generate a CommonJS file | | --remove-prefix | -r | string | | Remove prefix string from every endpoint | -| --head-template | | string | | Path to a local header-template file to use instead of the built-in one | +| --head-template | | string | | Path to a local head-template file to use instead of the built-in one | #### Examples From 39545553a5b7e5598075c14329d36e6150695cb8 Mon Sep 17 00:00:00 2001 From: Simon Donaldson Date: Thu, 29 Jan 2026 17:54:45 +0000 Subject: [PATCH 5/6] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a8471b9..75be80f 100644 --- a/README.md +++ b/README.md @@ -26,10 +26,10 @@ 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). Use multiple times if needed. Format: key=value | +| --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 | | string | | Path to a local head-template file to use instead of the built-in one | +| --head-template | -ht | string | | Path to a local head-template file | #### Examples @@ -43,9 +43,9 @@ Generate from a remote URL with a header: $ openapi-box https://api.com/doc.json -h 'Authorization=Bearer secrettoken' ``` -Use a custom header template: +Use a custom head template: ```bash -$ openapi-box ./openapi.json --head-template ./my-header-template.js +$ openapi-box ./openapi.json --head-template ./head-template.js ``` 1. Generate the schema from an OpenApi url (it can a be filepath too): From 2c57af7303a1da739d4c52ecb64092b9dc853847 Mon Sep 17 00:00:00 2001 From: Simon Donaldson Date: Thu, 29 Jan 2026 18:01:55 +0000 Subject: [PATCH 6/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 75be80f..5a07955 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ The `openapi-box` CLI supports the following flags: | --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 head-template file | +| --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