@accelbyte/codegen 1.0.0-alpha.6 → 1.0.0-alpha.8

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.
@@ -76,86 +76,6 @@ class CliParser {
76
76
  };
77
77
  }
78
78
 
79
- const Schema = zod.z.object({
80
- $ref: zod.z.string().nullish(),
81
- type: zod.z.union([
82
- zod.z.literal("array"),
83
- zod.z.literal("object"),
84
- zod.z.literal("file"),
85
- zod.z.literal("string"),
86
- zod.z.literal("boolean")
87
- ]).nullish(),
88
- items: zod.z.object({
89
- $ref: zod.z.string().nullish(),
90
- type: zod.z.string().nullish()
91
- }).nullish(),
92
- properties: zod.z.array(zod.z.string()).nullish(),
93
- description: zod.z.string().nullish(),
94
- additionalProperties: zod.z.object({
95
- type: zod.z.string().nullish()
96
- }).nullish()
97
- });
98
- const Definition = zod.z.object({
99
- required: zod.z.array(zod.z.string()).nullish(),
100
- properties: zod.z.record(zod.z.object({
101
- type: zod.z.string()
102
- })).nullish()
103
- });
104
- const Definitions = zod.z.record(Definition);
105
- const EndpointParametersType = zod.z.enum(["apiKey", "boolean", "int", "integer", "number", "string", "array", "file"]);
106
- const EndpointParametersIn = zod.z.enum(["body", "formData", "header", "path", "query"]);
107
- const EndpointParameters = zod.z.object({
108
- type: EndpointParametersType.nullish(),
109
- description: zod.z.string().nullish(),
110
- name: zod.z.string(),
111
- in: EndpointParametersIn,
112
- required: zod.z.boolean().nullish(),
113
- schema: Schema.nullish(),
114
- default: zod.z.union([zod.z.boolean(), zod.z.string(), zod.z.number()]).nullish(),
115
- enum: zod.z.array(zod.z.union([zod.z.boolean(), zod.z.string(), zod.z.number()])).nullish(),
116
- items: zod.z.object({
117
- type: zod.z.string()
118
- }).nullish()
119
- });
120
- const Endpoint = zod.z.object({
121
- description: zod.z.string().nullish(),
122
- consumes: zod.z.array(zod.z.string()).nullish(),
123
- produces: zod.z.array(zod.z.string()).nullish(),
124
- tags: zod.z.array(zod.z.string()).nullish(),
125
- summary: zod.z.string().nullish(),
126
- operationId: zod.z.string(),
127
- deprecated: zod.z.boolean().nullish(),
128
- responses: zod.z.record(zod.z.object({
129
- description: zod.z.string().nullish(),
130
- schema: Schema.nullish()
131
- })),
132
- parameters: zod.z.array(EndpointParameters).nullish()
133
- });
134
- const Operation = zod.z.object({
135
- get: Endpoint.nullish(),
136
- post: Endpoint.nullish(),
137
- patch: Endpoint.nullish(),
138
- delete: Endpoint.nullish(),
139
- put: Endpoint.nullish()
140
- });
141
- const Paths = zod.z.record(Operation);
142
- zod.z.object({
143
- paths: Paths,
144
- definitions: Definitions,
145
- basePath: zod.z.string(),
146
- info: zod.z.object({
147
- description: zod.z.string(),
148
- title: zod.z.string(),
149
- contact: zod.z.object({
150
- name: zod.z.string(),
151
- url: zod.z.string(),
152
- email: zod.z.string()
153
- }),
154
- version: zod.z.string()
155
- }),
156
- schemes: zod.z.array(zod.z.string()).nullish()
157
- });
158
-
159
79
  const getImportableVarMap = () => ({
160
80
  "@accelbyte/sdk": ["CodeGenUtil", "SdkCache", "IResponse", "IResponseWithSync", "Validate"],
161
81
  axios: ["AxiosRequestConfig", "AxiosResponse"],
@@ -207,10 +127,14 @@ class ParserUtils {
207
127
  return `${attrName}: ${defaultValue}`;
208
128
  };
209
129
  static parseType = (pathParam) => {
210
- if (pathParam.type === "int" || pathParam.type === "integer")
130
+ if (pathParam.type === "int" || pathParam.type === "integer" || pathParam?.schema?.type === "integer")
211
131
  return "number";
212
132
  if (pathParam.type === "array")
213
133
  return `${pathParam.items.type ?? "any"}[]`;
134
+ if (pathParam?.schema?.type === "array")
135
+ return `${pathParam.schema.items.type ?? "any"}[]`;
136
+ if (pathParam?.schema?.type)
137
+ return pathParam.schema.type;
214
138
  return pathParam.type;
215
139
  };
216
140
  static parseQueryParamsType = (queryParams) => {
@@ -263,12 +187,21 @@ class ParserUtils {
263
187
  if (definition.type && ParserUtils.parseType(definition) === "number") {
264
188
  return `${attrName}${required}: number`;
265
189
  }
190
+ if (definition?.schema?.type && ParserUtils.parseType(definition) === "number") {
191
+ return `${attrName}${required}: number`;
192
+ }
266
193
  if (definition.type && definition.type === "array") {
267
194
  return `${attrName}${required}: ${definition.items.type ?? "any"}[]`;
268
195
  }
196
+ if (definition?.schema?.type && definition.schema.type === "array") {
197
+ return `${attrName}${required}: ${definition.schema.items.type ?? "any"}[]`;
198
+ }
269
199
  if (definition.type && definition.type) {
270
200
  return `${attrName}${required}: ${definition.type} | null`;
271
201
  }
202
+ if (definition?.schema?.type && definition.schema.type) {
203
+ return `${attrName}${required}: ${definition.schema.type} | null`;
204
+ }
272
205
  return `${attrName}${required}: any`;
273
206
  };
274
207
  static parseBodyParamsType = (bodyParams) => {
@@ -300,11 +233,14 @@ class ParserUtils {
300
233
  keys.forEach((key) => {
301
234
  if (String(key).startsWith("2")) {
302
235
  const sch = methodEntity[key].schema;
236
+ const schV3 = methodEntity[key].content && methodEntity[key].content["application/json"].schema;
303
237
  if (sch?.$ref) {
304
238
  responseClass = ParserUtils.parseRefType(sch.$ref);
305
239
  } else if (sch?.type === "array" && sch.items?.$ref) {
306
240
  responseClass = ParserUtils.parseRefType(sch.items.$ref);
307
241
  responseClass = `${responseClass}Array`;
242
+ } else if (schV3?.$ref) {
243
+ responseClass = ParserUtils.parseRefType(schV3.$ref);
308
244
  } else ;
309
245
  }
310
246
  });
@@ -437,7 +373,104 @@ ${content}`;
437
373
  };
438
374
  }
439
375
 
440
- const templateJsdocMethod = ({ classMethod, httpMethod, path, pathParams, bodyParams, queryParams }) => {
376
+ const Schema = zod.z.object({
377
+ $ref: zod.z.string().nullish(),
378
+ type: zod.z.union([zod.z.literal("array"), zod.z.literal("object"), zod.z.literal("file"), zod.z.literal("string"), zod.z.literal("boolean"), zod.z.literal("integer")]).nullish(),
379
+ items: zod.z.object({
380
+ $ref: zod.z.string().nullish(),
381
+ type: zod.z.string().nullish()
382
+ }).nullish(),
383
+ properties: zod.z.union([zod.z.array(zod.z.string()).nullish(), zod.z.record(zod.z.object({ type: zod.z.string() })).nullish()]),
384
+ description: zod.z.string().nullish(),
385
+ additionalProperties: zod.z.object({
386
+ type: zod.z.string().nullish()
387
+ }).nullish()
388
+ });
389
+ const Definition = zod.z.object({
390
+ required: zod.z.array(zod.z.string()).nullish(),
391
+ properties: zod.z.record(zod.z.object({
392
+ type: zod.z.string()
393
+ })).nullish()
394
+ });
395
+ const Definitions = zod.z.record(Definition);
396
+ const EndpointParametersType = zod.z.enum(["apiKey", "boolean", "int", "integer", "number", "string", "array", "file"]);
397
+ const EndpointParametersIn = zod.z.enum(["body", "formData", "header", "path", "query"]);
398
+ const EndpointParameters = zod.z.object({
399
+ type: EndpointParametersType.nullish(),
400
+ description: zod.z.string().nullish(),
401
+ name: zod.z.string(),
402
+ in: EndpointParametersIn,
403
+ required: zod.z.boolean().nullish(),
404
+ schema: Schema.nullish(),
405
+ default: zod.z.union([zod.z.boolean(), zod.z.string(), zod.z.number()]).nullish(),
406
+ enum: zod.z.array(zod.z.union([zod.z.boolean(), zod.z.string(), zod.z.number()])).nullish(),
407
+ items: zod.z.object({
408
+ type: zod.z.string()
409
+ }).nullish()
410
+ });
411
+ const Endpoint = zod.z.object({
412
+ description: zod.z.string().nullish(),
413
+ consumes: zod.z.array(zod.z.string()).nullish(),
414
+ produces: zod.z.array(zod.z.string()).nullish(),
415
+ tags: zod.z.array(zod.z.string()).nullish(),
416
+ summary: zod.z.string().nullish(),
417
+ operationId: zod.z.string(),
418
+ deprecated: zod.z.boolean().nullish(),
419
+ responses: zod.z.record(zod.z.object({
420
+ description: zod.z.string().nullish(),
421
+ schema: Schema.nullish(),
422
+ content: zod.z.object({
423
+ "application/json": zod.z.object({
424
+ schema: Schema.nullish()
425
+ })
426
+ }).nullish()
427
+ })),
428
+ parameters: zod.z.array(EndpointParameters).nullish(),
429
+ requestBody: zod.z.object({
430
+ required: zod.z.boolean(),
431
+ content: zod.z.object({
432
+ "application/json": zod.z.object({
433
+ schema: Schema.nullish()
434
+ })
435
+ }).nullish()
436
+ }).nullish()
437
+ });
438
+ const Operation = zod.z.object({
439
+ get: Endpoint.nullish(),
440
+ post: Endpoint.nullish(),
441
+ patch: Endpoint.nullish(),
442
+ delete: Endpoint.nullish(),
443
+ put: Endpoint.nullish()
444
+ });
445
+ const Paths = zod.z.record(Operation);
446
+ zod.z.object({
447
+ paths: Paths,
448
+ definitions: Definitions,
449
+ basePath: zod.z.string(),
450
+ info: zod.z.object({
451
+ description: zod.z.string(),
452
+ title: zod.z.string(),
453
+ contact: zod.z.object({
454
+ name: zod.z.string(),
455
+ url: zod.z.string(),
456
+ email: zod.z.string()
457
+ }),
458
+ version: zod.z.string()
459
+ }),
460
+ schemes: zod.z.array(zod.z.string()).nullish(),
461
+ components: zod.z.object({
462
+ schemas: Definitions
463
+ }).nullish()
464
+ });
465
+
466
+ const templateJsdocMethod = ({
467
+ classMethod,
468
+ httpMethod,
469
+ path,
470
+ pathParams,
471
+ bodyParams,
472
+ queryParams
473
+ }) => {
441
474
  let jsdoc = "";
442
475
  let methodSignature = "";
443
476
  let newPath = path;
@@ -617,13 +650,17 @@ class TemplateZod {
617
650
  if (definition.additionalProperties) {
618
651
  return this.parseToZodAttribute("", definition, []);
619
652
  }
620
- if (!definition.properties) {
653
+ let properties;
654
+ if (definition.properties) {
655
+ properties = Object.entries(definition.properties);
656
+ } else if (definition.items?.properties) {
657
+ properties = Object.entries(definition.items.properties);
658
+ } else {
621
659
  return {
622
660
  schemaString: "z.any()",
623
661
  typeString: "any"
624
662
  };
625
663
  }
626
- const properties = Object.entries(definition.properties);
627
664
  const schemaFields = [];
628
665
  const typeFields = [];
629
666
  for (const property of properties) {
@@ -632,6 +669,12 @@ class TemplateZod {
632
669
  schemaFields.push(result.schemaString);
633
670
  typeFields.push(result.typeString);
634
671
  }
672
+ if (definition?.type === "array") {
673
+ return {
674
+ schemaString: `z.array(z.object({${schemaFields.join(",")}}))`,
675
+ typeString: typeFields.join(";")
676
+ };
677
+ }
635
678
  return {
636
679
  schemaString: `z.object({${schemaFields.join(",")}})`,
637
680
  typeString: typeFields.join(";")
@@ -673,7 +716,8 @@ class TemplateZod {
673
716
  };
674
717
  }
675
718
  if (type === "array") {
676
- const ref2 = definition.items?.$ref;
719
+ const items = definition.items;
720
+ const ref2 = items?.$ref;
677
721
  let model2;
678
722
  if (ref2) {
679
723
  const refType = ParserUtils.parseRefType(ref2);
@@ -682,9 +726,13 @@ class TemplateZod {
682
726
  schemaString: refType,
683
727
  typeString: refType
684
728
  };
685
- } else {
686
- const items = definition.items;
729
+ } else if (items) {
687
730
  model2 = this.parseEnumItems(items);
731
+ } else {
732
+ return {
733
+ schemaString: `${schemaAttribute} z.array(z.any())${schemaRequired}`,
734
+ typeString: `${typeAttribute} any[]${typeNullishability}`
735
+ };
688
736
  }
689
737
  return {
690
738
  schemaString: `${schemaAttribute} z.array(${model2.schemaString})${schemaRequired}`,
@@ -834,13 +882,22 @@ class CodeGenerator {
834
882
  const isFormUrlEncoded = ParserUtils.isFormUrlEncoded(httpMethod, endpoint.consumes);
835
883
  const queryParams = ParserUtils.filterQueryParameters(endpoint.parameters);
836
884
  const pathParams = ParserUtils.filterPathParams(endpoint.parameters);
837
- const bodyParams = ParserUtils.filterBodyParams(endpoint.parameters);
885
+ let bodyParams = ParserUtils.filterBodyParams(endpoint.parameters);
838
886
  const classMethod = ParserUtils.generateClassMethod({
839
887
  path: path2,
840
888
  endpoint,
841
889
  httpMethod,
842
890
  className
843
891
  });
892
+ if (endpoint.requestBody) {
893
+ bodyParams = [
894
+ {
895
+ name: "body",
896
+ in: "body",
897
+ schema: endpoint.requestBody.content["application/json"].schema
898
+ }
899
+ ];
900
+ }
844
901
  const pathWithBase = `${api.basePath ?? ""}${path2}`;
845
902
  const [generatedMethodString, importStatements] = templateMethod({
846
903
  classMethod,
@@ -891,8 +948,9 @@ class CodeGenerator {
891
948
  indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path.join(DIST_DIR, `${classGenName}`), targetSrcFolder));
892
949
  }
893
950
  const duplicates = /* @__PURE__ */ new Map();
894
- for (const ref in api.definitions) {
895
- const definition = api.definitions[ref];
951
+ const definitions = api?.components?.schemas || api.definitions;
952
+ for (const ref in definitions) {
953
+ const definition = definitions[ref];
896
954
  let fileName = ParserUtils.parseRefType(ref);
897
955
  const fileExist = fs.existsSync(path.join(DIST_DEFINITION_DIR, `${fileName}.ts`));
898
956
  if (fileExist) {
@@ -903,8 +961,8 @@ class CodeGenerator {
903
961
  ParserUtils.writeDefinitionFile(DIST_DEFINITION_DIR, fileName, buffer);
904
962
  indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path.join(DIST_DEFINITION_DIR, fileName), targetSrcFolder));
905
963
  }
906
- for (const ref in api.definitions) {
907
- const definition = api.definitions[ref];
964
+ for (const ref in definitions) {
965
+ const definition = definitions[ref];
908
966
  const fileName = ParserUtils.parseRefType(ref);
909
967
  const { buffer, duplicateFound } = new TemplateZod().render(fileName, definition, duplicates);
910
968
  if (duplicateFound) {