@bagelink/sdk 0.0.1082 → 0.0.1090

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,7 +127,12 @@ function generateResponseType(responses) {
127
127
  function generateAxiosFunction(method, formattedPath, allParams, responseTypeStr, parameters, requestBodyPayload) {
128
128
  if (allParams === "undefined")
129
129
  allParams = "";
130
- let axiosFunction = `async (${allParams})${responseTypeStr} => {`;
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} => {`;
131
136
  if (requestBodyPayload === "formData") {
132
137
  const paramStr = parameters?.config?.params ? `params: {${parameters.config.params}}` : "";
133
138
  axiosFunction += `
@@ -269,19 +274,52 @@ function createFunctionPlaceholder(path, method, operation) {
269
274
  function handlePathSegment(path, operation, existingObj = {}) {
270
275
  const methods = Object.keys(operation);
271
276
  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
+ }
272
283
  for (const method of methods) {
273
284
  let functionName = method.toLowerCase();
274
285
  if (hasConflict(path, method)) {
275
286
  const params = getParamsFromPath(path);
276
287
  functionName += params ? `By${toPascalCase(params.pop() || "")}` : "All";
277
288
  }
278
- obj[functionName] = createFunctionPlaceholder(path, method, operation[method]);
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));
279
299
  }
280
300
  return { ...obj, ...existingObj };
281
301
  }
282
302
  function generateFunctions(paths, baseUrl) {
283
303
  let tsString = "";
284
304
  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
+ }
285
323
  const allPathsClean = Object.keys(paths).map(cleanPath);
286
324
  for (const [path, operation] of Object.entries(paths)) {
287
325
  const splitPath = path.split("/").filter((p) => p && !/\{|\}/.test(p));
@@ -296,15 +334,26 @@ function generateFunctions(paths, baseUrl) {
296
334
  acc[objFuncKey] = createFunctionPlaceholder(path, methods[0], opp);
297
335
  } else if (index === array.length - 1) {
298
336
  acc[objFuncKey] = handlePathSegment(path, operation, acc[objFuncKey]);
299
- } else if (!acc[objFuncKey] || typeof acc[objFuncKey] !== "object") {
300
- 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
+ }
301
345
  }
302
346
  return acc[objFuncKey];
303
347
  }, body);
304
348
  }
305
349
  for (const [parent, object] of Object.entries(body)) {
306
- tsString += `export const ${parent} = ${JSON.stringify(object, void 0, 2)};
350
+ if (typeof object === "function") {
351
+ tsString += `export const ${parent} = ${object};
307
352
  `;
353
+ } else {
354
+ tsString += `export const ${parent} = ${JSON.stringify(object, void 0, 2)};
355
+ `;
356
+ }
308
357
  }
309
358
  Object.entries(functionsInventory).forEach(([key, value]) => {
310
359
  tsString = tsString.replace(`"${key}"`, value);
package/dist/index.mjs CHANGED
@@ -121,7 +121,12 @@ function generateResponseType(responses) {
121
121
  function generateAxiosFunction(method, formattedPath, allParams, responseTypeStr, parameters, requestBodyPayload) {
122
122
  if (allParams === "undefined")
123
123
  allParams = "";
124
- let axiosFunction = `async (${allParams})${responseTypeStr} => {`;
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} => {`;
125
130
  if (requestBodyPayload === "formData") {
126
131
  const paramStr = parameters?.config?.params ? `params: {${parameters.config.params}}` : "";
127
132
  axiosFunction += `
@@ -263,19 +268,52 @@ function createFunctionPlaceholder(path, method, operation) {
263
268
  function handlePathSegment(path, operation, existingObj = {}) {
264
269
  const methods = Object.keys(operation);
265
270
  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
+ }
266
277
  for (const method of methods) {
267
278
  let functionName = method.toLowerCase();
268
279
  if (hasConflict(path, method)) {
269
280
  const params = getParamsFromPath(path);
270
281
  functionName += params ? `By${toPascalCase(params.pop() || "")}` : "All";
271
282
  }
272
- obj[functionName] = createFunctionPlaceholder(path, method, operation[method]);
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));
273
293
  }
274
294
  return { ...obj, ...existingObj };
275
295
  }
276
296
  function generateFunctions(paths, baseUrl) {
277
297
  let tsString = "";
278
298
  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
+ }
279
317
  const allPathsClean = Object.keys(paths).map(cleanPath);
280
318
  for (const [path, operation] of Object.entries(paths)) {
281
319
  const splitPath = path.split("/").filter((p) => p && !/\{|\}/.test(p));
@@ -290,15 +328,26 @@ function generateFunctions(paths, baseUrl) {
290
328
  acc[objFuncKey] = createFunctionPlaceholder(path, methods[0], opp);
291
329
  } else if (index === array.length - 1) {
292
330
  acc[objFuncKey] = handlePathSegment(path, operation, acc[objFuncKey]);
293
- } else if (!acc[objFuncKey] || typeof acc[objFuncKey] !== "object") {
294
- 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
+ }
295
339
  }
296
340
  return acc[objFuncKey];
297
341
  }, body);
298
342
  }
299
343
  for (const [parent, object] of Object.entries(body)) {
300
- tsString += `export const ${parent} = ${JSON.stringify(object, void 0, 2)};
344
+ if (typeof object === "function") {
345
+ tsString += `export const ${parent} = ${object};
301
346
  `;
347
+ } else {
348
+ tsString += `export const ${parent} = ${JSON.stringify(object, void 0, 2)};
349
+ `;
350
+ }
302
351
  }
303
352
  Object.entries(functionsInventory).forEach(([key, value]) => {
304
353
  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.1082",
4
+ "version": "0.0.1090",
5
5
  "description": "Bagel core sdk packages",
6
6
  "author": {
7
7
  "name": "Neveh Allon",
@@ -77,7 +77,16 @@ function generateAxiosFunction(
77
77
  ): string {
78
78
  if (allParams === 'undefined') allParams = ''
79
79
 
80
- let axiosFunction = `async (${allParams})${responseTypeStr} => {`
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} => {`
81
90
 
82
91
  if (requestBodyPayload === 'formData') {
83
92
  const paramStr = parameters?.config?.params
@@ -272,50 +281,106 @@ function handlePathSegment(
272
281
  ) {
273
282
  const methods = Object.keys(operation)
274
283
  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
+
275
295
  for (const method of methods) {
276
296
  let functionName = method.toLowerCase()
277
297
  if (hasConflict(path, method)) {
278
298
  const params: string[] | undefined = getParamsFromPath(path)
279
299
  functionName += (params ? `By${toPascalCase(params.pop() || '')}` : 'All')
280
300
  }
281
- obj[functionName] = createFunctionPlaceholder(path, method, operation[method])
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])
282
308
  }
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
+
283
317
  return { ...obj, ...existingObj }
284
318
  }
285
319
 
286
320
  export function generateFunctions(paths: PathsObject, baseUrl: string) {
287
321
  let tsString = ''
288
322
  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
+
289
343
  const allPathsClean = Object.keys(paths).map(cleanPath)
290
344
  for (const [path, operation] of Object.entries(paths)) {
291
345
  const splitPath = path.split('/').filter(p => p && !(/\{|\}/).test(p))
292
346
  splitPath.reduce((acc, key: string, index: number, array: string[]) => {
293
347
  const objFuncKey = toCamelCase(key)
294
348
  if (!objFuncKey) return acc
349
+
295
350
  const methods = Object.keys(operation)
296
- if (
297
- index === array.length - 1
298
- && methods.length === 1
299
- && allPathsClean.filter(p => p === cleanPath(path)).length === 1
300
- ) {
351
+ if (index === array.length - 1 && methods.length === 1 && allPathsClean.filter(p => p === cleanPath(path)).length === 1) {
301
352
  const method: string = methods[0]
302
353
  const opp: any = { ...operation }[method]
303
354
  acc[objFuncKey] = createFunctionPlaceholder(path, methods[0], opp)
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] = {}
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
+ }
309
367
  }
310
368
  return acc[objFuncKey]
311
369
  }, body)
312
370
  }
371
+
313
372
  for (const [parent, object] of Object.entries(body)) {
314
- tsString += `export const ${parent} = ${JSON.stringify(object, undefined, 2)};\n`
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
+ }
315
378
  }
379
+
316
380
  Object.entries(functionsInventory).forEach(([key, value]) => {
317
381
  tsString = tsString.replace(`"${key}"`, value)
318
382
  })
383
+
319
384
  tsString = fileTemplate(tsString, allTypes, baseUrl)
320
385
  return tsString
321
386
  }