pwi-plata-type 0.3.8 → 0.3.9

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/libs/axios.ts ADDED
@@ -0,0 +1,43 @@
1
+ import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'
2
+ import { PlataExtra, PlataResultado } from './tools';
3
+
4
+ export async function PlataAxios<T = any>(config: AxiosRequestConfig): Promise<PlataResultado<AxiosResponse<T>>> {
5
+ config.timeout = config.timeout ?? 1000
6
+
7
+ if (config.timeout === 0) {
8
+ return PlataAxiosUnsafe(config)
9
+ }
10
+
11
+ const axiosPromise = PlataAxiosUnsafe(config)
12
+ const timeoutPromise = PlataExtra.timeout(config.timeout, {
13
+ errorID: 'PLAX0003',
14
+ msg: 'API externa não respondeu'
15
+ })
16
+
17
+ return Promise.race([axiosPromise, timeoutPromise])
18
+ }
19
+
20
+ export async function PlataAxiosUnsafe<T = any>(config: AxiosRequestConfig): Promise<PlataResultado<AxiosResponse<T>>> {
21
+ return axios(config).then(
22
+ resp => resp,
23
+ err => {
24
+ if (err.response) {
25
+ return err.response
26
+ }
27
+
28
+ if (err.request) {
29
+ return {
30
+ errorID: 'PLAX0001',
31
+ msg: 'Erro ao se comunicar com API externa',
32
+ error: err
33
+ }
34
+ }
35
+
36
+ return {
37
+ errorID: 'PLAX0002',
38
+ msg: 'Erro inesperado ao se comunicar com API externa',
39
+ error: err
40
+ }
41
+ }
42
+ )
43
+ }
package/libs/tools.ts CHANGED
@@ -246,7 +246,11 @@ export namespace PlataRequire {
246
246
  }
247
247
 
248
248
  export namespace PlataExtra {
249
- export function Sleep(ms: number) {
249
+ export function sleep(ms: number) {
250
250
  return new Promise(resolve => setTimeout(resolve, ms))
251
251
  }
252
+
253
+ export function timeout(ms: number, error: PlataError): Promise<PlataError> {
254
+ return new Promise((_, reject) => setTimeout(() => reject(error), ms))
255
+ }
252
256
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pwi-plata-type",
3
- "version": "0.3.8",
3
+ "version": "0.3.9",
4
4
  "main": "index.ts",
5
5
  "bin": {
6
6
  "plata": "bin/plata.ts",
@@ -21,6 +21,7 @@
21
21
  "@types/jest": "^28.1.1",
22
22
  "@types/node-schedule": "^2.1.0",
23
23
  "@types/swagger-ui-express": "^4.1.3",
24
+ "axios": "^0.27.2",
24
25
  "commander": "^9.4.0",
25
26
  "express": "^4.18.1",
26
27
  "glob": "^8.0.3",