openapi-sync 1.0.15 → 1.0.19
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.
|
@@ -72,11 +72,24 @@ const getEndpointDetails = (path, method) => {
|
|
|
72
72
|
const variables = [];
|
|
73
73
|
pathParts.forEach((part) => {
|
|
74
74
|
// check if part is a variable
|
|
75
|
+
//api/{userId}
|
|
75
76
|
if (part[0] === "{" && part[part.length - 1] === "}") {
|
|
76
77
|
const s = part.replace(/{/, "").replace(/}/, "");
|
|
77
78
|
variables.push(s);
|
|
78
79
|
part = `$${s}`;
|
|
79
80
|
}
|
|
81
|
+
//api/<userId>
|
|
82
|
+
else if (part[0] === "<" && part[part.length - 1] === ">") {
|
|
83
|
+
const s = part.replace(/</, "").replace(/>/, "");
|
|
84
|
+
variables.push(s);
|
|
85
|
+
part = `$${s}`;
|
|
86
|
+
}
|
|
87
|
+
//api/:userId
|
|
88
|
+
else if (part[0] === ":") {
|
|
89
|
+
const s = part.replace(/:/, "");
|
|
90
|
+
variables.push(s);
|
|
91
|
+
part = `$${s}`;
|
|
92
|
+
}
|
|
80
93
|
// parse to variable name
|
|
81
94
|
let partVal = "";
|
|
82
95
|
part.split("").forEach((char) => {
|
|
@@ -95,66 +108,68 @@ exports.getEndpointDetails = getEndpointDetails;
|
|
|
95
108
|
const parseSchemaToType = (apiDoc, schema, name, isRequired, options) => {
|
|
96
109
|
let typeName = name ? `\t${name}${isRequired ? "" : "?"}: ` : "";
|
|
97
110
|
let type = "";
|
|
98
|
-
if (schema
|
|
99
|
-
if (schema.$ref
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
111
|
+
if (schema) {
|
|
112
|
+
if (schema.$ref) {
|
|
113
|
+
if (schema.$ref[0] === "#") {
|
|
114
|
+
let pathToComponentParts = (schema.$ref || "").split("/");
|
|
115
|
+
pathToComponentParts.shift();
|
|
116
|
+
const pathToComponent = pathToComponentParts.join(".");
|
|
117
|
+
const component = lodash_1.default.get(apiDoc, pathToComponent, null);
|
|
118
|
+
if (component) {
|
|
119
|
+
const componentName = pathToComponentParts[pathToComponentParts.length - 1];
|
|
120
|
+
// Reference component via import instead of parsing
|
|
121
|
+
type += `${(options === null || options === void 0 ? void 0 : options.noSharedImport) ? "" : "Shared."}${(0, exports.getSharedComponentName)(componentName)}`;
|
|
122
|
+
// type += `${parseSchemaToType(apiDoc, component, "", isRequired)}`;
|
|
123
|
+
}
|
|
109
124
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
//TODO $ref is a uri - use axios to fetch doc
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
else if (schema.type) {
|
|
117
|
-
if (schema.enum && schema.enum.length > 0) {
|
|
118
|
-
if (schema.enum.length > 1)
|
|
119
|
-
type += "(";
|
|
120
|
-
type += schema.enum
|
|
121
|
-
.map((v) => `"${v}"`)
|
|
122
|
-
.join("|")
|
|
123
|
-
.toString();
|
|
124
|
-
if (schema.enum.length > 1)
|
|
125
|
-
type += ")";
|
|
126
|
-
}
|
|
127
|
-
else if (["string", "integer", "number", "array"].includes(schema.type)) {
|
|
128
|
-
if (schema.type === "string") {
|
|
129
|
-
type += `string`;
|
|
125
|
+
else {
|
|
126
|
+
type += "";
|
|
127
|
+
//TODO $ref is a uri - use axios to fetch doc
|
|
130
128
|
}
|
|
131
|
-
|
|
132
|
-
|
|
129
|
+
}
|
|
130
|
+
else if (schema.type) {
|
|
131
|
+
if (schema.enum && schema.enum.length > 0) {
|
|
132
|
+
if (schema.enum.length > 1)
|
|
133
|
+
type += "(";
|
|
134
|
+
type += schema.enum
|
|
135
|
+
.map((v) => `"${v}"`)
|
|
136
|
+
.join("|")
|
|
137
|
+
.toString();
|
|
138
|
+
if (schema.enum.length > 1)
|
|
139
|
+
type += ")";
|
|
133
140
|
}
|
|
134
|
-
else if (
|
|
135
|
-
if (schema.
|
|
136
|
-
type +=
|
|
141
|
+
else if (["string", "integer", "number", "array"].includes(schema.type)) {
|
|
142
|
+
if (schema.type === "string") {
|
|
143
|
+
type += `string`;
|
|
137
144
|
}
|
|
138
|
-
else {
|
|
139
|
-
type +=
|
|
145
|
+
else if (["integer", "number"].includes(schema.type)) {
|
|
146
|
+
type += `number`;
|
|
140
147
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
let typeCnt = "";
|
|
149
|
-
objKeys.forEach((key) => {
|
|
150
|
-
var _a;
|
|
151
|
-
typeCnt += `${(0, exports.parseSchemaToType)(apiDoc, (_a = schema.properties) === null || _a === void 0 ? void 0 : _a[key], key, requiredKeys.includes(key), options)}`;
|
|
152
|
-
});
|
|
153
|
-
if (typeCnt.length > 0) {
|
|
154
|
-
type += `{\n${typeCnt}}`;
|
|
148
|
+
else if (schema.type === "array") {
|
|
149
|
+
if (schema.items) {
|
|
150
|
+
type += `${(0, exports.parseSchemaToType)(apiDoc, schema.items, "", false, options)}[]`;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
type += "any[]";
|
|
154
|
+
}
|
|
155
155
|
}
|
|
156
|
-
|
|
157
|
-
|
|
156
|
+
}
|
|
157
|
+
else if (schema.type === "object") {
|
|
158
|
+
if (schema.properties) {
|
|
159
|
+
//parse object key one at a time
|
|
160
|
+
const objKeys = Object.keys(schema.properties);
|
|
161
|
+
const requiredKeys = schema.required || [];
|
|
162
|
+
let typeCnt = "";
|
|
163
|
+
objKeys.forEach((key) => {
|
|
164
|
+
var _a;
|
|
165
|
+
typeCnt += `${(0, exports.parseSchemaToType)(apiDoc, (_a = schema.properties) === null || _a === void 0 ? void 0 : _a[key], key, requiredKeys.includes(key), options)}`;
|
|
166
|
+
});
|
|
167
|
+
if (typeCnt.length > 0) {
|
|
168
|
+
type += `{\n${typeCnt}}`;
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
type += "object";
|
|
172
|
+
}
|
|
158
173
|
}
|
|
159
174
|
}
|
|
160
175
|
}
|
|
@@ -120,11 +120,22 @@ const OpenapiSync = (apiUrl, apiName, refetchInterval) => __awaiter(void 0, void
|
|
|
120
120
|
const endpoint = (0, helpers_1.getEndpointDetails)(endpointPath, method);
|
|
121
121
|
const endpointUrlTxt = endpoint.pathParts
|
|
122
122
|
.map((part) => {
|
|
123
|
+
console.log("2nd part", part);
|
|
123
124
|
// check if part is a variable
|
|
124
125
|
if (part[0] === "{" && part[part.length - 1] === "}") {
|
|
125
126
|
const s = part.replace(/{/, "").replace(/}/, "");
|
|
126
127
|
part = `\${${s}}`;
|
|
127
128
|
}
|
|
129
|
+
//api/<userId>
|
|
130
|
+
else if (part[0] === "<" && part[part.length - 1] === ">") {
|
|
131
|
+
const s = part.replace(/</, "").replace(/>/, "");
|
|
132
|
+
part = `\${${s}}`;
|
|
133
|
+
}
|
|
134
|
+
//api/:userId
|
|
135
|
+
else if (part[0] === ":") {
|
|
136
|
+
const s = part.replace(/:/, "");
|
|
137
|
+
part = `\${${s}}`;
|
|
138
|
+
}
|
|
128
139
|
return part;
|
|
129
140
|
})
|
|
130
141
|
.join("/");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openapi-sync",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.19",
|
|
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",
|