Skip to content

webpro/openapi-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

openapi-parser

The OpenAPi v3 parser extracted from openapi-typescript-codegen. So first of all, all credits go to Ferdi Koomen for his work on this great library. Only the parser is needed to render the models, services and hooks with custom templates, hence this stripped down version.

Installation

npm install -D openapi-parser

API

import { readFile, writeFile } from 'fs/promises';
import { parse } from 'openapi-parser';
import { renderModel, renderService } from './templates';

const specs = JSON.parse(await readFile('swagger.json', 'utf8'));

const { version, server, models, services } = parse(specs);

models.forEach(model => writeFile(`${model.name}.ts`, renderModel(model)));
services.forEach(service =>
  writeFile(`${service.name}.ts`, renderService(service))
);

Example render function

This could be anything from Handlebars templates to template literals. Here's a minimal example:

import { Operation, Service } from 'openapi-parser';

const renderOperation = (operation: Operation): string => {
  const [result] = operation.results;
  return `
/**
 * ${operation.summary}
 * ${operation.description}
 * @returns ${result.type} ${result.description}
 ${operation.deprecated ? '* @deprecated' : ''}
 */
export function ${operation.name}([...]): Promise<${result.type}> {
  return request({
    method: '${operation.method}',
    path: \`${operation.path}\`,
    [...]
  });
}`;
};

export default (service: Service) => {
  return services.operations.map(renderOperation).join(';');
};

About

OpenAPI v3 parser

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published