openapi-gen-typescript
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -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>; |
@@ -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); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
45796
4
19
902
1
- Removedjson-schema-to-typescript@^9.1.1
- Removedopenapi3-ts@^2.0.0
- Removed@apidevtools/json-schema-ref-parser@9.0.9(transitive)
- Removed@types/json-schema@7.0.15(transitive)
- Removedany-promise@1.3.0(transitive)
- Removedargparse@2.0.1(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedcli-color@2.0.4(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedd@1.0.2(transitive)
- Removedes5-ext@0.10.64(transitive)
- Removedes6-iterator@2.0.3(transitive)
- Removedes6-symbol@3.1.4(transitive)
- Removedes6-weak-map@2.0.3(transitive)
- Removedesniff@2.0.1(transitive)
- Removedevent-emitter@0.3.5(transitive)
- Removedext@1.7.0(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedglob@7.2.3(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-extglob@2.1.1(transitive)
- Removedis-glob@4.0.3(transitive)
- Removedis-promise@2.2.2(transitive)
- Removedjs-yaml@4.1.0(transitive)
- Removedjson-schema-ref-parser@9.0.9(transitive)
- Removedjson-schema-to-typescript@9.1.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedlodash@4.17.21(transitive)
- Removedlru-queue@0.1.0(transitive)
- Removedmemoizee@0.4.17(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmz@2.7.0(transitive)
- Removednext-tick@1.1.0(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedonce@1.4.0(transitive)
- Removedopenapi3-ts@2.0.2(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedprettier@2.8.8(transitive)
- Removedstdin@0.0.1(transitive)
- Removedthenify@3.3.1(transitive)
- Removedthenify-all@1.6.0(transitive)
- Removedtimers-ext@0.1.8(transitive)
- Removedtype@2.7.3(transitive)
- Removedwrappy@1.0.2(transitive)