Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": { "node": "14" }
}
],
"@babel/preset-typescript"
]
}
37 changes: 21 additions & 16 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
{
"parser": "babel-eslint",
"rules": {
"no-unused-vars": [
"error",
{
"vars": "all",
"args": "after-used",
"ignoreRestSiblings": false
"overrides": [
{
"files": ["*.js"],
"parser": "babel-eslint",
"rules": {
"no-unused-vars": [
"warn",
{
"vars": "all",
"args": "after-used",
"ignoreRestSiblings": false
}
]
},
"parserOptions": {
"ecmaVersion": 2017
},
"env": {
"es6": true
}
]
},
"parserOptions": {
"ecmaVersion": 2017
},
"env": {
"es6": true
}
}
]
}
37 changes: 36 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,39 @@ api-multi-file
ttt.js
*/**/code.js
*/**/code.d.ts
examples/**/api
examples/**/apidist
dist
openapi-2.0.0-3.tgz

# Created by https://www.toptal.com/developers/gitignore/api/macos
# Edit at https://www.toptal.com/developers/gitignore?templates=macos

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# End of https://www.toptal.com/developers/gitignore/api/macos
Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
2 changes: 1 addition & 1 deletion examples/api/multi-file-ref-url-v2-yaml/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require("fs");
const path = require("path");

const { openapiGenerate: swaggerToJs } = require("../../../src");
const { openapiGenerate: swaggerToJs } = require("../../../lib");

swaggerToJs({
file: "./src/mocks/petstore-v2-ref-url.yaml",
Expand Down
2 changes: 1 addition & 1 deletion examples/api/multi-file-v2-yaml/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require("fs");
const path = require("path");

const { openapiGenerate: swaggerToJs } = require("../../../src");
const { openapiGenerate: swaggerToJs } = require("../../../lib");

swaggerToJs({
file: "./src/mocks/petstore-v2-multi-file.yaml",
Expand Down
2 changes: 1 addition & 1 deletion examples/api/simple-v2-json/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require("fs");
const path = require("path");

const { openapiGenerate: swaggerToJs } = require("../../../src");
const { openapiGenerate: swaggerToJs } = require("../../../lib");

swaggerToJs({
file: "./src/mocks/petstore-v2.json",
Expand Down
2 changes: 1 addition & 1 deletion examples/api/simple-v2-yaml/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require("fs");
const path = require("path");

const { openapiGenerate: swaggerToJs } = require("../../../src");
const { openapiGenerate: swaggerToJs } = require("../../../lib");

swaggerToJs({
file: "./src/mocks/petstore-v2.yaml",
Expand Down
2 changes: 1 addition & 1 deletion examples/api/simple-v3-json/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require("fs");
const path = require("path");

const { openapiGenerate: swaggerToJs } = require("../../../src");
const { openapiGenerate: swaggerToJs } = require("../../../lib");

swaggerToJs({
file: "./src/mocks/petstore-v3.json",
Expand Down
5 changes: 2 additions & 3 deletions examples/api/use-templates/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const fs = require("fs");
const path = require("path");

const { openapiGenerate: swaggerToJs } = require("../../../src");
const { openapiGenerate: swaggerToJs } = require("../../../lib");

swaggerToJs({
file: "./src/mocks/petstore-v3.json",

templateCodeBefore: () => `import * as ef from "effector";\n\n`,
templateRequestCode: ({ name }) =>
`export function ${name}() {\n return ef.createEffect();\n};`,
templateRequestCode: ({ name }) => `export function ${name}() {\n return ef.createEffect();\n};`,

templateTypesBefore: () => `import * as ef from "effector";\n\n`,
templateRequestTypes: ({ name, index }) =>
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./dist";
6 changes: 0 additions & 6 deletions jsconfig.json

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/common/build-paths.js → lib/common/build-paths.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { isPathException } = require("../common/is-path-exception");
const { buildPathName } = require("../common/build-path-name");
const { isPathException } = require("./is-path-exception");
const { buildPathName } = require("./build-path-name");
const changeCase = require("change-case");

function buildPaths(buildOptions) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions lib/lib/presets/mocks/preset-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = (options = {}, internal) => {
const schemas = [];
console.log(internal);

return {
name: "preset-options",
onSchema(name, schema) {
schema.name = name;
schemas.push(schema);
},
postComponents() {
// console.log("POST COMPONENTS", schemas);
},
build(files) {
const name =
options.fileCase === "paramCase"
? internal.changeCase.paramCase(internal.root().info.title)
: internal.changeCase.camelCase(internal.root().info.title);
files.addFile(`${name}.json`, JSON.stringify(schemas[0].xml, null, 2));
},
};
};
9 changes: 9 additions & 0 deletions lib/lib/presets/mocks/preset-second.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = (options = {}, { changeCase, root } = {}) => {
const { fileName = "readme" } = options;
return {
name: "preset-second",
build(files) {
files.addFile(`${fileName}.md`, `# ${changeCase.sentenceCase(root().info.title)}`);
},
};
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
59 changes: 35 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "openapi",
"version": "1.0.0",
"version": "2.0.0-14",
"description": "Generate JavaScript or TypeScript code from Swagger/OpenAPI specifications",
"main": "./src/index.js",
"types": "./src/index.d.ts",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"repository": "openapijs/openapi",
"contributors": [
"e.fedotov <fedotov1290@gmail.com>",
"Sergey Sova <mail@sergeysova.com> (https://sova.dev)"
"Sergey Sova <mail@sergeysova.com> (https://sova.dev)",
"e.fedotov <fedotov1290@gmail.com>"
],
"license": "MIT",
"keywords": [
Expand All @@ -24,49 +24,60 @@
"url": "https://github.com/openapi/openapi/issues"
},
"scripts": {
"test": "jest --testPathIgnorePatterns package.test",
"prepublishOnly": "yarn test && yarn test-pack",
"test-pack": "yarn jest -i ./src/package.test.js"
"test": "jest --testPathIgnorePatterns package.test --passWithNoTests",
"prepublishOnly": "yarn build && yarn test --passWithNoTests && yarn test-pack",
"test-pack": "yarn jest --passWithNoTests -i ./src/package.test.js",
"build": "yarn build:types && yarn build:code",
"build:code": "babel -d ./dist --extensions '.ts' ./src",
"build:types": "dts-bundle-generator -o ./dist/index.d.ts ./src/index.ts"
},
"files": [
"src/common",
"src/lib",
"!src/lib/presets/mocks",
"src/v2/index.js",
"src/v3/index.js",
"src/cli/index.js",
"src/index.d.ts",
"src/index.js",
"request.js"
"dist/"
],
"bin": {
"openapi": "./src/cli/index.js"
"openapi": "./dist/cli/index.js"
},
"dependencies": {
"@types/jest": "^26.0.14",
"@jsdevtools/ono": "^7.1.3",
"change-case": "^4.1.1",
"commander": "^6.1.0",
"cosmiconfig": "^6.0.0",
"cosmiconfig": "^7.0.0",
"debug": "^4.3.1",
"is-url": "^1.2.4",
"js-yaml": "^3.13.1",
"node-fetch": "^2.6.0",
"js-yaml": "^4.0.0",
"node-fetch": "^2.6.1",
"object-hash": "^2.0.3",
"openapi-types": "^7.2.3",
"swagger2openapi": "^7.0.5",
"url-parse": "^1.4.7"
},
"devDependencies": {
"@babel/cli": "^7.12.17",
"@babel/core": "^7.12.17",
"@babel/preset-env": "^7.12.17",
"@babel/preset-typescript": "^7.12.17",
"@tsconfig/node14": "^1.0.0",
"@types/debug": "^4.1.5",
"@types/is-url": "^1.2.28",
"@types/jest": "^26.0.14",
"@types/js-yaml": "^4.0.0",
"@types/node": "^14.14.31",
"@types/node-fetch": "^2.5.8",
"babel-eslint": "^10.1.0",
"demo-swagger-to-js-preset": "^0.2.0",
"demo2-swagger-to-js-preset": "^0.1.0",
"demo3-swagger-to-js-preset": "^0.1.0",
"dts-bundle-generator": "^5.7.0",
"eslint": "^6.8.0",
"eslint-plugin-prettier": "^3.1.3",
"husky": "^4.2.5",
"jest": "^26.5.2",
"prettier": "^2.1.2"
"prettier": "^2.1.2",
"typescript": "^4.1.5"
},
"husky": {
"hooks": {
"pre-commit": "yarn eslint ./src",
"-pre-commit": "yarn eslint ./src",
"pre-push": "yarn test"
}
}
Expand Down
72 changes: 72 additions & 0 deletions src/api-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { open, FileHandle } from "fs/promises";
import * as path from "path";
import { ono } from "@jsdevtools/ono";
import { OpenAPIV2, OpenAPIV3 } from "openapi-types";
import isUrl from "is-url";
import fetch from "node-fetch";
import yaml from "js-yaml";
import createDebug from "debug";

import { Config } from "./config";

const debug = createDebug("openapi:api-file");

export async function readApiFile(config: Config): Promise<string> {
return isUrl(config.file) ? await readFileFromWeb(config) : await readFileFromDisk(config);
}

export async function parseContent(
filepath: string,
content: string,
): Promise<OpenAPIV3.Document | OpenAPIV2.Document> {
const extension = path.extname(filepath);

switch (extension) {
case ".yaml": {
const result: any = yaml.load(content, { filename: filepath });
if (!result) {
throw new Error(`Read file has incorrect content: ${result}`);
}
return result;
}

case ".json":
case "": {
const result: any = JSON.parse(content);
if (!result) {
throw new Error(`Read file has incorrect content: ${result}`);
}
return result;
}

default:
throw new Error(`Cannot parse file ${filepath} with extension "${extension}"`);
}
}

export function createFetchOptions(config: Config) {
return {
headers: config.authorization ? { authorization: config.authorization } : undefined,
};
}

async function readFileFromWeb(config: Config): Promise<string> {
const response = await fetch(config.file, createFetchOptions(config));

if (response.ok) {
return await response.text();
}
throw new Error(`Failed to fetch api file ${config.file}`);
}

async function readFileFromDisk(config: Config): Promise<string> {
let handle: FileHandle | null = null;
try {
handle = await open(config.file, "r");
return await handle?.readFile({ encoding: "utf8", flag: "r" });
} catch (error) {
throw ono(error, `Cannot read file ${config.file}`);
} finally {
await handle?.close();
}
}
Loading