openapi-sync 2.1.1 → 2.1.2

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.
@@ -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.forEach((v) => {
165
- let val = JSON.stringify(v);
166
- if (val)
167
- type += `|${val}`;
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
- if (["string", "integer", "number", "array", "boolean"].includes(schema.type)) {
174
- if (["integer", "number"].includes(schema.type)) {
175
- type += `number`;
176
- }
177
- else if (schema.type === "array") {
178
- //Since we would have already parsed the arrays keys above "schema.items" if it exists
179
- type += "any[]";
180
- /* if (schema.items) {
181
- type += `${parseSchemaToType(
182
- apiDoc,
183
- schema.items,
184
- "",
185
- false,
186
- options
187
- )}[]`;
188
- } else {
189
- type += "any[]";
190
- } */
191
- }
192
- else {
193
- type += schema.type;
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
- else if (schema.type === "object") {
197
- //Since we would have already parsed the object keys above "schema.properties" if it exists
198
- if (schema.additionalProperties) {
199
- type += `{[k: string]: ${parseSchemaToType(apiDoc, schema.additionalProperties, "", true, options) || "any"}}`;
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
- type += "{[k: string]: any}";
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
- if (["string", "integer", "number", "array", "boolean"].includes(schema.type)) {
285
- if (["integer", "number"].includes(schema.type)) {
286
- type += `123`;
287
- }
288
- else if (schema.type === "array") {
289
- //Since we would have already parsed the arrays keys above "schema.items" if it exists
290
- type += "[]";
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 (schema.type === "boolean") {
293
- type += `true`;
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
- type += `"${schema.type}"`;
351
+ typeCnt += "any";
297
352
  }
298
- }
299
- else if (schema.type === "object") {
300
- //Since we would have already parsed the object keys above "schema.properties" if it exists
301
- type += "{}";
302
- }
353
+ return typeCnt;
354
+ };
355
+ type = handleType(schema.type);
303
356
  }
304
357
  }
305
358
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openapi-sync",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
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",
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: "string" | "integer" | "number" | "array" | "object" | "boolean";
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: IConfigDoc;
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: IConfigDoc;
113
+ doc?: IConfigDoc;
106
114
  };
107
115
  };
108
116