chanfana icon indicating copy to clipboard operation
chanfana copied to clipboard

Standalone openapi yaml schema generation

Open marceloverdijk opened this issue 1 year ago • 5 comments
trafficstars

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 avatar Apr 08 '24 20:04 marceloverdijk

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 avatar Apr 13 '24 06:04 marceloverdijk

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.

marceloverdijk avatar Apr 13 '24 07:04 marceloverdijk

@marceloverdijk try using https://github.com/privatenumber/tsx instead of ts-node

arjunyel avatar Nov 18 '24 18:11 arjunyel