New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

openapi-to-typescript

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openapi-to-typescript - npm Package Compare versions

Comparing version 2.1.1 to 3.0.0

dist/cli.js

6

dist/compile.js

@@ -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())
})
```
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