openapi-sync 2.0.1 → 2.1.0

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.
@@ -40,7 +40,7 @@ const apiClient = axios_1.default.create({
40
40
  },
41
41
  });
42
42
  const OpenapiSync = (apiUrl, apiName, config, refetchInterval) => __awaiter(void 0, void 0, void 0, function* () {
43
- var _a, _b, _c, _d, _e;
43
+ var _a, _b, _c, _d, _e, _f;
44
44
  const specResponse = yield apiClient.get(apiUrl);
45
45
  const redoclyConfig = yield (0, openapi_core_1.createConfig)({
46
46
  extends: ["minimal"],
@@ -54,11 +54,13 @@ const OpenapiSync = (apiUrl, apiName, config, refetchInterval) => __awaiter(void
54
54
  });
55
55
  const folderPath = path_1.default.join((config === null || config === void 0 ? void 0 : config.folder) || "", apiName);
56
56
  const spec = lintResults.bundle.parsed;
57
- const serverUrl = (_a = spec.servers[(config === null || config === void 0 ? void 0 : config.server) || 0]) === null || _a === void 0 ? void 0 : _a.url;
58
- const typePrefix = typeof ((_c = (_b = config === null || config === void 0 ? void 0 : config.types) === null || _b === void 0 ? void 0 : _b.name) === null || _c === void 0 ? void 0 : _c.prefix) === "string"
57
+ const serverUrl = typeof (config === null || config === void 0 ? void 0 : config.server) === "string"
58
+ ? config === null || config === void 0 ? void 0 : config.server
59
+ : ((_b = (_a = spec === null || spec === void 0 ? void 0 : spec.servers) === null || _a === void 0 ? void 0 : _a[(config === null || config === void 0 ? void 0 : config.server) || 0]) === null || _b === void 0 ? void 0 : _b.url) || "";
60
+ const typePrefix = typeof ((_d = (_c = config === null || config === void 0 ? void 0 : config.types) === null || _c === void 0 ? void 0 : _c.name) === null || _d === void 0 ? void 0 : _d.prefix) === "string"
59
61
  ? config === null || config === void 0 ? void 0 : config.types.name.prefix
60
62
  : "I";
61
- const endpointPrefix = typeof ((_e = (_d = config === null || config === void 0 ? void 0 : config.endpoints) === null || _d === void 0 ? void 0 : _d.name) === null || _e === void 0 ? void 0 : _e.prefix) === "string"
63
+ const endpointPrefix = typeof ((_f = (_e = config === null || config === void 0 ? void 0 : config.endpoints) === null || _e === void 0 ? void 0 : _e.name) === null || _f === void 0 ? void 0 : _f.prefix) === "string"
62
64
  ? config === null || config === void 0 ? void 0 : config.endpoints.name.prefix
63
65
  : "";
64
66
  const getSharedComponentName = (componentName, componentType) => {
@@ -446,7 +448,7 @@ const OpenapiSync = (apiUrl, apiName, config, refetchInterval) => __awaiter(void
446
448
  const endpointSpec = spec.paths[endpointPath];
447
449
  const endpointMethods = Object.keys(endpointSpec);
448
450
  endpointMethods.forEach((_method) => {
449
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
451
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
450
452
  const method = _method;
451
453
  const endpoint = (0, helpers_1.getEndpointDetails)(endpointPath, method);
452
454
  const endpointUrlTxt = (((_b = (_a = config === null || config === void 0 ? void 0 : config.endpoints) === null || _a === void 0 ? void 0 : _a.value) === null || _b === void 0 ? void 0 : _b.includeServer) ? serverUrl : "") +
@@ -478,16 +480,6 @@ const OpenapiSync = (apiUrl, apiName, config, refetchInterval) => __awaiter(void
478
480
  //treat endpoint url
479
481
  endpointUrl = treatEndpointUrl(endpointUrl);
480
482
  const eSpec = endpointSpec[method];
481
- let name = `${endpoint.name}`;
482
- if ((_d = (_c = config === null || config === void 0 ? void 0 : config.endpoints) === null || _c === void 0 ? void 0 : _c.name) === null || _d === void 0 ? void 0 : _d.format) {
483
- const formattedName = config === null || config === void 0 ? void 0 : config.endpoints.name.format({
484
- method,
485
- path: endpointPath,
486
- summary: eSpec === null || eSpec === void 0 ? void 0 : eSpec.summary,
487
- });
488
- if (formattedName)
489
- name = formattedName;
490
- }
491
483
  let queryTypeCnt = "";
492
484
  if (eSpec === null || eSpec === void 0 ? void 0 : eSpec.parameters) {
493
485
  // create query parameters types
@@ -500,7 +492,7 @@ const OpenapiSync = (apiUrl, apiName, config, refetchInterval) => __awaiter(void
500
492
  if (queryTypeCnt) {
501
493
  queryTypeCnt = `{\n${queryTypeCnt}}`;
502
494
  let name = `${endpoint.name}Query`;
503
- if ((_f = (_e = config === null || config === void 0 ? void 0 : config.types) === null || _e === void 0 ? void 0 : _e.name) === null || _f === void 0 ? void 0 : _f.format) {
495
+ if ((_d = (_c = config === null || config === void 0 ? void 0 : config.types) === null || _c === void 0 ? void 0 : _c.name) === null || _d === void 0 ? void 0 : _d.format) {
504
496
  const formattedName = config === null || config === void 0 ? void 0 : config.types.name.format("endpoint", {
505
497
  code: "",
506
498
  type: "query",
@@ -521,7 +513,7 @@ const OpenapiSync = (apiUrl, apiName, config, refetchInterval) => __awaiter(void
521
513
  dtoTypeCnt = getBodySchemaType(requestBody);
522
514
  if (dtoTypeCnt) {
523
515
  let name = `${endpoint.name}DTO`;
524
- if ((_h = (_g = config === null || config === void 0 ? void 0 : config.types) === null || _g === void 0 ? void 0 : _g.name) === null || _h === void 0 ? void 0 : _h.format) {
516
+ if ((_f = (_e = config === null || config === void 0 ? void 0 : config.types) === null || _e === void 0 ? void 0 : _e.name) === null || _f === void 0 ? void 0 : _f.format) {
525
517
  const formattedName = config === null || config === void 0 ? void 0 : config.types.name.format("endpoint", {
526
518
  code: "",
527
519
  type: "dto",
@@ -588,9 +580,9 @@ const OpenapiSync = (apiUrl, apiName, config, refetchInterval) => __awaiter(void
588
580
  ? formatSecuritySpec(eSpec.security)
589
581
  : "";
590
582
  let doc = "";
591
- if (!((_k = (_j = config === null || config === void 0 ? void 0 : config.endpoints) === null || _j === void 0 ? void 0 : _j.doc) === null || _k === void 0 ? void 0 : _k.disable)) {
583
+ if (!((_h = (_g = config === null || config === void 0 ? void 0 : config.endpoints) === null || _g === void 0 ? void 0 : _g.doc) === null || _h === void 0 ? void 0 : _h.disable)) {
592
584
  let curl = "";
593
- if ((_m = (_l = config === null || config === void 0 ? void 0 : config.endpoints) === null || _l === void 0 ? void 0 : _l.doc) === null || _m === void 0 ? void 0 : _m.showCurl) {
585
+ if ((_k = (_j = config === null || config === void 0 ? void 0 : config.endpoints) === null || _j === void 0 ? void 0 : _j.doc) === null || _k === void 0 ? void 0 : _k.showCurl) {
594
586
  // console.log("cirl data", {
595
587
  // body: eSpec?.requestBody,
596
588
  // bodyContent:
@@ -601,7 +593,7 @@ const OpenapiSync = (apiUrl, apiName, config, refetchInterval) => __awaiter(void
601
593
  const headers = {};
602
594
  let body = "";
603
595
  let extras = "";
604
- if ((_o = eSpec.requestBody) === null || _o === void 0 ? void 0 : _o.content) {
596
+ if ((_l = eSpec.requestBody) === null || _l === void 0 ? void 0 : _l.content) {
605
597
  const contentTypes = Object.keys(eSpec.requestBody.content);
606
598
  contentTypes.forEach((contentType) => {
607
599
  // console.log("requestBody content", {
@@ -666,7 +658,7 @@ ${(0, curl_generator_1.CurlGenerator)({
666
658
  doc = `/**${(eSpec === null || eSpec === void 0 ? void 0 : eSpec.description) ? `\n* ${eSpec === null || eSpec === void 0 ? void 0 : eSpec.description} ` : ""}
667
659
  * **Method**: \`${method.toUpperCase()}\`
668
660
  * **Summary**: ${(eSpec === null || eSpec === void 0 ? void 0 : eSpec.summary) || ""}
669
- * **Tags**: [${((_p = eSpec === null || eSpec === void 0 ? void 0 : eSpec.tags) === null || _p === void 0 ? void 0 : _p.join(", ")) || ""}]
661
+ * **Tags**: [${((_m = eSpec === null || eSpec === void 0 ? void 0 : eSpec.tags) === null || _m === void 0 ? void 0 : _m.join(", ")) || ""}]
670
662
  * **OperationId**: ${(eSpec === null || eSpec === void 0 ? void 0 : eSpec.operationId) || ""} ${queryTypeCnt
671
663
  ? `\n * **Query**: ${(0, helpers_1.renderTypeRefMD)(queryTypeCnt)} `
672
664
  : ""}${dtoTypeCnt ? `\n * **DTO**: ${(0, helpers_1.renderTypeRefMD)(dtoTypeCnt)} ` : ""}${responseTypeCnt
@@ -676,8 +668,29 @@ ${(0, curl_generator_1.CurlGenerator)({
676
668
  : ""}${securitySpec ? `\n * **Security**: ${securitySpec}\n` : ""}${curl}
677
669
  */\n`;
678
670
  }
671
+ let name = ((_p = (_o = config === null || config === void 0 ? void 0 : config.endpoints) === null || _o === void 0 ? void 0 : _o.name) === null || _p === void 0 ? void 0 : _p.useOperationId) &&
672
+ ((_q = eSpec === null || eSpec === void 0 ? void 0 : eSpec.operationId) === null || _q === void 0 ? void 0 : _q.length) > 0
673
+ ? eSpec.operationId
674
+ : `${endpoint.name}`;
675
+ if ((_s = (_r = config === null || config === void 0 ? void 0 : config.endpoints) === null || _r === void 0 ? void 0 : _r.name) === null || _s === void 0 ? void 0 : _s.format) {
676
+ const formattedName = config === null || config === void 0 ? void 0 : config.endpoints.name.format({
677
+ method,
678
+ path: endpointPath,
679
+ summary: eSpec === null || eSpec === void 0 ? void 0 : eSpec.summary,
680
+ operationId: eSpec === null || eSpec === void 0 ? void 0 : eSpec.operationId,
681
+ });
682
+ if (formattedName)
683
+ name = formattedName;
684
+ }
685
+ const content = {
686
+ method: method,
687
+ operationId: eSpec === null || eSpec === void 0 ? void 0 : eSpec.operationId,
688
+ url: endpointUrl,
689
+ };
679
690
  // Add the endpoint url
680
- endpointsFileContent += `${doc}export const ${endpointPrefix}${name} = ${endpointUrl};
691
+ endpointsFileContent += `${doc}export const ${endpointPrefix}${name} = ${((_u = (_t = config === null || config === void 0 ? void 0 : config.endpoints) === null || _t === void 0 ? void 0 : _t.value) === null || _u === void 0 ? void 0 : _u.type) === "object"
692
+ ? JSON.stringify(content)
693
+ : endpointUrl};
681
694
  `;
682
695
  });
683
696
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openapi-sync",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "A developer-friendly tool designed to keep your API up-to-date by leveraging OpenAPI schemas. It automates the generation of endpoint URIs and type definitions, including shared types, directly from your OpenAPI specification.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/types.ts CHANGED
@@ -67,7 +67,7 @@ export type IConfig = {
67
67
  refetchInterval?: number;
68
68
  folder?: string;
69
69
  api: Record<string, string>;
70
- server?: number;
70
+ server?: number | string;
71
71
  types?: {
72
72
  name?: {
73
73
  prefix?: string;
@@ -90,14 +90,17 @@ export type IConfig = {
90
90
  value?: {
91
91
  replaceWords?: IConfigReplaceWord[];
92
92
  includeServer?: boolean;
93
+ type?: "string" | "object";
93
94
  };
94
95
  name?: {
95
96
  format?: (data: {
96
97
  method: Method;
97
98
  path: string;
98
99
  summary: string;
100
+ operationId: string;
99
101
  }) => string | null;
100
102
  prefix?: string;
103
+ useOperationId?: boolean;
101
104
  };
102
105
  doc: IConfigDoc;
103
106
  };