Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions bin/cli.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ const cli = meow(`
--header, -h Send headers to the request in case <input> 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,
Expand All @@ -43,6 +45,10 @@ const cli = meow(`
type: 'string',
shortFlag: 'r',
},
headTemplate: {
type: 'string',
shortFlag: 'ht',
},
},
})

Expand Down Expand Up @@ -72,6 +78,7 @@ try {
cjs: cli.flags.cjs,
headers,
removePrefix: cli.flags.removePrefix,
headTemplate: cli.flags.headTemplate,
})
} catch (err) {
spinner.fail(err.message)
Expand Down
18 changes: 16 additions & 2 deletions src/writer.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -32,6 +34,7 @@ const createCodeBlockWriter = () => new CodeBlockWriter({
* cjs?: boolean
* headers?: object
* removePrefix?: string
* headTemplate?: string
* }} [opts]
* @returns {Promise<string>}
*/
Expand All @@ -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<string, string>} */
Expand Down