@moccona/apicodegen 0.0.1 → 0.0.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.
package/npm/index.d.cts CHANGED
@@ -239,7 +239,7 @@ declare abstract class Base {
239
239
  * @param [operationId] - Unique identifier for the operation.
240
240
  * @returns - The generated function name.
241
241
  */
242
- static pathToFnName(path: string, method?: string, operationId?: string): string;
242
+ static pathToFnName(path: string, method?: string, _operationId?: string): string;
243
243
  /**
244
244
  * Normalizes a string by replacing special characters and avoiding TypeScript keywords.
245
245
  * @param text - Input text to normalize.
@@ -488,7 +488,7 @@ declare class Generator {
488
488
  * @param path - The base path string containing placeholders.
489
489
  * @param parameters - Array of parameter objects defining the parameters.
490
490
  * @param basePath - Optional base path to prepend (default: "").
491
- * @returns A TypeScript template expression node.
491
+ * @returns A TypeScript template expressi
492
492
  */
493
493
  static toUrlTemplate(path: string, parameters: ParameterObject[], basePath?: string): typescript.NoSubstitutionTemplateLiteral | typescript.TemplateExpression;
494
494
  /**
package/npm/index.d.ts CHANGED
@@ -239,7 +239,7 @@ declare abstract class Base {
239
239
  * @param [operationId] - Unique identifier for the operation.
240
240
  * @returns - The generated function name.
241
241
  */
242
- static pathToFnName(path: string, method?: string, operationId?: string): string;
242
+ static pathToFnName(path: string, method?: string, _operationId?: string): string;
243
243
  /**
244
244
  * Normalizes a string by replacing special characters and avoiding TypeScript keywords.
245
245
  * @param text - Input text to normalize.
@@ -488,7 +488,7 @@ declare class Generator {
488
488
  * @param path - The base path string containing placeholders.
489
489
  * @param parameters - Array of parameter objects defining the parameters.
490
490
  * @param basePath - Optional base path to prepend (default: "").
491
- * @returns A TypeScript template expression node.
491
+ * @returns A TypeScript template expressi
492
492
  */
493
493
  static toUrlTemplate(path: string, parameters: ParameterObject[], basePath?: string): typescript.NoSubstitutionTemplateLiteral | typescript.TemplateExpression;
494
494
  /**
package/npm/index.js CHANGED
@@ -205,10 +205,10 @@ var Base = class _Base {
205
205
  * @param [operationId] - Unique identifier for the operation.
206
206
  * @returns - The generated function name.
207
207
  */
208
- static pathToFnName(path, method, operationId) {
209
- const name = this.camelCase(this.normalize(path));
208
+ static pathToFnName(path, method, _operationId = "") {
209
+ const name = this.normalize(this.camelCase(this.normalize(path)));
210
210
  const suffix = method ? this.capitalize(this.upperCamelCase(`using_${method}`)) : "";
211
- return (operationId ? this.camelCase(this.normalize(operationId)) : name) + suffix;
211
+ return name + suffix;
212
212
  }
213
213
  /**
214
214
  * Normalizes a string by replacing special characters and avoiding TypeScript keywords.
@@ -219,7 +219,7 @@ var Base = class _Base {
219
219
  if (typescriptKeywords.has(text)) {
220
220
  text += "_";
221
221
  }
222
- return text.replace(/[/\-_{}():\s`,*<>$]/gm, "_").replaceAll("...", "");
222
+ return text.replace(/[/\-_{}():\s`,*<>$#.]/gm, "_").replace(/^\d./gm, "").replaceAll("...", "");
223
223
  }
224
224
  /**
225
225
  * Capitalizes the first character of a string.
@@ -435,7 +435,7 @@ var Generator = class _Generator {
435
435
  * @param path - The base path string containing placeholders.
436
436
  * @param parameters - Array of parameter objects defining the parameters.
437
437
  * @param basePath - Optional base path to prepend (default: "").
438
- * @returns A TypeScript template expression node.
438
+ * @returns A TypeScript template expressi
439
439
  */
440
440
  static toUrlTemplate(path, parameters, basePath = "") {
441
441
  const queryParameters = parameters.filter(
@@ -443,7 +443,7 @@ var Generator = class _Generator {
443
443
  );
444
444
  if (queryParameters.length > 0) {
445
445
  const queryString = queryParameters.map(
446
- (qp, index) => `${index === 0 ? "?" : "&"}${encodeURIComponent(qp.name)}={${qp.name}}`
446
+ (qp, index) => `${index === 0 ? "?" : "&"}${encodeURIComponent(qp.name)}={${Base.camelCase(Base.normalize(qp.name))}}`
447
447
  ).join("");
448
448
  path += queryString;
449
449
  }
@@ -558,7 +558,7 @@ var Generator = class _Generator {
558
558
  case "boolean" /* boolean */:
559
559
  return t.createToken(SyntaxKind.BooleanKeyword);
560
560
  case "file" /* file */:
561
- return t.createTypeReferenceNode(t.createIdentifier("File"));
561
+ return t.createTypeReferenceNode(t.createIdentifier("Blob"));
562
562
  default:
563
563
  const {
564
564
  format: format2,
@@ -577,7 +577,7 @@ var Generator = class _Generator {
577
577
  return t.createToken(SyntaxKind.BooleanKeyword);
578
578
  case "blob" /* blob */:
579
579
  case "binary" /* binary */:
580
- return t.createTypeReferenceNode(t.createIdentifier("File"));
580
+ return t.createTypeReferenceNode(t.createIdentifier("Blob"));
581
581
  default:
582
582
  }
583
583
  if (enum_) {
@@ -601,13 +601,13 @@ var Generator = class _Generator {
601
601
  );
602
602
  }
603
603
  if (allOf) {
604
- return t.createUnionTypeNode(
604
+ return t.createIntersectionTypeNode(
605
605
  allOf.map((schema2) => this.toTypeNode(schema2))
606
606
  );
607
607
  }
608
608
  if (type2 && typeof type2 === "string") {
609
609
  return t.createTypeReferenceNode(
610
- type2 !== "unknown" ? t.createIdentifier(Base.upperCamelCase(type2)) : type2
610
+ type2 !== "unknown" && type2 !== "null" ? t.createIdentifier(Base.upperCamelCase(type2)) : type2
611
611
  );
612
612
  }
613
613
  }
@@ -681,16 +681,11 @@ var Generator = class _Generator {
681
681
  )
682
682
  );
683
683
  statements.push(fdDeclaration);
684
- parameters.filter(
685
- (parameter) => parameter.ref !== void 0 && (parameter.in === "formData" /* formData */ || parameter.schema && this.isBinarySchema(parameter.schema))
686
- ).forEach((parameter) => {
684
+ parameters.forEach((parameter) => {
687
685
  statements.push(
688
686
  t.createExpressionStatement(
689
687
  t.createBinaryExpression(
690
- t.createElementAccessExpression(
691
- t.createIdentifier("req"),
692
- t.createStringLiteral(parameter.name)
693
- ),
688
+ t.createIdentifier(parameter.name),
694
689
  t.createToken(SyntaxKind.AmpersandAmpersandToken),
695
690
  t.createCallExpression(
696
691
  t.createPropertyAccessExpression(
@@ -734,7 +729,13 @@ var Generator = class _Generator {
734
729
  t.createStringLiteral(key),
735
730
  t.createIdentifier("file"),
736
731
  t.createPropertyAccessExpression(
737
- t.createIdentifier("file"),
732
+ t.createAsExpression(
733
+ t.createIdentifier("file"),
734
+ t.createTypeReferenceNode(
735
+ t.createIdentifier("File"),
736
+ void 0
737
+ )
738
+ ),
738
739
  t.createIdentifier("name")
739
740
  )
740
741
  ]
@@ -819,25 +820,28 @@ var Generator = class _Generator {
819
820
  );
820
821
  const shouldParseResponseToJSON = "application/json" === response?.type;
821
822
  const isRequestBodyBinary = requestBody?.schema && requestBody.schema.type === "array" /* array */ && this.isBinarySchema(requestBody.schema);
822
- const inFormDataParameters = parameters.filter(
823
- (p) => p.in === "formData" /* formData */
823
+ const parametersShouldPutInFormData = parameters.filter(
824
+ (p) => p.in === "formData" /* formData */ || p.schema && this.isBinarySchema(p.schema)
824
825
  );
825
- const notInFormDataParameters = parameters.filter(
826
- (p) => p.in !== "formData" /* formData */
826
+ const parametersShouldNotPutInFormData = parameters.filter(
827
+ (p) => !parametersShouldPutInFormData.includes(p)
827
828
  );
828
- const isRequestBodyContainsBinary = requestBody?.schema && "properties" in requestBody.schema && Object.values(requestBody.schema.properties).some(
829
+ const isRequestBodyContainsBinary = requestBody?.schema && "properties" in requestBody.schema && Object.values(requestBody.schema?.properties ?? {}).some(
829
830
  (p) => this.isBinarySchema(p)
830
831
  );
831
832
  const hasBinaryInParameters = parameters.some(
832
833
  (p) => p?.schema && this.isBinarySchema(p.schema)
833
834
  );
834
- const shouldPutParametersOrBodyInFormData = isFormDataRequest || isRequestBodyBinary || hasBinaryInParameters || isRequestBodyContainsBinary || inFormDataParameters.length > 0;
835
+ const shouldPutParametersOrBodyInFormData = isFormDataRequest || isRequestBodyBinary || hasBinaryInParameters || isRequestBodyContainsBinary || parametersShouldPutInFormData.length > 0;
835
836
  return t.createBlock([
836
- ...shouldPutParametersOrBodyInFormData ? this.toFormDataStatement(inFormDataParameters, requestBody?.schema) : [],
837
+ ...shouldPutParametersOrBodyInFormData ? this.toFormDataStatement(
838
+ parametersShouldPutInFormData,
839
+ requestBody?.schema
840
+ ) : [],
837
841
  ...adapter.client(
838
842
  uri,
839
843
  method,
840
- notInFormDataParameters,
844
+ parametersShouldNotPutInFormData,
841
845
  requestBody,
842
846
  response,
843
847
  adapter,
@@ -1173,7 +1177,7 @@ var FetchAdapter = class extends Adapter {
1173
1177
  ],
1174
1178
  void 0,
1175
1179
  t3.createToken(SyntaxKind2.EqualsGreaterThanToken),
1176
- t3.createAsExpression(
1180
+ response?.schema ? t3.createAsExpression(
1177
1181
  t3.createParenthesizedExpression(
1178
1182
  t3.createAwaitExpression(
1179
1183
  t3.createCallExpression(
@@ -1187,6 +1191,17 @@ var FetchAdapter = class extends Adapter {
1187
1191
  )
1188
1192
  ),
1189
1193
  response?.schema ? Generator.toTypeNode(response.schema) : t3.createToken(SyntaxKind2.UnknownKeyword)
1194
+ ) : t3.createParenthesizedExpression(
1195
+ t3.createAwaitExpression(
1196
+ t3.createCallExpression(
1197
+ t3.createPropertyAccessExpression(
1198
+ t3.createIdentifier("response"),
1199
+ t3.createIdentifier("json")
1200
+ ),
1201
+ void 0,
1202
+ []
1203
+ )
1204
+ )
1190
1205
  )
1191
1206
  )
1192
1207
  ]
@@ -1297,13 +1312,25 @@ var V2 = class {
1297
1312
  enum: enum_,
1298
1313
  format: format2,
1299
1314
  allOf: allOf?.map(
1300
- (s) => Base.isRef(s) ? { type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc)) } : this.toBaseSchema(s, enums)
1315
+ (s) => Base.isRef(s) ? {
1316
+ ...s,
1317
+ ref: s.$ref,
1318
+ type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
1319
+ } : this.toBaseSchema(s, enums)
1301
1320
  ),
1302
1321
  anyOf: anyOf?.map(
1303
- (s) => Base.isRef(s) ? { type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc)) } : this.toBaseSchema(s, enums)
1322
+ (s) => Base.isRef(s) ? {
1323
+ ...s,
1324
+ ref: s.$ref,
1325
+ type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
1326
+ } : this.toBaseSchema(s, enums)
1304
1327
  ),
1305
1328
  oneOf: oneOf?.map(
1306
- (s) => Base.isRef(s) ? { type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc)) } : this.toBaseSchema(s, enums)
1329
+ (s) => Base.isRef(s) ? {
1330
+ ...s,
1331
+ ref: s.$ref,
1332
+ type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
1333
+ } : this.toBaseSchema(s, enums)
1307
1334
  ),
1308
1335
  properties: Object.keys(properties).reduce((acc, p) => {
1309
1336
  const propSchema = properties[p];
@@ -1333,7 +1360,8 @@ var V2 = class {
1333
1360
  type,
1334
1361
  items,
1335
1362
  enum: enum_,
1336
- properties
1363
+ properties,
1364
+ schema
1337
1365
  } = parameter;
1338
1366
  if (enum_) {
1339
1367
  const type2 = Base.upperCamelCase(upLevelSchemaKey) + Base.upperCamelCase(name);
@@ -1355,17 +1383,36 @@ var V2 = class {
1355
1383
  }
1356
1384
  };
1357
1385
  }
1386
+ if (items) {
1387
+ return {
1388
+ name,
1389
+ required,
1390
+ description,
1391
+ in: parameter.in,
1392
+ schema: {
1393
+ type,
1394
+ items
1395
+ }
1396
+ };
1397
+ }
1398
+ if (schema && Base.isRef(schema)) {
1399
+ return {
1400
+ name,
1401
+ required,
1402
+ description,
1403
+ in: parameter.in,
1404
+ schema: {
1405
+ type: Base.upperCamelCase(Base.ref2name(schema.$ref))
1406
+ }
1407
+ };
1408
+ }
1358
1409
  return {
1359
1410
  name,
1360
1411
  required,
1361
1412
  description,
1362
1413
  in: parameter.in,
1363
- schema: items ? {
1414
+ schema: {
1364
1415
  type,
1365
- items
1366
- } : {
1367
- type,
1368
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
1369
1416
  properties
1370
1417
  }
1371
1418
  };
@@ -1444,6 +1491,7 @@ var V2 = class {
1444
1491
  if (code in responses2) {
1445
1492
  const response = responses2[code];
1446
1493
  const responseSchema = this.getResponseByRef(response);
1494
+ const inBodyOnlyHasBody = inBody && inBody.length === 1 && inBody[0].in === "body" && inBody[0].name === "body";
1447
1495
  methodApis.push({
1448
1496
  method,
1449
1497
  operationId,
@@ -1452,7 +1500,12 @@ var V2 = class {
1452
1500
  description: description_,
1453
1501
  parameters: uniqueParameterName.map((name) => notInBody.find((p) => p.name === name)).filter(Boolean),
1454
1502
  responses: responseSchema,
1455
- requestBody: inBody.length > 0 ? [
1503
+ requestBody: inBody.length > 0 ? inBodyOnlyHasBody ? [
1504
+ {
1505
+ type: "application/json" /* JSON */,
1506
+ schema: inBody[0].schema
1507
+ }
1508
+ ] : [
1456
1509
  {
1457
1510
  type: "application/json" /* JSON */,
1458
1511
  schema: {
@@ -1660,13 +1713,25 @@ var V3 = class {
1660
1713
  enum: enum_,
1661
1714
  format: format2,
1662
1715
  allOf: allOf?.map(
1663
- (s) => Base.isRef(s) ? { type: Base.capitalize(Base.ref2name(s.$ref, this.doc)) } : this.toBaseSchema(s, enums)
1716
+ (s) => Base.isRef(s) ? {
1717
+ ...s,
1718
+ ref: s.$ref,
1719
+ type: Base.capitalize(Base.ref2name(s.$ref, this.doc))
1720
+ } : this.toBaseSchema(s, enums)
1664
1721
  ),
1665
1722
  anyOf: anyOf?.map(
1666
- (s) => Base.isRef(s) ? { type: Base.capitalize(Base.ref2name(s.$ref, this.doc)) } : this.toBaseSchema(s, enums)
1723
+ (s) => Base.isRef(s) ? {
1724
+ ...s,
1725
+ ref: s.$ref,
1726
+ type: Base.capitalize(Base.ref2name(s.$ref, this.doc))
1727
+ } : this.toBaseSchema(s, enums)
1667
1728
  ),
1668
1729
  oneOf: oneOf?.map(
1669
- (s) => Base.isRef(s) ? { type: Base.capitalize(Base.ref2name(s.$ref, this.doc)) } : this.toBaseSchema(s, enums)
1730
+ (s) => Base.isRef(s) ? {
1731
+ ...s,
1732
+ ref: s.$ref,
1733
+ type: Base.capitalize(Base.ref2name(s.$ref, this.doc))
1734
+ } : this.toBaseSchema(s, enums)
1670
1735
  ),
1671
1736
  properties: Object.keys(properties).reduce((acc, p) => {
1672
1737
  const propSchema = properties[p];
@@ -1963,13 +2028,25 @@ var V3_1 = class {
1963
2028
  enum: enum_,
1964
2029
  format: format2,
1965
2030
  allOf: allOf?.map(
1966
- (s) => Base.isRef(s) ? { type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc)) } : this.toBaseSchema(s, enums)
2031
+ (s) => Base.isRef(s) ? {
2032
+ ...s,
2033
+ ref: s.$ref,
2034
+ type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
2035
+ } : this.toBaseSchema(s, enums)
1967
2036
  ),
1968
2037
  anyOf: anyOf?.map(
1969
- (s) => Base.isRef(s) ? { type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc)) } : this.toBaseSchema(s, enums)
2038
+ (s) => Base.isRef(s) ? {
2039
+ ...s,
2040
+ ref: s.$ref,
2041
+ type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
2042
+ } : this.toBaseSchema(s, enums)
1970
2043
  ),
1971
2044
  oneOf: oneOf?.map(
1972
- (s) => Base.isRef(s) ? { type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc)) } : this.toBaseSchema(s, enums)
2045
+ (s) => Base.isRef(s) ? {
2046
+ ...s,
2047
+ ref: s.$ref,
2048
+ type: Base.upperCamelCase(Base.ref2name(s.$ref, this.doc))
2049
+ } : this.toBaseSchema(s, enums)
1973
2050
  ),
1974
2051
  properties: Object.keys(properties).reduce((acc, p) => {
1975
2052
  const propSchema = properties[p];
@@ -2226,13 +2303,12 @@ function apiCodeGenPlugin(options) {
2226
2303
  await tsc();
2227
2304
  } catch (error) {
2228
2305
  logger2.error(error);
2229
- process.exit(1);
2230
2306
  }
2231
2307
  return {
2232
2308
  ...config,
2233
2309
  server: {
2234
2310
  ...config.server ?? {},
2235
- proxy: proxies
2311
+ proxy: Object.assign({}, config.server?.proxy ?? {}, proxies)
2236
2312
  }
2237
2313
  };
2238
2314
  }