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

Static generation #252

Open
jamesvillarrubia opened this issue Mar 23, 2023 · 1 comment
Open

Static generation #252

jamesvillarrubia opened this issue Mar 23, 2023 · 1 comment
Labels

Comments

@jamesvillarrubia
Copy link

Is there a way to generate the swagger json or yml and statically store it? I want to be able to lock the specs down, so that code must conform to the spec instead of the spec just representing the code in flux.

@Mairu
Copy link
Collaborator

Mairu commented Mar 23, 2023

It is not that hard to create a script to do that. feathers-swagger adds the generated specification as a property to the app object. (The name of the property could be adjusted with the appProperty option)

To add the property to the typescript definitions you have to adjust the declarations.ts:

// The application instance type that will be used everywhere else
export type Application = FeathersApplication<ServiceTypes, Configuration> & {
  docs?: Record<string, unknown>;
};

Then you can create a script / entrypoint in the src directory, for example generate-swagger.ts

import fs from 'node:fs';
import path from 'node:path';
import { app } from './app';
import { logger } from './logger';

app.setup().then(() => {
  fs.writeFileSync(path.join(__dirname, '..', 'swagger.json'), JSON.stringify(app.docs));
  logger.info('Wrote swagger.json');
});

You can then run that script with: npx ts-node src/generate-swagger.ts

If you prefer yaml output, you can of course also add a yaml writer package and write the object as yaml.

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

No branches or pull requests

2 participants