@bagelink/sdk 1.4.26 → 1.4.28

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
@@ -6,6 +6,20 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
6
6
 
7
7
  const axios__default = /*#__PURE__*/_interopDefaultCompat(axios$1);
8
8
 
9
+ function getPath(pathsObject, path) {
10
+ return pathsObject ? pathsObject[path] : void 0;
11
+ }
12
+ function isReferenceObject(obj) {
13
+ if (!obj) return false;
14
+ return Object.prototype.hasOwnProperty.call(obj, "$ref");
15
+ }
16
+ function isSchemaObject(schema) {
17
+ return !Object.prototype.hasOwnProperty.call(schema, "$ref");
18
+ }
19
+ function dereference(property) {
20
+ return property;
21
+ }
22
+
9
23
  function toCamelCase(str) {
10
24
  return str?.replaceAll(/[-._\s]+(.)?/g, (_, c) => c?.toUpperCase() || "").replace(/^./, (str2) => str2.toLowerCase()) || str || "";
11
25
  }
@@ -38,6 +52,13 @@ function handleArrayItems(schema) {
38
52
  }
39
53
  function schemaToType(schema) {
40
54
  if (!schema) return "any";
55
+ if (isReferenceObject(schema)) {
56
+ if (schema.$ref) return resolveReference(schema.$ref);
57
+ }
58
+ if (!isSchemaObject(schema)) {
59
+ console.warn("Schema is not a SchemaObject:", schema);
60
+ return "any";
61
+ }
41
62
  if (schema.anyOf) {
42
63
  let _t = schema.anyOf.map((s) => schemaToType(s)).filter((p) => p !== "any").join(" | ");
43
64
  if (_t === "" || _t === "null") _t = "any";
@@ -46,7 +67,6 @@ function schemaToType(schema) {
46
67
  if (schema.allOf) {
47
68
  return schema.allOf.map((s) => schemaToType(s)).filter((p) => p !== "any").join(" & ");
48
69
  }
49
- if (schema.$ref) return resolveReference(schema.$ref);
50
70
  switch (schema.type) {
51
71
  case "object":
52
72
  return "{ [key: string]: any }";
@@ -79,7 +99,7 @@ function isOptional(schema) {
79
99
  const splitType = type.split(/\s+\|\s+/);
80
100
  const includesNull = splitType.includes("null");
81
101
  const includesUndefined = splitType.includes("undefined");
82
- return includesNull || includesUndefined || schema.default !== void 0;
102
+ return includesNull || includesUndefined || schema?.default !== void 0;
83
103
  }
84
104
  function cleanOptionals(str) {
85
105
  return str.split(" | ").filter((t) => t !== "null" && t !== "undefined").join(" | ");
@@ -142,6 +162,9 @@ function schemaToTypeWithCollection(schema) {
142
162
  return type;
143
163
  }
144
164
  function getResponseType(response) {
165
+ if (isReferenceObject(response)) {
166
+ return void 0;
167
+ }
145
168
  const mediaTypeObject = response.content?.["application/json"];
146
169
  if (!mediaTypeObject?.schema) return void 0;
147
170
  return schemaToTypeWithCollection(mediaTypeObject.schema);
@@ -169,12 +192,13 @@ function formatPathWithParams(path) {
169
192
  return `\`${path.replace(/\{([^}]+)\}/g, (_, paramName) => `\${${toCamelCase(paramName)}}`)}\``;
170
193
  }
171
194
  function generateRequestBody(requestBody) {
172
- if (!requestBody?.content)
195
+ const content = dereference(requestBody)?.content;
196
+ if (!content || Object.keys(content).length === 0) {
173
197
  return { requestBodyParam: "", requestBodyPayload: "" };
174
- const { content } = requestBody;
198
+ }
175
199
  if (content["multipart/form-data"]) {
176
200
  const multipartFormData = content["multipart/form-data"];
177
- if (multipartFormData.schema?.$ref?.includes("Body_upload_files")) {
201
+ if (isReferenceObject(multipartFormData.schema) && multipartFormData.schema?.$ref?.includes("Body_upload_files")) {
178
202
  return {
179
203
  requestBodyParam: "file: File, options?: UploadOptions & { dirPath?: string, tags?: string[] }",
180
204
  requestBodyPayload: "formData"
@@ -189,7 +213,7 @@ function generateRequestBody(requestBody) {
189
213
  if (!jsonContent || !jsonContent.schema) {
190
214
  return { requestBodyParam: "", requestBodyPayload: "" };
191
215
  }
192
- const bodySchema = jsonContent.schema;
216
+ const bodySchema = dereference(jsonContent.schema);
193
217
  const requestBodyType = schemaToType(bodySchema);
194
218
  collectTypeForImportStatement(requestBodyType);
195
219
  const requestBodyPayload = toCamelCase(bodySchema.title) || toCamelCase(requestBodyType) || "requestBody";
@@ -213,8 +237,10 @@ function generateFunctionParameters(params, isFileUpload = false) {
213
237
  }
214
238
  const functionParams = [];
215
239
  const queryParams = [];
216
- for (const param of params) {
217
- const paramType = schemaToType(param.schema);
240
+ for (const p of params) {
241
+ const param = dereference(p);
242
+ const pSchema = dereference(param.schema);
243
+ const paramType = schemaToType(pSchema);
218
244
  collectTypeForImportStatement(paramType);
219
245
  const paramName = param.name;
220
246
  const varName = toCamelCase(param.name);
@@ -222,9 +248,9 @@ function generateFunctionParameters(params, isFileUpload = false) {
222
248
  functionParams.push(
223
249
  formatVarType({
224
250
  varName,
225
- schema: param.schema,
251
+ schema: pSchema,
226
252
  required: param.required,
227
- defaultValue: param.schema.default
253
+ defaultValue: pSchema?.default
228
254
  })
229
255
  );
230
256
  }
@@ -299,7 +325,8 @@ function buildJSDocComment(operation, method, path) {
299
325
  }
300
326
  function generateFunctionForOperation(method, path, operation) {
301
327
  if (!operation) return "";
302
- const isFileUpload = operation.requestBody?.content["multipart/form-data"]?.schema?.$ref?.includes("Body_upload_files");
328
+ const multiPartSchema = dereference(operation.requestBody)?.content?.["multipart/form-data"]?.schema;
329
+ const isFileUpload = isReferenceObject(multiPartSchema) && multiPartSchema?.$ref?.includes("Body_upload_files");
303
330
  const parameters = generateFunctionParameters(
304
331
  operation.parameters,
305
332
  isFileUpload
@@ -425,13 +452,16 @@ ${tsString}`;
425
452
  }
426
453
 
427
454
  function generateTypes(schemas) {
428
- return Object.entries(schemas).map(([typeName, schema]) => {
429
- typeName = formatType(typeName);
430
- if (schema.enum) {
455
+ if (!schemas) return "";
456
+ const schemaEntries = Object.entries(schemas);
457
+ return schemaEntries.map(([_typeName, schema]) => {
458
+ const typeName = formatType(_typeName);
459
+ if (!isSchemaObject(schema)) return "";
460
+ if (schema?.enum) {
431
461
  return `export type ${typeName} = ${schema.enum.map((item) => `'${item}'`).join(" | ")};
432
462
  `;
433
463
  }
434
- if (schema.type === "array" && schema.items) {
464
+ if (schema?.type === "array" && schema.items) {
435
465
  return `export type ${typeName} = (${schemaToType(schema.items)})[];
436
466
  `;
437
467
  }
@@ -576,7 +606,6 @@ function responses(key) {
576
606
  class BagelAuth {
577
607
  constructor(bagel) {
578
608
  this.bagel = bagel;
579
- this.bagel = bagel;
580
609
  }
581
610
  user = void 0;
582
611
  async validateUser() {
@@ -740,5 +769,9 @@ class Bagel {
740
769
  }
741
770
 
742
771
  exports.Bagel = Bagel;
772
+ exports.dereference = dereference;
743
773
  exports.formatAPIErrorMessage = formatAPIErrorMessage;
774
+ exports.getPath = getPath;
775
+ exports.isReferenceObject = isReferenceObject;
776
+ exports.isSchemaObject = isSchemaObject;
744
777
  exports.openAPI = index;
package/dist/index.d.cts CHANGED
@@ -1,3 +1,355 @@
1
+ declare function getPath(pathsObject: PathsObject | undefined, path: string): PathItemObject | undefined;
2
+ /**
3
+ * A type guard to check if the given value is a `ReferenceObject`.
4
+ * See https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types
5
+ *
6
+ * @param obj The value to check.
7
+ */
8
+ declare function isReferenceObject(obj: any): obj is ReferenceObject;
9
+ /**
10
+ * A type guard to check if the given object is a `SchemaObject`.
11
+ * Useful to distinguish from `ReferenceObject` values that can be used
12
+ * in most places where `SchemaObject` is allowed.
13
+ *
14
+ * See https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types
15
+ *
16
+ * @param schema The value to check.
17
+ */
18
+ declare function isSchemaObject(schema: SchemaObject | ReferenceObject): schema is SchemaObject;
19
+ declare function dereference<T>(property: ReferenceObject | T): Exclude<T, ReferenceObject>;
20
+
21
+ interface ServerObject {
22
+ url: string;
23
+ description?: string;
24
+ variables?: {
25
+ [v: string]: ServerVariableObject;
26
+ };
27
+ }
28
+ interface ServerVariableObject {
29
+ enum?: string[] | boolean[] | number[];
30
+ default: string | boolean | number;
31
+ description?: string;
32
+ }
33
+ interface OpenAPIObject {
34
+ openapi: string;
35
+ info: InfoObject;
36
+ servers?: ServerObject[];
37
+ paths?: PathsObject;
38
+ components?: ComponentsObject;
39
+ security?: SecurityRequirementObject[];
40
+ tags?: TagObject[];
41
+ externalDocs?: ExternalDocumentationObject;
42
+ /** Webhooks added in v. 3.1.0 */
43
+ webhooks?: PathsObject;
44
+ }
45
+ interface InfoObject {
46
+ title: string;
47
+ description?: string;
48
+ termsOfService?: string;
49
+ contact?: ContactObject;
50
+ license?: LicenseObject;
51
+ version: string;
52
+ }
53
+ interface ContactObject {
54
+ name?: string;
55
+ url?: string;
56
+ email?: string;
57
+ }
58
+ interface LicenseObject {
59
+ name: string;
60
+ identifier?: string;
61
+ url?: string;
62
+ }
63
+ interface ComponentsObject {
64
+ schemas?: {
65
+ [schema: string]: SchemaObject | ReferenceObject;
66
+ };
67
+ responses?: {
68
+ [response: string]: ResponseObject | ReferenceObject;
69
+ };
70
+ parameters?: {
71
+ [parameter: string]: ParameterObject | ReferenceObject;
72
+ };
73
+ examples?: {
74
+ [example: string]: ExampleObject | ReferenceObject;
75
+ };
76
+ requestBodies?: {
77
+ [request: string]: RequestBodyObject | ReferenceObject;
78
+ };
79
+ headers?: {
80
+ [header: string]: HeaderObject | ReferenceObject;
81
+ };
82
+ securitySchemes?: {
83
+ [securityScheme: string]: SecuritySchemeObject | ReferenceObject;
84
+ };
85
+ links?: {
86
+ [link: string]: LinkObject | ReferenceObject;
87
+ };
88
+ callbacks?: {
89
+ [callback: string]: CallbackObject | ReferenceObject;
90
+ };
91
+ }
92
+ /**
93
+ * Rename it to Paths Object to be consistent with the spec
94
+ * See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#pathsObject
95
+ */
96
+ interface PathsObject {
97
+ [path: string]: PathItemObject;
98
+ }
99
+ interface PathItemObject {
100
+ $ref?: string;
101
+ summary?: string;
102
+ description?: string;
103
+ get?: OperationObject;
104
+ put?: OperationObject;
105
+ post?: OperationObject;
106
+ delete?: OperationObject;
107
+ options?: OperationObject;
108
+ head?: OperationObject;
109
+ patch?: OperationObject;
110
+ trace?: OperationObject;
111
+ servers?: ServerObject[];
112
+ parameters?: (ParameterObject | ReferenceObject)[];
113
+ }
114
+ interface OperationObject {
115
+ tags?: string[];
116
+ summary?: string;
117
+ description?: string;
118
+ externalDocs?: ExternalDocumentationObject;
119
+ operationId?: string;
120
+ parameters?: (ParameterObject | ReferenceObject)[];
121
+ requestBody?: RequestBodyObject | ReferenceObject;
122
+ responses?: ResponsesObject;
123
+ callbacks?: CallbacksObject;
124
+ deprecated?: boolean;
125
+ security?: SecurityRequirementObject[];
126
+ servers?: ServerObject[];
127
+ }
128
+ interface ExternalDocumentationObject {
129
+ description?: string;
130
+ url: string;
131
+ }
132
+ /**
133
+ * The location of a parameter.
134
+ * Possible values are "query", "header", "path" or "cookie".
135
+ * Specification:
136
+ * https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#parameter-locations
137
+ */
138
+ type ParameterLocation = 'query' | 'header' | 'path' | 'cookie';
139
+ /**
140
+ * The style of a parameter.
141
+ * Describes how the parameter value will be serialized.
142
+ * (serialization is not implemented yet)
143
+ * Specification:
144
+ * https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#style-values
145
+ */
146
+ type ParameterStyle = 'matrix' | 'label' | 'form' | 'simple' | 'spaceDelimited' | 'pipeDelimited' | 'deepObject';
147
+ interface BaseParameterObject {
148
+ description?: string;
149
+ required?: boolean;
150
+ deprecated?: boolean;
151
+ allowEmptyValue?: boolean;
152
+ style?: ParameterStyle;
153
+ explode?: boolean;
154
+ allowReserved?: boolean;
155
+ schema?: SchemaObject | ReferenceObject;
156
+ examples?: {
157
+ [param: string]: ExampleObject | ReferenceObject;
158
+ };
159
+ example?: any;
160
+ content?: ContentObject;
161
+ }
162
+ interface ParameterObject extends BaseParameterObject {
163
+ name: string;
164
+ in: ParameterLocation;
165
+ }
166
+ interface RequestBodyObject {
167
+ description?: string;
168
+ content: ContentObject;
169
+ required?: boolean;
170
+ }
171
+ interface ContentObject {
172
+ [mediatype: string]: MediaTypeObject;
173
+ }
174
+ interface MediaTypeObject {
175
+ schema?: SchemaObject | ReferenceObject;
176
+ examples?: ExamplesObject;
177
+ example?: any;
178
+ encoding?: EncodingObject;
179
+ }
180
+ interface EncodingObject {
181
+ [property: string]: EncodingPropertyObject;
182
+ }
183
+ interface EncodingPropertyObject {
184
+ contentType?: string;
185
+ headers?: {
186
+ [key: string]: HeaderObject | ReferenceObject;
187
+ };
188
+ style?: string;
189
+ explode?: boolean;
190
+ allowReserved?: boolean;
191
+ }
192
+ interface ResponsesObject {
193
+ [statuscode: string]: ResponseObject | ReferenceObject;
194
+ }
195
+ interface ResponseObject {
196
+ description: string;
197
+ headers?: HeadersObject;
198
+ content?: ContentObject;
199
+ links?: LinksObject;
200
+ }
201
+ interface CallbacksObject {
202
+ [name: string]: CallbackObject | ReferenceObject;
203
+ }
204
+ interface CallbackObject {
205
+ [name: string]: PathItemObject;
206
+ }
207
+ interface HeadersObject {
208
+ [name: string]: HeaderObject | ReferenceObject;
209
+ }
210
+ interface ExampleObject {
211
+ summary?: string;
212
+ description?: string;
213
+ value?: any;
214
+ externalValue?: string;
215
+ }
216
+ interface LinksObject {
217
+ [name: string]: LinkObject | ReferenceObject;
218
+ }
219
+ interface LinkObject {
220
+ operationRef?: string;
221
+ operationId?: string;
222
+ parameters?: LinkParametersObject;
223
+ requestBody?: any | string;
224
+ description?: string;
225
+ server?: ServerObject;
226
+ }
227
+ interface LinkParametersObject {
228
+ [name: string]: any | string;
229
+ }
230
+ interface HeaderObject extends BaseParameterObject {
231
+ $ref?: string;
232
+ }
233
+ interface TagObject {
234
+ name: string;
235
+ description?: string;
236
+ externalDocs?: ExternalDocumentationObject;
237
+ }
238
+ interface ExamplesObject {
239
+ [name: string]: ExampleObject | ReferenceObject;
240
+ }
241
+ interface ReferenceObject {
242
+ $ref: string;
243
+ summary?: string;
244
+ description?: string;
245
+ }
246
+ type SchemaObjectType = 'integer' | 'number' | 'string' | 'boolean' | 'object' | 'null' | 'array' | 'undefined';
247
+ declare const TYPE_FORMATS: readonly ["uuid", "uri", "date-time", "date", "email", "binary", "password", "phone", "uuid4", "int32", "int64", "float", "double", "byte"];
248
+ type TypeFormatT = (typeof TYPE_FORMATS)[number] | (string & {});
249
+ interface SchemaObject {
250
+ discriminator?: DiscriminatorObject;
251
+ readOnly?: boolean;
252
+ writeOnly?: boolean;
253
+ xml?: XmlObject;
254
+ externalDocs?: ExternalDocumentationObject;
255
+ /** @deprecated use examples instead */
256
+ example?: any;
257
+ examples?: any[];
258
+ deprecated?: boolean;
259
+ type?: SchemaObjectType | SchemaObjectType[];
260
+ format?: TypeFormatT;
261
+ allOf?: (SchemaObject | ReferenceObject)[];
262
+ oneOf?: (SchemaObject | ReferenceObject)[];
263
+ anyOf?: (SchemaObject | ReferenceObject)[];
264
+ not?: SchemaObject | ReferenceObject;
265
+ items?: SchemaObject | ReferenceObject;
266
+ properties?: {
267
+ [propertyName: string]: SchemaObject | ReferenceObject;
268
+ };
269
+ additionalProperties?: SchemaObject | ReferenceObject | boolean;
270
+ propertyNames?: SchemaObject | ReferenceObject;
271
+ description?: string;
272
+ default?: any;
273
+ title?: string;
274
+ multipleOf?: number;
275
+ maximum?: number;
276
+ const?: any;
277
+ /** @desc In OpenAPI 3.1: number */
278
+ exclusiveMaximum?: number;
279
+ minimum?: number;
280
+ /** @desc In OpenAPI 3.1: number */
281
+ exclusiveMinimum?: number;
282
+ maxLength?: number;
283
+ minLength?: number;
284
+ pattern?: string;
285
+ maxItems?: number;
286
+ minItems?: number;
287
+ uniqueItems?: boolean;
288
+ maxProperties?: number;
289
+ minProperties?: number;
290
+ required?: string[];
291
+ enum?: any[];
292
+ prefixItems?: (SchemaObject | ReferenceObject)[];
293
+ /**
294
+ * @desc JSON Schema compliant Content-Type, optional when specified as a key of ContentObject
295
+ * @example image/png
296
+ */
297
+ contentMediaType?: string;
298
+ /**
299
+ * @desc Specifies the Content-Encoding for the schema, supports all encodings from RFC4648, and "quoted-printable" from RFC2045
300
+ * @override
301
+ * @see https://datatracker.ietf.org/doc/html/rfc4648
302
+ * @see https://datatracker.ietf.org/doc/html/rfc2045#section-6.7
303
+ * @example base64
304
+ */
305
+ contentEncoding?: string;
306
+ }
307
+ interface SchemasObject {
308
+ [schema: string]: SchemaObject;
309
+ }
310
+ interface DiscriminatorObject {
311
+ propertyName: string;
312
+ mapping?: {
313
+ [key: string]: string;
314
+ };
315
+ }
316
+ interface XmlObject {
317
+ name?: string;
318
+ namespace?: string;
319
+ prefix?: string;
320
+ attribute?: boolean;
321
+ wrapped?: boolean;
322
+ }
323
+ type SecuritySchemeType = 'apiKey' | 'http' | 'oauth2' | 'openIdConnect';
324
+ interface SecuritySchemeObject {
325
+ type: SecuritySchemeType;
326
+ description?: string;
327
+ name?: string;
328
+ in?: string;
329
+ scheme?: string;
330
+ bearerFormat?: string;
331
+ flows?: OAuthFlowsObject;
332
+ openIdConnectUrl?: string;
333
+ }
334
+ interface OAuthFlowsObject {
335
+ implicit?: OAuthFlowObject;
336
+ password?: OAuthFlowObject;
337
+ clientCredentials?: OAuthFlowObject;
338
+ authorizationCode?: OAuthFlowObject;
339
+ }
340
+ interface OAuthFlowObject {
341
+ authorizationUrl?: string;
342
+ tokenUrl?: string;
343
+ refreshUrl?: string;
344
+ scopes: ScopesObject;
345
+ }
346
+ interface ScopesObject {
347
+ [scope: string]: any;
348
+ }
349
+ interface SecurityRequirementObject {
350
+ [name: string]: string[];
351
+ }
352
+
1
353
  interface OpenAPIResponse {
2
354
  types: string;
3
355
  code: string;
@@ -8,7 +360,6 @@ declare function formatAPIErrorMessage(err: unknown): string;
8
360
 
9
361
  type Tables = '';
10
362
  type TableToTypeMapping = Record<Tables, any>;
11
-
12
363
  interface User {
13
364
  id: string;
14
365
  first_name?: string;
@@ -108,4 +459,4 @@ declare class Bagel {
108
459
  uploadFile<T>(file: File, options?: UploadOptions): Promise<T>;
109
460
  }
110
461
 
111
- export { Bagel, type TableToTypeMapping, type Tables, type UploadOptions, type User, formatAPIErrorMessage, _default as openAPI };
462
+ export { Bagel, type BaseParameterObject, type CallbackObject, type CallbacksObject, type ComponentsObject, type ContactObject, type ContentObject, type DiscriminatorObject, type EncodingObject, type EncodingPropertyObject, type ExampleObject, type ExamplesObject, type ExternalDocumentationObject, type HeaderObject, type HeadersObject, type InfoObject, type LicenseObject, type LinkObject, type LinkParametersObject, type LinksObject, type MediaTypeObject, type OAuthFlowObject, type OAuthFlowsObject, type OpenAPIObject, type OperationObject, type ParameterLocation, type ParameterObject, type ParameterStyle, type PathItemObject, type PathsObject, type ReferenceObject, type RequestBodyObject, type ResponseObject, type ResponsesObject, type SchemaObject, type SchemaObjectType, type SchemasObject, type ScopesObject, type SecurityRequirementObject, type SecuritySchemeObject, type SecuritySchemeType, type ServerObject, type ServerVariableObject, type TableToTypeMapping, type Tables, type TagObject, type TypeFormatT, type UploadOptions, type User, type XmlObject, dereference, formatAPIErrorMessage, getPath, isReferenceObject, isSchemaObject, _default as openAPI };