openapi-to-typescript
Advanced tools
Comparing version 2.1.1 to 3.0.0
@@ -18,6 +18,6 @@ "use strict"; | ||
const magicReader = (def, cb) => { | ||
const interfaceName = getSchemaNameByRef(def.url); | ||
const interfaceName = exports.getSchemaNameByRef(def.url); | ||
cb(null, { type: 'string', enum: [`$magic$${interfaceName}`] }); | ||
}; | ||
const getSchemaNameByRef = (url, scheme = 'internal') => { | ||
exports.getSchemaNameByRef = (url, scheme = 'internal') => { | ||
const objPath = url.substr(`${scheme}:/`.length).split('/'); | ||
@@ -30,3 +30,3 @@ const interfaceName = objPath[objPath.length - 1]; | ||
if (schema.$ref) | ||
return getSchemaNameByRef(schema.$ref); | ||
return exports.getSchemaNameByRef(schema.$ref); | ||
else | ||
@@ -33,0 +33,0 @@ switch (schema.type) { |
@@ -11,2 +11,4 @@ "use strict"; | ||
]; | ||
var fetch_1 = require("./fetch"); | ||
exports.FetchClientFormatter = fetch_1.FetchClientFormatter; | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const lodash_1 = require("lodash"); | ||
const formatter_1 = require("../formatter"); | ||
@@ -9,12 +10,17 @@ const compile_1 = require("../compile"); | ||
const parameters = this.operation.route.parameters || []; | ||
return parameters.length ? { | ||
return { | ||
[typeName]: await this.toTypescriptInterface(typeName, parameters) | ||
} : {}; | ||
}; | ||
} | ||
async toTypescriptInterface(typeName, parameters) { | ||
return [ | ||
`export interface ${typeName} {`, | ||
...parameters.map(param => ` ${param.name}: ${compile_1.getSchemaName(param.schema, param.name)}`), | ||
`}` | ||
].join('\n'); | ||
const requestSchema = lodash_1.get(this.operation.route, 'requestBody.content["application/json"].schema'); | ||
const aliasedType = requestSchema && requestSchema.$ref && compile_1.getSchemaNameByRef(requestSchema.$ref); | ||
if (aliasedType) | ||
return `export type ${typeName} = ${aliasedType}`; | ||
else | ||
return [ | ||
`export interface ${typeName} {`, | ||
...parameters.map(param => ` ${param.name}: ${compile_1.getSchemaName(param.schema, param.name)}`), | ||
`}` | ||
].join('\n'); | ||
} | ||
@@ -21,0 +27,0 @@ } |
@@ -21,3 +21,3 @@ "use strict"; | ||
compile_1.compileSchema(schema, typeName) : | ||
`export interface ${typeName} { /* unknown */ }`; | ||
`export type ${typeName} = any`; | ||
} | ||
@@ -24,0 +24,0 @@ getResponseSchemaDefinitions() { |
@@ -9,6 +9,7 @@ "use strict"; | ||
const formatters_1 = require("./formatters"); | ||
exports.GenerateTypings = async (parsedOpenAPISchema) => { | ||
const schemas = lodash_1.merge({}, parsedOpenAPISchema.components.schemas); | ||
const paths = lodash_1.merge({}, parsedOpenAPISchema.paths); | ||
exports.GenerateTypings = async (apiSchema, { operationFormatters = [] } = {}) => { | ||
const schemas = lodash_1.merge({}, apiSchema.components.schemas); | ||
const paths = lodash_1.merge({}, apiSchema.paths); | ||
const typeStore = new store_1.Store(); | ||
const clientStore = new store_1.Store(); | ||
new refs_1.InternalRefRewriter().rewrite(schemas); | ||
@@ -19,12 +20,26 @@ new refs_1.InternalRefRewriter().rewrite(paths); | ||
} | ||
const formatters = [...formatters_1.defaultOperationFormatters, ...operationFormatters]; | ||
for (let formatter of formatters) { | ||
if (typeof formatter.renderBoilerplate === 'function') { | ||
clientStore.assign({ | ||
[formatters.indexOf(formatter)]: await formatter.renderBoilerplate(apiSchema) | ||
}); | ||
} | ||
} | ||
for (const pathName of Object.keys(paths)) { | ||
for (const method of Object.keys(paths[pathName])) { | ||
const operation = new operation_1.Operation(paths[pathName][method], { pathName, method }); | ||
for (const Formatter of formatters_1.defaultOperationFormatters) { | ||
typeStore.assign(await new Formatter(operation).render()); | ||
for (const OperationFormatter of formatters) { | ||
const formatter = new OperationFormatter(operation); | ||
typeStore.assign(await formatter.render()); | ||
if (typeof formatter.renderAction === 'function') | ||
clientStore.assign(await formatter.renderAction()); | ||
} | ||
} | ||
} | ||
return typeStore; | ||
return { | ||
typeStore, | ||
clientStore, | ||
}; | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -9,2 +9,3 @@ "use strict"; | ||
this.name = lodash_1.upperFirst(lodash_1.camelCase(route.operationId || `${method} ${pathName}`)); | ||
this.method = method; | ||
} | ||
@@ -11,0 +12,0 @@ } |
{ | ||
"name": "openapi-to-typescript", | ||
"version": "2.1.1", | ||
"version": "3.0.0", | ||
"description": "Generate TypeScript typings based on an OpenAPI schema object.", | ||
@@ -9,2 +9,5 @@ "main": "dist/index.js", | ||
], | ||
"repository": { | ||
"url": "https://github.com/ifroz/openapi-to-typescript" | ||
}, | ||
"author": "László Szűcs <ifrozen@gmail.com>", | ||
@@ -14,7 +17,10 @@ "license": "MIT", | ||
"build": "rimraf dist && tsc", | ||
"commit": "git-cz", | ||
"test": "jest" | ||
}, | ||
"dependencies": { | ||
"@types/yargs": "^12.0.9", | ||
"json-schema-to-typescript": "^6.1.0", | ||
"lodash": "^4.17.11" | ||
"lodash": "^4.17.11", | ||
"yargs": "^13.2.1" | ||
}, | ||
@@ -25,2 +31,3 @@ "devDependencies": { | ||
"@types/node": "^11.9.5", | ||
"commitizen": "^3.0.7", | ||
"cz-conventional-changelog": "^2.1.0", | ||
@@ -27,0 +34,0 @@ "execa": "^1.0.0", |
@@ -12,16 +12,19 @@ # openapi-to-typescript | ||
`npm install openapi-to-typescript` | ||
Run `npm install openapi-to-typescript` or `yarn add openapi-to-typescript` | ||
`yarn add openapi-to-typescript` | ||
# Usage in javascript | ||
# Usage | ||
```javascript | ||
const { GenerateTypings } = require('openapi-to-typescript') | ||
const generatedTypescript = await GenerateTypings(openapiSchema) | ||
fs.writeFileSync('out.d.ts', generatedTypescript.toString()) | ||
const { typeStore, clientStore } = await GenerateTypings(openapiSchema) | ||
fs.writeFileSync('out.d.ts', typeStore.toString()) | ||
fs.writeFileSync('out.ts', clientStore.toString()) | ||
``` | ||
# CLI Usage | ||
`yarn ts-node lib/cli --help` | ||
### For development | ||
@@ -32,5 +35,6 @@ | ||
GenerateTypings(require('./fixtures/petstore.json')).then((typeStore) => { | ||
fs.writeFileSync('./dist/out.d.ts', typeStore.toString()) | ||
GenerateTypings(require('./fixtures/petstore.json')).then(({typeStore, clientStore}) => { | ||
fs.writeFileSync('out.d.ts', typeStore.toString()) | ||
fs.writeFileSync('out.ts', clientStore.toString()) | ||
}) | ||
``` |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
17334
14
359
38
4
13
2
+ Added@types/yargs@^12.0.9
+ Addedyargs@^13.2.1
+ Added@types/yargs@12.0.20(transitive)
+ Addedansi-regex@4.1.1(transitive)
+ Addedansi-styles@3.2.1(transitive)
+ Addedcamelcase@5.3.1(transitive)
+ Addedcliui@5.0.0(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addeddecamelize@1.2.0(transitive)
+ Addedemoji-regex@7.0.3(transitive)
+ Addedfind-up@3.0.0(transitive)
+ Addedget-caller-file@2.0.5(transitive)
+ Addedis-fullwidth-code-point@2.0.0(transitive)
+ Addedlocate-path@3.0.0(transitive)
+ Addedp-limit@2.3.0(transitive)
+ Addedp-locate@3.0.0(transitive)
+ Addedp-try@2.2.0(transitive)
+ Addedpath-exists@3.0.0(transitive)
+ Addedrequire-directory@2.1.1(transitive)
+ Addedrequire-main-filename@2.0.0(transitive)
+ Addedset-blocking@2.0.0(transitive)
+ Addedstring-width@3.1.0(transitive)
+ Addedstrip-ansi@5.2.0(transitive)
+ Addedwhich-module@2.0.1(transitive)
+ Addedwrap-ansi@5.1.0(transitive)
+ Addedy18n@4.0.3(transitive)
+ Addedyargs@13.3.2(transitive)
+ Addedyargs-parser@13.1.2(transitive)