Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Generate JavaScript or TypeScript code from Swagger/OpenAPI specifications
Generate JavaScript or TypeScript code from Swagger/OpenAPI specification.
npm install openapi
# or
yarn add openapi
# or to global space
yarn openapi --file ../src/mocks/local-file-api.json
# or
yarn openapi --file ../src/mocks/local-file-api.yaml
# or
yarn openapi --file protocol://url/api.json
yarn openapi [options]
Options:
-V, --version output the version number
--output-dir <path> Path output directory js api with types (default: './api')
--config <path> Path to config
--mode <type> Mode for additional info: 'prod' | 'dev' (default: 'prod')
--file <path> Path to file with api (*.json, *.yaml, url)
--authorization <value> Auth token for get api by url (it is header for request)
--deprecated <type> Action for deprecated methods: 'warning' | 'ignore' | 'exception' (default: 'warning')
--import-request Import request code in out code
--original-body Build with original request body
--ignore-description Print description of request
-h, --help display help for command
This package use cosmiconfig
for find config.
module.exports = {
// Path to file with api (*.json, *.yaml, url)
file: "./swagger-api.json", // string
// Api in json (if not use option 'file', more important than path to file)
apiJson: { ... },
// Auth token for get api by url (it is header for request)
authorization: "Token 123qwerty", // string
// Path output directory js api with types
outputDir: "./api", // string (default: "./api")
// Mode for additional info
mode: "prod", // "prod" | "dev" (default: "prod")
// Action for deprecated methods
deprecated: "warning", // "warning" | "ignore" | "exception" (default: "warning")
// Import request code in out code
// true — add import from `openapi/request`
// false — embed request to `outputDir` and import from it
// "disabled" — completely disable imporing `request`, use `templateCodeBefore`
importRequest: true, // (default: false)
// Build with original request body
originalBody: true, // (default: false)
// Ignore description of requests
ignoreDescription: true, // default: false
// Completely disable generating types file (.d.ts)
disableTypesGenerate: true, // (default: false)
/**
* Change file name for source code
* Also it can be a function
* @example
* templateFileNameCode: ({ swaggerData, changeCase }) => string,
*/
templateFileNameCode: 'my-api.js', // (default: 'index.js')
/**
* Change file name for typing
* Also it can be a function
* @example
* templateFileNameTypes: ({ swaggerData, changeCase }) => string,
*/
templateFileNameTypes: 'my-api.d.ts', // (default: 'index.d.js')
/**
* Load presets and merge local properties to it
* If preset created as a function, options can be passed
* @example
* presets: [
* ['my-super-openapi-preset', { passed: 'options' }],
* ['another-openapi-preset', { beautiful: 'options' }],
* ]
* If no options passed or used simple form, empty object passed to functional preset
*/
presets: ['my-super-openapi-preset'], // (default: [])
/**
* Template before main block code
* @param {{
* swaggerData: { info: object; paths: object; components: object; };
* changeCase: { paramCase: Function; camelCase: Function; pascalCase: Function; ... };
* }} extra
*/
templateCodeBefore: (extra) => "",
/**
* Template request code
* @param {{
* name: string;
* method: string;
* url: string;
* isWarningDeprecated: boolean;
* isExistParams: boolean;
* defaultParams: object;
* }} params
* @param {{
* swaggerData: { info: object; paths: object; components: object; };
* requestSwaggerData: { operationId: string; requestBody?: object; responses: object };
* changeCase: { paramCase: Function; camelCase: Function; pascalCase: Function; ... };
* }} extra
*/
templateRequestCode: (params, extra) => "",
/**
* Template after maon block code
* @param {{
* swaggerData: { info: object; paths: object; components: object; };
* changeCase: { paramCase: Function; camelCase: Function; pascalCase: Function; ... };
* }} extra
*/
templateCodeAfter: (extra) => "",
/**
* Template before main block types
* @param {{
* swaggerData: { info: object; paths: object; components: object; };
* changeCase: { paramCase: Function; camelCase: Function; pascalCase: Function; ... };
* }} extra
*/
templateTypesBefore: (extra) => "",
/**
* Template request types
* @param {{
* name: string;
* summary: string;
* description: string;
* countVariants: number;
* index: number;
* params: SwaggerData | null;
* addedParams: SwaggerData | null;
* result: SwaggerData | null;
* }} params
* * @param {{
* swaggerData: { info: object; paths: object; components: object; };
* requestSwaggerData: { operationId: string; requestBody?: object; responses: object };
* changeCase: { paramCase: Function; camelCase: Function; pascalCase: Function; ... };
* }} extra
*
* @type {https://swagger.io/docs/specification/data-models/} SwaggerData
*/
templateRequestTypes: (param, extra) => "",
/**
* Template after main block types
* @param {{
* swaggerData: { info: object; paths: object; components: object; };
* changeCase: { paramCase: Function; camelCase: Function; pascalCase: Function; ... };
* }} extra
*/
templateTypesAfter: (extra) => "",
};
import { openapiGenerate } from "openapi";
const { code, types } = openapiGenerate({
file: "./swagger-api.json",
});
console.log(code);
// => js code
console.log(types);
// => typescript types
npm init
there)openapi-preset-
prefix (ex.: openapi-preset-effector
)index.js
and set "main": "index.js"
in your package.jsonindex.js
with any properties from list beforepresets: ['openapi-preset-example']
Hint: if you want to use local file as a preset, just use
require.resolve
:presets: [require.resolve('./local-preset')]
It is works only in.js
configs
module.exports = (options) => ({
templateRequestCode: (request, extra) =>
options.parseBody
? generatorWithParser(request, extra)
: simpleGenerator(request, extra),
});
Usage openapi.config.js
:
module.exports = {
file: "./swagger-api.json",
presets: [
["openapi-preset-example", { parseBody: true }],
[
"openapi-preset-another",
{ requestImport: { module: "./axios-fabric", name: "axios" } },
],
],
};
nullable
yarn test && yarn test-pack
npm version 1.2.3
(use the version from a draft release)npm publish
git push origin master --tags
FAQs
Generate JavaScript or TypeScript code from Swagger/OpenAPI specifications
The npm package openapi receives a total of 4,436 weekly downloads. As such, openapi popularity was classified as popular.
We found that openapi demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.