@midwayjs/core 3.0.7 → 3.0.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/dist/error/framework.d.ts +4 -0
- package/dist/error/framework.js +8 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/util/httpclient.d.ts +21 -0
- package/dist/util/httpclient.js +93 -0
- package/dist/util/index.d.ts +1 -1
- package/dist/util/index.js +4 -1
- package/package.json +3 -2
|
@@ -13,6 +13,7 @@ export declare const FrameworkErrorEnum: {
|
|
|
13
13
|
readonly USE_WRONG_METHOD: "MIDWAY_10009";
|
|
14
14
|
readonly SINGLETON_INJECT_REQUEST: "MIDWAY_10010";
|
|
15
15
|
readonly MISSING_IMPORTS: "MIDWAY_10011";
|
|
16
|
+
readonly UTIL_HTTP_TIMEOUT: "MIDWAY_10012";
|
|
16
17
|
};
|
|
17
18
|
export declare class MidwayCommonError extends MidwayError {
|
|
18
19
|
constructor(message: string);
|
|
@@ -50,4 +51,7 @@ export declare class MidwaySingletonInjectRequestError extends MidwayError {
|
|
|
50
51
|
export declare class MidwayMissingImportComponentError extends MidwayError {
|
|
51
52
|
constructor(originName: string);
|
|
52
53
|
}
|
|
54
|
+
export declare class MidwayUtilHttpClientTimeoutError extends MidwayError {
|
|
55
|
+
constructor(message: string);
|
|
56
|
+
}
|
|
53
57
|
//# sourceMappingURL=framework.d.ts.map
|
package/dist/error/framework.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MidwayMissingImportComponentError = exports.MidwaySingletonInjectRequestError = exports.MidwayUseWrongMethodError = exports.MidwayDuplicateRouteError = exports.MidwayResolverMissingError = exports.MidwayConfigMissingError = exports.MidwayFeatureNotImplementedError = exports.MidwayFeatureNoLongerSupportedError = exports.MidwayDefinitionNotFoundError = exports.MidwayParameterError = exports.MidwayCommonError = exports.FrameworkErrorEnum = void 0;
|
|
3
|
+
exports.MidwayUtilHttpClientTimeoutError = exports.MidwayMissingImportComponentError = exports.MidwaySingletonInjectRequestError = exports.MidwayUseWrongMethodError = exports.MidwayDuplicateRouteError = exports.MidwayResolverMissingError = exports.MidwayConfigMissingError = exports.MidwayFeatureNotImplementedError = exports.MidwayFeatureNoLongerSupportedError = exports.MidwayDefinitionNotFoundError = exports.MidwayParameterError = exports.MidwayCommonError = exports.FrameworkErrorEnum = void 0;
|
|
4
4
|
const base_1 = require("./base");
|
|
5
5
|
exports.FrameworkErrorEnum = (0, base_1.registerErrorCode)('midway', {
|
|
6
6
|
UNKNOWN: 10000,
|
|
@@ -15,6 +15,7 @@ exports.FrameworkErrorEnum = (0, base_1.registerErrorCode)('midway', {
|
|
|
15
15
|
USE_WRONG_METHOD: 10009,
|
|
16
16
|
SINGLETON_INJECT_REQUEST: 10010,
|
|
17
17
|
MISSING_IMPORTS: 10011,
|
|
18
|
+
UTIL_HTTP_TIMEOUT: 10012,
|
|
18
19
|
});
|
|
19
20
|
class MidwayCommonError extends base_1.MidwayError {
|
|
20
21
|
constructor(message) {
|
|
@@ -100,4 +101,10 @@ class MidwayMissingImportComponentError extends base_1.MidwayError {
|
|
|
100
101
|
}
|
|
101
102
|
}
|
|
102
103
|
exports.MidwayMissingImportComponentError = MidwayMissingImportComponentError;
|
|
104
|
+
class MidwayUtilHttpClientTimeoutError extends base_1.MidwayError {
|
|
105
|
+
constructor(message) {
|
|
106
|
+
super(message, exports.FrameworkErrorEnum.UTIL_HTTP_TIMEOUT);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
exports.MidwayUtilHttpClientTimeoutError = MidwayUtilHttpClientTimeoutError;
|
|
103
110
|
//# sourceMappingURL=framework.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -28,6 +28,7 @@ export * from './common/fileDetector';
|
|
|
28
28
|
export * from './common/webGenerator';
|
|
29
29
|
export * from './common/middlewareManager';
|
|
30
30
|
export * from './util/pathToRegexp';
|
|
31
|
+
export * from './util/httpclient';
|
|
31
32
|
export * from './common/filterManager';
|
|
32
33
|
export * from './common/applicationManager';
|
|
33
34
|
export * from './setup';
|
package/dist/index.js
CHANGED
|
@@ -63,6 +63,7 @@ __exportStar(require("./common/fileDetector"), exports);
|
|
|
63
63
|
__exportStar(require("./common/webGenerator"), exports);
|
|
64
64
|
__exportStar(require("./common/middlewareManager"), exports);
|
|
65
65
|
__exportStar(require("./util/pathToRegexp"), exports);
|
|
66
|
+
__exportStar(require("./util/httpclient"), exports);
|
|
66
67
|
__exportStar(require("./common/filterManager"), exports);
|
|
67
68
|
__exportStar(require("./common/applicationManager"), exports);
|
|
68
69
|
__exportStar(require("./setup"), exports);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import http = require('http');
|
|
3
|
+
declare type MethodType = 'GET' | 'POST';
|
|
4
|
+
declare type MimeType = 'text' | 'json' | undefined;
|
|
5
|
+
interface IOptions {
|
|
6
|
+
method?: MethodType;
|
|
7
|
+
headers?: any;
|
|
8
|
+
contentType?: MimeType;
|
|
9
|
+
dataType?: MimeType;
|
|
10
|
+
data?: any;
|
|
11
|
+
timeout?: number;
|
|
12
|
+
}
|
|
13
|
+
export interface IResponse extends http.IncomingMessage {
|
|
14
|
+
status: number;
|
|
15
|
+
data: Buffer | string | any;
|
|
16
|
+
}
|
|
17
|
+
export declare class HttpClient {
|
|
18
|
+
request(url: string, options?: IOptions): Promise<IResponse>;
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=httpclient.d.ts.map
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpClient = void 0;
|
|
4
|
+
const http = require("http");
|
|
5
|
+
const https = require("https");
|
|
6
|
+
const url = require("url");
|
|
7
|
+
const util_1 = require("util");
|
|
8
|
+
const error_1 = require("../error");
|
|
9
|
+
const debug = (0, util_1.debuglog)('request-client');
|
|
10
|
+
const URL = url.URL;
|
|
11
|
+
const mimeMap = {
|
|
12
|
+
text: 'application/text',
|
|
13
|
+
json: 'application/json',
|
|
14
|
+
octet: 'application/octet-stream',
|
|
15
|
+
};
|
|
16
|
+
class HttpClient {
|
|
17
|
+
async request(url, options = {}) {
|
|
18
|
+
debug(`request '${url}'`);
|
|
19
|
+
const whatwgUrl = new URL(url);
|
|
20
|
+
const client = whatwgUrl.protocol === 'https:' ? https : http;
|
|
21
|
+
const contentType = options.contentType;
|
|
22
|
+
const dataType = options.dataType;
|
|
23
|
+
const method = options.method || 'GET';
|
|
24
|
+
const timeout = options.timeout || 5000;
|
|
25
|
+
const headers = {
|
|
26
|
+
Accept: mimeMap[dataType] || mimeMap.octet,
|
|
27
|
+
...options.headers,
|
|
28
|
+
};
|
|
29
|
+
let data;
|
|
30
|
+
if (method === 'GET' && options.data) {
|
|
31
|
+
for (const key of Object.keys(options.data)) {
|
|
32
|
+
whatwgUrl.searchParams.set(key, options.data[key]);
|
|
33
|
+
}
|
|
34
|
+
headers['Content-Length'] = 0;
|
|
35
|
+
}
|
|
36
|
+
else if (options.data) {
|
|
37
|
+
data = Buffer.from(JSON.stringify(options.data));
|
|
38
|
+
headers['Content-Type'] = mimeMap[contentType] || mimeMap.octet;
|
|
39
|
+
headers['Content-Length'] = data.byteLength;
|
|
40
|
+
}
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
const req = client.request(whatwgUrl.toString(), {
|
|
43
|
+
method,
|
|
44
|
+
headers,
|
|
45
|
+
}, res => {
|
|
46
|
+
res.setTimeout(timeout, () => {
|
|
47
|
+
res.destroy(new Error('Response Timeout'));
|
|
48
|
+
});
|
|
49
|
+
res.on('error', error => {
|
|
50
|
+
reject(error);
|
|
51
|
+
});
|
|
52
|
+
const chunks = [];
|
|
53
|
+
res.on('data', chunk => {
|
|
54
|
+
chunks.push(chunk);
|
|
55
|
+
});
|
|
56
|
+
res.on('end', () => {
|
|
57
|
+
let data = Buffer.concat(chunks);
|
|
58
|
+
if (dataType === 'text' || dataType === 'json') {
|
|
59
|
+
data = data.toString('utf8');
|
|
60
|
+
}
|
|
61
|
+
if (dataType === 'json') {
|
|
62
|
+
try {
|
|
63
|
+
data = JSON.parse(data);
|
|
64
|
+
}
|
|
65
|
+
catch (e) {
|
|
66
|
+
return reject(new Error('[httpclient] Unable to parse response data'));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
Object.assign(res, {
|
|
70
|
+
status: res.statusCode,
|
|
71
|
+
data,
|
|
72
|
+
});
|
|
73
|
+
debug(`request '${url}' resolved with status ${res.statusCode}`);
|
|
74
|
+
resolve(res);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
req.setTimeout(timeout, () => {
|
|
78
|
+
req.destroy(new error_1.MidwayUtilHttpClientTimeoutError('Request Timeout'));
|
|
79
|
+
});
|
|
80
|
+
req.on('error', error => {
|
|
81
|
+
reject(error);
|
|
82
|
+
});
|
|
83
|
+
if (method !== 'GET') {
|
|
84
|
+
req.end(data);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
req.end();
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.HttpClient = HttpClient;
|
|
93
|
+
//# sourceMappingURL=httpclient.js.map
|
package/dist/util/index.d.ts
CHANGED
|
@@ -38,7 +38,7 @@ export declare function joinURLPath(...strArray: any[]): string;
|
|
|
38
38
|
* @param constructors
|
|
39
39
|
* @since 2.0.0
|
|
40
40
|
*/
|
|
41
|
-
export declare function delegateTargetPrototypeMethod(derivedCtor: any, constructors: any[]): void;
|
|
41
|
+
export declare function delegateTargetPrototypeMethod(derivedCtor: any, constructors: any[], otherMethods?: string[]): void;
|
|
42
42
|
/**
|
|
43
43
|
* 代理目标原型上的特定方法
|
|
44
44
|
* @param derivedCtor
|
package/dist/util/index.js
CHANGED
|
@@ -114,7 +114,7 @@ exports.joinURLPath = joinURLPath;
|
|
|
114
114
|
* @param constructors
|
|
115
115
|
* @since 2.0.0
|
|
116
116
|
*/
|
|
117
|
-
function delegateTargetPrototypeMethod(derivedCtor, constructors) {
|
|
117
|
+
function delegateTargetPrototypeMethod(derivedCtor, constructors, otherMethods) {
|
|
118
118
|
constructors.forEach(baseCtor => {
|
|
119
119
|
Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
|
|
120
120
|
if (name !== 'constructor' && !/^_/.test(name)) {
|
|
@@ -124,6 +124,9 @@ function delegateTargetPrototypeMethod(derivedCtor, constructors) {
|
|
|
124
124
|
}
|
|
125
125
|
});
|
|
126
126
|
});
|
|
127
|
+
if (otherMethods) {
|
|
128
|
+
delegateTargetMethod(derivedCtor, otherMethods);
|
|
129
|
+
}
|
|
127
130
|
}
|
|
128
131
|
exports.delegateTargetPrototypeMethod = delegateTargetPrototypeMethod;
|
|
129
132
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/core",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.9",
|
|
4
4
|
"description": "midway core",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"koa": "2.13.4",
|
|
26
26
|
"midway-test-component": "*",
|
|
27
27
|
"mm": "3.2.0",
|
|
28
|
+
"raw-body": "2.4.3",
|
|
28
29
|
"sinon": "13.0.1"
|
|
29
30
|
},
|
|
30
31
|
"peerDependencies": {
|
|
@@ -44,5 +45,5 @@
|
|
|
44
45
|
"engines": {
|
|
45
46
|
"node": ">=12"
|
|
46
47
|
},
|
|
47
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "6fbf54fb8840844c229d2cf11b30128c67cbeaf1"
|
|
48
49
|
}
|