@bagelink/sdk 0.0.1125 → 0.0.1131

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/dist/index.cjs CHANGED
@@ -115,11 +115,15 @@ function collectTypeForImportStatement(typeName) {
115
115
  if (!typeName || isPrimitive) return;
116
116
  if (!allTypes.includes(typeName)) allTypes.push(typeName);
117
117
  }
118
+ function schemaToTypeWithCollection(schema) {
119
+ const type = schemaToType(schema);
120
+ collectTypeForImportStatement(type);
121
+ return type;
122
+ }
118
123
  function getResponseType(response) {
119
124
  const mediaTypeObject = response.content?.["application/json"];
120
125
  if (!mediaTypeObject || !mediaTypeObject.schema) return;
121
- const responseType = schemaToType(mediaTypeObject.schema);
122
- collectTypeForImportStatement(responseType);
126
+ const responseType = schemaToTypeWithCollection(mediaTypeObject.schema);
123
127
  return responseType;
124
128
  }
125
129
  function generateResponseType(responses) {
@@ -262,6 +266,7 @@ function generateFunctionForOperation(method, path, operation) {
262
266
  );
263
267
  }
264
268
  const generateRandomString = () => Math.random().toString(36).slice(7);
269
+ const DOUBLE_QUOTE_REGEX = /"([^"]+)":/g;
265
270
  function fileTemplate(tsString, typeForImport, baseURL) {
266
271
  const templateCode = `import ax from 'axios';
267
272
  import type { AxiosResponse } from 'axios';
@@ -275,8 +280,7 @@ function fileTemplate(tsString, typeForImport, baseURL) {
275
280
 
276
281
  export const axios = ax.create({baseURL:${baseURL}});
277
282
  ${tsString}`;
278
- const doubleQuoteRegex = /"([^"]+)":/g;
279
- return templateCode.replace(doubleQuoteRegex, "$1:");
283
+ return templateCode.replace(DOUBLE_QUOTE_REGEX, "$1:");
280
284
  }
281
285
  const functionsInventory = {};
282
286
  const pathOperations = [];
@@ -354,7 +358,15 @@ function generateTypes(schemas) {
354
358
  return `export type ${typeName} = ${schema.enum.map((item) => `'${item}'`).join(" | ")};
355
359
  `;
356
360
  }
357
- if (!schema.properties) return "";
361
+ if (schema.type === "array" && schema.items) {
362
+ return `export type ${typeName} = ${schemaToType(schema.items)}[];
363
+ `;
364
+ }
365
+ if (!schema.properties) {
366
+ console.log("No properties found for schema:", typeName);
367
+ console.log(JSON.stringify({ typeName, schema }, null, 2));
368
+ return "";
369
+ }
358
370
  const properties = Object.entries(schema.properties).map(([key, value]) => {
359
371
  const varType = formatVarType({ varName: key, schema: value });
360
372
  return ` ${varType}`;
package/dist/index.mjs CHANGED
@@ -109,11 +109,15 @@ function collectTypeForImportStatement(typeName) {
109
109
  if (!typeName || isPrimitive) return;
110
110
  if (!allTypes.includes(typeName)) allTypes.push(typeName);
111
111
  }
112
+ function schemaToTypeWithCollection(schema) {
113
+ const type = schemaToType(schema);
114
+ collectTypeForImportStatement(type);
115
+ return type;
116
+ }
112
117
  function getResponseType(response) {
113
118
  const mediaTypeObject = response.content?.["application/json"];
114
119
  if (!mediaTypeObject || !mediaTypeObject.schema) return;
115
- const responseType = schemaToType(mediaTypeObject.schema);
116
- collectTypeForImportStatement(responseType);
120
+ const responseType = schemaToTypeWithCollection(mediaTypeObject.schema);
117
121
  return responseType;
118
122
  }
119
123
  function generateResponseType(responses) {
@@ -256,6 +260,7 @@ function generateFunctionForOperation(method, path, operation) {
256
260
  );
257
261
  }
258
262
  const generateRandomString = () => Math.random().toString(36).slice(7);
263
+ const DOUBLE_QUOTE_REGEX = /"([^"]+)":/g;
259
264
  function fileTemplate(tsString, typeForImport, baseURL) {
260
265
  const templateCode = `import ax from 'axios';
261
266
  import type { AxiosResponse } from 'axios';
@@ -269,8 +274,7 @@ function fileTemplate(tsString, typeForImport, baseURL) {
269
274
 
270
275
  export const axios = ax.create({baseURL:${baseURL}});
271
276
  ${tsString}`;
272
- const doubleQuoteRegex = /"([^"]+)":/g;
273
- return templateCode.replace(doubleQuoteRegex, "$1:");
277
+ return templateCode.replace(DOUBLE_QUOTE_REGEX, "$1:");
274
278
  }
275
279
  const functionsInventory = {};
276
280
  const pathOperations = [];
@@ -348,7 +352,15 @@ function generateTypes(schemas) {
348
352
  return `export type ${typeName} = ${schema.enum.map((item) => `'${item}'`).join(" | ")};
349
353
  `;
350
354
  }
351
- if (!schema.properties) return "";
355
+ if (schema.type === "array" && schema.items) {
356
+ return `export type ${typeName} = ${schemaToType(schema.items)}[];
357
+ `;
358
+ }
359
+ if (!schema.properties) {
360
+ console.log("No properties found for schema:", typeName);
361
+ console.log(JSON.stringify({ typeName, schema }, null, 2));
362
+ return "";
363
+ }
352
364
  const properties = Object.entries(schema.properties).map(([key, value]) => {
353
365
  const varType = formatVarType({ varName: key, schema: value });
354
366
  return ` ${varType}`;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bagelink/sdk",
3
3
  "type": "module",
4
- "version": "0.0.1125",
4
+ "version": "0.0.1131",
5
5
  "description": "Bagel core sdk packages",
6
6
  "author": {
7
7
  "name": "Neveh Allon",
@@ -5,6 +5,7 @@ import type {
5
5
  RequestBodyObject,
6
6
  ResponseObject,
7
7
  ResponsesObject,
8
+ SchemaObject,
8
9
  } from './openApiTypes'
9
10
 
10
11
  import {
@@ -44,11 +45,16 @@ function collectTypeForImportStatement(typeName: string) {
44
45
  if (!allTypes.includes(typeName)) allTypes.push(typeName)
45
46
  }
46
47
 
48
+ function schemaToTypeWithCollection(schema?: SchemaObject): string {
49
+ const type = schemaToType(schema)
50
+ collectTypeForImportStatement(type)
51
+ return type
52
+ }
53
+
47
54
  function getResponseType(response: ResponseObject): string | undefined {
48
55
  const mediaTypeObject = response.content?.['application/json']
49
56
  if (!mediaTypeObject || !mediaTypeObject.schema) return
50
- const responseType = schemaToType(mediaTypeObject.schema)
51
- collectTypeForImportStatement(responseType)
57
+ const responseType = schemaToTypeWithCollection(mediaTypeObject.schema)
52
58
  return responseType
53
59
  }
54
60
 
@@ -280,6 +286,8 @@ function generateFunctionForOperation(
280
286
 
281
287
  const generateRandomString = () => Math.random().toString(36).slice(7)
282
288
 
289
+ const DOUBLE_QUOTE_REGEX = /"([^"]+)":/g // ! TODO: @nevehallon believes this may not be necessary
290
+
283
291
  function fileTemplate(
284
292
  tsString: string,
285
293
  typeForImport: string[],
@@ -297,8 +305,7 @@ function fileTemplate(
297
305
 
298
306
  export const axios = ax.create({baseURL:${baseURL}});
299
307
  ${tsString}`
300
- const doubleQuoteRegex = /"([^"]+)":/g
301
- return templateCode.replace(doubleQuoteRegex, '$1:')
308
+ return templateCode.replace(DOUBLE_QUOTE_REGEX, '$1:')
302
309
  }
303
310
 
304
311
  interface PathOperation {
@@ -1,21 +1,38 @@
1
1
  import type { SchemasObject } from './openApiTypes'
2
- import { formatType, formatVarType } from './utils'
2
+ import { formatType, formatVarType, schemaToType } from './utils'
3
3
 
4
4
  export function generateTypes(schemas: SchemasObject): string {
5
- return Object.entries(schemas).map(([typeName, schema]) => {
6
- typeName = formatType(typeName)
5
+ return Object.entries(schemas)
6
+ .map(([typeName, schema]) => {
7
+ typeName = formatType(typeName)
7
8
 
8
- if (schema.enum) {
9
- return `export type ${typeName} = ${schema.enum.map((item: string) => `'${item}'`).join(' | ')};\n`
10
- }
11
- if (!schema.properties) return ''
9
+ if (schema.enum) {
10
+ return `export type ${typeName} = ${schema.enum.map((item: string) => `'${item}'`).join(' | ')};\n`
11
+ }
12
12
 
13
- const properties = Object.entries(schema.properties).map(([key, value]) => {
14
- const varType = formatVarType({ varName: key, schema: value })
13
+ if (schema.type === 'array' && schema.items) {
14
+ return `export type ${typeName} = ${schemaToType(schema.items)}[];\n`
15
+ }
15
16
 
16
- return `\t\t${varType}`
17
- }).join(';\n ')
17
+ if (!schema.properties) {
18
+ // #region Debug
19
+ console.log('No properties found for schema:', typeName)
18
20
 
19
- return `export type ${typeName} = {\n ${properties};\n };\n`
20
- }).join('\n')
21
+ console.log(JSON.stringify({ typeName, schema }, null, 2))
22
+ // #endregion Debug
23
+
24
+ return ''
25
+ }
26
+
27
+ const properties = Object.entries(schema.properties)
28
+ .map(([key, value]) => {
29
+ const varType = formatVarType({ varName: key, schema: value })
30
+
31
+ return `\t\t${varType}`
32
+ })
33
+ .join(';\n ')
34
+
35
+ return `export type ${typeName} = {\n ${properties};\n };\n`
36
+ })
37
+ .join('\n')
21
38
  }