@cimo/request 1.0.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.
- package/LICENSE.md +21 -0
- package/README.md +76 -0
- package/dist/Interface.d.ts +6 -0
- package/dist/Interface.js +3 -0
- package/dist/Interface.js.map +1 -0
- package/dist/Main.d.ts +2 -0
- package/dist/Main.js +29 -0
- package/dist/Main.js.map +1 -0
- package/dist/Manager.d.ts +5 -0
- package/dist/Manager.js +99 -0
- package/dist/Manager.js.map +1 -0
- package/package.json +46 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 cimo - reinventsoftware.org
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# Request
|
|
2
|
+
|
|
3
|
+
Request management. Light, fast and secure.
|
|
4
|
+
|
|
5
|
+
## Publish
|
|
6
|
+
|
|
7
|
+
1. npm run build
|
|
8
|
+
2. npm login --auth-type=legacy
|
|
9
|
+
3. npm publish --auth-type=legacy --access public
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
1. Link for npm package -> https://www.npmjs.com/package/@cimo/request
|
|
14
|
+
|
|
15
|
+
## Client
|
|
16
|
+
|
|
17
|
+
- Index.ts
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
...
|
|
21
|
+
|
|
22
|
+
import * as Cr from "@cimo/request";
|
|
23
|
+
|
|
24
|
+
...
|
|
25
|
+
|
|
26
|
+
Cr.create("https://localhost", 30000);
|
|
27
|
+
|
|
28
|
+
Cr.setRequestInterceptor((config) => {
|
|
29
|
+
//...
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
...config,
|
|
33
|
+
headers: {
|
|
34
|
+
"Accept-Language": "en,ja;q=0.9"
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
Cr.setResponseInterceptor((response) => {
|
|
40
|
+
if (response.ok) {
|
|
41
|
+
// Success
|
|
42
|
+
} else {
|
|
43
|
+
// Fail
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
...
|
|
48
|
+
|
|
49
|
+
const data = {
|
|
50
|
+
token_api: "1234",
|
|
51
|
+
name: "test",
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const formData = new FormData();
|
|
55
|
+
formData.append("token_api", "1234");
|
|
56
|
+
formData.append("name", "test");
|
|
57
|
+
|
|
58
|
+
Cr.post("/test_post", {}, formData) // Or use json data
|
|
59
|
+
.then((data) => {
|
|
60
|
+
// Response
|
|
61
|
+
})
|
|
62
|
+
.catch((error) => {
|
|
63
|
+
// Error
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
Cr.post("/test_get", {})
|
|
67
|
+
.then((data) => {
|
|
68
|
+
// Response
|
|
69
|
+
})
|
|
70
|
+
.catch((error) => {
|
|
71
|
+
// Error
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
...
|
|
75
|
+
|
|
76
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Interface.js","sourceRoot":"","sources":["../src/Interface.ts"],"names":[],"mappings":""}
|
package/dist/Main.d.ts
ADDED
package/dist/Main.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.Cr = exports.CrInterface = void 0;
|
|
27
|
+
exports.CrInterface = __importStar(require("./Interface"));
|
|
28
|
+
exports.Cr = __importStar(require("./Manager"));
|
|
29
|
+
//# sourceMappingURL=Main.js.map
|
package/dist/Main.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Main.js","sourceRoot":"","sources":["../src/Main.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2DAA2C;AAC3C,gDAAgC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const create: (baseUrlValue: string, timeoutValue: number) => void;
|
|
2
|
+
export declare const setRequestInterceptor: (callback: (config: RequestInit) => RequestInit) => void;
|
|
3
|
+
export declare const setResponseInterceptor: (callback: (response: Response) => void) => void;
|
|
4
|
+
export declare const post: <T>(partialUrl: string, config: RequestInit, bodyValue: Record<string, unknown> | FormData) => Promise<T>;
|
|
5
|
+
export declare const get: <T>(partialUrl: string, config: RequestInit) => Promise<T>;
|
package/dist/Manager.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.get = exports.post = exports.setResponseInterceptor = exports.setRequestInterceptor = exports.create = void 0;
|
|
4
|
+
let baseUrl;
|
|
5
|
+
let timeout;
|
|
6
|
+
let requestInterceptor;
|
|
7
|
+
let responseInterceptor;
|
|
8
|
+
const create = (baseUrlValue, timeoutValue) => {
|
|
9
|
+
baseUrl = baseUrlValue;
|
|
10
|
+
timeout = timeoutValue;
|
|
11
|
+
requestInterceptor = null;
|
|
12
|
+
responseInterceptor = null;
|
|
13
|
+
};
|
|
14
|
+
exports.create = create;
|
|
15
|
+
const setRequestInterceptor = (callback) => {
|
|
16
|
+
requestInterceptor = callback;
|
|
17
|
+
};
|
|
18
|
+
exports.setRequestInterceptor = setRequestInterceptor;
|
|
19
|
+
const setResponseInterceptor = (callback) => {
|
|
20
|
+
responseInterceptor = callback;
|
|
21
|
+
};
|
|
22
|
+
exports.setResponseInterceptor = setResponseInterceptor;
|
|
23
|
+
const post = (partialUrl, config, bodyValue) => {
|
|
24
|
+
const isJson = JSON.stringify(bodyValue).length > 2 ? true : false;
|
|
25
|
+
if (requestInterceptor) {
|
|
26
|
+
config = requestInterceptor(config || {});
|
|
27
|
+
}
|
|
28
|
+
return new Promise((resolve, reject) => {
|
|
29
|
+
const fetchConfig = {
|
|
30
|
+
...config,
|
|
31
|
+
signal: null,
|
|
32
|
+
method: "POST",
|
|
33
|
+
headers: isJson ? { ...config.headers, "Content-Type": "application/json" } : config.headers,
|
|
34
|
+
body: isJson ? JSON.stringify(bodyValue) : bodyValue
|
|
35
|
+
};
|
|
36
|
+
if (timeout > 0) {
|
|
37
|
+
const controller = new AbortController();
|
|
38
|
+
setTimeout(() => {
|
|
39
|
+
controller.abort();
|
|
40
|
+
}, timeout);
|
|
41
|
+
fetchConfig.signal = controller.signal;
|
|
42
|
+
}
|
|
43
|
+
fetch(`${baseUrl}${partialUrl}`, fetchConfig)
|
|
44
|
+
.then((response) => {
|
|
45
|
+
if (responseInterceptor) {
|
|
46
|
+
responseInterceptor(response);
|
|
47
|
+
}
|
|
48
|
+
if (!response.ok) {
|
|
49
|
+
reject(new Error(`Request failed with status: ${response.status}`));
|
|
50
|
+
}
|
|
51
|
+
return response.json();
|
|
52
|
+
})
|
|
53
|
+
.then((data) => {
|
|
54
|
+
resolve(data);
|
|
55
|
+
})
|
|
56
|
+
.catch((error) => {
|
|
57
|
+
reject(error);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
exports.post = post;
|
|
62
|
+
const get = (partialUrl, config) => {
|
|
63
|
+
if (requestInterceptor) {
|
|
64
|
+
config = requestInterceptor(config || {});
|
|
65
|
+
}
|
|
66
|
+
return new Promise((resolve, reject) => {
|
|
67
|
+
const fetchConfig = {
|
|
68
|
+
...config,
|
|
69
|
+
signal: null,
|
|
70
|
+
method: "POST",
|
|
71
|
+
headers: config.headers
|
|
72
|
+
};
|
|
73
|
+
if (timeout > 0) {
|
|
74
|
+
const controller = new AbortController();
|
|
75
|
+
setTimeout(() => {
|
|
76
|
+
controller.abort();
|
|
77
|
+
}, timeout);
|
|
78
|
+
fetchConfig.signal = controller.signal;
|
|
79
|
+
}
|
|
80
|
+
fetch(`${baseUrl}${partialUrl}`, fetchConfig)
|
|
81
|
+
.then((response) => {
|
|
82
|
+
if (responseInterceptor) {
|
|
83
|
+
responseInterceptor(response);
|
|
84
|
+
}
|
|
85
|
+
if (!response.ok) {
|
|
86
|
+
reject(new Error(`Request failed with status: ${response.status}`));
|
|
87
|
+
}
|
|
88
|
+
return response.json();
|
|
89
|
+
})
|
|
90
|
+
.then((data) => {
|
|
91
|
+
resolve(data);
|
|
92
|
+
})
|
|
93
|
+
.catch((error) => {
|
|
94
|
+
reject(error);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
exports.get = get;
|
|
99
|
+
//# sourceMappingURL=Manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Manager.js","sourceRoot":"","sources":["../src/Manager.ts"],"names":[],"mappings":";;;AAEA,IAAI,OAAe,CAAC;AACpB,IAAI,OAAe,CAAC;AACpB,IAAI,kBAAwD,CAAC;AAC7D,IAAI,mBAA0D,CAAC;AAExD,MAAM,MAAM,GAAG,CAAC,YAAoB,EAAE,YAAoB,EAAE,EAAE;IACjE,OAAO,GAAG,YAAY,CAAC;IACvB,OAAO,GAAG,YAAY,CAAC;IACvB,kBAAkB,GAAG,IAAI,CAAC;IAC1B,mBAAmB,GAAG,IAAI,CAAC;AAC/B,CAAC,CAAC;AALW,QAAA,MAAM,UAKjB;AAEK,MAAM,qBAAqB,GAAG,CAAC,QAA8C,EAAE,EAAE;IACpF,kBAAkB,GAAG,QAAQ,C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AAW,CAAC;aACxC,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE;YACf,IAAI,mBAAmB,EAAE;gBACrB,mBAAmB,CAAC,QAAQ,CAAC,CAAC;aACjC;YAED,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;gBACd,MAAM,CAAC,IAAI,KAAK,CAAC,+BAA+B,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;aACvE;YAED,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC;QAC3B,CAAC,CAAC;aACD,IAAI,CAAC,CAAC,IAAO,EAAE,EAAE;YACd,OAAO,CAAC,IAAI,CAAC,CAAC;QAClB,CAAC,CAAC;aACD,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;YACb,MAAM,CAAC,KAAK,CAAC,CAAC;QAClB,CAAC,CAAC,CAAC;IACX,CAAC,CAAC,CAAC;AACP,CAAC,CAAC;AA1CW,QAAA,GAAG,OA0Cd"}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cimo/request",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Request management. Light, fast and secure.",
|
|
5
|
+
"author": "cimo",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/cimo/Request.git"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"preinstall": "([ ! -f package-lock.json ] && npm install --package-lock-only --ignore-scripts --no-audit); npx npm-force-resolutions",
|
|
13
|
+
"check": "eslint . --fix",
|
|
14
|
+
"remove_dist": "node -e \"const fs = require('fs'); try{ fs.rmdirSync('./dist/', { recursive: true }) } catch{}; process.exit(0);\"",
|
|
15
|
+
"build": "npm run check && npm run remove_dist && tsc --build tsconfig.build.json",
|
|
16
|
+
"pack": "npm run build && npm pack"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/node": "18.14.4",
|
|
21
|
+
"@typescript-eslint/parser": "5.54.0",
|
|
22
|
+
"@typescript-eslint/eslint-plugin": "5.54.0",
|
|
23
|
+
"eslint": "8.35.0",
|
|
24
|
+
"eslint-plugin-prettier": "4.2.1",
|
|
25
|
+
"eslint-config-prettier": "8.8.0",
|
|
26
|
+
"prettier": "2.8.7",
|
|
27
|
+
"typescript": "4.9.4"
|
|
28
|
+
},
|
|
29
|
+
"resolutions": {
|
|
30
|
+
"node-fetch": "2.6.7",
|
|
31
|
+
"**/node-fetch": "2.6.7"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"./dist/"
|
|
35
|
+
],
|
|
36
|
+
"main": "./dist/Main.js",
|
|
37
|
+
"types": "./dist/Main.d.ts",
|
|
38
|
+
"keywords": [
|
|
39
|
+
"request",
|
|
40
|
+
"management",
|
|
41
|
+
"request management",
|
|
42
|
+
"nodejs",
|
|
43
|
+
"typescript",
|
|
44
|
+
"cimo"
|
|
45
|
+
]
|
|
46
|
+
}
|