@moccona/apicodegen 0.0.4 → 0.0.7

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.
@@ -572,7 +572,9 @@ var Base = class Base {
572
572
  */
573
573
  static camelCase(text) {
574
574
  text = text.trim();
575
- return text.split("_").filter(Boolean).map((t, index) => index === 0 ? t : Base.capitalize(t)).join("");
575
+ const parts = text.split("_").filter(Boolean);
576
+ while (parts[0]?.match(/^\d/)) parts.shift();
577
+ return parts.map((t, index) => index === 0 ? t : Base.capitalize(t)).join("");
576
578
  }
577
579
  /**
578
580
  * Converts a string to UpperCamelCase.
@@ -801,9 +803,12 @@ var Generator = class Generator {
801
803
  static addComments(node, comments) {
802
804
  if (!Array.isArray(comments) || comments.filter(Boolean).length === 0) return;
803
805
  const formatComment = (comment) => {
804
- return comment.tag ? ` @${comment.tag} ${comment.comment ?? ""}` : ` ${comment.comment}`;
806
+ if (comment.tag === "returns") return `* @returns {${comment.type}} ${comment.comment ?? ""}`;
807
+ if (comment.tag === "param") return comment.comment ? `* @param ${comment.paramName} - ${comment.comment}` : `* @param ${comment.paramName}`;
808
+ if (comment.tag) return `* @${comment.tag} ${comment.comment ?? ""}`;
809
+ return `* ${comment.comment}`;
805
810
  };
806
- const formattedComments = "*\n" + comments.map(formatComment).join("\n").trim() + "\n";
811
+ const formattedComments = comments.map(formatComment).join("\n").trim() + "\n";
807
812
  addSyntheticLeadingComment(node, SyntaxKind.MultiLineCommentTrivia, formattedComments, true);
808
813
  }
809
814
  /**
@@ -820,6 +825,53 @@ var Generator = class Generator {
820
825
  const nonArraySchema = schema;
821
826
  return nonArraySchema.format === "blob" || nonArraySchema.format === "binary" || nonArraySchema.type === "file";
822
827
  }
828
+ static schemaToTypeString(schema) {
829
+ if (schema.type === "array") {
830
+ const arraySchema = schema;
831
+ return arraySchema.items ? `${Generator.schemaToTypeString(arraySchema.items)}[]` : "unknown";
832
+ }
833
+ const singleSchema = schema;
834
+ if (schema.type === "string") return "string";
835
+ if (schema.type === "number" || schema.type === "integer") return "number";
836
+ if (schema.type === "boolean") return "boolean";
837
+ if (schema.type === "object" || schema.properties) return "object";
838
+ if (singleSchema.format === "binary" || singleSchema.type === "file") return "Blob";
839
+ if (singleSchema.format === "blob") return "Blob";
840
+ if (singleSchema.ref) return singleSchema.ref;
841
+ return "unknown";
842
+ }
843
+ static generateParamTags(parameters, requestBody) {
844
+ const tags = [];
845
+ for (const p of parameters) {
846
+ const paramName = Base.camelCase(Base.normalize(p.name));
847
+ let paramType = "unknown";
848
+ if (p.schema) paramType = Generator.schemaToTypeString(p.schema);
849
+ const isOptional = p.required === false;
850
+ tags.push({
851
+ tag: "param",
852
+ paramName,
853
+ type: `${paramType}${isOptional ? " | undefined" : ""}`,
854
+ comment: p.description ?? ""
855
+ });
856
+ }
857
+ if (requestBody?.schema && "properties" in requestBody.schema) {
858
+ const properties = requestBody.schema.properties;
859
+ const required = requestBody.schema.required;
860
+ const requiredArray = Array.isArray(required) ? required : [];
861
+ for (const [key, schema] of Object.entries(properties ?? {})) {
862
+ const paramName = `req.${key}`;
863
+ const paramType = Generator.schemaToTypeString(schema);
864
+ const isOptional = !requiredArray.includes(key);
865
+ tags.push({
866
+ tag: "param",
867
+ paramName,
868
+ type: `${paramType}${isOptional ? " | undefined" : ""}`,
869
+ comment: schema.description ?? ""
870
+ });
871
+ }
872
+ }
873
+ return tags;
874
+ }
823
875
  static toRequestBodyTypeNode(schema) {
824
876
  return factory.createParameterDeclaration(void 0, void 0, factory.createIdentifier("req"), void 0, Generator.toTypeNode(schema));
825
877
  }
@@ -898,8 +950,8 @@ var Generator = class Generator {
898
950
  factory.createIdentifier("file"),
899
951
  factory.createPropertyAccessExpression(factory.createAsExpression(factory.createIdentifier("file"), factory.createTypeReferenceNode(factory.createIdentifier("File"), void 0)), factory.createIdentifier("name"))
900
952
  ]))])));
901
- else if (schemaByKey.required) statements.push(factory.createExpressionStatement(factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier("fd"), factory.createIdentifier("append")), void 0, [factory.createStringLiteral(key), schemaByKey.type === "string" ? factory.createElementAccessExpression(factory.createIdentifier("req"), factory.createStringLiteral(key)) : factory.createCallExpression(factory.createIdentifier("String"), void 0, [factory.createElementAccessExpression(factory.createIdentifier("req"), factory.createStringLiteral(key))])])));
902
- else statements.push(factory.createExpressionStatement(factory.createBinaryExpression(factory.createElementAccessExpression(factory.createIdentifier("req"), factory.createStringLiteral(key)), factory.createToken(SyntaxKind.AmpersandAmpersandToken), factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier("fd"), factory.createIdentifier("append")), void 0, [factory.createStringLiteral(key), schemaByKey.type === "string" ? factory.createElementAccessExpression(factory.createIdentifier("req"), factory.createStringLiteral(key)) : factory.createCallExpression(factory.createIdentifier("String"), void 0, [factory.createElementAccessExpression(factory.createIdentifier("req"), factory.createStringLiteral(key))])]))));
953
+ else if (schemaByKey.required) statements.push(factory.createExpressionStatement(factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier("fd"), factory.createIdentifier("append")), void 0, [factory.createStringLiteral(key), schemaByKey.type === "string" || Generator.isBinarySchema(schemaByKey) || schemaByKey.isRef ? factory.createElementAccessExpression(factory.createIdentifier("req"), factory.createStringLiteral(key)) : schemaByKey.type === "array" || schemaByKey.type === "object" ? factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier("JSON"), factory.createIdentifier("stringify")), void 0, [factory.createElementAccessExpression(factory.createIdentifier("req"), factory.createStringLiteral(key))]) : factory.createCallExpression(factory.createIdentifier("String"), void 0, [factory.createElementAccessExpression(factory.createIdentifier("req"), factory.createStringLiteral(key))])])));
954
+ else statements.push(factory.createExpressionStatement(factory.createBinaryExpression(factory.createElementAccessExpression(factory.createIdentifier("req"), factory.createStringLiteral(key)), factory.createToken(SyntaxKind.AmpersandAmpersandToken), factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier("fd"), factory.createIdentifier("append")), void 0, [factory.createStringLiteral(key), schemaByKey.type === "string" || Generator.isBinarySchema(schemaByKey) || schemaByKey.isRef ? factory.createElementAccessExpression(factory.createIdentifier("req"), factory.createStringLiteral(key)) : schemaByKey.type === "array" || schemaByKey.type === "object" ? factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier("JSON"), factory.createIdentifier("stringify")), void 0, [factory.createElementAccessExpression(factory.createIdentifier("req"), factory.createStringLiteral(key))]) : factory.createCallExpression(factory.createIdentifier("String"), void 0, [factory.createElementAccessExpression(factory.createIdentifier("req"), factory.createStringLiteral(key))])]))));
903
955
  });
904
956
  return statements;
905
957
  }
@@ -911,7 +963,7 @@ var Generator = class Generator {
911
963
  const parametersShouldNotPutInFormData = parameters.filter((p) => !parametersShouldPutInFormData.includes(p));
912
964
  const isRequestBodyContainsBinary = requestBody?.schema && "properties" in requestBody.schema && Object.values(requestBody.schema?.properties ?? {}).some((p) => Generator.isBinarySchema(p));
913
965
  const hasBinaryInParameters = parameters.some((p) => p?.schema && Generator.isBinarySchema(p.schema));
914
- const shouldPutParametersOrBodyInFormData = isFormDataRequest || isRequestBodyBinary || hasBinaryInParameters || isRequestBodyContainsBinary || parametersShouldPutInFormData.length > 0;
966
+ const shouldPutParametersOrBodyInFormData = !!isFormDataRequest && (isRequestBodyBinary || hasBinaryInParameters || isRequestBodyContainsBinary || parametersShouldPutInFormData.length > 0);
915
967
  return factory.createBlock([...shouldPutParametersOrBodyInFormData ? Generator.toFormDataStatement(parametersShouldPutInFormData, requestBody?.schema) : [], ...adapter.client(uri, method, parametersShouldNotPutInFormData, requestBody, response, adapter, shouldPutParametersOrBodyInFormData, shouldParseResponseToJSON)]);
916
968
  }
917
969
  static schemaToStatemets(parsedDoc, adaptor, options) {
@@ -938,10 +990,11 @@ var Generator = class Generator {
938
990
  const shouldAddExtraMethodNameSuffix = requestBody.length > 1;
939
991
  for (const req of requestBody) {
940
992
  const statement = factory.createFunctionDeclaration([factory.createModifier(SyntaxKind.ExportKeyword), factory.createModifier(SyntaxKind.AsyncKeyword)], void 0, Base.pathToFnName(uri, method, operationId) + (shouldAddExtraMethodNameSuffix ? Base.capitalize(req.type.split("/")[1]) : ""), void 0, [...parameters.length > 0 ? Generator.toDeclarationNodes(parameters) : [], ...req?.schema ? [Generator.toRequestBodyTypeNode(req.schema)] : []].filter(Boolean), void 0, Generator.bodyBlock(options.baseURL + uri, method, parameters, req, responses[0], adaptor));
993
+ const mergedDescription = [description, summary].filter(Boolean).join(". ");
941
994
  Generator.addComments(statement, [
942
- description && { comment: description },
943
- summary && { comment: summary },
944
- deprecated && { tag: "deprecated" }
995
+ mergedDescription && { comment: mergedDescription },
996
+ deprecated && { tag: "deprecated" },
997
+ ...Generator.generateParamTags(parameters, req)
945
998
  ].filter(Boolean));
946
999
  statements.push(statement);
947
1000
  }
@@ -1464,7 +1517,10 @@ var V3 = class {
1464
1517
  const propSchema = properties[p];
1465
1518
  return {
1466
1519
  ...acc,
1467
- [p]: Base.isRef(propSchema) ? { type: Base.capitalize(Base.ref2name(propSchema.$ref, this.doc)) } : this.toBaseSchema(propSchema, enums, p, upLevelSchemaKey)
1520
+ [p]: Base.isRef(propSchema) ? {
1521
+ type: Base.capitalize(Base.ref2name(propSchema.$ref, this.doc)),
1522
+ isRef: true
1523
+ } : this.toBaseSchema(propSchema, enums, p, upLevelSchemaKey)
1468
1524
  };
1469
1525
  }, {})
1470
1526
  };
@@ -1699,7 +1755,10 @@ var V3_1 = class {
1699
1755
  const propSchema = properties[p];
1700
1756
  return {
1701
1757
  ...acc,
1702
- [p]: Base.isRef(propSchema) ? { type: Base.capitalize(Base.ref2name(propSchema.$ref, this.doc)) } : this.toBaseSchema(propSchema, enums, p, upLevelSchemaKey)
1758
+ [p]: Base.isRef(propSchema) ? {
1759
+ type: Base.capitalize(Base.ref2name(propSchema.$ref, this.doc)),
1760
+ isRef: true
1761
+ } : this.toBaseSchema(propSchema, enums, p, upLevelSchemaKey)
1703
1762
  };
1704
1763
  }, {})
1705
1764
  };