dff-util 1.2.7 → 1.2.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/README.md +3 -19
- package/dist/main/case-mapper.d.ts +1 -1
- package/dist/main/case-mapper.js +14 -13
- package/dist/main/const-type.d.ts +3 -8
- package/dist/main/http.d.ts +2 -2
- package/dist/main/http.js +18 -18
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -264,17 +264,6 @@ export type SessionUser = {
|
|
|
264
264
|
};
|
|
265
265
|
```
|
|
266
266
|
|
|
267
|
-
## ResponseType
|
|
268
|
-
|
|
269
|
-
```
|
|
270
|
-
ResponseType = {
|
|
271
|
-
status?: string | number;
|
|
272
|
-
data?: any;
|
|
273
|
-
error?: any;
|
|
274
|
-
total?: number;
|
|
275
|
-
};
|
|
276
|
-
|
|
277
|
-
```
|
|
278
267
|
|
|
279
268
|
## SearchByLimitType
|
|
280
269
|
```
|
|
@@ -354,6 +343,9 @@ export type ResponseType = {
|
|
|
354
343
|
status?: string | number;
|
|
355
344
|
data?: any;
|
|
356
345
|
error?: any;
|
|
346
|
+
total?: number;
|
|
347
|
+
skip?: number;
|
|
348
|
+
limit?: number;
|
|
357
349
|
};
|
|
358
350
|
|
|
359
351
|
export type SearchByType = {
|
|
@@ -363,14 +355,6 @@ export type SearchByType = {
|
|
|
363
355
|
searchTerm?: string;
|
|
364
356
|
};
|
|
365
357
|
|
|
366
|
-
export type PaginationResponseType = {
|
|
367
|
-
status?: string | number;
|
|
368
|
-
data?: any;
|
|
369
|
-
error?: any;
|
|
370
|
-
total?: number;
|
|
371
|
-
skip?: number;
|
|
372
|
-
limit?: number;
|
|
373
|
-
};
|
|
374
358
|
|
|
375
359
|
export type SearchByLimitType = {
|
|
376
360
|
limit: number;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare function toCamelCase(str: string): string;
|
|
2
|
-
export declare function toSnakeCase(str: string
|
|
2
|
+
export declare function toSnakeCase(str: string): string;
|
|
3
3
|
export declare function toPascalCase(str: string): string;
|
|
4
4
|
export declare function toUpperCase(str: string): string;
|
|
5
5
|
export declare function toLowerCase(str: string): string;
|
package/dist/main/case-mapper.js
CHANGED
|
@@ -13,9 +13,7 @@ function toCamelCase(str) {
|
|
|
13
13
|
.replace(/[_\s-]+(.)?/g, (_, chr) => (chr ? chr.toUpperCase() : ''))
|
|
14
14
|
.replace(/^[A-Z]/, (chr) => chr.toLowerCase());
|
|
15
15
|
}
|
|
16
|
-
function toSnakeCase(str
|
|
17
|
-
if (str === 'id' && isMongoId)
|
|
18
|
-
return '_id';
|
|
16
|
+
function toSnakeCase(str) {
|
|
19
17
|
return str
|
|
20
18
|
.replace(/([a-z0-9])([A-Z])/g, '$1_$2') // camelCase → camel_Case
|
|
21
19
|
.replace(/[\s-]+/g, '_') // space/dash → underscore
|
|
@@ -44,39 +42,42 @@ function toLowerCase(str) {
|
|
|
44
42
|
.replace(/^_+|_+$/g, '')
|
|
45
43
|
.toLowerCase();
|
|
46
44
|
}
|
|
45
|
+
function isPlainObject(obj) {
|
|
46
|
+
return Object.prototype.toString.call(obj) === '[object Object]';
|
|
47
|
+
}
|
|
47
48
|
function toViewMapper(obj) {
|
|
48
49
|
if (Array.isArray(obj)) {
|
|
49
50
|
return obj.map(toViewMapper);
|
|
50
51
|
}
|
|
51
|
-
else if (obj
|
|
52
|
+
else if (isPlainObject(obj)) {
|
|
52
53
|
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
|
|
53
|
-
toCamelCase(key),
|
|
54
|
+
toCamelCase(key === '_id' ? 'id' : key),
|
|
54
55
|
toViewMapper(value),
|
|
55
56
|
]));
|
|
56
57
|
}
|
|
57
|
-
return obj;
|
|
58
|
+
return obj; // Date, Buffer, Map, etc. returned as-is
|
|
58
59
|
}
|
|
59
60
|
function toEntityMapper(obj) {
|
|
60
61
|
if (Array.isArray(obj)) {
|
|
61
62
|
return obj.map(toEntityMapper);
|
|
62
63
|
}
|
|
63
|
-
else if (obj
|
|
64
|
+
else if (isPlainObject(obj)) {
|
|
64
65
|
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
|
|
65
66
|
toSnakeCase(key),
|
|
66
67
|
toEntityMapper(value),
|
|
67
68
|
]));
|
|
68
69
|
}
|
|
69
|
-
return obj;
|
|
70
|
+
return obj; // Date, Buffer, Map, etc. returned as-is
|
|
70
71
|
}
|
|
71
72
|
function toSchemaMapper(obj) {
|
|
72
73
|
if (Array.isArray(obj)) {
|
|
73
|
-
return obj.map(
|
|
74
|
+
return obj.map(toEntityMapper);
|
|
74
75
|
}
|
|
75
|
-
else if (obj
|
|
76
|
+
else if (isPlainObject(obj)) {
|
|
76
77
|
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
|
|
77
|
-
toSnakeCase(key
|
|
78
|
-
|
|
78
|
+
toSnakeCase(key === 'id' ? '_id' : key),
|
|
79
|
+
toEntityMapper(value),
|
|
79
80
|
]));
|
|
80
81
|
}
|
|
81
|
-
return obj;
|
|
82
|
+
return obj; // Date, Buffer, Map, etc. returned as-is
|
|
82
83
|
}
|
|
@@ -23,6 +23,9 @@ export type ResponseType = {
|
|
|
23
23
|
status?: string | number;
|
|
24
24
|
data?: any;
|
|
25
25
|
error?: any;
|
|
26
|
+
total?: number;
|
|
27
|
+
skip?: number;
|
|
28
|
+
limit?: number;
|
|
26
29
|
};
|
|
27
30
|
export type SearchByType = {
|
|
28
31
|
query?: Record<string, any>;
|
|
@@ -30,14 +33,6 @@ export type SearchByType = {
|
|
|
30
33
|
order?: "ASC" | "DESC";
|
|
31
34
|
searchTerm?: string;
|
|
32
35
|
};
|
|
33
|
-
export type PaginationResponseType = {
|
|
34
|
-
status?: string | number;
|
|
35
|
-
data?: any;
|
|
36
|
-
error?: any;
|
|
37
|
-
total?: number;
|
|
38
|
-
skip?: number;
|
|
39
|
-
limit?: number;
|
|
40
|
-
};
|
|
41
36
|
export type SearchByLimitType = {
|
|
42
37
|
limit: number;
|
|
43
38
|
skip: number;
|
package/dist/main/http.d.ts
CHANGED
|
@@ -8,11 +8,11 @@ export declare enum RespType {
|
|
|
8
8
|
export declare const HttpHeaders: {
|
|
9
9
|
'Content-Type': string;
|
|
10
10
|
authorization: null;
|
|
11
|
-
'
|
|
11
|
+
'request-id': null;
|
|
12
12
|
};
|
|
13
13
|
export declare const BaseHeaders: {
|
|
14
14
|
authorization: null;
|
|
15
|
-
'
|
|
15
|
+
'request-id': null;
|
|
16
16
|
};
|
|
17
17
|
export declare const HttpMethods: {
|
|
18
18
|
GET: string;
|
package/dist/main/http.js
CHANGED
|
@@ -22,11 +22,11 @@ var RespType;
|
|
|
22
22
|
exports.HttpHeaders = {
|
|
23
23
|
'Content-Type': 'application/json',
|
|
24
24
|
authorization: null,
|
|
25
|
-
'
|
|
25
|
+
'request-id': null,
|
|
26
26
|
};
|
|
27
27
|
exports.BaseHeaders = {
|
|
28
28
|
authorization: null,
|
|
29
|
-
'
|
|
29
|
+
'request-id': null,
|
|
30
30
|
};
|
|
31
31
|
exports.HttpMethods = {
|
|
32
32
|
GET: "GET",
|
|
@@ -69,14 +69,14 @@ class Http {
|
|
|
69
69
|
url = url.startsWith('http') ? url : Http.API_BASE_URL + url;
|
|
70
70
|
const fullUrl = params ? `${url}?${new URLSearchParams(params).toString()}` : url;
|
|
71
71
|
headers = headers || exports.HttpHeaders;
|
|
72
|
-
headers['
|
|
72
|
+
headers['request-id'] = headers['request-id'] || (0, util_1.AppUUID4)();
|
|
73
73
|
try {
|
|
74
74
|
const requestOptions = { method: 'GET', headers: headers };
|
|
75
75
|
const response = yield fetch(fullUrl, requestOptions);
|
|
76
76
|
return yield Http.RespData(response, type);
|
|
77
77
|
}
|
|
78
78
|
catch (error) {
|
|
79
|
-
throw { url: fullUrl, requestId: headers['
|
|
79
|
+
throw { url: fullUrl, requestId: headers['request-id'], error: error };
|
|
80
80
|
}
|
|
81
81
|
});
|
|
82
82
|
}
|
|
@@ -84,14 +84,14 @@ class Http {
|
|
|
84
84
|
return __awaiter(this, arguments, void 0, function* (url, data, headers, type = RespType.JSON) {
|
|
85
85
|
url = url.startsWith('http') ? url : Http.API_BASE_URL + url;
|
|
86
86
|
headers = headers || exports.HttpHeaders;
|
|
87
|
-
headers['
|
|
87
|
+
headers['request-id'] = headers['request-id'] || (0, util_1.AppUUID4)();
|
|
88
88
|
try {
|
|
89
89
|
const requestOptions = { method: 'POST', body: JSON.stringify(data), headers: headers };
|
|
90
90
|
const response = yield fetch(url, requestOptions);
|
|
91
91
|
return yield Http.RespData(response, type);
|
|
92
92
|
}
|
|
93
93
|
catch (error) {
|
|
94
|
-
throw { url: url, requestId: headers['
|
|
94
|
+
throw { url: url, requestId: headers['request-id'], body: data, error: error };
|
|
95
95
|
}
|
|
96
96
|
});
|
|
97
97
|
}
|
|
@@ -99,14 +99,14 @@ class Http {
|
|
|
99
99
|
return __awaiter(this, arguments, void 0, function* (url, data, headers, type = RespType.JSON) {
|
|
100
100
|
url = url.startsWith('http') ? url : Http.API_BASE_URL + url;
|
|
101
101
|
headers = headers || exports.HttpHeaders;
|
|
102
|
-
headers['
|
|
102
|
+
headers['request-id'] = headers['request-id'] || (0, util_1.AppUUID4)();
|
|
103
103
|
try {
|
|
104
104
|
const requestOptions = { method: 'PUT', body: JSON.stringify(data), headers: headers };
|
|
105
105
|
const response = yield fetch(url, requestOptions);
|
|
106
106
|
return yield Http.RespData(response, type);
|
|
107
107
|
}
|
|
108
108
|
catch (error) {
|
|
109
|
-
throw { url: url, requestId: headers['
|
|
109
|
+
throw { url: url, requestId: headers['request-id'], body: data, error: error };
|
|
110
110
|
}
|
|
111
111
|
});
|
|
112
112
|
}
|
|
@@ -115,14 +115,14 @@ class Http {
|
|
|
115
115
|
url = url.startsWith('http') ? url : Http.API_BASE_URL + url;
|
|
116
116
|
const fullUrl = params ? `${url}?${new URLSearchParams(params).toString()}` : url;
|
|
117
117
|
headers = headers || exports.HttpHeaders;
|
|
118
|
-
headers['
|
|
118
|
+
headers['request-id'] = headers['request-id'] || (0, util_1.AppUUID4)();
|
|
119
119
|
try {
|
|
120
120
|
const requestOptions = { method: 'DELETE', headers: headers };
|
|
121
121
|
const response = yield fetch(fullUrl, requestOptions);
|
|
122
122
|
return yield Http.RespData(response, type);
|
|
123
123
|
}
|
|
124
124
|
catch (error) {
|
|
125
|
-
throw { url: fullUrl, requestId: headers['
|
|
125
|
+
throw { url: fullUrl, requestId: headers['request-id'], error: error };
|
|
126
126
|
}
|
|
127
127
|
});
|
|
128
128
|
}
|
|
@@ -132,14 +132,14 @@ class Http {
|
|
|
132
132
|
const params = { key: appKey, payload: JSON.stringify(sessionInfo) };
|
|
133
133
|
const fullUrl = `${url}?${new URLSearchParams(params).toString()}`;
|
|
134
134
|
const headers = Object.assign(Object.assign({}, Headers), { 'X-API-KEY': apiKey || '' });
|
|
135
|
-
headers['
|
|
135
|
+
headers['request-id'] = headers['request-id'] || (0, util_1.AppUUID4)();
|
|
136
136
|
try {
|
|
137
137
|
const requestOptions = { method: 'GET', headers: headers };
|
|
138
138
|
const response = yield fetch(fullUrl, requestOptions);
|
|
139
139
|
return yield Http.RespData(response, type);
|
|
140
140
|
}
|
|
141
141
|
catch (error) {
|
|
142
|
-
throw { url: fullUrl, requestId: headers['
|
|
142
|
+
throw { url: fullUrl, requestId: headers['request-id'], error: error };
|
|
143
143
|
}
|
|
144
144
|
});
|
|
145
145
|
}
|
|
@@ -156,7 +156,7 @@ class Http {
|
|
|
156
156
|
if (isHeadersAddMultiForm) {
|
|
157
157
|
headers = Object.assign(Object.assign({}, headers), { 'Content-Type': headers['Content-Type'] || 'multipart/form-data' });
|
|
158
158
|
}
|
|
159
|
-
headers['
|
|
159
|
+
headers['request-id'] = headers['request-id'] || (0, util_1.AppUUID4)();
|
|
160
160
|
try {
|
|
161
161
|
const requestOptions = {
|
|
162
162
|
method: method,
|
|
@@ -167,7 +167,7 @@ class Http {
|
|
|
167
167
|
return yield Http.RespData(response, type);
|
|
168
168
|
}
|
|
169
169
|
catch (error) {
|
|
170
|
-
throw { url: url, requestId: headers['
|
|
170
|
+
throw { url: url, requestId: headers['request-id'], error: error };
|
|
171
171
|
}
|
|
172
172
|
});
|
|
173
173
|
}
|
|
@@ -176,7 +176,7 @@ class Http {
|
|
|
176
176
|
headers = headers || {
|
|
177
177
|
'Content-Type': file.type,
|
|
178
178
|
};
|
|
179
|
-
headers['
|
|
179
|
+
headers['request-id'] = headers['request-id'] || (0, util_1.AppUUID4)();
|
|
180
180
|
try {
|
|
181
181
|
const requestOptions = {
|
|
182
182
|
method: method,
|
|
@@ -187,7 +187,7 @@ class Http {
|
|
|
187
187
|
return response;
|
|
188
188
|
}
|
|
189
189
|
catch (error) {
|
|
190
|
-
throw { url: fullUrl, requestId: headers['
|
|
190
|
+
throw { url: fullUrl, requestId: headers['request-id'], error: error };
|
|
191
191
|
}
|
|
192
192
|
});
|
|
193
193
|
}
|
|
@@ -195,7 +195,7 @@ class Http {
|
|
|
195
195
|
return __awaiter(this, arguments, void 0, function* (fullUrl, params, headers = exports.HttpHeaders, method = 'GET') {
|
|
196
196
|
fullUrl = params ? `${fullUrl}?${new URLSearchParams(params).toString()}` : fullUrl;
|
|
197
197
|
headers = headers || exports.HttpHeaders;
|
|
198
|
-
headers['
|
|
198
|
+
headers['request-id'] = headers['request-id'] || (0, util_1.AppUUID4)();
|
|
199
199
|
try {
|
|
200
200
|
const requestOptions = { method: method, headers: headers };
|
|
201
201
|
const response = yield fetch(fullUrl, requestOptions);
|
|
@@ -204,7 +204,7 @@ class Http {
|
|
|
204
204
|
return buffer;
|
|
205
205
|
}
|
|
206
206
|
catch (error) {
|
|
207
|
-
throw { url: fullUrl, requestId: headers['
|
|
207
|
+
throw { url: fullUrl, requestId: headers['request-id'], error: error };
|
|
208
208
|
}
|
|
209
209
|
});
|
|
210
210
|
}
|