openapi-typescript
Advanced tools
Comparing version 3.0.4 to 3.1.0-alpha.0
@@ -7,3 +7,3 @@ #!/usr/bin/env node | ||
const meow = require("meow"); | ||
const { default: swaggerToTS } = require("../dist-node"); | ||
const { default: swaggerToTS } = require("../dist/cjs/index.js"); | ||
const { loadSpec } = require("./loaders"); | ||
@@ -17,3 +17,4 @@ | ||
--help display this | ||
--output, -o (optional) specify output file (default: stdout) | ||
--output, -o Specify output file (default: stdout) | ||
--auth (optional) Provide an authentication token for private URL | ||
--prettier-config (optional) specify path to Prettier config file | ||
@@ -29,2 +30,5 @@ --raw-schema (optional) Read from raw schema instead of document | ||
}, | ||
auth: { | ||
type: "string", | ||
}, | ||
prettierConfig: { | ||
@@ -60,3 +64,6 @@ type: "string", | ||
try { | ||
spec = await loadSpec(pathToSpec, { log: output !== "STDOUT" }); | ||
spec = await loadSpec(pathToSpec, { | ||
auth: cli.flags.auth, | ||
log: output !== "STDOUT", | ||
}); | ||
} catch (err) { | ||
@@ -63,0 +70,0 @@ process.exitCode = 1; // needed for async functions |
@@ -0,1 +1,2 @@ | ||
const mime = require("mime"); | ||
const yaml = require("js-yaml"); | ||
@@ -7,7 +8,7 @@ const { bold, yellow } = require("kleur"); | ||
async function load(pathToSpec) { | ||
async function load(pathToSpec, { auth }) { | ||
// option 1: remote URL | ||
if (/^https?:\/\//.test(pathToSpec)) { | ||
try { | ||
const rawSpec = await loadFromHttp(pathToSpec); | ||
const rawSpec = await loadFromHttp(pathToSpec, { auth }); | ||
return rawSpec; | ||
@@ -28,31 +29,30 @@ } catch (e) { | ||
function isYamlSpec(rawSpec, pathToSpec) { | ||
return /\.ya?ml$/i.test(pathToSpec) || rawSpec[0] !== "{"; | ||
} | ||
module.exports.loadSpec = async (pathToSpec, { log = true }) => { | ||
async function loadSpec(pathToSpec, { auth, log = true }) { | ||
if (log === true) { | ||
console.log(yellow(`🤞 Loading spec from ${bold(pathToSpec)}…`)); // only log if not writing to stdout | ||
} | ||
const rawSpec = await load(pathToSpec); | ||
try { | ||
if (isYamlSpec(rawSpec, pathToSpec)) { | ||
return yaml.load(rawSpec); | ||
} | ||
} catch (err) { | ||
let message = `The spec under ${pathToSpec} seems to be YAML, but it couldn’t be parsed.`; | ||
const rawSpec = await load(pathToSpec, { auth }); | ||
if (err.message) { | ||
message += `\n${err.message}`; | ||
switch (mime.getType(pathToSpec)) { | ||
case "text/yaml": { | ||
try { | ||
return yaml.load(rawSpec); | ||
} catch (err) { | ||
throw new Error(`YAML: ${err.toString()}`); | ||
} | ||
} | ||
throw new Error(message); | ||
case "application/json": | ||
case "application/json5": { | ||
try { | ||
return JSON.parse(rawSpec); | ||
} catch (err) { | ||
throw new Error(`JSON: ${err.toString()}`); | ||
} | ||
} | ||
default: { | ||
throw new Error(`Unknown format: "${contentType}". Only YAML or JSON supported.`); | ||
} | ||
} | ||
try { | ||
return JSON.parse(rawSpec); | ||
} catch { | ||
throw new Error(`The spec under ${pathToSpec} couldn’t be parsed neither as YAML nor JSON.`); | ||
} | ||
}; | ||
} | ||
exports.loadSpec = loadSpec; |
const fs = require("fs"); | ||
const path = require("path"); | ||
module.exports = (pathToSpec) => { | ||
function loadFromFs(pathToSpec) { | ||
const pathname = path.resolve(process.cwd(), pathToSpec); | ||
@@ -13,2 +13,3 @@ const pathExists = fs.existsSync(pathname); | ||
return fs.readFileSync(pathname, "utf8"); | ||
}; | ||
} | ||
module.exports = loadFromFs; |
const url = require("url"); | ||
const adapters = { | ||
"http:": require("http"), | ||
"https:": require("https"), | ||
}; | ||
function fetchFrom(inputUrl) { | ||
return adapters[url.parse(inputUrl).protocol]; | ||
} | ||
function loadFromHttp(pathToSpec, { auth }) { | ||
const { protocol } = url.parse(pathToSpec); | ||
function buildOptions(pathToSpec) { | ||
const requestUrl = url.parse(pathToSpec); | ||
return { | ||
method: "GET", | ||
hostname: requestUrl.hostname, | ||
port: requestUrl.port, | ||
path: requestUrl.path, | ||
}; | ||
} | ||
if (protocol !== "http:" && protocol !== "https:") { | ||
throw new Error(`Unsupported protocol: "${protocol}". URL must start with "http://" or "https://".`); | ||
} | ||
module.exports = (pathToSpec) => { | ||
const fetch = require(protocol === "https:" ? "https" : "http"); | ||
return new Promise((resolve, reject) => { | ||
const opts = buildOptions(pathToSpec); | ||
const req = fetchFrom(pathToSpec).request(opts, (res) => { | ||
let rawData = ""; | ||
res.setEncoding("utf8"); | ||
res.on("data", (chunk) => { | ||
rawData += chunk; | ||
}); | ||
res.on("end", () => { | ||
resolve(rawData); | ||
}); | ||
}); | ||
const req = fetch.request( | ||
pathToSpec, | ||
{ | ||
method: "GET", | ||
auth, | ||
}, | ||
(res) => { | ||
let rawData = ""; | ||
res.setEncoding("utf8"); | ||
res.on("data", (chunk) => { | ||
rawData += chunk; | ||
}); | ||
res.on("end", () => { | ||
if (res.statusCode >= 200 && res.statusCode < 300) { | ||
resolve(rawData); | ||
} else { | ||
reject(rawData || `${res.statusCode} ${res.statusMessage}`); | ||
} | ||
}); | ||
} | ||
); | ||
req.on("error", (err) => { | ||
@@ -39,2 +38,3 @@ reject(err); | ||
}); | ||
}; | ||
} | ||
module.exports = loadFromHttp; |
{ | ||
"name": "openapi-typescript", | ||
"description": "Generate TypeScript types from Swagger OpenAPI specs", | ||
"version": "3.0.4", | ||
"version": "3.1.0-alpha.0", | ||
"engines": { | ||
"node": ">= 10.0.0" | ||
}, | ||
"author": "drew@pow.rs", | ||
"license": "ISC", | ||
@@ -9,9 +13,20 @@ "bin": { | ||
}, | ||
"main": "./dist/cjs/index.js", | ||
"module": "./dist/esm/index.js", | ||
"exports": { | ||
"browser": "./dist/esm/index.js", | ||
"import": "./dist/esm/index.js", | ||
"require": "./dist/cjs/index.js" | ||
}, | ||
"files": [ | ||
"dist-*/", | ||
"bin/", | ||
"bin/" | ||
"bin", | ||
"dist", | ||
"src", | ||
"types" | ||
], | ||
"pika": true, | ||
"sideEffects": false, | ||
"types": "./types/index.d.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/drwpow/openapi-typescript" | ||
}, | ||
"keywords": [ | ||
@@ -28,9 +43,15 @@ "swagger", | ||
], | ||
"homepage": "https://github.com/drwpow/openapi-typescript#readme", | ||
"bugs": { | ||
"url": "https://github.com/drwpow/openapi-typescript/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/drwpow/openapi-typescript" | ||
"homepage": "https://github.com/drwpow/openapi-typescript#readme", | ||
"scripts": { | ||
"build": "rm -rf dist && tsc --build tsconfig.json && tsc --build tsconfig.cjs.json", | ||
"format": "yarn prettier -w .", | ||
"lint": "eslint --ignore-path .gitignore --ext .js,.ts src", | ||
"pregenerate": "npm run build", | ||
"test": "npm run build && jest --no-cache", | ||
"test:coverage": "npm run build && jest --no-cache --coverage && codecov", | ||
"typecheck": "tsc --noEmit", | ||
"version": "npm run build" | ||
}, | ||
@@ -41,12 +62,8 @@ "dependencies": { | ||
"meow": "^9.0.0", | ||
"mime": "^2.5.2", | ||
"prettier": "^2.2.1" | ||
}, | ||
"devDependencies": { | ||
"@pika/pack": "^0.5.0", | ||
"@pika/plugin-build-node": "^0.9.2", | ||
"@pika/plugin-copy-assets": "^0.9.2", | ||
"@pika/plugin-ts-standard-pkg": "^0.9.2", | ||
"@types/jest": "^26.0.14", | ||
"@types/js-yaml": "^4.0.0", | ||
"@types/prettier": "^2.1.5", | ||
"@typescript-eslint/eslint-plugin": "^4.4.1", | ||
@@ -61,9 +78,3 @@ "@typescript-eslint/parser": "^4.4.1", | ||
"typescript": "^4.1.3" | ||
}, | ||
"engines": { | ||
"node": ">= 10.0.0" | ||
}, | ||
"source": "dist-src/index.js", | ||
"types": "dist-types/index.d.ts", | ||
"main": "dist-node/index.js" | ||
} | ||
} |
@@ -112,2 +112,3 @@ [![version(scoped)](https://img.shields.io/npm/v/openapi-typescript.svg)](https://www.npmjs.com/package/openapi-typescript) | ||
| `--output [location]` | `-o` | (stdout) | Where should the output file be saved? | | ||
| `--auth [token]` | | | (optional) Provide an auth token to be passed along in the request (only if accessing a private schema). | | ||
| `--prettier-config [location]` | | | (optional) Path to your custom Prettier configuration for output | | ||
@@ -114,0 +115,0 @@ | `--raw-schema` | | `false` | Generate TS types from partial schema (e.g. having `components.schema` at the top level) | |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
136178
11
71
1938
0
219
5
1
3
+ Addedmime@^2.5.2
+ Addedmime@2.6.0(transitive)