openapi-sync 2.1.1 → 2.1.3
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/db.json +1 -1
- package/dist/Openapi-sync/index.js +100 -47
- package/dist/index.js +19 -2
- package/dist/openapi.sync.sample.js +60 -0
- package/package.json +2 -1
- package/types.ts +11 -3
|
@@ -118,16 +118,19 @@ const OpenapiSync = (apiUrl, apiName, config, refetchInterval) => __awaiter(void
|
|
|
118
118
|
else if (schema.anyOf) {
|
|
119
119
|
type += `(${schema.anyOf
|
|
120
120
|
.map((v) => parseSchemaToType(apiDoc, v, "", isRequired, options))
|
|
121
|
+
.filter((v) => !!v)
|
|
121
122
|
.join("|")})`;
|
|
122
123
|
}
|
|
123
124
|
else if (schema.oneOf) {
|
|
124
125
|
type += `(${schema.oneOf
|
|
125
126
|
.map((v) => parseSchemaToType(apiDoc, v, "", isRequired, options))
|
|
127
|
+
.filter((v) => !!v)
|
|
126
128
|
.join("|")})`;
|
|
127
129
|
}
|
|
128
130
|
else if (schema.allOf) {
|
|
129
131
|
type += `(${schema.allOf
|
|
130
132
|
.map((v) => parseSchemaToType(apiDoc, v, "", isRequired, options))
|
|
133
|
+
.filter((v) => !!v)
|
|
131
134
|
.join("&")})`;
|
|
132
135
|
}
|
|
133
136
|
else if (schema.items) {
|
|
@@ -161,47 +164,71 @@ const OpenapiSync = (apiUrl, apiName, config, refetchInterval) => __awaiter(void
|
|
|
161
164
|
else if (schema.enum && schema.enum.length > 0) {
|
|
162
165
|
if (schema.enum.length > 1)
|
|
163
166
|
type += "(";
|
|
164
|
-
schema.enum
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
167
|
+
schema.enum
|
|
168
|
+
.map((v) => JSON.stringify(v))
|
|
169
|
+
.filter((v) => !!v)
|
|
170
|
+
.forEach((v, i) => {
|
|
171
|
+
type += `${i === 0 ? "" : "|"}${v}`;
|
|
168
172
|
});
|
|
169
173
|
if (schema.enum.length > 1)
|
|
170
174
|
type += ")";
|
|
171
175
|
}
|
|
172
176
|
else if (schema.type) {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
"",
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
177
|
+
const handleType = (_type) => {
|
|
178
|
+
let typeCnt = "";
|
|
179
|
+
if (typeof _type === "string") {
|
|
180
|
+
if ([
|
|
181
|
+
"string",
|
|
182
|
+
"integer",
|
|
183
|
+
"number",
|
|
184
|
+
"array",
|
|
185
|
+
"boolean",
|
|
186
|
+
"null",
|
|
187
|
+
].includes(_type)) {
|
|
188
|
+
if (["integer", "number"].includes(_type)) {
|
|
189
|
+
typeCnt += `number`;
|
|
190
|
+
}
|
|
191
|
+
else if (_type === "array") {
|
|
192
|
+
//Since we would have already parsed the arrays keys above "schema.items" if it exists
|
|
193
|
+
typeCnt += "any[]";
|
|
194
|
+
/* if (schema.items) {
|
|
195
|
+
typeCnt += `${parseSchemaToType(
|
|
196
|
+
apiDoc,
|
|
197
|
+
schema.items,
|
|
198
|
+
"",
|
|
199
|
+
false,
|
|
200
|
+
options
|
|
201
|
+
)}[]`;
|
|
202
|
+
} else {
|
|
203
|
+
typeCnt += "any[]";
|
|
204
|
+
} */
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
typeCnt += _type;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
else if (_type === "object") {
|
|
211
|
+
//Since we would have already parsed the object keys above "schema.properties" if it exists
|
|
212
|
+
if (schema.additionalProperties) {
|
|
213
|
+
typeCnt += `{[k: string]: ${parseSchemaToType(apiDoc, schema.additionalProperties, "", true, options) || "any"}}`;
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
typeCnt += "{[k: string]: any}";
|
|
217
|
+
}
|
|
218
|
+
}
|
|
194
219
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
220
|
+
else if (Array.isArray(_type)) {
|
|
221
|
+
const arrType = _type.map((v) => handleType(v));
|
|
222
|
+
arrType.filter((v) => v !== "");
|
|
223
|
+
if (arrType.length > 1)
|
|
224
|
+
typeCnt += "(" + arrType.join("|") + ")";
|
|
200
225
|
}
|
|
201
226
|
else {
|
|
202
|
-
|
|
227
|
+
typeCnt += "any";
|
|
203
228
|
}
|
|
204
|
-
|
|
229
|
+
return typeCnt;
|
|
230
|
+
};
|
|
231
|
+
type = handleType(schema.type);
|
|
205
232
|
}
|
|
206
233
|
}
|
|
207
234
|
else {
|
|
@@ -281,25 +308,51 @@ const OpenapiSync = (apiUrl, apiName, config, refetchInterval) => __awaiter(void
|
|
|
281
308
|
type += JSON.stringify(schema.example);
|
|
282
309
|
}
|
|
283
310
|
else {
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
311
|
+
const handleType = (_type) => {
|
|
312
|
+
let typeCnt = "";
|
|
313
|
+
if (typeof _type === "string") {
|
|
314
|
+
if ([
|
|
315
|
+
"string",
|
|
316
|
+
"integer",
|
|
317
|
+
"number",
|
|
318
|
+
"array",
|
|
319
|
+
"boolean",
|
|
320
|
+
"null",
|
|
321
|
+
].includes(_type)) {
|
|
322
|
+
if (["integer", "number"].includes(_type)) {
|
|
323
|
+
typeCnt += `123`;
|
|
324
|
+
}
|
|
325
|
+
else if (_type === "array") {
|
|
326
|
+
//Since we would have already parsed the arrays keys above "schema.items" if it exists
|
|
327
|
+
typeCnt += "[]";
|
|
328
|
+
}
|
|
329
|
+
else if (_type === "boolean") {
|
|
330
|
+
typeCnt += `true`;
|
|
331
|
+
}
|
|
332
|
+
else if (_type === "null") {
|
|
333
|
+
typeCnt += `null`;
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
336
|
+
typeCnt += `"${_type}"`;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
else if (_type === "object") {
|
|
340
|
+
//Since we would have already parsed the object keys above "schema.properties" if it exists
|
|
341
|
+
typeCnt += "{}";
|
|
342
|
+
}
|
|
291
343
|
}
|
|
292
|
-
else if (
|
|
293
|
-
|
|
344
|
+
else if (Array.isArray(_type)) {
|
|
345
|
+
const arrType = _type.map((v) => handleType(v));
|
|
346
|
+
arrType.filter((v) => v !== "");
|
|
347
|
+
if (arrType.length > 1)
|
|
348
|
+
typeCnt += arrType.join("|");
|
|
294
349
|
}
|
|
295
350
|
else {
|
|
296
|
-
|
|
351
|
+
typeCnt += "any";
|
|
297
352
|
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
type += "{}";
|
|
302
|
-
}
|
|
353
|
+
return typeCnt;
|
|
354
|
+
};
|
|
355
|
+
type = handleType(schema.type);
|
|
303
356
|
}
|
|
304
357
|
}
|
|
305
358
|
}
|
package/dist/index.js
CHANGED
|
@@ -16,12 +16,13 @@ exports.Init = void 0;
|
|
|
16
16
|
const Openapi_sync_1 = __importDefault(require("./Openapi-sync"));
|
|
17
17
|
const dotenv_1 = __importDefault(require("dotenv"));
|
|
18
18
|
const path_1 = __importDefault(require("path"));
|
|
19
|
+
const fs_1 = __importDefault(require("fs"));
|
|
19
20
|
const state_1 = require("./Openapi-sync/state");
|
|
20
21
|
dotenv_1.default.config();
|
|
21
22
|
const rootUsingCwd = process.cwd();
|
|
22
23
|
const Init = (options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
23
24
|
// Load config file
|
|
24
|
-
let configJS, configJson;
|
|
25
|
+
let configJS, configJson, configTS;
|
|
25
26
|
try {
|
|
26
27
|
configJS = require(path_1.default.join(rootUsingCwd, "openapi.sync.js"));
|
|
27
28
|
}
|
|
@@ -34,7 +35,23 @@ const Init = (options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
34
35
|
catch (e) {
|
|
35
36
|
// console.log(e);
|
|
36
37
|
}
|
|
37
|
-
|
|
38
|
+
try {
|
|
39
|
+
// Check if TypeScript config file exists first
|
|
40
|
+
const tsConfigPath = path_1.default.join(rootUsingCwd, "openapi.sync.ts");
|
|
41
|
+
if (fs_1.default.existsSync(tsConfigPath)) {
|
|
42
|
+
// Register TypeScript loader before requiring the file
|
|
43
|
+
try {
|
|
44
|
+
require("esbuild-register");
|
|
45
|
+
}
|
|
46
|
+
catch (registerError) {
|
|
47
|
+
throw registerError;
|
|
48
|
+
}
|
|
49
|
+
// Now try to load TypeScript config
|
|
50
|
+
configTS = require(tsConfigPath);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch (e) { }
|
|
54
|
+
const config = configTS || configJS || configJson;
|
|
38
55
|
const apiNames = Object.keys(config.api);
|
|
39
56
|
const refetchInterval = options &&
|
|
40
57
|
"refetchInterval" in options &&
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// TypeScript config file for openapi-sync
|
|
3
|
+
// To use this file, install a TypeScript loader:
|
|
4
|
+
// npm install --save-dev esbuild-register (recommended - fastest & lightest)
|
|
5
|
+
// or: npm install --save-dev tsx
|
|
6
|
+
// or: npm install --save-dev @swc/register
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
const config = {
|
|
9
|
+
refetchInterval: 5000,
|
|
10
|
+
folder: "/inputed/path/",
|
|
11
|
+
api: {
|
|
12
|
+
example1: "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore.json",
|
|
13
|
+
example2: "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore.yaml",
|
|
14
|
+
},
|
|
15
|
+
server: 0,
|
|
16
|
+
types: {
|
|
17
|
+
name: {
|
|
18
|
+
prefix: "",
|
|
19
|
+
format: (source, data) => {
|
|
20
|
+
if (source === "shared") {
|
|
21
|
+
return `${data.name}`;
|
|
22
|
+
}
|
|
23
|
+
else if (source === "endpoint") {
|
|
24
|
+
return `${data.method.toLowerCase()}${data
|
|
25
|
+
.path.replace(/\//g, "_")
|
|
26
|
+
.replace(/{|}/g, "")}${data.code}${data.type}`;
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
doc: {
|
|
31
|
+
disable: true,
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
endpoints: {
|
|
35
|
+
value: {
|
|
36
|
+
replaceWords: [
|
|
37
|
+
{
|
|
38
|
+
replace: "/api/v\\d/",
|
|
39
|
+
with: "",
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
includeServer: true,
|
|
43
|
+
type: "object",
|
|
44
|
+
},
|
|
45
|
+
name: {
|
|
46
|
+
prefix: "",
|
|
47
|
+
useOperationId: true,
|
|
48
|
+
format: ({ method, path, summary, operationId }) => {
|
|
49
|
+
if (path === "/")
|
|
50
|
+
return "root";
|
|
51
|
+
return path.replace(/\//g, "_").replace(/{|}/g, "");
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
doc: {
|
|
55
|
+
disable: false,
|
|
56
|
+
showCurl: true,
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
module.exports = config;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openapi-sync",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.3",
|
|
4
4
|
"description": "A developer-friendly tool designed to keep your API up-to-date by leveraging OpenAPI schemas. It automates the generation of endpoint URIs and type definitions, including shared types, directly from your OpenAPI specification.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"@types/js-yaml": "^4.0.9",
|
|
42
42
|
"@types/lodash": "^4.17.7",
|
|
43
43
|
"@types/node": "^22.1.0",
|
|
44
|
+
"esbuild-register": "^3.6.0",
|
|
44
45
|
"typescript": "^5.5.4"
|
|
45
46
|
},
|
|
46
47
|
"dependencies": {
|
package/types.ts
CHANGED
|
@@ -4,7 +4,15 @@ export type IOpenApiSpec = Record<"openapi", string> & Record<string, any>;
|
|
|
4
4
|
|
|
5
5
|
export type IOpenApSchemaSpec = {
|
|
6
6
|
nullable?: boolean;
|
|
7
|
-
type:
|
|
7
|
+
type:
|
|
8
|
+
| "string"
|
|
9
|
+
| "integer"
|
|
10
|
+
| "number"
|
|
11
|
+
| "array"
|
|
12
|
+
| "object"
|
|
13
|
+
| "boolean"
|
|
14
|
+
| "null"
|
|
15
|
+
| any[];
|
|
8
16
|
example?: any;
|
|
9
17
|
enum?: string[];
|
|
10
18
|
format?: string;
|
|
@@ -84,7 +92,7 @@ export type IConfig = {
|
|
|
84
92
|
}
|
|
85
93
|
) => string | null | undefined;
|
|
86
94
|
};
|
|
87
|
-
doc
|
|
95
|
+
doc?: IConfigDoc;
|
|
88
96
|
};
|
|
89
97
|
endpoints?: {
|
|
90
98
|
value?: {
|
|
@@ -102,7 +110,7 @@ export type IConfig = {
|
|
|
102
110
|
prefix?: string;
|
|
103
111
|
useOperationId?: boolean;
|
|
104
112
|
};
|
|
105
|
-
doc
|
|
113
|
+
doc?: IConfigDoc;
|
|
106
114
|
};
|
|
107
115
|
};
|
|
108
116
|
|