3.0.4 • Published 7 days ago

@flexbase/openapi-generator v3.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
7 days ago

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
OptionArgumentDescription
--includeglobSpecifies the glob pattern for files to parse
--sharedTemplatesglobSpecifies the glob pattern for shared templates
--configfileSpecify a configuration to use. Defaults to .openapigenerator.json
--no-prettierDisable prettier
--no-skipemptyGenerate empty files
--no-tagsDisable 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();
  },
);
3.0.4

7 days ago

3.0.3

10 days ago

3.0.2

11 days ago

3.0.1

15 days ago

3.0.0

23 days ago

2.3.2-beta.15

23 days ago

2.3.2-beta.17

23 days ago

2.3.2-beta.18

23 days ago

2.3.2

8 months ago

2.3.3

8 months ago

1.0.0

1 year ago

2.2.0

12 months ago

2.0.0

1 year ago

1.1.0

1 year ago

0.50.0-beta.1

1 year ago

2.3.0

12 months ago

2.1.2

12 months ago

2.1.1

1 year ago

2.3.1

12 months ago

2.1.0

1 year ago

0.50.0

1 year ago

0.48.0

1 year ago

0.46.0

1 year ago

0.49.0

1 year ago

0.47.0

1 year ago

0.43.0

1 year ago

0.41.0

1 year ago

0.38.0

1 year ago

0.36.0

1 year ago

0.34.0

1 year ago

0.32.0

1 year ago

0.30.0

1 year ago

0.29.0

1 year ago

0.44.0

1 year ago

0.42.0

1 year ago

0.40.0

1 year ago

0.37.0

1 year ago

0.35.0

1 year ago

0.33.0

1 year ago

0.31.0

1 year ago

0.45.0

1 year ago

0.28.0

1 year ago

0.27.0

1 year ago

0.26.0

1 year ago

0.25.0

1 year ago

0.24.0

1 year ago

0.23.0

1 year ago

0.22.0

1 year ago

0.21.0

1 year ago

0.20.0

1 year ago

0.19.0

1 year ago

0.18.0

1 year ago

0.17.0

1 year ago

0.15.0

1 year ago

0.14.0-beta.4

1 year ago

0.14.0-beta.3

1 year ago

0.14.0-beta.1

1 year ago

0.14.0

1 year ago

0.13.0

1 year ago

0.12.0

1 year ago

0.11.0

1 year ago

0.10.0

1 year ago

0.9.0

1 year ago

0.8.0

1 year ago

0.7.0

1 year ago

0.6.0

1 year ago

0.5.0

1 year ago

0.4.0

1 year ago

0.3.0

1 year ago

0.2.0

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago