openapi-sync 1.0.7 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -6
- package/bin/cli.js +3 -0
- package/dist/Openapi-sync/components/helpers.js +2 -2
- package/package.json +11 -5
- package/Openapi-sync/components/helpers.ts +0 -150
- package/Openapi-sync/components/regex.ts +0 -2
- package/Openapi-sync/index.ts +0 -252
- package/Openapi-sync/state.ts +0 -15
- package/Openapi-sync/types.ts +0 -43
- package/bin/cli +0 -4
- package/index.d.ts +0 -1
- package/index.ts +0 -24
- package/openapi.sync.sample.json +0 -8
- package/tsconfig.json +0 -108
package/README.md
CHANGED
|
@@ -1,11 +1,37 @@
|
|
|
1
|
-
|
|
1
|
+
# Openapi-sync
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Openapi-sync** leverages the power of OpenAPI schemas, just like Swagger UI, Postman, Redoc, and other popular tools. This package automates the creation of endpoint URIs and all defined types (including shared types) in a simple and developer-friendly manner and ensures your API remains up-to-date by checking for updates at intervals or right before committing your code (pre-commit).
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
To install `openapi-sync`, run the following command:
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
```bash
|
|
10
|
+
npm install openapi-sync
|
|
11
|
+
```
|
|
10
12
|
|
|
11
|
-
|
|
13
|
+
## Configuration
|
|
14
|
+
|
|
15
|
+
Create an `openapi.sync.json` file at the root of your project to configure openapi-sync. You can use the provided `openapi.sync.sample.json` as a reference.
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
To start using openapi-sync, simply run the following command in your terminal:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npx openapi-sync
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
You can also add it as a script in your package.json for easy access:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
"scripts": {
|
|
29
|
+
"api-sync": "openapi-sync",
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Features
|
|
34
|
+
|
|
35
|
+
- Endpoint URI Generation: Automatically generate endpoint URIs from your OpenAPI schema.
|
|
36
|
+
- Types Generation: Generate all defined types, including shared types, from your OpenAPI schema.
|
|
37
|
+
- CLI Commands: Use the command-line interface to regenerate files at any time—on app start, pre-commit, or whenever needed.
|
package/bin/cli.js
ADDED
|
@@ -28,8 +28,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.parseSchemaToType = exports.getEndpointDetails = exports.getSharedComponentName = exports.capitalize = exports.yamlStringToJson = exports.isYamlString = exports.isJson = void 0;
|
|
30
30
|
const regex_1 = require("./regex");
|
|
31
|
-
const property_expr_1 = __importDefault(require("property-expr"));
|
|
32
31
|
const yaml = __importStar(require("js-yaml"));
|
|
32
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
33
33
|
const isJson = (value) => {
|
|
34
34
|
return ["object"].includes(typeof value) && !(value instanceof Blob);
|
|
35
35
|
};
|
|
@@ -100,7 +100,7 @@ const parseSchemaToType = (apiDoc, schema, name, isRequired, options) => {
|
|
|
100
100
|
let pathToComponentParts = (schema.$ref || "").split("/");
|
|
101
101
|
pathToComponentParts.shift();
|
|
102
102
|
const pathToComponent = pathToComponentParts.join(".");
|
|
103
|
-
const component =
|
|
103
|
+
const component = lodash_1.default.get(apiDoc, pathToComponent, null);
|
|
104
104
|
if (component) {
|
|
105
105
|
const componentName = pathToComponentParts[pathToComponentParts.length - 1];
|
|
106
106
|
// Reference component via import instead of parsing
|
package/package.json
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openapi-sync",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"description": "sync openapi variables",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"bin": {
|
|
8
|
-
"openapi-sync": "./bin/cli"
|
|
8
|
+
"openapi-sync": "./bin/cli.js"
|
|
9
9
|
},
|
|
10
|
+
"files": [
|
|
11
|
+
"bin",
|
|
12
|
+
"dist",
|
|
13
|
+
"LICENSE",
|
|
14
|
+
"README.md",
|
|
15
|
+
"package.json"
|
|
16
|
+
],
|
|
10
17
|
"scripts": {
|
|
11
18
|
"test": "echo \"Error: no test specified\"",
|
|
12
19
|
"build": "tsc",
|
|
13
|
-
"publish": "npm run build && npm version patch &&
|
|
20
|
+
"publish-package": "npm run build && npm version patch && npm publish",
|
|
14
21
|
"start": "ts-node index.ts"
|
|
15
22
|
},
|
|
16
23
|
"author": "P-Technologies",
|
|
@@ -28,7 +35,6 @@
|
|
|
28
35
|
"axios-retry": "^4.5.0",
|
|
29
36
|
"dotenv": "^16.4.5",
|
|
30
37
|
"js-yaml": "^4.1.0",
|
|
31
|
-
"lodash": "^4.17.21"
|
|
32
|
-
"property-expr": "^2.0.6"
|
|
38
|
+
"lodash": "^4.17.21"
|
|
33
39
|
}
|
|
34
40
|
}
|
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
import { IOpenApiSpec, IOpenApSchemaSpec } from "../types";
|
|
2
|
-
import { variableNameChar } from "./regex";
|
|
3
|
-
import propertyExpr from "property-expr";
|
|
4
|
-
import * as yaml from "js-yaml";
|
|
5
|
-
|
|
6
|
-
export const isJson = (value: any) => {
|
|
7
|
-
return ["object"].includes(typeof value) && !(value instanceof Blob);
|
|
8
|
-
};
|
|
9
|
-
export const isYamlString = (fileContent: string) => {
|
|
10
|
-
try {
|
|
11
|
-
yaml.load(fileContent);
|
|
12
|
-
return true;
|
|
13
|
-
} catch (en) {
|
|
14
|
-
const e = en as any;
|
|
15
|
-
if (e instanceof yaml.YAMLException) {
|
|
16
|
-
return false;
|
|
17
|
-
} else {
|
|
18
|
-
throw e;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export const yamlStringToJson = (fileContent: string) => {
|
|
24
|
-
if (isYamlString(fileContent)) {
|
|
25
|
-
const content = yaml.load(fileContent);
|
|
26
|
-
|
|
27
|
-
const jsonString = JSON.stringify(content, null, 2);
|
|
28
|
-
const json = JSON.parse(jsonString);
|
|
29
|
-
return json;
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export const capitalize = (text: string) => {
|
|
34
|
-
const capitalizedWord =
|
|
35
|
-
text.substring(0, 1).toUpperCase() + text.substring(1);
|
|
36
|
-
return capitalizedWord;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export const getSharedComponentName = (componentName: string) =>
|
|
40
|
-
`IApi${capitalize(componentName)}`;
|
|
41
|
-
|
|
42
|
-
export const getEndpointDetails = (path: string, method: string) => {
|
|
43
|
-
const pathParts = path.split("/");
|
|
44
|
-
let name = `${capitalize(method)}`;
|
|
45
|
-
const variables: string[] = [];
|
|
46
|
-
pathParts.forEach((part) => {
|
|
47
|
-
// check if part is a variable
|
|
48
|
-
if (part[0] === "{" && part[part.length - 1] === "}") {
|
|
49
|
-
const s = part.replace(/{/, "").replace(/}/, "");
|
|
50
|
-
variables.push(s);
|
|
51
|
-
part = `$${s}`;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// parse to variable name
|
|
55
|
-
let partVal = "";
|
|
56
|
-
part.split("").forEach((char) => {
|
|
57
|
-
let c = char;
|
|
58
|
-
if (!variableNameChar.test(char)) c = "/";
|
|
59
|
-
partVal += c;
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
partVal.split("/").forEach((val) => {
|
|
63
|
-
name += capitalize(val);
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
return { name, variables, pathParts };
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
export const parseSchemaToType = (
|
|
71
|
-
apiDoc: IOpenApiSpec,
|
|
72
|
-
schema: IOpenApSchemaSpec,
|
|
73
|
-
name: string,
|
|
74
|
-
isRequired?: boolean,
|
|
75
|
-
options?: {
|
|
76
|
-
noSharedImport?: boolean;
|
|
77
|
-
}
|
|
78
|
-
) => {
|
|
79
|
-
let typeName = name ? `\t${name}${isRequired ? "" : "?"}: ` : "";
|
|
80
|
-
let type = "";
|
|
81
|
-
if (schema.$ref) {
|
|
82
|
-
if (schema.$ref[0] === "#") {
|
|
83
|
-
let pathToComponentParts = (schema.$ref || "").split("/");
|
|
84
|
-
pathToComponentParts.shift();
|
|
85
|
-
const pathToComponent = pathToComponentParts.join(".");
|
|
86
|
-
const component = propertyExpr.getter(pathToComponent)(
|
|
87
|
-
apiDoc
|
|
88
|
-
) as IOpenApSchemaSpec;
|
|
89
|
-
|
|
90
|
-
if (component) {
|
|
91
|
-
const componentName =
|
|
92
|
-
pathToComponentParts[pathToComponentParts.length - 1];
|
|
93
|
-
// Reference component via import instead of parsing
|
|
94
|
-
type += `${
|
|
95
|
-
options?.noSharedImport ? "" : "Shared."
|
|
96
|
-
}${getSharedComponentName(componentName)}`;
|
|
97
|
-
// type += `${parseSchemaToType(apiDoc, component, "", isRequired)}`;
|
|
98
|
-
}
|
|
99
|
-
} else {
|
|
100
|
-
type += "";
|
|
101
|
-
//TODO $ref is a uri - use axios to fetch doc
|
|
102
|
-
}
|
|
103
|
-
} else if (schema.type) {
|
|
104
|
-
if (schema.enum && schema.enum.length > 0) {
|
|
105
|
-
if (schema.enum.length > 1) type += "(";
|
|
106
|
-
type += schema.enum
|
|
107
|
-
.map((v) => `"${v}"`)
|
|
108
|
-
.join("|")
|
|
109
|
-
.toString();
|
|
110
|
-
if (schema.enum.length > 1) type += ")";
|
|
111
|
-
} else if (["string", "integer", "number", "array"].includes(schema.type)) {
|
|
112
|
-
if (schema.type === "string") {
|
|
113
|
-
type += `string`;
|
|
114
|
-
} else if (["integer", "number"].includes(schema.type)) {
|
|
115
|
-
type += `number`;
|
|
116
|
-
} else if (schema.type === "array") {
|
|
117
|
-
if (schema.items) {
|
|
118
|
-
type += `${parseSchemaToType(
|
|
119
|
-
apiDoc,
|
|
120
|
-
schema.items,
|
|
121
|
-
"",
|
|
122
|
-
false,
|
|
123
|
-
options
|
|
124
|
-
)}[]`;
|
|
125
|
-
} else {
|
|
126
|
-
type += "any[]";
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
} else if (schema.type === "object") {
|
|
130
|
-
if (schema.properties) {
|
|
131
|
-
//parse object key one at a time
|
|
132
|
-
const objKeys = Object.keys(schema.properties);
|
|
133
|
-
const requiredKeys = schema.required || [];
|
|
134
|
-
type += "{\n";
|
|
135
|
-
objKeys.forEach((key) => {
|
|
136
|
-
type += `${parseSchemaToType(
|
|
137
|
-
apiDoc,
|
|
138
|
-
schema.properties?.[key] as IOpenApSchemaSpec,
|
|
139
|
-
key,
|
|
140
|
-
requiredKeys.includes(key),
|
|
141
|
-
options
|
|
142
|
-
)}`;
|
|
143
|
-
});
|
|
144
|
-
type += "}";
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
return type.length > 0 ? `${typeName}${type}${name ? ";\n" : ""}` : "";
|
|
150
|
-
};
|
package/Openapi-sync/index.ts
DELETED
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import {
|
|
4
|
-
getEndpointDetails,
|
|
5
|
-
getSharedComponentName,
|
|
6
|
-
isJson,
|
|
7
|
-
isYamlString,
|
|
8
|
-
parseSchemaToType,
|
|
9
|
-
yamlStringToJson,
|
|
10
|
-
} from "./components/helpers";
|
|
11
|
-
import {
|
|
12
|
-
IOpenApiMediaTypeSpec,
|
|
13
|
-
IOpenApiParameterSpec,
|
|
14
|
-
IOpenApiRequestBodySpec,
|
|
15
|
-
IOpenApiResponseSpec,
|
|
16
|
-
IOpenApiSpec,
|
|
17
|
-
IOpenApSchemaSpec,
|
|
18
|
-
} from "./types";
|
|
19
|
-
import { isEqual } from "lodash";
|
|
20
|
-
import axios from "axios";
|
|
21
|
-
import axiosRetry from "axios-retry";
|
|
22
|
-
import { bundleFromString, createConfig } from "@redocly/openapi-core";
|
|
23
|
-
import { getState, setState } from "./state";
|
|
24
|
-
|
|
25
|
-
const rootUsingCwd = process.cwd();
|
|
26
|
-
let fetchTimeout: null | NodeJS.Timeout = null;
|
|
27
|
-
|
|
28
|
-
// Create an Axios instance
|
|
29
|
-
const apiClient = axios.create({
|
|
30
|
-
timeout: 30000, // Timeout after 30 seconds
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
// Configure axios-retry
|
|
34
|
-
axiosRetry(apiClient, {
|
|
35
|
-
retries: 20, // Number of retry attempts
|
|
36
|
-
retryCondition: (error) => {
|
|
37
|
-
// Retry on network error
|
|
38
|
-
return (
|
|
39
|
-
error.code === "ECONNABORTED" || error.message.includes("Network Error")
|
|
40
|
-
);
|
|
41
|
-
},
|
|
42
|
-
retryDelay: (retryCount) => {
|
|
43
|
-
return retryCount * 1000; // Exponential back-off: 1s, 2s, 3s, etc.
|
|
44
|
-
},
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
const OpenapiSync = async (apiUrl: string, apiName: string) => {
|
|
48
|
-
const specResponse = await apiClient.get(apiUrl);
|
|
49
|
-
|
|
50
|
-
const redoclyConfig = await createConfig({
|
|
51
|
-
extends: ["minimal"],
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
const source = JSON.stringify(
|
|
55
|
-
isJson(specResponse.data)
|
|
56
|
-
? specResponse.data
|
|
57
|
-
: yamlStringToJson(specResponse.data)
|
|
58
|
-
);
|
|
59
|
-
|
|
60
|
-
const lintResults = await bundleFromString({
|
|
61
|
-
source,
|
|
62
|
-
config: redoclyConfig,
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
// Load config file
|
|
66
|
-
const config = require(path.join(rootUsingCwd, "openapi.sync.json"));
|
|
67
|
-
const folderPath = path.join(config.folder || "", apiName);
|
|
68
|
-
|
|
69
|
-
const spec: IOpenApiSpec = lintResults.bundle.parsed;
|
|
70
|
-
// auto update only on dev
|
|
71
|
-
if (
|
|
72
|
-
!(
|
|
73
|
-
process.env.NODE_ENV &&
|
|
74
|
-
["production", "prod", "test", "staging"].includes(process.env.NODE_ENV)
|
|
75
|
-
)
|
|
76
|
-
) {
|
|
77
|
-
// compare new spec with old spec, continuing only if spec it different
|
|
78
|
-
// auto sync at interval
|
|
79
|
-
if (fetchTimeout) clearTimeout(fetchTimeout);
|
|
80
|
-
|
|
81
|
-
if (!isNaN(config.refetchInterval) && config.refetchInterval) {
|
|
82
|
-
// use config interval or 1 hour
|
|
83
|
-
fetchTimeout = setTimeout(
|
|
84
|
-
() => OpenapiSync(apiUrl, apiName),
|
|
85
|
-
config.refetchInterval || 60000
|
|
86
|
-
);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const prevSpec = getState(apiName);
|
|
90
|
-
if (isEqual(prevSpec, spec)) return;
|
|
91
|
-
|
|
92
|
-
setState(apiName, spec);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
let endpointsFileContent = "";
|
|
96
|
-
let typesFileContent = "";
|
|
97
|
-
let sharedTypesFileContent = "";
|
|
98
|
-
|
|
99
|
-
if (spec.components && spec.components.schemas) {
|
|
100
|
-
// Create components (shared) types
|
|
101
|
-
const components: Record<string, IOpenApiMediaTypeSpec> =
|
|
102
|
-
spec.components.schemas;
|
|
103
|
-
const contentKeys = Object.keys(components);
|
|
104
|
-
// only need 1 schema so will us the first schema provided
|
|
105
|
-
contentKeys.forEach((key) => {
|
|
106
|
-
const typeCnt = `${parseSchemaToType(
|
|
107
|
-
spec,
|
|
108
|
-
components[key] as IOpenApSchemaSpec,
|
|
109
|
-
"",
|
|
110
|
-
true,
|
|
111
|
-
{
|
|
112
|
-
noSharedImport: true,
|
|
113
|
-
}
|
|
114
|
-
)}`;
|
|
115
|
-
if (typeCnt) {
|
|
116
|
-
sharedTypesFileContent += `export type ${getSharedComponentName(
|
|
117
|
-
key
|
|
118
|
-
)} = ${typeCnt};\n`;
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const getBodySchemaType = (requestBody: IOpenApiRequestBodySpec) => {
|
|
124
|
-
let typeCnt = "";
|
|
125
|
-
if (requestBody.content) {
|
|
126
|
-
const contentKeys = Object.keys(requestBody.content);
|
|
127
|
-
// only need 1 schema so will us the first schema provided
|
|
128
|
-
if (contentKeys[0] && requestBody.content[contentKeys[0]].schema) {
|
|
129
|
-
typeCnt += `${parseSchemaToType(
|
|
130
|
-
spec,
|
|
131
|
-
requestBody.content[contentKeys[0]].schema as IOpenApSchemaSpec,
|
|
132
|
-
""
|
|
133
|
-
)}`;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
return typeCnt;
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
Object.keys(spec.paths || {}).forEach((endpointPath) => {
|
|
140
|
-
const endpointSpec = spec.paths[endpointPath];
|
|
141
|
-
const endpointMethods = Object.keys(endpointSpec);
|
|
142
|
-
endpointMethods.forEach((method: string) => {
|
|
143
|
-
const endpoint = getEndpointDetails(endpointPath, method);
|
|
144
|
-
|
|
145
|
-
const endpointUrlTxt = endpoint.pathParts
|
|
146
|
-
.map((part) => {
|
|
147
|
-
// check if part is a variable
|
|
148
|
-
if (part[0] === "{" && part[part.length - 1] === "}") {
|
|
149
|
-
const s = part.replace(/{/, "").replace(/}/, "");
|
|
150
|
-
part = `\${${s}}`;
|
|
151
|
-
}
|
|
152
|
-
return part;
|
|
153
|
-
})
|
|
154
|
-
.join("/");
|
|
155
|
-
|
|
156
|
-
let endpointUrl = `"${endpointUrlTxt}"`;
|
|
157
|
-
if (endpoint.variables.length > 0) {
|
|
158
|
-
const params = endpoint.variables.map((v) => `${v}:string`).join(",");
|
|
159
|
-
endpointUrl = `(${params})=> \`${endpointUrlTxt}\``;
|
|
160
|
-
}
|
|
161
|
-
// Add the endpoint url
|
|
162
|
-
endpointsFileContent += `export const ${endpoint.name} = ${endpointUrl};
|
|
163
|
-
`;
|
|
164
|
-
|
|
165
|
-
if (endpointSpec[method]?.parameters) {
|
|
166
|
-
// create query parameters types
|
|
167
|
-
const parameters: IOpenApiParameterSpec[] =
|
|
168
|
-
endpointSpec[method]?.parameters;
|
|
169
|
-
let typeCnt = "";
|
|
170
|
-
parameters.forEach((param) => {
|
|
171
|
-
if (param.in === "query" && param.name) {
|
|
172
|
-
typeCnt += `${parseSchemaToType(
|
|
173
|
-
spec,
|
|
174
|
-
param.schema as any,
|
|
175
|
-
param.name,
|
|
176
|
-
param.required
|
|
177
|
-
)}`;
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
if (typeCnt) {
|
|
182
|
-
typesFileContent += `export type I${endpoint.name}Query = {\n${typeCnt}};\n`;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (endpointSpec[method]?.requestBody) {
|
|
187
|
-
//create requestBody types
|
|
188
|
-
const requestBody: IOpenApiRequestBodySpec =
|
|
189
|
-
endpointSpec[method]?.requestBody;
|
|
190
|
-
|
|
191
|
-
let typeCnt = getBodySchemaType(requestBody);
|
|
192
|
-
if (typeCnt) {
|
|
193
|
-
typesFileContent += `export type I${endpoint.name}DTO = ${typeCnt};\n`;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
if (endpointSpec[method]?.responses) {
|
|
198
|
-
// create request response types
|
|
199
|
-
const responses: IOpenApiResponseSpec = endpointSpec[method]?.responses;
|
|
200
|
-
const resCodes = Object.keys(responses);
|
|
201
|
-
resCodes.forEach((code) => {
|
|
202
|
-
let typeCnt = getBodySchemaType(responses[code]);
|
|
203
|
-
if (typeCnt) {
|
|
204
|
-
typesFileContent += `export type I${endpoint.name}${code}Response = ${typeCnt};\n`;
|
|
205
|
-
}
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
});
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
// Create the necessary directories
|
|
212
|
-
const endpointsFilePath = path.join(rootUsingCwd, folderPath, "endpoints.ts");
|
|
213
|
-
await fs.promises.mkdir(path.dirname(endpointsFilePath), { recursive: true });
|
|
214
|
-
// Create the file asynchronously
|
|
215
|
-
await fs.promises.writeFile(endpointsFilePath, endpointsFileContent);
|
|
216
|
-
|
|
217
|
-
if (sharedTypesFileContent.length > 0) {
|
|
218
|
-
// Create the necessary directories
|
|
219
|
-
const sharedTypesFilePath = path.join(
|
|
220
|
-
rootUsingCwd,
|
|
221
|
-
folderPath,
|
|
222
|
-
"types",
|
|
223
|
-
"shared.ts"
|
|
224
|
-
);
|
|
225
|
-
await fs.promises.mkdir(path.dirname(sharedTypesFilePath), {
|
|
226
|
-
recursive: true,
|
|
227
|
-
});
|
|
228
|
-
// Create the file asynchronously
|
|
229
|
-
await fs.promises.writeFile(sharedTypesFilePath, sharedTypesFileContent);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
if (typesFileContent.length > 0) {
|
|
233
|
-
// Create the necessary directories
|
|
234
|
-
const typesFilePath = path.join(
|
|
235
|
-
rootUsingCwd,
|
|
236
|
-
folderPath,
|
|
237
|
-
"types",
|
|
238
|
-
"index.ts"
|
|
239
|
-
);
|
|
240
|
-
await fs.promises.mkdir(path.dirname(typesFilePath), { recursive: true });
|
|
241
|
-
// Create the file asynchronously
|
|
242
|
-
await fs.promises.writeFile(
|
|
243
|
-
typesFilePath,
|
|
244
|
-
`${
|
|
245
|
-
sharedTypesFileContent.length > 0
|
|
246
|
-
? `import * as Shared from "./shared";\n\n`
|
|
247
|
-
: ""
|
|
248
|
-
}${typesFileContent}`
|
|
249
|
-
);
|
|
250
|
-
}
|
|
251
|
-
};
|
|
252
|
-
export default OpenapiSync;
|
package/Openapi-sync/state.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { IOpenApiSpec } from "./types";
|
|
2
|
-
|
|
3
|
-
let state: Record<string, IOpenApiSpec> = {};
|
|
4
|
-
|
|
5
|
-
export const setState = (key: string, value: IOpenApiSpec) => {
|
|
6
|
-
state[key] = value;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
export const getState = (key: string): IOpenApiSpec | undefined => {
|
|
10
|
-
return state[key];
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export const resetState = () => {
|
|
14
|
-
state = {};
|
|
15
|
-
};
|
package/Openapi-sync/types.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
export type IOpenApiSpec = Record<"openapi", string> & Record<string, any>;
|
|
2
|
-
|
|
3
|
-
export type IOpenApSchemaSpec = {
|
|
4
|
-
type: "string" | "integer" | "number" | "array" | "object";
|
|
5
|
-
example?: any;
|
|
6
|
-
enum?: string[];
|
|
7
|
-
format?: string;
|
|
8
|
-
items?: IOpenApSchemaSpec;
|
|
9
|
-
required?: string[];
|
|
10
|
-
$ref?: string;
|
|
11
|
-
properties?: Record<string, IOpenApSchemaSpec>;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export type IOpenApiParameterSpec = {
|
|
15
|
-
name: string;
|
|
16
|
-
in: string;
|
|
17
|
-
enum?: string[];
|
|
18
|
-
description?: string;
|
|
19
|
-
required?: boolean;
|
|
20
|
-
deprecated?: boolean;
|
|
21
|
-
allowEmptyValue?: boolean;
|
|
22
|
-
style?: string;
|
|
23
|
-
explode?: boolean;
|
|
24
|
-
allowReserved?: boolean;
|
|
25
|
-
schema?: IOpenApSchemaSpec;
|
|
26
|
-
example?: any;
|
|
27
|
-
examples?: any[];
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export type IOpenApiMediaTypeSpec = {
|
|
31
|
-
schema?: IOpenApSchemaSpec;
|
|
32
|
-
example?: any;
|
|
33
|
-
examples?: any[];
|
|
34
|
-
encoding?: any;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
export type IOpenApiRequestBodySpec = {
|
|
38
|
-
description?: string;
|
|
39
|
-
required?: boolean;
|
|
40
|
-
content: Record<string, IOpenApiMediaTypeSpec>;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export type IOpenApiResponseSpec = Record<string, IOpenApiRequestBodySpec>;
|
package/bin/cli
DELETED
package/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./Openapi-sync/types";
|
package/index.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import OpenapiSync from "./Openapi-sync";
|
|
2
|
-
import dotenv from "dotenv";
|
|
3
|
-
import path from "path";
|
|
4
|
-
import { resetState } from "./Openapi-sync/state";
|
|
5
|
-
|
|
6
|
-
dotenv.config();
|
|
7
|
-
|
|
8
|
-
const rootUsingCwd = process.cwd();
|
|
9
|
-
|
|
10
|
-
const Init = async () => {
|
|
11
|
-
// Load config file
|
|
12
|
-
const config = require(path.join(rootUsingCwd, "openapi.sync.json"));
|
|
13
|
-
const apiNames = Object.keys(config.api);
|
|
14
|
-
|
|
15
|
-
resetState();
|
|
16
|
-
for (let i = 0; i < apiNames.length; i += 1) {
|
|
17
|
-
const apiName = apiNames[i];
|
|
18
|
-
const apiUrl = config.api[apiName];
|
|
19
|
-
|
|
20
|
-
OpenapiSync(apiUrl, apiName);
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
Init();
|
package/openapi.sync.sample.json
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"refetchInterval": 5000,
|
|
3
|
-
"folder": "inputed/path",
|
|
4
|
-
"api": {
|
|
5
|
-
"example1": "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore.json",
|
|
6
|
-
"example2": "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore.yaml"
|
|
7
|
-
}
|
|
8
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
/* Visit https://aka.ms/tsconfig to read more about this file */
|
|
4
|
-
|
|
5
|
-
/* Projects */
|
|
6
|
-
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
7
|
-
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
8
|
-
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
9
|
-
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
10
|
-
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
11
|
-
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
12
|
-
|
|
13
|
-
/* Language and Environment */
|
|
14
|
-
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
15
|
-
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
16
|
-
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
17
|
-
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
|
|
18
|
-
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
19
|
-
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
20
|
-
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
21
|
-
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
22
|
-
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
23
|
-
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
24
|
-
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
25
|
-
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
26
|
-
|
|
27
|
-
/* Modules */
|
|
28
|
-
"module": "commonjs", /* Specify what module code is generated. */
|
|
29
|
-
// "rootDir": "./", /* Specify the root folder within your source files. */
|
|
30
|
-
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
31
|
-
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
32
|
-
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
33
|
-
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
34
|
-
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
|
35
|
-
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
36
|
-
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
37
|
-
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
38
|
-
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
|
|
39
|
-
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
|
|
40
|
-
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
|
|
41
|
-
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
|
|
42
|
-
"resolveJsonModule": true, /* Enable importing .json files. */
|
|
43
|
-
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
|
|
44
|
-
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
45
|
-
|
|
46
|
-
/* JavaScript Support */
|
|
47
|
-
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
|
48
|
-
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
49
|
-
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
50
|
-
|
|
51
|
-
/* Emit */
|
|
52
|
-
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
53
|
-
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
54
|
-
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
55
|
-
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
56
|
-
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
57
|
-
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
58
|
-
"outDir": "./dist", /* Specify an output folder for all emitted files. */
|
|
59
|
-
// "removeComments": true, /* Disable emitting comments. */
|
|
60
|
-
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
61
|
-
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
62
|
-
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
63
|
-
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
64
|
-
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
65
|
-
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
66
|
-
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
67
|
-
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
68
|
-
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
69
|
-
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
70
|
-
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
71
|
-
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
72
|
-
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
73
|
-
|
|
74
|
-
/* Interop Constraints */
|
|
75
|
-
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
76
|
-
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
|
77
|
-
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
|
|
78
|
-
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
79
|
-
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
|
80
|
-
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
81
|
-
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
|
82
|
-
|
|
83
|
-
/* Type Checking */
|
|
84
|
-
"strict": true, /* Enable all strict type-checking options. */
|
|
85
|
-
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
86
|
-
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
87
|
-
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
88
|
-
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
89
|
-
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
90
|
-
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
91
|
-
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
92
|
-
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
93
|
-
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
94
|
-
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
95
|
-
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
96
|
-
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
97
|
-
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
98
|
-
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
99
|
-
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
100
|
-
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
101
|
-
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
102
|
-
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
103
|
-
|
|
104
|
-
/* Completeness */
|
|
105
|
-
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
106
|
-
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
107
|
-
}
|
|
108
|
-
}
|