Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standalone openapi yaml schema generation #142

Open
marceloverdijk opened this issue Apr 8, 2024 · 2 comments
Open

Standalone openapi yaml schema generation #142

marceloverdijk opened this issue Apr 8, 2024 · 2 comments

Comments

@marceloverdijk
Copy link
Contributor

Is it possible to generate a openapi yaml schema file from the command line? Eg running a npm command? Without the server running.

Rationale:
In development workflow I would like to generate the yaml as it will be used as input for another npm task to generate a openapi client.
During (prod) build I would like to run both to make sure everything is correct.

Second question is whether it is possible to disable the docs endpoint and yaml/json when server is running.

@marceloverdijk
Copy link
Contributor Author

To answer my second question:

const router = OpenAPIRouter({
  docs_url: null,
  redoc_url: null,
  openapi_url: null,
});

See also https://cloudflare.github.io/itty-router-openapi/user-guide/router-options/

@marceloverdijk
Copy link
Contributor Author

For my first question this is interesting: https://cloudflare.github.io/itty-router-openapi/advanced-user-guide/ci-cd-pipelines/

import fs from 'fs'
import { router } from '../src/router'

// Get the Schema from itty-router-openapi
const schema = router.schema

// Optionaly: update the schema with some costumizations for publishing

// Write the final schema
fs.writeFileSync('./public-api.json', JSON.stringify(schema, null, 2))

I tried that like with generate-openapi.ts in my project root containing:

import fs from 'fs';
import { router } from './functions/api/_middleware';

// Get the Schema from itty-router-openapi
const schema = router.schema;

// Write the final schema
fs.writeFileSync('./public-api.json', JSON.stringify(schema, null, 2));

and running it using a script defined in my package.lock like:
"generate-openapi": "ts-node ./generate-openapi.ts",

but it gives:

node:13239) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Users/marceloverdijk/workspace/my-project/generate-openapi.ts:1
import fs from 'fs';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:128:18)
    at wrapSafe (node:internal/modules/cjs/loader:1280:20)
    at Module._compile (node:internal/modules/cjs/loader:1332:27)
    at Module.m._compile (/Users/marceloverdijk/workspace/my-project/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/marceloverdijk/workspace/my-project/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1206:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1022:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at phase4 (/Users/marceloverdijk/workspace/my-project/node_modules/ts-node/src/bin.ts:649:14)

I tried to resolve it change package.json, introducing a special tsconfig for the script, but I had no luck unfortunately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant