luca 1.0.0 → 1.0.1

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Luca · [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/iZemil/luca/blob/main/LICENSE) ![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/izemil/luca)
2
2
 
3
- Tiny [NodeJS package](https://www.npmjs.com/package/luca) to check urls by name list and getting statuses.
3
+ Tiny [NPM package](https://www.npmjs.com/package/luca) to check urls by name list and getting statuses.
4
4
 
5
5
  ## Installation
6
6
 
@@ -1,2 +1,4 @@
1
+ import { AxiosError } from 'axios';
1
2
  import { TConfig } from './types';
3
+ export declare function isAxiosError(error: any): error is AxiosError;
2
4
  export declare const runConfig: (options?: TConfig) => Promise<void>;
@@ -35,7 +35,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
35
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
36
36
  };
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.runConfig = void 0;
38
+ exports.runConfig = exports.isAxiosError = void 0;
39
39
  const axios_1 = __importDefault(require("axios"));
40
40
  const chalk_1 = __importDefault(require("chalk"));
41
41
  const fs = __importStar(require("fs/promises"));
@@ -48,40 +48,47 @@ function isAxiosError(error) {
48
48
  }
49
49
  return false;
50
50
  }
51
+ exports.isAxiosError = isAxiosError;
51
52
  const log = console.log;
53
+ // TODO: error retry
54
+ // TODO: run from last element
55
+ // TODO: clear result
56
+ // TODO: validat config - no config
52
57
  const runConfig = (options = require((0, consts_1.getConfigPath)())) => __awaiter(void 0, void 0, void 0, function* () {
53
58
  const resultPath = (0, consts_1.getResultPath)();
54
59
  try {
55
- (0, validateConfig_1.validateConfig)(options);
56
60
  log(chalk_1.default.blue('Luca is running...'));
57
- const { baseUrl, items, query, handler } = options;
61
+ const config = (0, validateConfig_1.validateConfig)(options);
62
+ const { baseUrl, items, query, handler } = config;
58
63
  const total = items.length;
59
64
  let index = 0;
60
65
  for (const item of items) {
66
+ const num = (index += 1);
67
+ const isFirst = num === 1;
68
+ const isLast = num === total;
61
69
  const url = query(baseUrl, item);
62
- let status = undefined;
63
70
  let response = null;
71
+ let isErroredRequest = false;
64
72
  try {
65
- response = (yield axios_1.default.get(url));
66
- status = response.status;
73
+ response = yield axios_1.default.get(url);
67
74
  }
68
75
  catch (e) {
76
+ isErroredRequest = true;
69
77
  if (isAxiosError(e) && e.response) {
70
- status = e.response.status;
78
+ response = e.response;
71
79
  }
72
80
  }
73
- log(chalk_1.default.blue(`${index + 1}/${total}`), `GET ${url} (${status})`);
81
+ finally {
82
+ log(chalk_1.default.blue(`${num}/${total}`), `GET ${chalk_1.default[isErroredRequest ? 'red' : 'green'](`(${response === null || response === void 0 ? void 0 : response.status})`)}:`, url);
83
+ }
74
84
  try {
75
85
  const data = handler(response, item);
76
- const isFirst = index === 0;
77
- const isLast = index + 1 === total;
78
86
  yield fs.appendFile(resultPath, `${isFirst ? '[' : ''}${JSON.stringify(data)}${isLast ? ']' : ','}`);
79
87
  }
80
88
  catch (error) {
81
89
  console.error('handler error', error);
82
90
  }
83
91
  finally {
84
- index += 1;
85
92
  yield (0, utils_1.delay)(options.delay);
86
93
  }
87
94
  }
@@ -1,2 +1,2 @@
1
1
  import { TConfig } from './types';
2
- export declare const validateConfig: (config: TConfig) => void;
2
+ export declare const validateConfig: (config: TConfig) => TConfig;
@@ -12,5 +12,6 @@ const validateConfig = (config) => {
12
12
  if (!handler) {
13
13
  throw new Error(`Specify config handler`);
14
14
  }
15
+ return config;
15
16
  };
16
17
  exports.validateConfig = validateConfig;
package/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  "dist"
17
17
  ],
18
18
  "license": "MIT",
19
- "version": "1.0.0",
19
+ "version": "1.0.1",
20
20
  "scripts": {
21
21
  "build": "tsc",
22
22
  "dev": "tsc-watch ./src/dev.ts --outDir ./dist/ --onSuccess \"node ./dist/dev.js\" --esModuleInterop",