Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

openapi-gen-typescript

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openapi-gen-typescript - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

dist/schemaToTypes/transform.d.ts

5

dist/index.d.ts

@@ -1,8 +0,9 @@

import { OpenAPIObject } from 'openapi3-ts';
import { OpenAPI } from "openapi-types";
export declare function gen(options: {
url?: string;
version: string;
object?: OpenAPIObject;
object?: OpenAPI.Document;
outputDir: string;
fetchModuleFile?: string;
pascalCase?: boolean;
}): Promise<void>;

28

dist/index.js

@@ -29,7 +29,7 @@ "use strict";

const mkdirp = require("mkdirp");
const json_schema_to_typescript_1 = require("json-schema-to-typescript");
const formatter_1 = require("json-schema-to-typescript/dist/src/formatter");
const camelcase = require("camelcase");
const fs = require("fs");
const path = require("path");
const transform_1 = require("./schemaToTypes/transform");
const util_1 = require("./util");
function getCamelcase(urlPath, options) {

@@ -66,6 +66,3 @@ return camelcase(urlPath.split('/').join('_'), options);

responseTypeNames.push(responseTypeName);
return yield json_schema_to_typescript_1.compile(Object.assign(Object.assign({}, content[mediaType].schema), { title: responseTypeName }), responseTypeName, {
bannerComment: `/** ${comment} */`,
$refOptions: { resolve: { file: null, external: true } },
});
return `export type ${responseTypeName} = ${transform_1.transform(content[mediaType].schema)}`;
})))).join('\n');

@@ -76,3 +73,3 @@ });

return __awaiter(this, void 0, void 0, function* () {
const { url, version, object, fetchModuleFile = `${__dirname}/defaultFetch.ts`, outputDir, } = options;
const { url, version, object, fetchModuleFile = `${__dirname}/defaultFetch.ts`, outputDir, pascalCase = true, } = options;
const fetchModuleImportCode = `import fetchImpl from '${path.relative(outputDir, fetchModuleFile).replace(/\.ts$/, '')}';`;

@@ -85,3 +82,3 @@ let openApiData;

});
openApiData = (yield swaggerParser.dereference(openapi.openapi));
openApiData = openapi.openapi || (yield swaggerParser.dereference(openapi.openapi));
}

@@ -106,6 +103,7 @@ else {

schemasCode = (yield Promise.all(Object.keys(schemas).map((schemaKey) => __awaiter(this, void 0, void 0, function* () {
return yield json_schema_to_typescript_1.compile(Object.assign(Object.assign({}, schemas[schemaKey]), { title: schemaKey }), schemaKey, {
bannerComment: '',
unreachableDefinitions: true,
});
const schemaObject = schemas[schemaKey];
if (pascalCase) {
schemaKey = camelcase(schemaKey, { pascalCase: true });
}
return `export type ${schemaKey} = ${transform_1.transform(schemaObject)}`;
})))).join('\n');

@@ -188,3 +186,3 @@ }

.join('\n');
const code = formatter_1.format([
const code = util_1.format([
`/* tslint:disable */

@@ -196,5 +194,5 @@ /**

fetchModuleImportCode,
`export namespace Schema {${schemasCode}}`,
`export namespace components { export namespace schemas { ${schemasCode} } }`,
`export namespace Api { ${pathsCode} }`,
].join('\n'), json_schema_to_typescript_1.DEFAULT_OPTIONS);
].join('\n'));
yield mkdirp(outputDir);

@@ -201,0 +199,0 @@ fs.writeFileSync(`${outputDir}/index.ts`, code);

{
"name": "openapi-gen-typescript",
"version": "0.0.3",
"version": "0.0.4",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "Apache License 2.0",
"homepage": "https://github.com/yunkeCN/openapi-gen-typescript",
"scripts": {

@@ -24,7 +25,5 @@ "prepublish": "npm run compile",

"camelcase": "^6.1.0",
"json-schema-to-typescript": "^9.1.1",
"mkdirp": "^1.0.4",
"openapi3-ts": "^2.0.0",
"swagger2openapi": "^7.0.3"
}
}

@@ -1,2 +0,2 @@

# openapi-ts
# openapi-gen-typescript
Generate typescript code for openapi.

@@ -3,0 +3,0 @@

@@ -6,11 +6,2 @@ // @ts-ignore

import * as mkdirp from 'mkdirp';
import { compile, DEFAULT_OPTIONS } from 'json-schema-to-typescript'
import { format } from 'json-schema-to-typescript/dist/src/formatter'
import {
BaseParameterObject, ContentObject,
MediaTypeObject,
OpenAPIObject,
OperationObject,
ParameterObject, RequestBodyObject
} from 'openapi3-ts';
import * as camelcase from 'camelcase';

@@ -20,3 +11,18 @@ import { Options } from "camelcase";

import * as path from "path";
import { transform } from "./schemaToTypes/transform";
import { format } from "./util";
import { IJsonSchema, OpenAPI, OpenAPIV3 } from "openapi-types";
import ParameterBaseObject = OpenAPIV3.ParameterBaseObject;
import MediaTypeObject = OpenAPIV3.MediaTypeObject;
import OperationObject = OpenAPIV3.OperationObject;
import PathItemObject = OpenAPIV3.PathItemObject;
import ResponseObject = OpenAPIV3.ResponseObject;
import ReferenceObject = OpenAPIV3.ReferenceObject;
import ParameterObject = OpenAPIV3.ParameterObject;
import RequestBodyObject = OpenAPIV3.RequestBodyObject;
type ContentObject = {
[media: string]: MediaTypeObject;
}
function getCamelcase(urlPath: string, options?: Options): string {

@@ -26,3 +32,3 @@ return camelcase(urlPath.split('/').join('_'), options);

function getCodeFromParameter(parameter: BaseParameterObject, name: string): string {
function getCodeFromParameter(parameter: ParameterBaseObject, name: string): string {
const { description, required } = parameter;

@@ -42,3 +48,3 @@

interface ParameterMap {
[name: string]: BaseParameterObject;
[name: string]: ParameterBaseObject;
}

@@ -74,13 +80,3 @@

responseTypeNames.push(responseTypeName);
return await compile(
{
...(content[mediaType] as MediaTypeObject).schema,
title: responseTypeName,
},
responseTypeName,
{
bannerComment: `/** ${comment} */`,
$refOptions: { resolve: { file: null, external: true } as any },
},
);
return `export type ${responseTypeName} = ${transform((content[mediaType] as MediaTypeObject).schema as IJsonSchema)}`
}))).join('\n');

@@ -92,3 +88,3 @@ }

version: string;
object?: OpenAPIObject;
object?: OpenAPI.Document;
// dir of output files

@@ -98,2 +94,3 @@ outputDir: string;

fetchModuleFile?: string;
pascalCase?: boolean;
}) {

@@ -106,2 +103,3 @@ const {

outputDir,
pascalCase = true,
} = options;

@@ -111,3 +109,3 @@

let openApiData: OpenAPIObject;
let openApiData: OpenAPIV3.Document;
if (url) {

@@ -118,5 +116,5 @@ if (version === '2') {

});
openApiData = await swaggerParser.dereference(openapi.openapi) as OpenAPIObject;
openApiData = openapi.openapi || await swaggerParser.dereference(openapi.openapi);
} else {
openApiData = await swaggerParser.parse(url) as OpenAPIObject;
openApiData = await swaggerParser.parse(url) as OpenAPIV3.Document;
}

@@ -126,3 +124,3 @@ } else if (!object) {

} else {
openApiData = object;
openApiData = object as OpenAPIV3.Document;
}

@@ -139,6 +137,7 @@

schemasCode = (await Promise.all(Object.keys(schemas).map(async (schemaKey) => {
return await compile({ ...schemas[schemaKey], title: schemaKey }, schemaKey, {
bannerComment: '',
unreachableDefinitions: true,
});
const schemaObject = schemas[schemaKey] as IJsonSchema;
if (pascalCase) {
schemaKey = camelcase(schemaKey, { pascalCase: true });
}
return `export type ${schemaKey} = ${transform(schemaObject)}`;
}))).join('\n');

@@ -151,6 +150,6 @@ }

.map(async (urlPath) => {
const pathsObject = paths[urlPath];
return (await Promise.all(methods.filter(method => !!pathsObject[method])
const pathsObject: PathItemObject = paths[urlPath];
return (await Promise.all(methods.filter(method => !!(pathsObject as any)[method])
.map(async (method) => {
const objectElement: OperationObject = pathsObject[method];
const objectElement: OperationObject = (pathsObject as any)[method] as OperationObject;
const {

@@ -165,6 +164,6 @@ operationId,

const responseTypeNames: string[] = [];
const responsesCode: string = (await Promise.all(Object.keys(responses)
const responsesCode: string = (await Promise.all(Object.keys(responses as Object)
.filter(key => key !== 'default')
.map(async (statusCode) => {
const responsesObjectElement = responses[statusCode];
const responsesObjectElement: ResponseObject & ReferenceObject = (responses as any)[statusCode];
const { $ref, content, description } = responsesObjectElement;

@@ -178,3 +177,8 @@

const typeNamePrefix = `Response${camelcase(statusCode, { pascalCase: true })}`;
const responseCode = await getCodeFromContent(content, typeNamePrefix, description, responseTypeNames);
const responseCode = await getCodeFromContent(
content as ContentObject,
typeNamePrefix,
description,
responseTypeNames,
);

@@ -244,5 +248,5 @@ return responseCode;

fetchModuleImportCode,
`export namespace Schema {${schemasCode}}`,
`export namespace components { export namespace schemas { ${schemasCode} } }`,
`export namespace Api { ${pathsCode} }`,
].join('\n'), DEFAULT_OPTIONS);
].join('\n'));

@@ -249,0 +253,0 @@ await mkdirp(outputDir);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc