@bagelink/sdk 0.0.1090 → 0.0.1094

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
@@ -127,12 +127,7 @@ function generateResponseType(responses) {
127
127
  function generateAxiosFunction(method, formattedPath, allParams, responseTypeStr, parameters, requestBodyPayload) {
128
128
  if (allParams === "undefined")
129
129
  allParams = "";
130
- const pathParams = formattedPath.match(/\$\{[^}]+\}/g)?.map((p) => p.slice(2, -1)) || [];
131
- const functionParams = allParams.split(",").filter((param) => {
132
- const paramName = param.split(":")[0].trim();
133
- return !pathParams.includes(paramName);
134
- }).join(",");
135
- let axiosFunction = `async (${functionParams})${responseTypeStr} => {`;
130
+ let axiosFunction = `async (${allParams})${responseTypeStr} => {`;
136
131
  if (requestBodyPayload === "formData") {
137
132
  const paramStr = parameters?.config?.params ? `params: {${parameters.config.params}}` : "";
138
133
  axiosFunction += `
@@ -274,52 +269,19 @@ function createFunctionPlaceholder(path, method, operation) {
274
269
  function handlePathSegment(path, operation, existingObj = {}) {
275
270
  const methods = Object.keys(operation);
276
271
  const obj = {};
277
- const pathParams = path.match(/\{[^}]+\}/g)?.map((p) => p.slice(1, -1)) || [];
278
- let currentObj = obj;
279
- for (const param of pathParams) {
280
- const paramName = toCamelCase(param);
281
- currentObj = { [paramName]: `(${paramName}: string) => ({` };
282
- }
283
272
  for (const method of methods) {
284
273
  let functionName = method.toLowerCase();
285
274
  if (hasConflict(path, method)) {
286
275
  const params = getParamsFromPath(path);
287
276
  functionName += params ? `By${toPascalCase(params.pop() || "")}` : "All";
288
277
  }
289
- let target2 = currentObj;
290
- while (typeof target2[Object.keys(target2)[0]] === "string") {
291
- target2 = JSON.parse(target2[Object.keys(target2)[0]].slice(0, -1));
292
- }
293
- target2[functionName] = createFunctionPlaceholder(path, method, operation[method]);
294
- }
295
- let target = currentObj;
296
- while (typeof target[Object.keys(target)[0]] === "string") {
297
- target[Object.keys(target)[0]] += "})";
298
- target = JSON.parse(target[Object.keys(target)[0]].slice(0, -2));
278
+ obj[functionName] = createFunctionPlaceholder(path, method, operation[method]);
299
279
  }
300
280
  return { ...obj, ...existingObj };
301
281
  }
302
282
  function generateFunctions(paths, baseUrl) {
303
283
  let tsString = "";
304
284
  const body = {};
305
- function createContextFunction(contextParam, contextValue, operations) {
306
- let funcStr = `(${contextParam}: string) => ({`;
307
- for (const [opName, operation] of Object.entries(operations)) {
308
- if (typeof operation === "function") {
309
- const opStr = operation.toString().replace(
310
- new RegExp(`${contextValue}\\??:\\s*string,?\\s*`),
311
- ""
312
- );
313
- funcStr += `
314
- ${opName}: ${opStr},`;
315
- } else {
316
- funcStr += `
317
- ${opName}: ${createContextFunction(contextParam, contextValue, operation)},`;
318
- }
319
- }
320
- funcStr += "})";
321
- return funcStr;
322
- }
323
285
  const allPathsClean = Object.keys(paths).map(cleanPath);
324
286
  for (const [path, operation] of Object.entries(paths)) {
325
287
  const splitPath = path.split("/").filter((p) => p && !/\{|\}/.test(p));
@@ -334,26 +296,15 @@ function generateFunctions(paths, baseUrl) {
334
296
  acc[objFuncKey] = createFunctionPlaceholder(path, methods[0], opp);
335
297
  } else if (index === array.length - 1) {
336
298
  acc[objFuncKey] = handlePathSegment(path, operation, acc[objFuncKey]);
337
- } else {
338
- const pathParam = path.match(new RegExp(`/${key}/\\{([^}]+)\\}`));
339
- if (pathParam) {
340
- const paramName = toCamelCase(pathParam[1]);
341
- acc[objFuncKey] = createContextFunction(paramName, paramName, acc[objFuncKey] || {});
342
- } else if (!acc[objFuncKey] || typeof acc[objFuncKey] !== "object") {
343
- acc[objFuncKey] = {};
344
- }
299
+ } else if (!acc[objFuncKey] || typeof acc[objFuncKey] !== "object") {
300
+ acc[objFuncKey] = {};
345
301
  }
346
302
  return acc[objFuncKey];
347
303
  }, body);
348
304
  }
349
305
  for (const [parent, object] of Object.entries(body)) {
350
- if (typeof object === "function") {
351
- tsString += `export const ${parent} = ${object};
306
+ tsString += `export const ${parent} = ${JSON.stringify(object, void 0, 2)};
352
307
  `;
353
- } else {
354
- tsString += `export const ${parent} = ${JSON.stringify(object, void 0, 2)};
355
- `;
356
- }
357
308
  }
358
309
  Object.entries(functionsInventory).forEach(([key, value]) => {
359
310
  tsString = tsString.replace(`"${key}"`, value);
package/dist/index.mjs CHANGED
@@ -121,12 +121,7 @@ function generateResponseType(responses) {
121
121
  function generateAxiosFunction(method, formattedPath, allParams, responseTypeStr, parameters, requestBodyPayload) {
122
122
  if (allParams === "undefined")
123
123
  allParams = "";
124
- const pathParams = formattedPath.match(/\$\{[^}]+\}/g)?.map((p) => p.slice(2, -1)) || [];
125
- const functionParams = allParams.split(",").filter((param) => {
126
- const paramName = param.split(":")[0].trim();
127
- return !pathParams.includes(paramName);
128
- }).join(",");
129
- let axiosFunction = `async (${functionParams})${responseTypeStr} => {`;
124
+ let axiosFunction = `async (${allParams})${responseTypeStr} => {`;
130
125
  if (requestBodyPayload === "formData") {
131
126
  const paramStr = parameters?.config?.params ? `params: {${parameters.config.params}}` : "";
132
127
  axiosFunction += `
@@ -268,52 +263,19 @@ function createFunctionPlaceholder(path, method, operation) {
268
263
  function handlePathSegment(path, operation, existingObj = {}) {
269
264
  const methods = Object.keys(operation);
270
265
  const obj = {};
271
- const pathParams = path.match(/\{[^}]+\}/g)?.map((p) => p.slice(1, -1)) || [];
272
- let currentObj = obj;
273
- for (const param of pathParams) {
274
- const paramName = toCamelCase(param);
275
- currentObj = { [paramName]: `(${paramName}: string) => ({` };
276
- }
277
266
  for (const method of methods) {
278
267
  let functionName = method.toLowerCase();
279
268
  if (hasConflict(path, method)) {
280
269
  const params = getParamsFromPath(path);
281
270
  functionName += params ? `By${toPascalCase(params.pop() || "")}` : "All";
282
271
  }
283
- let target2 = currentObj;
284
- while (typeof target2[Object.keys(target2)[0]] === "string") {
285
- target2 = JSON.parse(target2[Object.keys(target2)[0]].slice(0, -1));
286
- }
287
- target2[functionName] = createFunctionPlaceholder(path, method, operation[method]);
288
- }
289
- let target = currentObj;
290
- while (typeof target[Object.keys(target)[0]] === "string") {
291
- target[Object.keys(target)[0]] += "})";
292
- target = JSON.parse(target[Object.keys(target)[0]].slice(0, -2));
272
+ obj[functionName] = createFunctionPlaceholder(path, method, operation[method]);
293
273
  }
294
274
  return { ...obj, ...existingObj };
295
275
  }
296
276
  function generateFunctions(paths, baseUrl) {
297
277
  let tsString = "";
298
278
  const body = {};
299
- function createContextFunction(contextParam, contextValue, operations) {
300
- let funcStr = `(${contextParam}: string) => ({`;
301
- for (const [opName, operation] of Object.entries(operations)) {
302
- if (typeof operation === "function") {
303
- const opStr = operation.toString().replace(
304
- new RegExp(`${contextValue}\\??:\\s*string,?\\s*`),
305
- ""
306
- );
307
- funcStr += `
308
- ${opName}: ${opStr},`;
309
- } else {
310
- funcStr += `
311
- ${opName}: ${createContextFunction(contextParam, contextValue, operation)},`;
312
- }
313
- }
314
- funcStr += "})";
315
- return funcStr;
316
- }
317
279
  const allPathsClean = Object.keys(paths).map(cleanPath);
318
280
  for (const [path, operation] of Object.entries(paths)) {
319
281
  const splitPath = path.split("/").filter((p) => p && !/\{|\}/.test(p));
@@ -328,26 +290,15 @@ function generateFunctions(paths, baseUrl) {
328
290
  acc[objFuncKey] = createFunctionPlaceholder(path, methods[0], opp);
329
291
  } else if (index === array.length - 1) {
330
292
  acc[objFuncKey] = handlePathSegment(path, operation, acc[objFuncKey]);
331
- } else {
332
- const pathParam = path.match(new RegExp(`/${key}/\\{([^}]+)\\}`));
333
- if (pathParam) {
334
- const paramName = toCamelCase(pathParam[1]);
335
- acc[objFuncKey] = createContextFunction(paramName, paramName, acc[objFuncKey] || {});
336
- } else if (!acc[objFuncKey] || typeof acc[objFuncKey] !== "object") {
337
- acc[objFuncKey] = {};
338
- }
293
+ } else if (!acc[objFuncKey] || typeof acc[objFuncKey] !== "object") {
294
+ acc[objFuncKey] = {};
339
295
  }
340
296
  return acc[objFuncKey];
341
297
  }, body);
342
298
  }
343
299
  for (const [parent, object] of Object.entries(body)) {
344
- if (typeof object === "function") {
345
- tsString += `export const ${parent} = ${object};
300
+ tsString += `export const ${parent} = ${JSON.stringify(object, void 0, 2)};
346
301
  `;
347
- } else {
348
- tsString += `export const ${parent} = ${JSON.stringify(object, void 0, 2)};
349
- `;
350
- }
351
302
  }
352
303
  Object.entries(functionsInventory).forEach(([key, value]) => {
353
304
  tsString = tsString.replace(`"${key}"`, value);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bagelink/sdk",
3
3
  "type": "module",
4
- "version": "0.0.1090",
4
+ "version": "0.0.1094",
5
5
  "description": "Bagel core sdk packages",
6
6
  "author": {
7
7
  "name": "Neveh Allon",
@@ -77,16 +77,7 @@ function generateAxiosFunction(
77
77
  ): string {
78
78
  if (allParams === 'undefined') allParams = ''
79
79
 
80
- // Extract path parameters from the path using non-capturing group
81
- const pathParams = formattedPath.match(/\$\{[^}]+\}/g)?.map(p => p.slice(2, -1)) || []
82
-
83
- // Remove path parameters from allParams as they will be provided by context
84
- const functionParams = allParams.split(',').filter((param) => {
85
- const paramName = param.split(':')[0].trim()
86
- return !pathParams.includes(paramName)
87
- }).join(',')
88
-
89
- let axiosFunction = `async (${functionParams})${responseTypeStr} => {`
80
+ let axiosFunction = `async (${allParams})${responseTypeStr} => {`
90
81
 
91
82
  if (requestBodyPayload === 'formData') {
92
83
  const paramStr = parameters?.config?.params
@@ -281,106 +272,50 @@ function handlePathSegment(
281
272
  ) {
282
273
  const methods = Object.keys(operation)
283
274
  const obj: { [key: string]: any } = {}
284
-
285
- // Extract path parameters for this segment using non-capturing group
286
- const pathParams = path.match(/\{[^}]+\}/g)?.map(p => p.slice(1, -1)) || []
287
-
288
- // Create context functions for path parameters
289
- let currentObj = obj
290
- for (const param of pathParams) {
291
- const paramName = toCamelCase(param)
292
- currentObj = { [paramName]: `(${paramName}: string) => ({` }
293
- }
294
-
295
275
  for (const method of methods) {
296
276
  let functionName = method.toLowerCase()
297
277
  if (hasConflict(path, method)) {
298
278
  const params: string[] | undefined = getParamsFromPath(path)
299
279
  functionName += (params ? `By${toPascalCase(params.pop() || '')}` : 'All')
300
280
  }
301
-
302
- // Add the function to the innermost context
303
- let target = currentObj
304
- while (typeof target[Object.keys(target)[0]] === 'string') {
305
- target = JSON.parse(target[Object.keys(target)[0]].slice(0, -1))
306
- }
307
- target[functionName] = createFunctionPlaceholder(path, method, operation[method])
281
+ obj[functionName] = createFunctionPlaceholder(path, method, operation[method])
308
282
  }
309
-
310
- // Close all context function strings
311
- let target = currentObj
312
- while (typeof target[Object.keys(target)[0]] === 'string') {
313
- target[Object.keys(target)[0]] += '})'
314
- target = JSON.parse(target[Object.keys(target)[0]].slice(0, -2))
315
- }
316
-
317
283
  return { ...obj, ...existingObj }
318
284
  }
319
285
 
320
286
  export function generateFunctions(paths: PathsObject, baseUrl: string) {
321
287
  let tsString = ''
322
288
  const body: { [key: string]: any } = {}
323
-
324
- // Helper to create context-based functions
325
- function createContextFunction(contextParam: string, contextValue: string, operations: any) {
326
- let funcStr = `(${contextParam}: string) => ({`
327
- for (const [opName, operation] of Object.entries(operations)) {
328
- if (typeof operation === 'function') {
329
- // Remove the context parameter from the operation parameters
330
- const opStr = operation.toString().replace(
331
- new RegExp(`${contextValue}\\??:\\s*string,?\\s*`),
332
- ''
333
- )
334
- funcStr += `\n ${opName}: ${opStr},`
335
- } else {
336
- funcStr += `\n ${opName}: ${createContextFunction(contextParam, contextValue, operation)},`
337
- }
338
- }
339
- funcStr += '})'
340
- return funcStr
341
- }
342
-
343
289
  const allPathsClean = Object.keys(paths).map(cleanPath)
344
290
  for (const [path, operation] of Object.entries(paths)) {
345
291
  const splitPath = path.split('/').filter(p => p && !(/\{|\}/).test(p))
346
292
  splitPath.reduce((acc, key: string, index: number, array: string[]) => {
347
293
  const objFuncKey = toCamelCase(key)
348
294
  if (!objFuncKey) return acc
349
-
350
295
  const methods = Object.keys(operation)
351
- if (index === array.length - 1 && methods.length === 1 && allPathsClean.filter(p => p === cleanPath(path)).length === 1) {
296
+ if (
297
+ index === array.length - 1
298
+ && methods.length === 1
299
+ && allPathsClean.filter(p => p === cleanPath(path)).length === 1
300
+ ) {
352
301
  const method: string = methods[0]
353
302
  const opp: any = { ...operation }[method]
354
303
  acc[objFuncKey] = createFunctionPlaceholder(path, methods[0], opp)
355
- } else if (index === array.length - 1) {
356
- acc[objFuncKey] = handlePathSegment(path, operation, acc[objFuncKey])
357
- } else {
358
- // Check if this segment contains a path parameter
359
- const pathParam = path.match(new RegExp(`/${key}/\\{([^}]+)\\}`))
360
- if (pathParam) {
361
- // This segment should be a function that takes the path parameter
362
- const paramName = toCamelCase(pathParam[1])
363
- acc[objFuncKey] = createContextFunction(paramName, paramName, acc[objFuncKey] || {})
364
- } else if (!acc[objFuncKey] || typeof acc[objFuncKey] !== 'object') {
365
- acc[objFuncKey] = {}
366
- }
304
+ } else if (
305
+ index === array.length - 1
306
+ ) { acc[objFuncKey] = handlePathSegment(path, operation, acc[objFuncKey])
307
+ }
308
+ else if (!acc[objFuncKey] || typeof acc[objFuncKey] !== 'object') { acc[objFuncKey] = {}
367
309
  }
368
310
  return acc[objFuncKey]
369
311
  }, body)
370
312
  }
371
-
372
313
  for (const [parent, object] of Object.entries(body)) {
373
- if (typeof object === 'function') {
374
- tsString += `export const ${parent} = ${object};\n`
375
- } else {
376
- tsString += `export const ${parent} = ${JSON.stringify(object, undefined, 2)};\n`
377
- }
314
+ tsString += `export const ${parent} = ${JSON.stringify(object, undefined, 2)};\n`
378
315
  }
379
-
380
316
  Object.entries(functionsInventory).forEach(([key, value]) => {
381
317
  tsString = tsString.replace(`"${key}"`, value)
382
318
  })
383
-
384
319
  tsString = fileTemplate(tsString, allTypes, baseUrl)
385
320
  return tsString
386
321
  }