openapi-sync 1.0.11 → 1.0.13

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/bin/cli.js CHANGED
@@ -1,3 +1,20 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- require("../dist/index");
3
+ const OpenApisync = require("../dist/index");
4
+
5
+ const yargs = require("yargs/yargs");
6
+ const { hideBin } = require("yargs/helpers");
7
+
8
+ // Setup yargs
9
+ const argv = yargs(hideBin(process.argv))
10
+ .option("refreshinterval", {
11
+ alias: "ri",
12
+ type: "number",
13
+ description: "Interval at which to refretch specifiations",
14
+ })
15
+ .help().argv;
16
+
17
+ // Use the flags
18
+ OpenApisync.Init({
19
+ refetchInterval: argv.refreshinterval,
20
+ });
package/db.json ADDED
@@ -0,0 +1 @@
1
+ {}
@@ -21,10 +21,10 @@ const axios_retry_1 = __importDefault(require("axios-retry"));
21
21
  const openapi_core_1 = require("@redocly/openapi-core");
22
22
  const state_1 = require("./state");
23
23
  const rootUsingCwd = process.cwd();
24
- let fetchTimeout = null;
24
+ let fetchTimeout = {};
25
25
  // Create an Axios instance
26
26
  const apiClient = axios_1.default.create({
27
- timeout: 30000, // Timeout after 30 seconds
27
+ timeout: 60000, // Timeout after 1min
28
28
  });
29
29
  // Configure axios-retry
30
30
  (0, axios_retry_1.default)(apiClient, {
@@ -37,7 +37,7 @@ const apiClient = axios_1.default.create({
37
37
  return retryCount * 1000; // Exponential back-off: 1s, 2s, 3s, etc.
38
38
  },
39
39
  });
40
- const OpenapiSync = (apiUrl, apiName) => __awaiter(void 0, void 0, void 0, function* () {
40
+ const OpenapiSync = (apiUrl, apiName, refetchInterval) => __awaiter(void 0, void 0, void 0, function* () {
41
41
  const specResponse = yield apiClient.get(apiUrl);
42
42
  const redoclyConfig = yield (0, openapi_core_1.createConfig)({
43
43
  extends: ["minimal"],
@@ -54,21 +54,21 @@ const OpenapiSync = (apiUrl, apiName) => __awaiter(void 0, void 0, void 0, funct
54
54
  const folderPath = path_1.default.join(config.folder || "", apiName);
55
55
  const spec = lintResults.bundle.parsed;
56
56
  // auto update only on dev
57
- if (!(process.env.NODE_ENV &&
58
- ["production", "prod", "test", "staging"].includes(process.env.NODE_ENV))) {
59
- // compare new spec with old spec, continuing only if spec it different
60
- // auto sync at interval
61
- if (fetchTimeout)
62
- clearTimeout(fetchTimeout);
63
- if (!isNaN(config.refetchInterval) && config.refetchInterval) {
64
- // use config interval or 1 hour
65
- fetchTimeout = setTimeout(() => OpenapiSync(apiUrl, apiName), config.refetchInterval || 60000);
57
+ if (refetchInterval && !isNaN(refetchInterval) && refetchInterval > 0) {
58
+ if (!(process.env.NODE_ENV &&
59
+ ["production", "prod", "test", "staging"].includes(process.env.NODE_ENV))) {
60
+ // auto sync at interval
61
+ if (fetchTimeout[apiName])
62
+ clearTimeout(fetchTimeout[apiName]);
63
+ // set next request timeout
64
+ fetchTimeout[apiName] = setTimeout(() => OpenapiSync(apiUrl, apiName, refetchInterval), refetchInterval);
66
65
  }
67
- const prevSpec = (0, state_1.getState)(apiName);
68
- if ((0, lodash_1.isEqual)(prevSpec, spec))
69
- return;
70
- (0, state_1.setState)(apiName, spec);
71
66
  }
67
+ // compare new spec with old spec, continuing only if spec it different
68
+ const prevSpec = (0, state_1.getState)(apiName);
69
+ if ((0, lodash_1.isEqual)(prevSpec, spec))
70
+ return;
71
+ (0, state_1.setState)(apiName, spec);
72
72
  let endpointsFileContent = "";
73
73
  let typesFileContent = "";
74
74
  let sharedTypesFileContent = "";
@@ -97,6 +97,21 @@ const OpenapiSync = (apiUrl, apiName) => __awaiter(void 0, void 0, void 0, funct
97
97
  }
98
98
  return typeCnt;
99
99
  };
100
+ const treatEndpointUrl = (endpointUrl) => {
101
+ var _a, _b;
102
+ if (((_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.replaceWords) &&
103
+ Array.isArray(config.endpoints.value.replaceWords)) {
104
+ let newEndpointUrl = endpointUrl;
105
+ config.endpoints.value.replaceWords.forEach((replaceWord, indx) => {
106
+ const regexp = new RegExp(replaceWord.replace, "g");
107
+ newEndpointUrl = newEndpointUrl.replace(regexp, replaceWord.with || "");
108
+ });
109
+ return newEndpointUrl;
110
+ }
111
+ else {
112
+ return endpointUrl;
113
+ }
114
+ };
100
115
  Object.keys(spec.paths || {}).forEach((endpointPath) => {
101
116
  const endpointSpec = spec.paths[endpointPath];
102
117
  const endpointMethods = Object.keys(endpointSpec);
@@ -118,6 +133,8 @@ const OpenapiSync = (apiUrl, apiName) => __awaiter(void 0, void 0, void 0, funct
118
133
  const params = endpoint.variables.map((v) => `${v}:string`).join(",");
119
134
  endpointUrl = `(${params})=> \`${endpointUrlTxt}\``;
120
135
  }
136
+ //treat endpoint url
137
+ endpointUrl = treatEndpointUrl(endpointUrl);
121
138
  // Add the endpoint url
122
139
  endpointsFileContent += `export const ${endpoint.name} = ${endpointUrl};
123
140
  `;
@@ -1,9 +1,19 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.resetState = exports.getState = exports.setState = void 0;
4
- let state = {};
7
+ const path_1 = __importDefault(require("path"));
8
+ const fs_1 = __importDefault(require("fs"));
9
+ const dbPath = path_1.default.join(__dirname, "../", "../db.json");
10
+ let state = require(dbPath);
11
+ const updateDB = (data) => {
12
+ fs_1.default.writeFileSync(dbPath, JSON.stringify(data));
13
+ };
5
14
  const setState = (key, value) => {
6
15
  state[key] = value;
16
+ updateDB(state);
7
17
  };
8
18
  exports.setState = setState;
9
19
  const getState = (key) => {
@@ -12,5 +22,6 @@ const getState = (key) => {
12
22
  exports.getState = getState;
13
23
  const resetState = () => {
14
24
  state = {};
25
+ updateDB(state);
15
26
  };
16
27
  exports.resetState = resetState;
package/dist/index.js CHANGED
@@ -12,21 +12,27 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.Init = void 0;
15
16
  const Openapi_sync_1 = __importDefault(require("./Openapi-sync"));
16
17
  const dotenv_1 = __importDefault(require("dotenv"));
17
18
  const path_1 = __importDefault(require("path"));
18
19
  const state_1 = require("./Openapi-sync/state");
19
20
  dotenv_1.default.config();
20
21
  const rootUsingCwd = process.cwd();
21
- const Init = () => __awaiter(void 0, void 0, void 0, function* () {
22
+ const Init = (options) => __awaiter(void 0, void 0, void 0, function* () {
22
23
  // Load config file
23
24
  const config = require(path_1.default.join(rootUsingCwd, "openapi.sync.json"));
24
25
  const apiNames = Object.keys(config.api);
26
+ const refetchInterval = options &&
27
+ "refetchInterval" in options &&
28
+ !isNaN(options === null || options === void 0 ? void 0 : options.refetchInterval)
29
+ ? options.refetchInterval
30
+ : config.refetchInterval;
25
31
  (0, state_1.resetState)();
26
32
  for (let i = 0; i < apiNames.length; i += 1) {
27
33
  const apiName = apiNames[i];
28
34
  const apiUrl = config.api[apiName];
29
- (0, Openapi_sync_1.default)(apiUrl, apiName);
35
+ (0, Openapi_sync_1.default)(apiUrl, apiName, refetchInterval);
30
36
  }
31
37
  });
32
- Init();
38
+ exports.Init = Init;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openapi-sync",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "description": "sync openapi variables",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -23,6 +23,7 @@
23
23
  "files": [
24
24
  "bin",
25
25
  "dist",
26
+ "db.json",
26
27
  "LICENSE",
27
28
  "README.md",
28
29
  "package.json"
@@ -31,7 +32,7 @@
31
32
  "test": "echo \"Error: no test specified\"",
32
33
  "build": "tsc",
33
34
  "publish-package": "npm run build && npm version patch && npm publish",
34
- "start": "ts-node index.ts"
35
+ "start": "npm run build && openapi-sync"
35
36
  },
36
37
  "author": "P-Technologies",
37
38
  "license": "ISC",
@@ -39,7 +40,6 @@
39
40
  "@types/js-yaml": "^4.0.9",
40
41
  "@types/lodash": "^4.17.7",
41
42
  "@types/node": "^22.1.0",
42
- "ts-node": "^10.9.2",
43
43
  "typescript": "^5.5.4"
44
44
  },
45
45
  "dependencies": {
@@ -48,6 +48,7 @@
48
48
  "axios-retry": "^4.5.0",
49
49
  "dotenv": "^16.4.5",
50
50
  "js-yaml": "^4.1.0",
51
- "lodash": "^4.17.21"
51
+ "lodash": "^4.17.21",
52
+ "yargs": "^17.7.2"
52
53
  }
53
54
  }