npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@flexbase/openapi-generator

v3.0.12

Published

Open API code generator

Downloads

301

Readme

openapi-generator

Coverage Quality Gate Status

OpenAPI code generator.

Getting started

yarn add @flexbase/openapi-generator --dev

or

npm i @flexbase/openapi-generator -D

Usage

openapi-generator -i <openapispec>.yaml -o . -t <template>.hbs

| Option | Argument | Description | | ------------------------ | ---------------------------------------------------------- | -------------------------------------------------------------------- | | --include | glob | Specifies the glob pattern for files to parse | | --sharedTemplates | glob | Specifies the glob pattern for shared templates | | --config | file | Specify a configuration to use. Defaults to .openapigenerator.json | | --no-prettier | | Disable prettier | | --no-skipempty | | Generate empty files | | --no-tags | | Disable organization by tags | | -d or --debug [path] | | Output the internal representation |

Configuration

Example configuration

{
  "include": ["./tests/data/*.yaml"],
  "sharedTemplates": ["./templates/server/*.hbs"],
  "generate": {
    "router.ts": {
      "target": "./output/{api}/router.ts",
      "template": "./templates/server/router.hbs"
    },
    "index": {
      "target": "./output/{api}/{name}/generated/{name}.ts",
      "template": "./templates/server/index.hbs"
    },
    "routes": {
      "target": "./output/{api}/{name}/generated/{name}.routes.ts",
      "template": "./templates/server/routes.hbs"
    },
    "models": {
      "target": "./output/{api}/{name}/generated/{name}.models.ts",
      "template": "./templates/server/models.hbs"
    },
    "validations": {
      "target": "./output/{api}/{name}/generated/{name}.validations.ts",
      "template": "./templates/server/validations.hbs"
    }
  },
  "prettier": true,
  "tags": true,
  "debugPath": "./output/debug/{api}/"
}

Templates

Below are some example handlebars templates to generate a typescript output file

models.hbs template

https://github.com/flexbase-eng/openapi-generator/blob/main/templates/server/models.hbs

model.declaration.hbs partial template

https://github.com/flexbase-eng/openapi-generator/blob/main/templates/server/model.declaration.hbs

model.expression.hbs partial template

https://github.com/flexbase-eng/openapi-generator/blob/main/templates/server/model.expression.hbs

Example

Generating the petstore openapi spec by running the following:

openapi-generator

or

yarn start

will generate code under ./output/swagger-petstore-openapi-3-0/

Output {output}/pet/generated/pet.routes.ts

/* eslint-disable */
/* ------------------------------------------------------------
 File auto-generated

 @summary pet
 @description Everything about your Pets
 @version 1.0.17
------------------------------------------------------------ */

import * as middleware from '@middleware/index.js';
import * as utilities from '@shared/utilities/openapi.utilities.js';
import * as models from './pet.models.js';
import * as validators from './pet.validations.js';
import * as handlers from '@modules/swagger-petstore-openapi-3-0/routes/pet/pet.handlers.js';
import { Swagger_Petstore_OpenAPI_3_0Router } from '@modules/swagger-petstore-openapi-3-0/routes/router.js';

/**
 * @summary Update an existing pet
 * @description Update an existing pet by Id
 * @remarks required scopes: []
 */
Swagger_Petstore_OpenAPI_3_0Router.put<object, object, models.PetModel | models.PetModel | models.PetModel>(
  '/pet',
  middleware.routerAuthMiddleware([]),
  middleware.bodyParserMiddleware(['application/json', 'application/xml', 'application/x-www-form-urlencoded']),
  async (ctx, next) => {
    let body;
    if (ctx.request.type === 'application/json') {
      body = utilities.handleValidation<models.PetModel>(ctx.body, validators.PetModelValidator, utilities.handleRequestValidatorErrors);
    } else if (ctx.request.type === 'application/xml') {
      body = utilities.handleValidation<models.PetModel>(ctx.body, validators.PetModelValidator, utilities.handleRequestValidatorErrors);
    } else {
      body = utilities.handleValidation<models.PetModel>(ctx.body, validators.PetModelValidator, utilities.handleRequestValidatorErrors);
    }
    const response: models.updatePetResponse = await handlers.updatePet(ctx, body);

    utilities.serializeResponse(ctx, response, ['application/xml', 'application/json']);

    await next();
  },
);

/**
 * @summary Add a new pet to the store
 * @description Add a new pet to the store
 * @remarks required scopes: []
 */
Swagger_Petstore_OpenAPI_3_0Router.post<object, object, models.PetModel | models.PetModel | models.PetModel>(
  '/pet',
  middleware.routerAuthMiddleware([]),
  middleware.bodyParserMiddleware(['application/json', 'application/xml', 'application/x-www-form-urlencoded']),
  async (ctx, next) => {
    let body;
    if (ctx.request.type === 'application/json') {
      body = utilities.handleValidation<models.PetModel>(ctx.body, validators.PetModelValidator, utilities.handleRequestValidatorErrors);
    } else if (ctx.request.type === 'application/xml') {
      body = utilities.handleValidation<models.PetModel>(ctx.body, validators.PetModelValidator, utilities.handleRequestValidatorErrors);
    } else {
      body = utilities.handleValidation<models.PetModel>(ctx.body, validators.PetModelValidator, utilities.handleRequestValidatorErrors);
    }
    const response: models.addPetResponse = await handlers.addPet(ctx, body);

    utilities.serializeResponse(ctx, response, ['application/xml', 'application/json']);

    await next();
  },
);

/**
 * @summary Find pet by ID
 * @description Returns a single pet
 * @remarks required scopes: []
 */
Swagger_Petstore_OpenAPI_3_0Router.get<object, object>(
  '/pet/:petId',
  middleware.routerAuthMiddleware([]),
  middleware.bodyParserMiddleware([]),
  async (ctx, next) => {
    const params = utilities.handleValidation<models.getPetByIdPathParameter>(
      ctx.params,
      validators.getPetByIdPathParameterValidator,
      utilities.handleRequestValidatorErrors,
    );

    const response: models.getPetByIdResponse = await handlers.getPetById(ctx, params);

    utilities.serializeResponse(ctx, response, ['application/xml', 'application/json']);

    await next();
  },
);

/**
 * @summary Updates a pet in the store with form data
 * @remarks required scopes: []
 */
Swagger_Petstore_OpenAPI_3_0Router.post<object, object>(
  '/pet/:petId',
  middleware.routerAuthMiddleware([]),
  middleware.bodyParserMiddleware([]),
  async (ctx, next) => {
    const params = utilities.handleValidation<models.updatePetWithFormPathParameter>(
      ctx.params,
      validators.updatePetWithFormPathParameterValidator,
      utilities.handleRequestValidatorErrors,
    );

    const query = utilities.handleValidation<models.updatePetWithFormQueryParameter>(
      ctx.request.query,
      validators.updatePetWithFormQueryParameterValidator,
      utilities.handleRequestValidatorErrors,
    );

    const response: models.updatePetWithFormResponse = await handlers.updatePetWithForm(ctx, params, query);

    utilities.serializeResponse(ctx, response, []);

    await next();
  },
);

/**
 * @summary Deletes a pet
 * @remarks required scopes: []
 */
Swagger_Petstore_OpenAPI_3_0Router.delete<object, object>(
  '/pet/:petId',
  middleware.routerAuthMiddleware([]),
  middleware.bodyParserMiddleware([]),
  async (ctx, next) => {
    const params = utilities.handleValidation<models.deletePetPathParameter>(
      ctx.params,
      validators.deletePetPathParameterValidator,
      utilities.handleRequestValidatorErrors,
    );

    const headers = utilities.handleValidation<models.deletePetHeaderParameter>(
      ctx.request.headers,
      validators.deletePetHeaderParameterValidator,
      utilities.handleRequestValidatorErrors,
    );
    const response: models.deletePetResponse = await handlers.deletePet(ctx, params, headers);

    utilities.serializeResponse(ctx, response, []);

    await next();
  },
);

/**
 * @summary uploads an image
 * @remarks required scopes: []
 */
Swagger_Petstore_OpenAPI_3_0Router.post<object, object, models.uploadFileRequestObject>(
  '/pet/:petId/uploadImage',
  middleware.routerAuthMiddleware([]),
  middleware.bodyParserMiddleware(['application/octet-stream']),
  async (ctx, next) => {
    const body = utilities.handleValidation<models.uploadFileRequestObject>(
      ctx.body,
      validators.uploadFileRequestObjectValidator,
      utilities.handleRequestValidatorErrors,
    );

    const params = utilities.handleValidation<models.uploadFilePathParameter>(
      ctx.params,
      validators.uploadFilePathParameterValidator,
      utilities.handleRequestValidatorErrors,
    );

    const query = utilities.handleValidation<models.uploadFileQueryParameter>(
      ctx.request.query,
      validators.uploadFileQueryParameterValidator,
      utilities.handleRequestValidatorErrors,
    );

    const response: models.uploadFileResponse = await handlers.uploadFile(ctx, body, params, query);

    utilities.serializeResponse(ctx, response, ['application/json']);

    await next();
  },
);

/**
 * @summary Finds Pets by status
 * @description Multiple status values can be provided with comma separated strings
 * @remarks required scopes: []
 */
Swagger_Petstore_OpenAPI_3_0Router.get<object, object>(
  '/pet/findByStatus',
  middleware.routerAuthMiddleware([]),
  middleware.bodyParserMiddleware([]),
  async (ctx, next) => {
    const query = utilities.handleValidation<models.findPetsByStatusQueryParameter>(
      ctx.request.query,
      validators.findPetsByStatusQueryParameterValidator,
      utilities.handleRequestValidatorErrors,
    );

    const response: models.findPetsByStatusResponse = await handlers.findPetsByStatus(ctx, query);

    utilities.serializeResponse(ctx, response, ['application/xml', 'application/json']);

    await next();
  },
);

/**
 * @summary Finds Pets by tags
 * @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
 * @remarks required scopes: []
 */
Swagger_Petstore_OpenAPI_3_0Router.get<object, object>(
  '/pet/findByTags',
  middleware.routerAuthMiddleware([]),
  middleware.bodyParserMiddleware([]),
  async (ctx, next) => {
    const query = utilities.handleValidation<models.findPetsByTagsQueryParameter>(
      ctx.request.query,
      validators.findPetsByTagsQueryParameterValidator,
      utilities.handleRequestValidatorErrors,
    );

    const response: models.findPetsByTagsResponse = await handlers.findPetsByTags(ctx, query);

    utilities.serializeResponse(ctx, response, ['application/xml', 'application/json']);

    await next();
  },
);