cloudflare / chanfana

OpenAPI 3 and 3.1 schema generator and validator for Hono, itty-router and more!
https://chanfana.pages.dev
MIT License
335 stars 42 forks source link

Standalone openapi yaml schema generation #142

Open marceloverdijk opened 7 months ago

marceloverdijk commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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.

arjunyel commented 5 days ago

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