@iamnnort/request 1.8.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 +44 -0
- package/dist/index.d.mts +82 -0
- package/dist/index.d.ts +82 -0
- package/dist/index.js +405 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +367 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +60 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 mingchuno
|
|
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,44 @@
|
|
|
1
|
+
## Info
|
|
2
|
+
|
|
3
|
+
Request handler for Node.js - Fast - Interactive - Simple
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
yarn install @iamnnort/request
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
import { RequestDataSource, HttpMethods } from '@iamnnort/request';
|
|
15
|
+
|
|
16
|
+
const dataSource = new RequestDataSource({
|
|
17
|
+
baseUrl: '...',
|
|
18
|
+
url: '/users'
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const users = await dataSource.search();
|
|
22
|
+
|
|
23
|
+
const user = await dataSource.get();
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Parameters
|
|
27
|
+
|
|
28
|
+
| Parameter | Description |
|
|
29
|
+
| ------------------ | -------------------------------------------------------------------------- |
|
|
30
|
+
| `baseUrl` | Main part of the server URL that will be used for the request |
|
|
31
|
+
| `url` | Server URL that will be used for the request |
|
|
32
|
+
| `urlParts` | Additional parts of URL that will be used for the request |
|
|
33
|
+
| `method` | Request method to be used when making the request |
|
|
34
|
+
| `params` | URL parameters to be sent with the request |
|
|
35
|
+
| `data` | Data to be sent as the request body |
|
|
36
|
+
| `headers` | Custom headers to be sent |
|
|
37
|
+
| `serializer` | Config that allows you to customize serializing |
|
|
38
|
+
| `serializer.array` | Array element separator (`"indices"`, `"brackets"`, `"repeat"`, `"comma"`) |
|
|
39
|
+
| `logger` | Enable a logger |
|
|
40
|
+
| `debug` | Enable a debug mode |
|
|
41
|
+
|
|
42
|
+
## License
|
|
43
|
+
|
|
44
|
+
This project is licensed under the MIT license. See the [LICENSE](LICENSE) file for more info.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
|
2
|
+
|
|
3
|
+
type RequestParams = Pick<AxiosRequestConfig, 'params' | 'data'>;
|
|
4
|
+
type RequestConfigParams = Pick<AxiosRequestConfig, 'params' | 'data'>;
|
|
5
|
+
type RequestConfig = Omit<AxiosRequestConfig, 'baseURL' | 'url'> & {
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
baseUrlName?: string;
|
|
8
|
+
url?: number | string;
|
|
9
|
+
urlParts?: (number | string)[];
|
|
10
|
+
bearerToken?: string;
|
|
11
|
+
urlencoded?: boolean;
|
|
12
|
+
multipart?: boolean;
|
|
13
|
+
xml?: boolean;
|
|
14
|
+
};
|
|
15
|
+
type BaseRequestConfig = Pick<AxiosRequestConfig, 'auth' | 'headers'> & {
|
|
16
|
+
name?: string;
|
|
17
|
+
baseUrl?: string;
|
|
18
|
+
baseUrlMap?: Record<string, string>;
|
|
19
|
+
url?: number | string;
|
|
20
|
+
urlParts?: (number | string)[];
|
|
21
|
+
bearerToken?: string;
|
|
22
|
+
debug?: boolean;
|
|
23
|
+
logger?: boolean;
|
|
24
|
+
serializer?: {
|
|
25
|
+
array?: 'indices' | 'brackets' | 'repeat' | 'comma';
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
declare enum HttpMethods {
|
|
29
|
+
GET = "get",
|
|
30
|
+
POST = "post",
|
|
31
|
+
PUT = "put",
|
|
32
|
+
DELETE = "delete"
|
|
33
|
+
}
|
|
34
|
+
declare enum HttpStatuses {
|
|
35
|
+
OK = 200,
|
|
36
|
+
CREATED = 201,
|
|
37
|
+
ACCEPTED = 202,
|
|
38
|
+
NO_CONTENT = 204,
|
|
39
|
+
BAD_REQUEST = 400,
|
|
40
|
+
UNAUTHORIZED = 401,
|
|
41
|
+
FORBIDDEN = 403,
|
|
42
|
+
NOT_FOUND = 404,
|
|
43
|
+
CONFLICT = 409,
|
|
44
|
+
INTERNAL_SERVER_ERROR = 500
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
declare class RequestBuilder {
|
|
48
|
+
baseConfig: BaseRequestConfig;
|
|
49
|
+
requestConfig: RequestConfig;
|
|
50
|
+
config: AxiosRequestConfig;
|
|
51
|
+
constructor(params: {
|
|
52
|
+
baseConfig: BaseRequestConfig;
|
|
53
|
+
requestConfig: RequestConfig;
|
|
54
|
+
});
|
|
55
|
+
makeContentType(): this;
|
|
56
|
+
makeAuth(): this;
|
|
57
|
+
makeUrl(): this;
|
|
58
|
+
makeMethod(): this;
|
|
59
|
+
makeData(): this;
|
|
60
|
+
makeParams(): this;
|
|
61
|
+
makeSerializer(): this;
|
|
62
|
+
build(): AxiosRequestConfig<any>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
declare class RequestDataSource<Entity extends Record<string, any> = any, SearchParams extends RequestConfigParams = any, SearchResponse extends Record<string, any> = any, CreateParams extends RequestConfigParams = any, UpdateParams extends RequestConfigParams = any> {
|
|
66
|
+
baseConfig: BaseRequestConfig;
|
|
67
|
+
constructor(baseConfig: BaseRequestConfig);
|
|
68
|
+
common<T>(requestConfig: RequestConfig): Promise<T>;
|
|
69
|
+
search(config?: SearchParams): Promise<SearchResponse>;
|
|
70
|
+
get(id: number | string, config?: SearchParams): Promise<Entity>;
|
|
71
|
+
create(config: CreateParams): Promise<Entity>;
|
|
72
|
+
bulkCreate(config: CreateParams): Promise<Entity[]>;
|
|
73
|
+
update(config: UpdateParams): Promise<Entity>;
|
|
74
|
+
bulkUpdate(config: UpdateParams): Promise<Entity[]>;
|
|
75
|
+
remove(id: number | string, config?: SearchParams): Promise<void>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
declare class RequestHelper {
|
|
79
|
+
sleep(seconds: number): Promise<unknown>;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { BaseRequestConfig, HttpMethods, HttpStatuses, RequestBuilder, RequestConfig, RequestConfigParams, RequestDataSource, RequestHelper, RequestParams };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
|
2
|
+
|
|
3
|
+
type RequestParams = Pick<AxiosRequestConfig, 'params' | 'data'>;
|
|
4
|
+
type RequestConfigParams = Pick<AxiosRequestConfig, 'params' | 'data'>;
|
|
5
|
+
type RequestConfig = Omit<AxiosRequestConfig, 'baseURL' | 'url'> & {
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
baseUrlName?: string;
|
|
8
|
+
url?: number | string;
|
|
9
|
+
urlParts?: (number | string)[];
|
|
10
|
+
bearerToken?: string;
|
|
11
|
+
urlencoded?: boolean;
|
|
12
|
+
multipart?: boolean;
|
|
13
|
+
xml?: boolean;
|
|
14
|
+
};
|
|
15
|
+
type BaseRequestConfig = Pick<AxiosRequestConfig, 'auth' | 'headers'> & {
|
|
16
|
+
name?: string;
|
|
17
|
+
baseUrl?: string;
|
|
18
|
+
baseUrlMap?: Record<string, string>;
|
|
19
|
+
url?: number | string;
|
|
20
|
+
urlParts?: (number | string)[];
|
|
21
|
+
bearerToken?: string;
|
|
22
|
+
debug?: boolean;
|
|
23
|
+
logger?: boolean;
|
|
24
|
+
serializer?: {
|
|
25
|
+
array?: 'indices' | 'brackets' | 'repeat' | 'comma';
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
declare enum HttpMethods {
|
|
29
|
+
GET = "get",
|
|
30
|
+
POST = "post",
|
|
31
|
+
PUT = "put",
|
|
32
|
+
DELETE = "delete"
|
|
33
|
+
}
|
|
34
|
+
declare enum HttpStatuses {
|
|
35
|
+
OK = 200,
|
|
36
|
+
CREATED = 201,
|
|
37
|
+
ACCEPTED = 202,
|
|
38
|
+
NO_CONTENT = 204,
|
|
39
|
+
BAD_REQUEST = 400,
|
|
40
|
+
UNAUTHORIZED = 401,
|
|
41
|
+
FORBIDDEN = 403,
|
|
42
|
+
NOT_FOUND = 404,
|
|
43
|
+
CONFLICT = 409,
|
|
44
|
+
INTERNAL_SERVER_ERROR = 500
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
declare class RequestBuilder {
|
|
48
|
+
baseConfig: BaseRequestConfig;
|
|
49
|
+
requestConfig: RequestConfig;
|
|
50
|
+
config: AxiosRequestConfig;
|
|
51
|
+
constructor(params: {
|
|
52
|
+
baseConfig: BaseRequestConfig;
|
|
53
|
+
requestConfig: RequestConfig;
|
|
54
|
+
});
|
|
55
|
+
makeContentType(): this;
|
|
56
|
+
makeAuth(): this;
|
|
57
|
+
makeUrl(): this;
|
|
58
|
+
makeMethod(): this;
|
|
59
|
+
makeData(): this;
|
|
60
|
+
makeParams(): this;
|
|
61
|
+
makeSerializer(): this;
|
|
62
|
+
build(): AxiosRequestConfig<any>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
declare class RequestDataSource<Entity extends Record<string, any> = any, SearchParams extends RequestConfigParams = any, SearchResponse extends Record<string, any> = any, CreateParams extends RequestConfigParams = any, UpdateParams extends RequestConfigParams = any> {
|
|
66
|
+
baseConfig: BaseRequestConfig;
|
|
67
|
+
constructor(baseConfig: BaseRequestConfig);
|
|
68
|
+
common<T>(requestConfig: RequestConfig): Promise<T>;
|
|
69
|
+
search(config?: SearchParams): Promise<SearchResponse>;
|
|
70
|
+
get(id: number | string, config?: SearchParams): Promise<Entity>;
|
|
71
|
+
create(config: CreateParams): Promise<Entity>;
|
|
72
|
+
bulkCreate(config: CreateParams): Promise<Entity[]>;
|
|
73
|
+
update(config: UpdateParams): Promise<Entity>;
|
|
74
|
+
bulkUpdate(config: UpdateParams): Promise<Entity[]>;
|
|
75
|
+
remove(id: number | string, config?: SearchParams): Promise<void>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
declare class RequestHelper {
|
|
79
|
+
sleep(seconds: number): Promise<unknown>;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { BaseRequestConfig, HttpMethods, HttpStatuses, RequestBuilder, RequestConfig, RequestConfigParams, RequestDataSource, RequestHelper, RequestParams };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defProps = Object.defineProperties;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
9
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
10
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
12
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
+
var __spreadValues = (a, b) => {
|
|
14
|
+
for (var prop in b || (b = {}))
|
|
15
|
+
if (__hasOwnProp.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
if (__getOwnPropSymbols)
|
|
18
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
19
|
+
if (__propIsEnum.call(b, prop))
|
|
20
|
+
__defNormalProp(a, prop, b[prop]);
|
|
21
|
+
}
|
|
22
|
+
return a;
|
|
23
|
+
};
|
|
24
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
25
|
+
var __export = (target, all) => {
|
|
26
|
+
for (var name in all)
|
|
27
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
28
|
+
};
|
|
29
|
+
var __copyProps = (to, from, except, desc) => {
|
|
30
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
31
|
+
for (let key of __getOwnPropNames(from))
|
|
32
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
33
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
34
|
+
}
|
|
35
|
+
return to;
|
|
36
|
+
};
|
|
37
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
38
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
39
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
40
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
41
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
42
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
43
|
+
mod
|
|
44
|
+
));
|
|
45
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
46
|
+
|
|
47
|
+
// src/index.ts
|
|
48
|
+
var src_exports = {};
|
|
49
|
+
__export(src_exports, {
|
|
50
|
+
HttpMethods: () => HttpMethods,
|
|
51
|
+
HttpStatuses: () => HttpStatuses,
|
|
52
|
+
RequestBuilder: () => RequestBuilder,
|
|
53
|
+
RequestDataSource: () => RequestDataSource,
|
|
54
|
+
RequestHelper: () => RequestHelper
|
|
55
|
+
});
|
|
56
|
+
module.exports = __toCommonJS(src_exports);
|
|
57
|
+
|
|
58
|
+
// src/builder.ts
|
|
59
|
+
var import_qs = require("qs");
|
|
60
|
+
|
|
61
|
+
// src/types.ts
|
|
62
|
+
var HttpMethods = /* @__PURE__ */ ((HttpMethods2) => {
|
|
63
|
+
HttpMethods2["GET"] = "get";
|
|
64
|
+
HttpMethods2["POST"] = "post";
|
|
65
|
+
HttpMethods2["PUT"] = "put";
|
|
66
|
+
HttpMethods2["DELETE"] = "delete";
|
|
67
|
+
return HttpMethods2;
|
|
68
|
+
})(HttpMethods || {});
|
|
69
|
+
var HttpStatuses = /* @__PURE__ */ ((HttpStatuses2) => {
|
|
70
|
+
HttpStatuses2[HttpStatuses2["OK"] = 200] = "OK";
|
|
71
|
+
HttpStatuses2[HttpStatuses2["CREATED"] = 201] = "CREATED";
|
|
72
|
+
HttpStatuses2[HttpStatuses2["ACCEPTED"] = 202] = "ACCEPTED";
|
|
73
|
+
HttpStatuses2[HttpStatuses2["NO_CONTENT"] = 204] = "NO_CONTENT";
|
|
74
|
+
HttpStatuses2[HttpStatuses2["BAD_REQUEST"] = 400] = "BAD_REQUEST";
|
|
75
|
+
HttpStatuses2[HttpStatuses2["UNAUTHORIZED"] = 401] = "UNAUTHORIZED";
|
|
76
|
+
HttpStatuses2[HttpStatuses2["FORBIDDEN"] = 403] = "FORBIDDEN";
|
|
77
|
+
HttpStatuses2[HttpStatuses2["NOT_FOUND"] = 404] = "NOT_FOUND";
|
|
78
|
+
HttpStatuses2[HttpStatuses2["CONFLICT"] = 409] = "CONFLICT";
|
|
79
|
+
HttpStatuses2[HttpStatuses2["INTERNAL_SERVER_ERROR"] = 500] = "INTERNAL_SERVER_ERROR";
|
|
80
|
+
return HttpStatuses2;
|
|
81
|
+
})(HttpStatuses || {});
|
|
82
|
+
|
|
83
|
+
// src/builder.ts
|
|
84
|
+
var RequestBuilder = class {
|
|
85
|
+
constructor(params) {
|
|
86
|
+
this.baseConfig = params.baseConfig;
|
|
87
|
+
this.requestConfig = params.requestConfig;
|
|
88
|
+
this.config = {
|
|
89
|
+
headers: __spreadValues(__spreadValues({
|
|
90
|
+
Accept: "application/json",
|
|
91
|
+
"Content-Type": "application/json"
|
|
92
|
+
}, params.baseConfig.headers), params.requestConfig.headers)
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
makeContentType() {
|
|
96
|
+
if (this.requestConfig.multipart) {
|
|
97
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
98
|
+
headers: __spreadProps(__spreadValues({}, this.config.headers), {
|
|
99
|
+
"Content-Type": "multipart/form-data"
|
|
100
|
+
})
|
|
101
|
+
});
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
if (this.requestConfig.urlencoded) {
|
|
105
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
106
|
+
headers: __spreadProps(__spreadValues({}, this.config.headers), {
|
|
107
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
108
|
+
})
|
|
109
|
+
});
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
if (this.requestConfig.xml) {
|
|
113
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
114
|
+
headers: __spreadProps(__spreadValues({}, this.config.headers), {
|
|
115
|
+
"Content-Type": "text/xml"
|
|
116
|
+
})
|
|
117
|
+
});
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
makeAuth() {
|
|
123
|
+
if (this.baseConfig.auth) {
|
|
124
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
125
|
+
auth: this.baseConfig.auth
|
|
126
|
+
});
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
const bearerToken = this.baseConfig.bearerToken || this.requestConfig.bearerToken;
|
|
130
|
+
if (bearerToken) {
|
|
131
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
132
|
+
headers: __spreadProps(__spreadValues({}, this.config.headers), {
|
|
133
|
+
Authorization: `Bearer ${bearerToken}`
|
|
134
|
+
})
|
|
135
|
+
});
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
makeUrl() {
|
|
141
|
+
const urlParts = [
|
|
142
|
+
this.baseConfig.baseUrlMap && this.requestConfig.baseUrlName ? this.baseConfig.baseUrlMap[this.requestConfig.baseUrlName] : this.baseConfig.baseUrl,
|
|
143
|
+
this.baseConfig.url,
|
|
144
|
+
...this.baseConfig.urlParts || [],
|
|
145
|
+
this.requestConfig.baseUrl,
|
|
146
|
+
this.requestConfig.url,
|
|
147
|
+
...this.requestConfig.urlParts || []
|
|
148
|
+
].map((urlPart) => urlPart == null ? void 0 : urlPart.toString());
|
|
149
|
+
const isSecureProtocol = urlParts.some(
|
|
150
|
+
(urlPart) => urlPart == null ? void 0 : urlPart.includes("https")
|
|
151
|
+
);
|
|
152
|
+
const protocol = isSecureProtocol ? "https" : "http";
|
|
153
|
+
const actualUrlParts = urlParts.filter((urlPart) => urlPart).map((urlPart) => {
|
|
154
|
+
return urlPart == null ? void 0 : urlPart.replace(/(^(https?:\/\/|\/))/, "");
|
|
155
|
+
});
|
|
156
|
+
const baseUrl = `${protocol}://${actualUrlParts[0]}`;
|
|
157
|
+
const url = `/${actualUrlParts.slice(1).join("/")}`;
|
|
158
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
159
|
+
baseURL: baseUrl,
|
|
160
|
+
url
|
|
161
|
+
});
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
makeMethod() {
|
|
165
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
166
|
+
method: this.requestConfig.method
|
|
167
|
+
});
|
|
168
|
+
return this;
|
|
169
|
+
}
|
|
170
|
+
makeData() {
|
|
171
|
+
if (this.requestConfig.method === "get" /* GET */) {
|
|
172
|
+
return this;
|
|
173
|
+
}
|
|
174
|
+
if (this.requestConfig.urlencoded) {
|
|
175
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
176
|
+
data: (0, import_qs.stringify)(this.requestConfig.data)
|
|
177
|
+
});
|
|
178
|
+
return this;
|
|
179
|
+
}
|
|
180
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
181
|
+
data: this.requestConfig.data
|
|
182
|
+
});
|
|
183
|
+
return this;
|
|
184
|
+
}
|
|
185
|
+
makeParams() {
|
|
186
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
187
|
+
params: this.requestConfig.params
|
|
188
|
+
});
|
|
189
|
+
return this;
|
|
190
|
+
}
|
|
191
|
+
makeSerializer() {
|
|
192
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
193
|
+
paramsSerializer: (params) => {
|
|
194
|
+
var _a;
|
|
195
|
+
return (0, import_qs.stringify)(params, {
|
|
196
|
+
arrayFormat: (_a = this.baseConfig.serializer) == null ? void 0 : _a.array
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
return this;
|
|
201
|
+
}
|
|
202
|
+
build() {
|
|
203
|
+
return this.config;
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
// src/data-source.ts
|
|
208
|
+
var import_axios = __toESM(require("axios"));
|
|
209
|
+
|
|
210
|
+
// src/logger/logger.ts
|
|
211
|
+
var import_qs2 = require("qs");
|
|
212
|
+
var makeType = (type) => {
|
|
213
|
+
return `[${type}]`;
|
|
214
|
+
};
|
|
215
|
+
var makeUrl = (dto = {}) => {
|
|
216
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
217
|
+
const url = ((_a = dto.request) == null ? void 0 : _a.url) || ((_c = (_b = dto.response) == null ? void 0 : _b.config) == null ? void 0 : _c.url) || ((_e = (_d = dto.error) == null ? void 0 : _d.response) == null ? void 0 : _e.config.url);
|
|
218
|
+
const params = ((_f = dto.request) == null ? void 0 : _f.params) || ((_h = (_g = dto.response) == null ? void 0 : _g.config) == null ? void 0 : _h.params) || ((_j = (_i = dto.error) == null ? void 0 : _i.response) == null ? void 0 : _j.config.params);
|
|
219
|
+
if (!url) {
|
|
220
|
+
return "";
|
|
221
|
+
}
|
|
222
|
+
if (params) {
|
|
223
|
+
delete params["0"];
|
|
224
|
+
return [url, (0, import_qs2.stringify)(params)].filter((_) => _).join("?");
|
|
225
|
+
} else {
|
|
226
|
+
return url;
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
var makeMethod = (dto = {}) => {
|
|
230
|
+
var _a, _b, _c, _d, _e;
|
|
231
|
+
const method = ((_a = dto.request) == null ? void 0 : _a.method) || ((_c = (_b = dto.response) == null ? void 0 : _b.config) == null ? void 0 : _c.method) || ((_e = (_d = dto.error) == null ? void 0 : _d.response) == null ? void 0 : _e.config.method);
|
|
232
|
+
if (!method) {
|
|
233
|
+
return "";
|
|
234
|
+
}
|
|
235
|
+
return method.toUpperCase();
|
|
236
|
+
};
|
|
237
|
+
var makeRequestData = (dto = {}) => {
|
|
238
|
+
var _a, _b, _c, _d, _e, _f;
|
|
239
|
+
const data = ((_a = dto.request) == null ? void 0 : _a.body) || ((_b = dto.request) == null ? void 0 : _b.data) || ((_d = (_c = dto.response) == null ? void 0 : _c.config) == null ? void 0 : _d.data) || ((_f = (_e = dto.error) == null ? void 0 : _e.response) == null ? void 0 : _f.config.data);
|
|
240
|
+
if (!data) {
|
|
241
|
+
return "";
|
|
242
|
+
}
|
|
243
|
+
if (typeof data === "string") {
|
|
244
|
+
return data;
|
|
245
|
+
}
|
|
246
|
+
return JSON.stringify(data);
|
|
247
|
+
};
|
|
248
|
+
var makeResponseData = (dto = {}) => {
|
|
249
|
+
var _a, _b, _c;
|
|
250
|
+
const data = ((_a = dto.response) == null ? void 0 : _a.data) || ((_c = (_b = dto.error) == null ? void 0 : _b.response) == null ? void 0 : _c.data);
|
|
251
|
+
if (!data) {
|
|
252
|
+
return "";
|
|
253
|
+
}
|
|
254
|
+
if (typeof data === "string") {
|
|
255
|
+
return data;
|
|
256
|
+
}
|
|
257
|
+
return JSON.stringify(data);
|
|
258
|
+
};
|
|
259
|
+
var makeStatus = (dto = {}) => {
|
|
260
|
+
var _a, _b, _c, _d, _e, _f;
|
|
261
|
+
const status = ((_a = dto.response) == null ? void 0 : _a.status) || ((_c = (_b = dto.error) == null ? void 0 : _b.response) == null ? void 0 : _c.status);
|
|
262
|
+
if (!status) {
|
|
263
|
+
return "";
|
|
264
|
+
}
|
|
265
|
+
const statusText = ((_d = dto.response) == null ? void 0 : _d.statusText) || ((_f = (_e = dto.error) == null ? void 0 : _e.response) == null ? void 0 : _f.statusText);
|
|
266
|
+
if (statusText) {
|
|
267
|
+
return `${status} ${statusText}`;
|
|
268
|
+
}
|
|
269
|
+
return `${status}`;
|
|
270
|
+
};
|
|
271
|
+
var logRequest = (request) => {
|
|
272
|
+
log([
|
|
273
|
+
makeType("Request"),
|
|
274
|
+
makeMethod({ request }),
|
|
275
|
+
makeUrl({ request }),
|
|
276
|
+
makeRequestData({ request })
|
|
277
|
+
]);
|
|
278
|
+
};
|
|
279
|
+
var logResponse = (response) => {
|
|
280
|
+
log([
|
|
281
|
+
makeType("Response"),
|
|
282
|
+
makeMethod({ response }),
|
|
283
|
+
makeUrl({ response }),
|
|
284
|
+
makeRequestData({ response }),
|
|
285
|
+
makeStatus({ response }),
|
|
286
|
+
makeResponseData({ response })
|
|
287
|
+
]);
|
|
288
|
+
};
|
|
289
|
+
var logRequestError = (error) => {
|
|
290
|
+
log([
|
|
291
|
+
makeType("Error"),
|
|
292
|
+
makeMethod({ error }),
|
|
293
|
+
makeUrl({ error }),
|
|
294
|
+
makeRequestData({ error }),
|
|
295
|
+
makeStatus({ error }),
|
|
296
|
+
makeResponseData({ error })
|
|
297
|
+
]);
|
|
298
|
+
};
|
|
299
|
+
var log = (messageParts) => {
|
|
300
|
+
const message = messageParts.filter((_) => _).join(" ");
|
|
301
|
+
console.log(message);
|
|
302
|
+
};
|
|
303
|
+
var loggerHelper = {
|
|
304
|
+
makeType,
|
|
305
|
+
makeUrl,
|
|
306
|
+
makeMethod,
|
|
307
|
+
makeRequestData,
|
|
308
|
+
makeResponseData,
|
|
309
|
+
makeStatus,
|
|
310
|
+
logRequest,
|
|
311
|
+
logResponse,
|
|
312
|
+
logRequestError
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
// src/data-source.ts
|
|
316
|
+
var RequestDataSource = class {
|
|
317
|
+
constructor(baseConfig) {
|
|
318
|
+
this.baseConfig = baseConfig;
|
|
319
|
+
}
|
|
320
|
+
common(requestConfig) {
|
|
321
|
+
const requestBuilder = new RequestBuilder({
|
|
322
|
+
baseConfig: this.baseConfig,
|
|
323
|
+
requestConfig
|
|
324
|
+
});
|
|
325
|
+
const request = requestBuilder.makeContentType().makeAuth().makeUrl().makeMethod().makeParams().makeData().makeSerializer().build();
|
|
326
|
+
return import_axios.default.request(request).then((response) => {
|
|
327
|
+
if (this.baseConfig.logger) {
|
|
328
|
+
loggerHelper.logResponse(response);
|
|
329
|
+
}
|
|
330
|
+
return response.data;
|
|
331
|
+
}).catch((error) => {
|
|
332
|
+
var _a;
|
|
333
|
+
if (this.baseConfig.debug) {
|
|
334
|
+
console.log("Error:", error);
|
|
335
|
+
}
|
|
336
|
+
if (this.baseConfig.logger) {
|
|
337
|
+
loggerHelper.logRequestError(error);
|
|
338
|
+
}
|
|
339
|
+
throw ((_a = error.response) == null ? void 0 : _a.data) || error.response || new Error(error.message);
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
search(config = {}) {
|
|
343
|
+
return this.common(__spreadProps(__spreadValues({}, config), {
|
|
344
|
+
method: "get" /* GET */
|
|
345
|
+
}));
|
|
346
|
+
}
|
|
347
|
+
get(id, config = {}) {
|
|
348
|
+
return this.common(__spreadProps(__spreadValues({}, config), {
|
|
349
|
+
method: "get" /* GET */,
|
|
350
|
+
url: id
|
|
351
|
+
}));
|
|
352
|
+
}
|
|
353
|
+
create(config) {
|
|
354
|
+
return this.common(__spreadProps(__spreadValues({}, config), {
|
|
355
|
+
method: "post" /* POST */
|
|
356
|
+
}));
|
|
357
|
+
}
|
|
358
|
+
bulkCreate(config) {
|
|
359
|
+
return this.common(__spreadProps(__spreadValues({}, config), {
|
|
360
|
+
method: "post" /* POST */,
|
|
361
|
+
url: "/bulk",
|
|
362
|
+
data: {
|
|
363
|
+
bulk: config.data
|
|
364
|
+
}
|
|
365
|
+
}));
|
|
366
|
+
}
|
|
367
|
+
update(config) {
|
|
368
|
+
return this.common(__spreadProps(__spreadValues({}, config), {
|
|
369
|
+
method: "put" /* PUT */
|
|
370
|
+
}));
|
|
371
|
+
}
|
|
372
|
+
bulkUpdate(config) {
|
|
373
|
+
return this.common(__spreadProps(__spreadValues({}, config), {
|
|
374
|
+
method: "put" /* PUT */,
|
|
375
|
+
url: "/bulk",
|
|
376
|
+
data: {
|
|
377
|
+
bulk: config.data
|
|
378
|
+
}
|
|
379
|
+
}));
|
|
380
|
+
}
|
|
381
|
+
remove(id, config = {}) {
|
|
382
|
+
return this.common(__spreadProps(__spreadValues({}, config), {
|
|
383
|
+
method: "delete" /* DELETE */,
|
|
384
|
+
url: id
|
|
385
|
+
}));
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
// src/helper.ts
|
|
390
|
+
var RequestHelper = class {
|
|
391
|
+
sleep(seconds) {
|
|
392
|
+
return new Promise((resolve) => {
|
|
393
|
+
setTimeout(resolve, seconds * 1e3);
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
398
|
+
0 && (module.exports = {
|
|
399
|
+
HttpMethods,
|
|
400
|
+
HttpStatuses,
|
|
401
|
+
RequestBuilder,
|
|
402
|
+
RequestDataSource,
|
|
403
|
+
RequestHelper
|
|
404
|
+
});
|
|
405
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/builder.ts","../src/types.ts","../src/data-source.ts","../src/logger/logger.ts","../src/helper.ts"],"sourcesContent":["export * from './builder';\nexport * from './data-source';\nexport * from './helper';\nexport * from './types';\n","import { AxiosRequestConfig } from 'axios';\nimport { stringify } from 'qs';\n\nimport { BaseRequestConfig, HttpMethods, RequestConfig } from './types';\n\nexport class RequestBuilder {\n baseConfig: BaseRequestConfig;\n requestConfig: RequestConfig;\n config: AxiosRequestConfig;\n\n constructor(params: {\n baseConfig: BaseRequestConfig;\n requestConfig: RequestConfig;\n }) {\n this.baseConfig = params.baseConfig;\n this.requestConfig = params.requestConfig;\n this.config = {\n headers: {\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n ...params.baseConfig.headers,\n ...params.requestConfig.headers,\n },\n };\n }\n\n makeContentType() {\n if (this.requestConfig.multipart) {\n this.config = {\n ...this.config,\n headers: {\n ...this.config.headers,\n 'Content-Type': 'multipart/form-data',\n },\n };\n\n return this;\n }\n\n if (this.requestConfig.urlencoded) {\n this.config = {\n ...this.config,\n headers: {\n ...this.config.headers,\n 'Content-Type': 'application/x-www-form-urlencoded',\n },\n };\n\n return this;\n }\n\n if (this.requestConfig.xml) {\n this.config = {\n ...this.config,\n headers: {\n ...this.config.headers,\n 'Content-Type': 'text/xml',\n },\n };\n\n return this;\n }\n\n return this;\n }\n\n makeAuth() {\n if (this.baseConfig.auth) {\n this.config = {\n ...this.config,\n auth: this.baseConfig.auth,\n };\n\n return this;\n }\n\n const bearerToken =\n this.baseConfig.bearerToken || this.requestConfig.bearerToken;\n\n if (bearerToken) {\n this.config = {\n ...this.config,\n headers: {\n ...this.config.headers,\n Authorization: `Bearer ${bearerToken}`,\n },\n };\n\n return this;\n }\n\n return this;\n }\n\n makeUrl() {\n const urlParts = [\n this.baseConfig.baseUrlMap && this.requestConfig.baseUrlName\n ? this.baseConfig.baseUrlMap[this.requestConfig.baseUrlName]\n : this.baseConfig.baseUrl,\n this.baseConfig.url,\n ...(this.baseConfig.urlParts || []),\n this.requestConfig.baseUrl,\n this.requestConfig.url,\n ...(this.requestConfig.urlParts || []),\n ].map((urlPart) => urlPart?.toString());\n\n const isSecureProtocol = urlParts.some(\n (urlPart) => urlPart?.includes('https'),\n );\n const protocol = isSecureProtocol ? 'https' : 'http';\n\n const actualUrlParts = urlParts\n .filter((urlPart) => urlPart)\n .map((urlPart) => {\n return urlPart?.replace(/(^(https?:\\/\\/|\\/))/, '');\n });\n\n const baseUrl = `${protocol}://${actualUrlParts[0]}`;\n const url = `/${actualUrlParts.slice(1).join('/')}`;\n\n this.config = {\n ...this.config,\n baseURL: baseUrl,\n url,\n };\n\n return this;\n }\n\n makeMethod() {\n this.config = {\n ...this.config,\n method: this.requestConfig.method,\n };\n\n return this;\n }\n\n makeData() {\n if (this.requestConfig.method === HttpMethods.GET) {\n return this;\n }\n\n if (this.requestConfig.urlencoded) {\n this.config = {\n ...this.config,\n data: stringify(this.requestConfig.data),\n };\n\n return this;\n }\n\n this.config = {\n ...this.config,\n data: this.requestConfig.data,\n };\n\n return this;\n }\n\n makeParams() {\n this.config = {\n ...this.config,\n params: this.requestConfig.params,\n };\n\n return this;\n }\n\n makeSerializer() {\n this.config = {\n ...this.config,\n paramsSerializer: (params: any) => {\n return stringify(params, {\n arrayFormat: this.baseConfig.serializer?.array,\n });\n },\n };\n\n return this;\n }\n\n build() {\n return this.config;\n }\n}\n","import { AxiosRequestConfig } from 'axios';\n\nexport type RequestParams = Pick<AxiosRequestConfig, 'params' | 'data'>;\n\nexport type RequestConfigParams = Pick<AxiosRequestConfig, 'params' | 'data'>;\n\nexport type RequestConfig = Omit<AxiosRequestConfig, 'baseURL' | 'url'> & {\n baseUrl?: string;\n baseUrlName?: string;\n url?: number | string;\n urlParts?: (number | string)[];\n bearerToken?: string;\n urlencoded?: boolean;\n multipart?: boolean;\n xml?: boolean;\n};\n\nexport type BaseRequestConfig = Pick<AxiosRequestConfig, 'auth' | 'headers'> & {\n name?: string;\n baseUrl?: string;\n baseUrlMap?: Record<string, string>;\n url?: number | string;\n urlParts?: (number | string)[];\n bearerToken?: string;\n debug?: boolean;\n logger?: boolean;\n serializer?: {\n array?: 'indices' | 'brackets' | 'repeat' | 'comma';\n };\n};\n\nexport enum HttpMethods {\n GET = 'get',\n POST = 'post',\n PUT = 'put',\n DELETE = 'delete',\n}\n\nexport enum HttpStatuses {\n OK = 200,\n CREATED = 201,\n ACCEPTED = 202,\n NO_CONTENT = 204,\n BAD_REQUEST = 400,\n UNAUTHORIZED = 401,\n FORBIDDEN = 403,\n NOT_FOUND = 404,\n CONFLICT = 409,\n INTERNAL_SERVER_ERROR = 500,\n}\n","import axios, { AxiosError, AxiosResponse } from 'axios';\nimport { RequestBuilder } from './builder';\nimport {\n BaseRequestConfig,\n HttpMethods,\n RequestConfig,\n RequestConfigParams,\n} from './types';\nimport { loggerHelper } from './logger/logger';\n\nexport class RequestDataSource<\n Entity extends Record<string, any> = any,\n SearchParams extends RequestConfigParams = any,\n SearchResponse extends Record<string, any> = any,\n CreateParams extends RequestConfigParams = any,\n UpdateParams extends RequestConfigParams = any,\n> {\n baseConfig: BaseRequestConfig;\n\n constructor(baseConfig: BaseRequestConfig) {\n this.baseConfig = baseConfig;\n }\n\n common<T>(requestConfig: RequestConfig) {\n const requestBuilder = new RequestBuilder({\n baseConfig: this.baseConfig,\n requestConfig,\n });\n\n const request = requestBuilder\n .makeContentType()\n .makeAuth()\n .makeUrl()\n .makeMethod()\n .makeParams()\n .makeData()\n .makeSerializer()\n .build();\n\n return axios\n .request(request)\n .then((response: AxiosResponse<T>) => {\n if (this.baseConfig.logger) {\n loggerHelper.logResponse(response as any);\n }\n\n return response.data;\n })\n .catch((error: AxiosError) => {\n if (this.baseConfig.debug) {\n console.log('Error:', error);\n }\n\n if (this.baseConfig.logger) {\n loggerHelper.logRequestError(error as any);\n }\n\n throw (\n error.response?.data || error.response || new Error(error.message)\n );\n });\n }\n\n search(config: SearchParams = {} as SearchParams) {\n return this.common<SearchResponse>({\n ...config,\n method: HttpMethods.GET,\n });\n }\n\n get(id: number | string, config: SearchParams = {} as SearchParams) {\n return this.common<Entity>({\n ...config,\n method: HttpMethods.GET,\n url: id,\n });\n }\n\n create(config: CreateParams) {\n return this.common<Entity>({\n ...config,\n method: HttpMethods.POST,\n });\n }\n\n bulkCreate(config: CreateParams) {\n return this.common<Entity[]>({\n ...config,\n method: HttpMethods.POST,\n url: '/bulk',\n data: {\n bulk: config.data,\n },\n });\n }\n\n update(config: UpdateParams) {\n return this.common<Entity>({\n ...config,\n method: HttpMethods.PUT,\n });\n }\n\n bulkUpdate(config: UpdateParams) {\n return this.common<Entity[]>({\n ...config,\n method: HttpMethods.PUT,\n url: '/bulk',\n data: {\n bulk: config.data,\n },\n });\n }\n\n remove(id: number | string, config: SearchParams = {} as SearchParams) {\n return this.common<void>({\n ...config,\n method: HttpMethods.DELETE,\n url: id,\n });\n }\n}\n","import { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from 'axios';\nimport { stringify } from 'qs';\n\ntype LoggerDto = {\n request?: InternalAxiosRequestConfig & Request;\n response?: AxiosResponse & Response;\n error?: AxiosError;\n};\n\nconst makeType = (type: string) => {\n return `[${type}]`;\n};\n\nconst makeUrl = (dto: LoggerDto = {}) => {\n const url =\n dto.request?.url ||\n dto.response?.config?.url ||\n dto.error?.response?.config.url;\n\n const params =\n dto.request?.params ||\n dto.response?.config?.params ||\n dto.error?.response?.config.params;\n\n if (!url) {\n return '';\n }\n\n if (params) {\n delete params['0'];\n return [url, stringify(params)].filter((_) => _).join('?');\n } else {\n return url;\n }\n};\n\nconst makeMethod = (dto: LoggerDto = {}) => {\n const method =\n dto.request?.method ||\n dto.response?.config?.method ||\n dto.error?.response?.config.method;\n\n if (!method) {\n return '';\n }\n\n return method.toUpperCase();\n};\n\nconst makeRequestData = (dto: LoggerDto = {}) => {\n const data =\n dto.request?.body ||\n dto.request?.data ||\n dto.response?.config?.data ||\n dto.error?.response?.config.data;\n\n if (!data) {\n return '';\n }\n\n if (typeof data === 'string') {\n return data;\n }\n\n return JSON.stringify(data);\n};\n\nconst makeResponseData = (dto: LoggerDto = {}) => {\n const data = dto.response?.data || dto.error?.response?.data;\n\n if (!data) {\n return '';\n }\n\n if (typeof data === 'string') {\n return data;\n }\n\n return JSON.stringify(data);\n};\n\nconst makeStatus = (dto: LoggerDto = {}) => {\n const status = dto.response?.status || dto.error?.response?.status;\n\n if (!status) {\n return '';\n }\n\n const statusText =\n dto.response?.statusText || dto.error?.response?.statusText;\n\n if (statusText) {\n return `${status} ${statusText}`;\n }\n\n return `${status}`;\n};\n\nconst logRequest = (request: InternalAxiosRequestConfig & Request) => {\n log([\n makeType('Request'),\n makeMethod({ request }),\n makeUrl({ request }),\n makeRequestData({ request }),\n ]);\n};\n\nconst logResponse = (response: AxiosResponse & Response) => {\n log([\n makeType('Response'),\n makeMethod({ response }),\n makeUrl({ response }),\n makeRequestData({ response }),\n makeStatus({ response }),\n makeResponseData({ response }),\n ]);\n};\n\nconst logRequestError = (error: AxiosError) => {\n log([\n makeType('Error'),\n makeMethod({ error }),\n makeUrl({ error }),\n makeRequestData({ error }),\n makeStatus({ error }),\n makeResponseData({ error }),\n ]);\n};\n\nconst log = (messageParts: string[]) => {\n const message = messageParts.filter((_) => _).join(' ');\n\n console.log(message);\n};\n\nexport const loggerHelper = {\n makeType,\n makeUrl,\n makeMethod,\n makeRequestData,\n makeResponseData,\n makeStatus,\n logRequest,\n logResponse,\n logRequestError,\n};\n","export class RequestHelper {\n sleep(seconds: number) {\n return new Promise((resolve) => {\n setTimeout(resolve, seconds * 1000);\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,gBAA0B;;;AC8BnB,IAAK,cAAL,kBAAKA,iBAAL;AACL,EAAAA,aAAA,SAAM;AACN,EAAAA,aAAA,UAAO;AACP,EAAAA,aAAA,SAAM;AACN,EAAAA,aAAA,YAAS;AAJC,SAAAA;AAAA,GAAA;AAOL,IAAK,eAAL,kBAAKC,kBAAL;AACL,EAAAA,4BAAA,QAAK,OAAL;AACA,EAAAA,4BAAA,aAAU,OAAV;AACA,EAAAA,4BAAA,cAAW,OAAX;AACA,EAAAA,4BAAA,gBAAa,OAAb;AACA,EAAAA,4BAAA,iBAAc,OAAd;AACA,EAAAA,4BAAA,kBAAe,OAAf;AACA,EAAAA,4BAAA,eAAY,OAAZ;AACA,EAAAA,4BAAA,eAAY,OAAZ;AACA,EAAAA,4BAAA,cAAW,OAAX;AACA,EAAAA,4BAAA,2BAAwB,OAAxB;AAVU,SAAAA;AAAA,GAAA;;;ADjCL,IAAM,iBAAN,MAAqB;AAAA,EAK1B,YAAY,QAGT;AACD,SAAK,aAAa,OAAO;AACzB,SAAK,gBAAgB,OAAO;AAC5B,SAAK,SAAS;AAAA,MACZ,SAAS;AAAA,QACP,QAAQ;AAAA,QACR,gBAAgB;AAAA,SACb,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I;AAAA,IACpC,CAAC;AAAA,EACH;AACF;","names":["HttpMethods","HttpStatuses","import_qs","axios"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
|
|
21
|
+
// src/builder.ts
|
|
22
|
+
import { stringify } from "qs";
|
|
23
|
+
|
|
24
|
+
// src/types.ts
|
|
25
|
+
var HttpMethods = /* @__PURE__ */ ((HttpMethods2) => {
|
|
26
|
+
HttpMethods2["GET"] = "get";
|
|
27
|
+
HttpMethods2["POST"] = "post";
|
|
28
|
+
HttpMethods2["PUT"] = "put";
|
|
29
|
+
HttpMethods2["DELETE"] = "delete";
|
|
30
|
+
return HttpMethods2;
|
|
31
|
+
})(HttpMethods || {});
|
|
32
|
+
var HttpStatuses = /* @__PURE__ */ ((HttpStatuses2) => {
|
|
33
|
+
HttpStatuses2[HttpStatuses2["OK"] = 200] = "OK";
|
|
34
|
+
HttpStatuses2[HttpStatuses2["CREATED"] = 201] = "CREATED";
|
|
35
|
+
HttpStatuses2[HttpStatuses2["ACCEPTED"] = 202] = "ACCEPTED";
|
|
36
|
+
HttpStatuses2[HttpStatuses2["NO_CONTENT"] = 204] = "NO_CONTENT";
|
|
37
|
+
HttpStatuses2[HttpStatuses2["BAD_REQUEST"] = 400] = "BAD_REQUEST";
|
|
38
|
+
HttpStatuses2[HttpStatuses2["UNAUTHORIZED"] = 401] = "UNAUTHORIZED";
|
|
39
|
+
HttpStatuses2[HttpStatuses2["FORBIDDEN"] = 403] = "FORBIDDEN";
|
|
40
|
+
HttpStatuses2[HttpStatuses2["NOT_FOUND"] = 404] = "NOT_FOUND";
|
|
41
|
+
HttpStatuses2[HttpStatuses2["CONFLICT"] = 409] = "CONFLICT";
|
|
42
|
+
HttpStatuses2[HttpStatuses2["INTERNAL_SERVER_ERROR"] = 500] = "INTERNAL_SERVER_ERROR";
|
|
43
|
+
return HttpStatuses2;
|
|
44
|
+
})(HttpStatuses || {});
|
|
45
|
+
|
|
46
|
+
// src/builder.ts
|
|
47
|
+
var RequestBuilder = class {
|
|
48
|
+
constructor(params) {
|
|
49
|
+
this.baseConfig = params.baseConfig;
|
|
50
|
+
this.requestConfig = params.requestConfig;
|
|
51
|
+
this.config = {
|
|
52
|
+
headers: __spreadValues(__spreadValues({
|
|
53
|
+
Accept: "application/json",
|
|
54
|
+
"Content-Type": "application/json"
|
|
55
|
+
}, params.baseConfig.headers), params.requestConfig.headers)
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
makeContentType() {
|
|
59
|
+
if (this.requestConfig.multipart) {
|
|
60
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
61
|
+
headers: __spreadProps(__spreadValues({}, this.config.headers), {
|
|
62
|
+
"Content-Type": "multipart/form-data"
|
|
63
|
+
})
|
|
64
|
+
});
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
if (this.requestConfig.urlencoded) {
|
|
68
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
69
|
+
headers: __spreadProps(__spreadValues({}, this.config.headers), {
|
|
70
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
71
|
+
})
|
|
72
|
+
});
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
if (this.requestConfig.xml) {
|
|
76
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
77
|
+
headers: __spreadProps(__spreadValues({}, this.config.headers), {
|
|
78
|
+
"Content-Type": "text/xml"
|
|
79
|
+
})
|
|
80
|
+
});
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
makeAuth() {
|
|
86
|
+
if (this.baseConfig.auth) {
|
|
87
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
88
|
+
auth: this.baseConfig.auth
|
|
89
|
+
});
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
const bearerToken = this.baseConfig.bearerToken || this.requestConfig.bearerToken;
|
|
93
|
+
if (bearerToken) {
|
|
94
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
95
|
+
headers: __spreadProps(__spreadValues({}, this.config.headers), {
|
|
96
|
+
Authorization: `Bearer ${bearerToken}`
|
|
97
|
+
})
|
|
98
|
+
});
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
makeUrl() {
|
|
104
|
+
const urlParts = [
|
|
105
|
+
this.baseConfig.baseUrlMap && this.requestConfig.baseUrlName ? this.baseConfig.baseUrlMap[this.requestConfig.baseUrlName] : this.baseConfig.baseUrl,
|
|
106
|
+
this.baseConfig.url,
|
|
107
|
+
...this.baseConfig.urlParts || [],
|
|
108
|
+
this.requestConfig.baseUrl,
|
|
109
|
+
this.requestConfig.url,
|
|
110
|
+
...this.requestConfig.urlParts || []
|
|
111
|
+
].map((urlPart) => urlPart == null ? void 0 : urlPart.toString());
|
|
112
|
+
const isSecureProtocol = urlParts.some(
|
|
113
|
+
(urlPart) => urlPart == null ? void 0 : urlPart.includes("https")
|
|
114
|
+
);
|
|
115
|
+
const protocol = isSecureProtocol ? "https" : "http";
|
|
116
|
+
const actualUrlParts = urlParts.filter((urlPart) => urlPart).map((urlPart) => {
|
|
117
|
+
return urlPart == null ? void 0 : urlPart.replace(/(^(https?:\/\/|\/))/, "");
|
|
118
|
+
});
|
|
119
|
+
const baseUrl = `${protocol}://${actualUrlParts[0]}`;
|
|
120
|
+
const url = `/${actualUrlParts.slice(1).join("/")}`;
|
|
121
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
122
|
+
baseURL: baseUrl,
|
|
123
|
+
url
|
|
124
|
+
});
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
makeMethod() {
|
|
128
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
129
|
+
method: this.requestConfig.method
|
|
130
|
+
});
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
makeData() {
|
|
134
|
+
if (this.requestConfig.method === "get" /* GET */) {
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
if (this.requestConfig.urlencoded) {
|
|
138
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
139
|
+
data: stringify(this.requestConfig.data)
|
|
140
|
+
});
|
|
141
|
+
return this;
|
|
142
|
+
}
|
|
143
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
144
|
+
data: this.requestConfig.data
|
|
145
|
+
});
|
|
146
|
+
return this;
|
|
147
|
+
}
|
|
148
|
+
makeParams() {
|
|
149
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
150
|
+
params: this.requestConfig.params
|
|
151
|
+
});
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
makeSerializer() {
|
|
155
|
+
this.config = __spreadProps(__spreadValues({}, this.config), {
|
|
156
|
+
paramsSerializer: (params) => {
|
|
157
|
+
var _a;
|
|
158
|
+
return stringify(params, {
|
|
159
|
+
arrayFormat: (_a = this.baseConfig.serializer) == null ? void 0 : _a.array
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
return this;
|
|
164
|
+
}
|
|
165
|
+
build() {
|
|
166
|
+
return this.config;
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
// src/data-source.ts
|
|
171
|
+
import axios from "axios";
|
|
172
|
+
|
|
173
|
+
// src/logger/logger.ts
|
|
174
|
+
import { stringify as stringify2 } from "qs";
|
|
175
|
+
var makeType = (type) => {
|
|
176
|
+
return `[${type}]`;
|
|
177
|
+
};
|
|
178
|
+
var makeUrl = (dto = {}) => {
|
|
179
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
180
|
+
const url = ((_a = dto.request) == null ? void 0 : _a.url) || ((_c = (_b = dto.response) == null ? void 0 : _b.config) == null ? void 0 : _c.url) || ((_e = (_d = dto.error) == null ? void 0 : _d.response) == null ? void 0 : _e.config.url);
|
|
181
|
+
const params = ((_f = dto.request) == null ? void 0 : _f.params) || ((_h = (_g = dto.response) == null ? void 0 : _g.config) == null ? void 0 : _h.params) || ((_j = (_i = dto.error) == null ? void 0 : _i.response) == null ? void 0 : _j.config.params);
|
|
182
|
+
if (!url) {
|
|
183
|
+
return "";
|
|
184
|
+
}
|
|
185
|
+
if (params) {
|
|
186
|
+
delete params["0"];
|
|
187
|
+
return [url, stringify2(params)].filter((_) => _).join("?");
|
|
188
|
+
} else {
|
|
189
|
+
return url;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
var makeMethod = (dto = {}) => {
|
|
193
|
+
var _a, _b, _c, _d, _e;
|
|
194
|
+
const method = ((_a = dto.request) == null ? void 0 : _a.method) || ((_c = (_b = dto.response) == null ? void 0 : _b.config) == null ? void 0 : _c.method) || ((_e = (_d = dto.error) == null ? void 0 : _d.response) == null ? void 0 : _e.config.method);
|
|
195
|
+
if (!method) {
|
|
196
|
+
return "";
|
|
197
|
+
}
|
|
198
|
+
return method.toUpperCase();
|
|
199
|
+
};
|
|
200
|
+
var makeRequestData = (dto = {}) => {
|
|
201
|
+
var _a, _b, _c, _d, _e, _f;
|
|
202
|
+
const data = ((_a = dto.request) == null ? void 0 : _a.body) || ((_b = dto.request) == null ? void 0 : _b.data) || ((_d = (_c = dto.response) == null ? void 0 : _c.config) == null ? void 0 : _d.data) || ((_f = (_e = dto.error) == null ? void 0 : _e.response) == null ? void 0 : _f.config.data);
|
|
203
|
+
if (!data) {
|
|
204
|
+
return "";
|
|
205
|
+
}
|
|
206
|
+
if (typeof data === "string") {
|
|
207
|
+
return data;
|
|
208
|
+
}
|
|
209
|
+
return JSON.stringify(data);
|
|
210
|
+
};
|
|
211
|
+
var makeResponseData = (dto = {}) => {
|
|
212
|
+
var _a, _b, _c;
|
|
213
|
+
const data = ((_a = dto.response) == null ? void 0 : _a.data) || ((_c = (_b = dto.error) == null ? void 0 : _b.response) == null ? void 0 : _c.data);
|
|
214
|
+
if (!data) {
|
|
215
|
+
return "";
|
|
216
|
+
}
|
|
217
|
+
if (typeof data === "string") {
|
|
218
|
+
return data;
|
|
219
|
+
}
|
|
220
|
+
return JSON.stringify(data);
|
|
221
|
+
};
|
|
222
|
+
var makeStatus = (dto = {}) => {
|
|
223
|
+
var _a, _b, _c, _d, _e, _f;
|
|
224
|
+
const status = ((_a = dto.response) == null ? void 0 : _a.status) || ((_c = (_b = dto.error) == null ? void 0 : _b.response) == null ? void 0 : _c.status);
|
|
225
|
+
if (!status) {
|
|
226
|
+
return "";
|
|
227
|
+
}
|
|
228
|
+
const statusText = ((_d = dto.response) == null ? void 0 : _d.statusText) || ((_f = (_e = dto.error) == null ? void 0 : _e.response) == null ? void 0 : _f.statusText);
|
|
229
|
+
if (statusText) {
|
|
230
|
+
return `${status} ${statusText}`;
|
|
231
|
+
}
|
|
232
|
+
return `${status}`;
|
|
233
|
+
};
|
|
234
|
+
var logRequest = (request) => {
|
|
235
|
+
log([
|
|
236
|
+
makeType("Request"),
|
|
237
|
+
makeMethod({ request }),
|
|
238
|
+
makeUrl({ request }),
|
|
239
|
+
makeRequestData({ request })
|
|
240
|
+
]);
|
|
241
|
+
};
|
|
242
|
+
var logResponse = (response) => {
|
|
243
|
+
log([
|
|
244
|
+
makeType("Response"),
|
|
245
|
+
makeMethod({ response }),
|
|
246
|
+
makeUrl({ response }),
|
|
247
|
+
makeRequestData({ response }),
|
|
248
|
+
makeStatus({ response }),
|
|
249
|
+
makeResponseData({ response })
|
|
250
|
+
]);
|
|
251
|
+
};
|
|
252
|
+
var logRequestError = (error) => {
|
|
253
|
+
log([
|
|
254
|
+
makeType("Error"),
|
|
255
|
+
makeMethod({ error }),
|
|
256
|
+
makeUrl({ error }),
|
|
257
|
+
makeRequestData({ error }),
|
|
258
|
+
makeStatus({ error }),
|
|
259
|
+
makeResponseData({ error })
|
|
260
|
+
]);
|
|
261
|
+
};
|
|
262
|
+
var log = (messageParts) => {
|
|
263
|
+
const message = messageParts.filter((_) => _).join(" ");
|
|
264
|
+
console.log(message);
|
|
265
|
+
};
|
|
266
|
+
var loggerHelper = {
|
|
267
|
+
makeType,
|
|
268
|
+
makeUrl,
|
|
269
|
+
makeMethod,
|
|
270
|
+
makeRequestData,
|
|
271
|
+
makeResponseData,
|
|
272
|
+
makeStatus,
|
|
273
|
+
logRequest,
|
|
274
|
+
logResponse,
|
|
275
|
+
logRequestError
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
// src/data-source.ts
|
|
279
|
+
var RequestDataSource = class {
|
|
280
|
+
constructor(baseConfig) {
|
|
281
|
+
this.baseConfig = baseConfig;
|
|
282
|
+
}
|
|
283
|
+
common(requestConfig) {
|
|
284
|
+
const requestBuilder = new RequestBuilder({
|
|
285
|
+
baseConfig: this.baseConfig,
|
|
286
|
+
requestConfig
|
|
287
|
+
});
|
|
288
|
+
const request = requestBuilder.makeContentType().makeAuth().makeUrl().makeMethod().makeParams().makeData().makeSerializer().build();
|
|
289
|
+
return axios.request(request).then((response) => {
|
|
290
|
+
if (this.baseConfig.logger) {
|
|
291
|
+
loggerHelper.logResponse(response);
|
|
292
|
+
}
|
|
293
|
+
return response.data;
|
|
294
|
+
}).catch((error) => {
|
|
295
|
+
var _a;
|
|
296
|
+
if (this.baseConfig.debug) {
|
|
297
|
+
console.log("Error:", error);
|
|
298
|
+
}
|
|
299
|
+
if (this.baseConfig.logger) {
|
|
300
|
+
loggerHelper.logRequestError(error);
|
|
301
|
+
}
|
|
302
|
+
throw ((_a = error.response) == null ? void 0 : _a.data) || error.response || new Error(error.message);
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
search(config = {}) {
|
|
306
|
+
return this.common(__spreadProps(__spreadValues({}, config), {
|
|
307
|
+
method: "get" /* GET */
|
|
308
|
+
}));
|
|
309
|
+
}
|
|
310
|
+
get(id, config = {}) {
|
|
311
|
+
return this.common(__spreadProps(__spreadValues({}, config), {
|
|
312
|
+
method: "get" /* GET */,
|
|
313
|
+
url: id
|
|
314
|
+
}));
|
|
315
|
+
}
|
|
316
|
+
create(config) {
|
|
317
|
+
return this.common(__spreadProps(__spreadValues({}, config), {
|
|
318
|
+
method: "post" /* POST */
|
|
319
|
+
}));
|
|
320
|
+
}
|
|
321
|
+
bulkCreate(config) {
|
|
322
|
+
return this.common(__spreadProps(__spreadValues({}, config), {
|
|
323
|
+
method: "post" /* POST */,
|
|
324
|
+
url: "/bulk",
|
|
325
|
+
data: {
|
|
326
|
+
bulk: config.data
|
|
327
|
+
}
|
|
328
|
+
}));
|
|
329
|
+
}
|
|
330
|
+
update(config) {
|
|
331
|
+
return this.common(__spreadProps(__spreadValues({}, config), {
|
|
332
|
+
method: "put" /* PUT */
|
|
333
|
+
}));
|
|
334
|
+
}
|
|
335
|
+
bulkUpdate(config) {
|
|
336
|
+
return this.common(__spreadProps(__spreadValues({}, config), {
|
|
337
|
+
method: "put" /* PUT */,
|
|
338
|
+
url: "/bulk",
|
|
339
|
+
data: {
|
|
340
|
+
bulk: config.data
|
|
341
|
+
}
|
|
342
|
+
}));
|
|
343
|
+
}
|
|
344
|
+
remove(id, config = {}) {
|
|
345
|
+
return this.common(__spreadProps(__spreadValues({}, config), {
|
|
346
|
+
method: "delete" /* DELETE */,
|
|
347
|
+
url: id
|
|
348
|
+
}));
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
// src/helper.ts
|
|
353
|
+
var RequestHelper = class {
|
|
354
|
+
sleep(seconds) {
|
|
355
|
+
return new Promise((resolve) => {
|
|
356
|
+
setTimeout(resolve, seconds * 1e3);
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
export {
|
|
361
|
+
HttpMethods,
|
|
362
|
+
HttpStatuses,
|
|
363
|
+
RequestBuilder,
|
|
364
|
+
RequestDataSource,
|
|
365
|
+
RequestHelper
|
|
366
|
+
};
|
|
367
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/builder.ts","../src/types.ts","../src/data-source.ts","../src/logger/logger.ts","../src/helper.ts"],"sourcesContent":["import { AxiosRequestConfig } from 'axios';\nimport { stringify } from 'qs';\n\nimport { BaseRequestConfig, HttpMethods, RequestConfig } from './types';\n\nexport class RequestBuilder {\n baseConfig: BaseRequestConfig;\n requestConfig: RequestConfig;\n config: AxiosRequestConfig;\n\n constructor(params: {\n baseConfig: BaseRequestConfig;\n requestConfig: RequestConfig;\n }) {\n this.baseConfig = params.baseConfig;\n this.requestConfig = params.requestConfig;\n this.config = {\n headers: {\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n ...params.baseConfig.headers,\n ...params.requestConfig.headers,\n },\n };\n }\n\n makeContentType() {\n if (this.requestConfig.multipart) {\n this.config = {\n ...this.config,\n headers: {\n ...this.config.headers,\n 'Content-Type': 'multipart/form-data',\n },\n };\n\n return this;\n }\n\n if (this.requestConfig.urlencoded) {\n this.config = {\n ...this.config,\n headers: {\n ...this.config.headers,\n 'Content-Type': 'application/x-www-form-urlencoded',\n },\n };\n\n return this;\n }\n\n if (this.requestConfig.xml) {\n this.config = {\n ...this.config,\n headers: {\n ...this.config.headers,\n 'Content-Type': 'text/xml',\n },\n };\n\n return this;\n }\n\n return this;\n }\n\n makeAuth() {\n if (this.baseConfig.auth) {\n this.config = {\n ...this.config,\n auth: this.baseConfig.auth,\n };\n\n return this;\n }\n\n const bearerToken =\n this.baseConfig.bearerToken || this.requestConfig.bearerToken;\n\n if (bearerToken) {\n this.config = {\n ...this.config,\n headers: {\n ...this.config.headers,\n Authorization: `Bearer ${bearerToken}`,\n },\n };\n\n return this;\n }\n\n return this;\n }\n\n makeUrl() {\n const urlParts = [\n this.baseConfig.baseUrlMap && this.requestConfig.baseUrlName\n ? this.baseConfig.baseUrlMap[this.requestConfig.baseUrlName]\n : this.baseConfig.baseUrl,\n this.baseConfig.url,\n ...(this.baseConfig.urlParts || []),\n this.requestConfig.baseUrl,\n this.requestConfig.url,\n ...(this.requestConfig.urlParts || []),\n ].map((urlPart) => urlPart?.toString());\n\n const isSecureProtocol = urlParts.some(\n (urlPart) => urlPart?.includes('https'),\n );\n const protocol = isSecureProtocol ? 'https' : 'http';\n\n const actualUrlParts = urlParts\n .filter((urlPart) => urlPart)\n .map((urlPart) => {\n return urlPart?.replace(/(^(https?:\\/\\/|\\/))/, '');\n });\n\n const baseUrl = `${protocol}://${actualUrlParts[0]}`;\n const url = `/${actualUrlParts.slice(1).join('/')}`;\n\n this.config = {\n ...this.config,\n baseURL: baseUrl,\n url,\n };\n\n return this;\n }\n\n makeMethod() {\n this.config = {\n ...this.config,\n method: this.requestConfig.method,\n };\n\n return this;\n }\n\n makeData() {\n if (this.requestConfig.method === HttpMethods.GET) {\n return this;\n }\n\n if (this.requestConfig.urlencoded) {\n this.config = {\n ...this.config,\n data: stringify(this.requestConfig.data),\n };\n\n return this;\n }\n\n this.config = {\n ...this.config,\n data: this.requestConfig.data,\n };\n\n return this;\n }\n\n makeParams() {\n this.config = {\n ...this.config,\n params: this.requestConfig.params,\n };\n\n return this;\n }\n\n makeSerializer() {\n this.config = {\n ...this.config,\n paramsSerializer: (params: any) => {\n return stringify(params, {\n arrayFormat: this.baseConfig.serializer?.array,\n });\n },\n };\n\n return this;\n }\n\n build() {\n return this.config;\n }\n}\n","import { AxiosRequestConfig } from 'axios';\n\nexport type RequestParams = Pick<AxiosRequestConfig, 'params' | 'data'>;\n\nexport type RequestConfigParams = Pick<AxiosRequestConfig, 'params' | 'data'>;\n\nexport type RequestConfig = Omit<AxiosRequestConfig, 'baseURL' | 'url'> & {\n baseUrl?: string;\n baseUrlName?: string;\n url?: number | string;\n urlParts?: (number | string)[];\n bearerToken?: string;\n urlencoded?: boolean;\n multipart?: boolean;\n xml?: boolean;\n};\n\nexport type BaseRequestConfig = Pick<AxiosRequestConfig, 'auth' | 'headers'> & {\n name?: string;\n baseUrl?: string;\n baseUrlMap?: Record<string, string>;\n url?: number | string;\n urlParts?: (number | string)[];\n bearerToken?: string;\n debug?: boolean;\n logger?: boolean;\n serializer?: {\n array?: 'indices' | 'brackets' | 'repeat' | 'comma';\n };\n};\n\nexport enum HttpMethods {\n GET = 'get',\n POST = 'post',\n PUT = 'put',\n DELETE = 'delete',\n}\n\nexport enum HttpStatuses {\n OK = 200,\n CREATED = 201,\n ACCEPTED = 202,\n NO_CONTENT = 204,\n BAD_REQUEST = 400,\n UNAUTHORIZED = 401,\n FORBIDDEN = 403,\n NOT_FOUND = 404,\n CONFLICT = 409,\n INTERNAL_SERVER_ERROR = 500,\n}\n","import axios, { AxiosError, AxiosResponse } from 'axios';\nimport { RequestBuilder } from './builder';\nimport {\n BaseRequestConfig,\n HttpMethods,\n RequestConfig,\n RequestConfigParams,\n} from './types';\nimport { loggerHelper } from './logger/logger';\n\nexport class RequestDataSource<\n Entity extends Record<string, any> = any,\n SearchParams extends RequestConfigParams = any,\n SearchResponse extends Record<string, any> = any,\n CreateParams extends RequestConfigParams = any,\n UpdateParams extends RequestConfigParams = any,\n> {\n baseConfig: BaseRequestConfig;\n\n constructor(baseConfig: BaseRequestConfig) {\n this.baseConfig = baseConfig;\n }\n\n common<T>(requestConfig: RequestConfig) {\n const requestBuilder = new RequestBuilder({\n baseConfig: this.baseConfig,\n requestConfig,\n });\n\n const request = requestBuilder\n .makeContentType()\n .makeAuth()\n .makeUrl()\n .makeMethod()\n .makeParams()\n .makeData()\n .makeSerializer()\n .build();\n\n return axios\n .request(request)\n .then((response: AxiosResponse<T>) => {\n if (this.baseConfig.logger) {\n loggerHelper.logResponse(response as any);\n }\n\n return response.data;\n })\n .catch((error: AxiosError) => {\n if (this.baseConfig.debug) {\n console.log('Error:', error);\n }\n\n if (this.baseConfig.logger) {\n loggerHelper.logRequestError(error as any);\n }\n\n throw (\n error.response?.data || error.response || new Error(error.message)\n );\n });\n }\n\n search(config: SearchParams = {} as SearchParams) {\n return this.common<SearchResponse>({\n ...config,\n method: HttpMethods.GET,\n });\n }\n\n get(id: number | string, config: SearchParams = {} as SearchParams) {\n return this.common<Entity>({\n ...config,\n method: HttpMethods.GET,\n url: id,\n });\n }\n\n create(config: CreateParams) {\n return this.common<Entity>({\n ...config,\n method: HttpMethods.POST,\n });\n }\n\n bulkCreate(config: CreateParams) {\n return this.common<Entity[]>({\n ...config,\n method: HttpMethods.POST,\n url: '/bulk',\n data: {\n bulk: config.data,\n },\n });\n }\n\n update(config: UpdateParams) {\n return this.common<Entity>({\n ...config,\n method: HttpMethods.PUT,\n });\n }\n\n bulkUpdate(config: UpdateParams) {\n return this.common<Entity[]>({\n ...config,\n method: HttpMethods.PUT,\n url: '/bulk',\n data: {\n bulk: config.data,\n },\n });\n }\n\n remove(id: number | string, config: SearchParams = {} as SearchParams) {\n return this.common<void>({\n ...config,\n method: HttpMethods.DELETE,\n url: id,\n });\n }\n}\n","import { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from 'axios';\nimport { stringify } from 'qs';\n\ntype LoggerDto = {\n request?: InternalAxiosRequestConfig & Request;\n response?: AxiosResponse & Response;\n error?: AxiosError;\n};\n\nconst makeType = (type: string) => {\n return `[${type}]`;\n};\n\nconst makeUrl = (dto: LoggerDto = {}) => {\n const url =\n dto.request?.url ||\n dto.response?.config?.url ||\n dto.error?.response?.config.url;\n\n const params =\n dto.request?.params ||\n dto.response?.config?.params ||\n dto.error?.response?.config.params;\n\n if (!url) {\n return '';\n }\n\n if (params) {\n delete params['0'];\n return [url, stringify(params)].filter((_) => _).join('?');\n } else {\n return url;\n }\n};\n\nconst makeMethod = (dto: LoggerDto = {}) => {\n const method =\n dto.request?.method ||\n dto.response?.config?.method ||\n dto.error?.response?.config.method;\n\n if (!method) {\n return '';\n }\n\n return method.toUpperCase();\n};\n\nconst makeRequestData = (dto: LoggerDto = {}) => {\n const data =\n dto.request?.body ||\n dto.request?.data ||\n dto.response?.config?.data ||\n dto.error?.response?.config.data;\n\n if (!data) {\n return '';\n }\n\n if (typeof data === 'string') {\n return data;\n }\n\n return JSON.stringify(data);\n};\n\nconst makeResponseData = (dto: LoggerDto = {}) => {\n const data = dto.response?.data || dto.error?.response?.data;\n\n if (!data) {\n return '';\n }\n\n if (typeof data === 'string') {\n return data;\n }\n\n return JSON.stringify(data);\n};\n\nconst makeStatus = (dto: LoggerDto = {}) => {\n const status = dto.response?.status || dto.error?.response?.status;\n\n if (!status) {\n return '';\n }\n\n const statusText =\n dto.response?.statusText || dto.error?.response?.statusText;\n\n if (statusText) {\n return `${status} ${statusText}`;\n }\n\n return `${status}`;\n};\n\nconst logRequest = (request: InternalAxiosRequestConfig & Request) => {\n log([\n makeType('Request'),\n makeMethod({ request }),\n makeUrl({ request }),\n makeRequestData({ request }),\n ]);\n};\n\nconst logResponse = (response: AxiosResponse & Response) => {\n log([\n makeType('Response'),\n makeMethod({ response }),\n makeUrl({ response }),\n makeRequestData({ response }),\n makeStatus({ response }),\n makeResponseData({ response }),\n ]);\n};\n\nconst logRequestError = (error: AxiosError) => {\n log([\n makeType('Error'),\n makeMethod({ error }),\n makeUrl({ error }),\n makeRequestData({ error }),\n makeStatus({ error }),\n makeResponseData({ error }),\n ]);\n};\n\nconst log = (messageParts: string[]) => {\n const message = messageParts.filter((_) => _).join(' ');\n\n console.log(message);\n};\n\nexport const loggerHelper = {\n makeType,\n makeUrl,\n makeMethod,\n makeRequestData,\n makeResponseData,\n makeStatus,\n logRequest,\n logResponse,\n logRequestError,\n};\n","export class RequestHelper {\n sleep(seconds: number) {\n return new Promise((resolve) => {\n setTimeout(resolve, seconds * 1000);\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AACA,SAAS,iBAAiB;;;AC8BnB,IAAK,cAAL,kBAAKA,iBAAL;AACL,EAAAA,aAAA,SAAM;AACN,EAAAA,aAAA,UAAO;AACP,EAAAA,aAAA,SAAM;AACN,EAAAA,aAAA,YAAS;AAJC,SAAAA;AAAA,GAAA;AAOL,IAAK,eAAL,kBAAKC,kBAAL;AACL,EAAAA,4BAAA,QAAK,OAAL;AACA,EAAAA,4BAAA,aAAU,OAAV;AACA,EAAAA,4BAAA,cAAW,OAAX;AACA,EAAAA,4BAAA,gBAAa,OAAb;AACA,EAAAA,4BAAA,iBAAc,OAAd;AACA,EAAAA,4BAAA,kBAAe,OAAf;AACA,EAAAA,4BAAA,eAAY,OAAZ;AACA,EAAAA,4BAAA,eAAY,OAAZ;AACA,EAAAA,4BAAA,cAAW,OAAX;AACA,EAAAA,4BAAA,2BAAwB,OAAxB;AAVU,SAAAA;AAAA,GAAA;;;ADjCL,IAAM,iBAAN,MAAqB;AAAA,EAK1B,YAAY,QAGT;AACD,SAAK,aAAa,OAAO;AACzB,SAAK,gBAAgB,OAAO;AAC5B,SAAK,SAAS;AAAA,MACZ,SAAS;AAAA,QACP,QAAQ;AAAA,QACR,gBAAgB;AAAA,SACb,OAAO,WAAW,UAClB,OAAO,cAAc;AAAA,IAE5B;AAAA,E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HttpStatuses","stringify"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@iamnnort/request",
|
|
3
|
+
"version": "1.8.0",
|
|
4
|
+
"description": "Request handler for Node.js - Fast - Interactive - Simple",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"request",
|
|
7
|
+
"fetch",
|
|
8
|
+
"xhr",
|
|
9
|
+
"http",
|
|
10
|
+
"ajax",
|
|
11
|
+
"promise"
|
|
12
|
+
],
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"module": "./dist/index.mjs",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"start": "yarn ts-node ./examples/simple",
|
|
21
|
+
"build": "tsup",
|
|
22
|
+
"lint": "eslint ./{src,examples}/**/*.ts --fix",
|
|
23
|
+
"unit": "jest",
|
|
24
|
+
"test": "yarn lint && yarn unit"
|
|
25
|
+
},
|
|
26
|
+
"pre-commit": [
|
|
27
|
+
"lint"
|
|
28
|
+
],
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/iamnnort/request.git"
|
|
32
|
+
},
|
|
33
|
+
"author": "Nort Red",
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"bugs": {
|
|
36
|
+
"url": "https://github.com/iamnnort/request/issues"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://github.com/iamnnort/request#readme",
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"axios": "^1.5.0",
|
|
41
|
+
"qs": "^6.11.2"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/jest": "^29.5.5",
|
|
45
|
+
"@types/qs": "^6.9.8",
|
|
46
|
+
"jest": "^29.7.0",
|
|
47
|
+
"path": "^0.12.7",
|
|
48
|
+
"ts-jest": "^29.1.1",
|
|
49
|
+
"ts-node": "^10.9.1",
|
|
50
|
+
"tsup": "^7.2.0",
|
|
51
|
+
"typescript": "^5.2.2",
|
|
52
|
+
"@typescript-eslint/eslint-plugin": "^6.9.0",
|
|
53
|
+
"@typescript-eslint/parser": "^6.9.0",
|
|
54
|
+
"eslint": "^8.52.0",
|
|
55
|
+
"eslint-config-prettier": "^9.0.0",
|
|
56
|
+
"eslint-plugin-prettier": "^5.0.1",
|
|
57
|
+
"pre-commit": "^1.2.2",
|
|
58
|
+
"prettier": "^3.0.3"
|
|
59
|
+
}
|
|
60
|
+
}
|