@midwayjs/core 3.0.4-beta.1 → 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/baseFramework.d.ts +3 -5
- package/dist/baseFramework.js +8 -17
- package/dist/context/container.js +3 -4
- package/dist/context/managedResolverFactory.d.ts +2 -1
- package/dist/context/managedResolverFactory.js +14 -0
- package/dist/error/framework.d.ts +8 -0
- package/dist/error/framework.js +16 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/interface.d.ts +3 -4
- package/dist/setup.js +2 -2
- 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 +6 -5
package/dist/baseFramework.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CommonMiddlewareUnion, IConfigurationOptions, IMidwayApplication, IMidwayBootstrapOptions, IMidwayContainer, IMidwayContext, IMidwayFramework, CommonFilterUnion, CommonMiddleware, MiddlewareRespond } from './interface';
|
|
2
2
|
import { FrameworkType } from '@midwayjs/decorator';
|
|
3
|
-
import { ILogger, LoggerOptions } from '@midwayjs/logger';
|
|
3
|
+
import { ILogger, LoggerOptions, LoggerContextFormat } from '@midwayjs/logger';
|
|
4
4
|
import { MidwayEnvironmentService } from './service/environmentService';
|
|
5
5
|
import { MidwayConfigService } from './service/configService';
|
|
6
6
|
import { MidwayInformationService } from './service/informationService';
|
|
@@ -15,8 +15,8 @@ export declare abstract class BaseFramework<APP extends IMidwayApplication<CTX>,
|
|
|
15
15
|
protected logger: ILogger;
|
|
16
16
|
protected appLogger: ILogger;
|
|
17
17
|
protected defaultContext: {};
|
|
18
|
-
protected
|
|
19
|
-
protected
|
|
18
|
+
protected contextLoggerApplyLogger: string;
|
|
19
|
+
protected contextLoggerFormat: LoggerContextFormat;
|
|
20
20
|
protected middlewareManager: ContextMiddlewareManager<CTX, ResOrNext, Next>;
|
|
21
21
|
protected filterManager: FilterManager<CTX, ResOrNext, Next>;
|
|
22
22
|
protected composeMiddleware: any;
|
|
@@ -49,7 +49,6 @@ export declare abstract class BaseFramework<APP extends IMidwayApplication<CTX>,
|
|
|
49
49
|
abstract applicationInitialize(options: IMidwayBootstrapOptions): any;
|
|
50
50
|
abstract getFrameworkType(): FrameworkType;
|
|
51
51
|
abstract run(): Promise<void>;
|
|
52
|
-
setContextLoggerClass(BaseContextLogger: any): void;
|
|
53
52
|
protected createContextLogger(ctx: CTX, name?: string): ILogger;
|
|
54
53
|
stop(): Promise<void>;
|
|
55
54
|
getAppDir(): string;
|
|
@@ -78,7 +77,6 @@ export declare abstract class BaseFramework<APP extends IMidwayApplication<CTX>,
|
|
|
78
77
|
createLogger(name: string, option?: LoggerOptions): ILogger;
|
|
79
78
|
getProjectName(): string;
|
|
80
79
|
getFrameworkName(): string;
|
|
81
|
-
getDefaultContextLoggerClass(): any;
|
|
82
80
|
useMiddleware(Middleware: CommonMiddlewareUnion<CTX, ResOrNext, Next>): void;
|
|
83
81
|
getMiddleware(): ContextMiddlewareManager<CTX, ResOrNext, Next>;
|
|
84
82
|
useFilter(Filter: CommonFilterUnion<CTX, ResOrNext, Next>): void;
|
package/dist/baseFramework.js
CHANGED
|
@@ -12,7 +12,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.BaseFramework = void 0;
|
|
13
13
|
const interface_1 = require("./interface");
|
|
14
14
|
const decorator_1 = require("@midwayjs/decorator");
|
|
15
|
-
const logger_1 = require("@midwayjs/logger");
|
|
16
15
|
const requestContainer_1 = require("./context/requestContainer");
|
|
17
16
|
const environmentService_1 = require("./service/environmentService");
|
|
18
17
|
const configService_1 = require("./service/configService");
|
|
@@ -32,12 +31,11 @@ class BaseFramework {
|
|
|
32
31
|
this.composeMiddleware = null;
|
|
33
32
|
}
|
|
34
33
|
async init() {
|
|
35
|
-
var _a, _b
|
|
34
|
+
var _a, _b;
|
|
36
35
|
this.configurationOptions = (_a = this.configure()) !== null && _a !== void 0 ? _a : {};
|
|
37
|
-
this.
|
|
38
|
-
(_b = this.configurationOptions.
|
|
39
|
-
this.
|
|
40
|
-
(_c = this.configurationOptions.ContextLoggerApplyLogger) !== null && _c !== void 0 ? _c : 'appLogger';
|
|
36
|
+
this.contextLoggerApplyLogger =
|
|
37
|
+
(_b = this.configurationOptions.contextLoggerApplyLogger) !== null && _b !== void 0 ? _b : 'appLogger';
|
|
38
|
+
this.contextLoggerFormat = this.configurationOptions.contextLoggerFormat;
|
|
41
39
|
this.logger = this.loggerService.getLogger('coreLogger');
|
|
42
40
|
this.appLogger = this.loggerService.getLogger('appLogger');
|
|
43
41
|
return this;
|
|
@@ -86,12 +84,11 @@ class BaseFramework {
|
|
|
86
84
|
getApplication() {
|
|
87
85
|
return this.app;
|
|
88
86
|
}
|
|
89
|
-
setContextLoggerClass(BaseContextLogger) {
|
|
90
|
-
this.BaseContextLoggerClass = BaseContextLogger;
|
|
91
|
-
}
|
|
92
87
|
createContextLogger(ctx, name) {
|
|
93
|
-
const appLogger = this.getLogger(name !== null && name !== void 0 ? name : this.
|
|
94
|
-
return
|
|
88
|
+
const appLogger = this.getLogger(name !== null && name !== void 0 ? name : this.contextLoggerApplyLogger);
|
|
89
|
+
return appLogger.createContextLogger(ctx, {
|
|
90
|
+
contextFormat: this.contextLoggerFormat,
|
|
91
|
+
});
|
|
95
92
|
}
|
|
96
93
|
async stop() {
|
|
97
94
|
await this.beforeStop();
|
|
@@ -162,9 +159,6 @@ class BaseFramework {
|
|
|
162
159
|
};
|
|
163
160
|
return ctx;
|
|
164
161
|
},
|
|
165
|
-
setContextLoggerClass: (BaseContextLogger) => {
|
|
166
|
-
return this.setContextLoggerClass(BaseContextLogger);
|
|
167
|
-
},
|
|
168
162
|
addConfigObject: (obj) => {
|
|
169
163
|
this.configService.addObject(obj);
|
|
170
164
|
},
|
|
@@ -249,9 +243,6 @@ class BaseFramework {
|
|
|
249
243
|
getFrameworkName() {
|
|
250
244
|
return this.getFrameworkType().name;
|
|
251
245
|
}
|
|
252
|
-
getDefaultContextLoggerClass() {
|
|
253
|
-
return logger_1.MidwayContextLogger;
|
|
254
|
-
}
|
|
255
246
|
useMiddleware(Middleware) {
|
|
256
247
|
this.middlewareManager.insertLast(Middleware);
|
|
257
248
|
}
|
|
@@ -165,8 +165,6 @@ class ContainerConfiguration {
|
|
|
165
165
|
return this.namespaceList;
|
|
166
166
|
}
|
|
167
167
|
}
|
|
168
|
-
class ObjectCreateEventTarget extends EventEmitter {
|
|
169
|
-
}
|
|
170
168
|
class MidwayContainer {
|
|
171
169
|
constructor(parent) {
|
|
172
170
|
this._resolverFactory = null;
|
|
@@ -189,7 +187,7 @@ class MidwayContainer {
|
|
|
189
187
|
}
|
|
190
188
|
get objectCreateEventTarget() {
|
|
191
189
|
if (!this._objectCreateEventTarget) {
|
|
192
|
-
this._objectCreateEventTarget = new
|
|
190
|
+
this._objectCreateEventTarget = new EventEmitter();
|
|
193
191
|
}
|
|
194
192
|
return this._objectCreateEventTarget;
|
|
195
193
|
}
|
|
@@ -228,7 +226,7 @@ class MidwayContainer {
|
|
|
228
226
|
configuration.load(module);
|
|
229
227
|
for (const ns of configuration.getNamespaceList()) {
|
|
230
228
|
this.namespaceSet.add(ns);
|
|
231
|
-
debug(`[core]: load configuration in namespace="${ns} complete
|
|
229
|
+
debug(`[core]: load configuration in namespace="${ns}" complete`);
|
|
232
230
|
}
|
|
233
231
|
}
|
|
234
232
|
}
|
|
@@ -294,6 +292,7 @@ class MidwayContainer {
|
|
|
294
292
|
const refManaged = new managedResolverFactory_1.ManagedReference();
|
|
295
293
|
refManaged.args = propertyMeta.args;
|
|
296
294
|
refManaged.name = propertyMeta.value;
|
|
295
|
+
refManaged.injectMode = propertyMeta['injectMode'];
|
|
297
296
|
definition.properties.set(propertyMeta['targetKey'], refManaged);
|
|
298
297
|
}
|
|
299
298
|
// inject custom properties
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 管理对象解析构建
|
|
3
3
|
*/
|
|
4
|
-
import { IManagedInstance, ObjectIdentifier } from '@midwayjs/decorator';
|
|
4
|
+
import { IManagedInstance, InjectModeEnum, ObjectIdentifier } from '@midwayjs/decorator';
|
|
5
5
|
import { IManagedResolver, IObjectDefinition, IManagedResolverFactoryCreateOptions, IMidwayContainer } from '../interface';
|
|
6
6
|
export declare class ManagedReference implements IManagedInstance {
|
|
7
7
|
type: string;
|
|
8
8
|
name: string;
|
|
9
|
+
injectMode: InjectModeEnum;
|
|
9
10
|
args?: any;
|
|
10
11
|
}
|
|
11
12
|
/**
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ManagedResolverFactory = exports.ManagedReference = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* 管理对象解析构建
|
|
6
|
+
*/
|
|
7
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
4
8
|
const constants_1 = require("../common/constants");
|
|
5
9
|
const interface_1 = require("../interface");
|
|
6
10
|
const util = require("util");
|
|
@@ -24,13 +28,23 @@ class RefResolver {
|
|
|
24
28
|
return constants_1.KEYS.REF_ELEMENT;
|
|
25
29
|
}
|
|
26
30
|
resolve(managed, originName) {
|
|
31
|
+
var _a;
|
|
27
32
|
const mr = managed;
|
|
33
|
+
if (mr.injectMode === decorator_1.InjectModeEnum.Class &&
|
|
34
|
+
!((_a = this.factory.context.parent) !== null && _a !== void 0 ? _a : this.factory.context).hasDefinition(mr.name)) {
|
|
35
|
+
throw new error_1.MidwayMissingImportComponentError(originName);
|
|
36
|
+
}
|
|
28
37
|
return this.factory.context.get(mr.name, mr.args, {
|
|
29
38
|
originName,
|
|
30
39
|
});
|
|
31
40
|
}
|
|
32
41
|
async resolveAsync(managed, originName) {
|
|
42
|
+
var _a;
|
|
33
43
|
const mr = managed;
|
|
44
|
+
if (mr.injectMode === decorator_1.InjectModeEnum.Class &&
|
|
45
|
+
!((_a = this.factory.context.parent) !== null && _a !== void 0 ? _a : this.factory.context).hasDefinition(mr.name)) {
|
|
46
|
+
throw new error_1.MidwayMissingImportComponentError(originName);
|
|
47
|
+
}
|
|
34
48
|
return this.factory.context.getAsync(mr.name, mr.args, {
|
|
35
49
|
originName,
|
|
36
50
|
});
|
|
@@ -12,6 +12,8 @@ export declare const FrameworkErrorEnum: {
|
|
|
12
12
|
readonly DUPLICATE_ROUTER: "MIDWAY_10008";
|
|
13
13
|
readonly USE_WRONG_METHOD: "MIDWAY_10009";
|
|
14
14
|
readonly SINGLETON_INJECT_REQUEST: "MIDWAY_10010";
|
|
15
|
+
readonly MISSING_IMPORTS: "MIDWAY_10011";
|
|
16
|
+
readonly UTIL_HTTP_TIMEOUT: "MIDWAY_10012";
|
|
15
17
|
};
|
|
16
18
|
export declare class MidwayCommonError extends MidwayError {
|
|
17
19
|
constructor(message: string);
|
|
@@ -46,4 +48,10 @@ export declare class MidwayUseWrongMethodError extends MidwayError {
|
|
|
46
48
|
export declare class MidwaySingletonInjectRequestError extends MidwayError {
|
|
47
49
|
constructor(singletonScopeName: string, requestScopeName: string);
|
|
48
50
|
}
|
|
51
|
+
export declare class MidwayMissingImportComponentError extends MidwayError {
|
|
52
|
+
constructor(originName: string);
|
|
53
|
+
}
|
|
54
|
+
export declare class MidwayUtilHttpClientTimeoutError extends MidwayError {
|
|
55
|
+
constructor(message: string);
|
|
56
|
+
}
|
|
49
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.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,
|
|
@@ -14,6 +14,8 @@ exports.FrameworkErrorEnum = (0, base_1.registerErrorCode)('midway', {
|
|
|
14
14
|
DUPLICATE_ROUTER: 10008,
|
|
15
15
|
USE_WRONG_METHOD: 10009,
|
|
16
16
|
SINGLETON_INJECT_REQUEST: 10010,
|
|
17
|
+
MISSING_IMPORTS: 10011,
|
|
18
|
+
UTIL_HTTP_TIMEOUT: 10012,
|
|
17
19
|
});
|
|
18
20
|
class MidwayCommonError extends base_1.MidwayError {
|
|
19
21
|
constructor(message) {
|
|
@@ -92,4 +94,17 @@ class MidwaySingletonInjectRequestError extends base_1.MidwayError {
|
|
|
92
94
|
}
|
|
93
95
|
}
|
|
94
96
|
exports.MidwaySingletonInjectRequestError = MidwaySingletonInjectRequestError;
|
|
97
|
+
class MidwayMissingImportComponentError extends base_1.MidwayError {
|
|
98
|
+
constructor(originName) {
|
|
99
|
+
const text = `"${originName}" can't inject and maybe forgot add "{imports: [***]}" in @Configuration.`;
|
|
100
|
+
super(text, exports.FrameworkErrorEnum.MISSING_IMPORTS);
|
|
101
|
+
}
|
|
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;
|
|
95
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);
|
package/dist/interface.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { ObjectIdentifier, IManagedInstance, IMethodAspect, ScopeEnum, FrameworkType } from '@midwayjs/decorator';
|
|
3
|
-
import { ILogger, LoggerOptions } from '@midwayjs/logger';
|
|
3
|
+
import { ILogger, LoggerOptions, LoggerContextFormat } from '@midwayjs/logger';
|
|
4
4
|
import * as EventEmitter from 'events';
|
|
5
5
|
import { ContextMiddlewareManager } from './common/middlewareManager';
|
|
6
6
|
import _default from './config/config.default';
|
|
@@ -427,8 +427,8 @@ export interface IMidwayBootstrapOptions {
|
|
|
427
427
|
export interface IConfigurationOptions {
|
|
428
428
|
logger?: ILogger;
|
|
429
429
|
appLogger?: ILogger;
|
|
430
|
-
|
|
431
|
-
|
|
430
|
+
contextLoggerApplyLogger?: string;
|
|
431
|
+
contextLoggerFormat?: LoggerContextFormat;
|
|
432
432
|
}
|
|
433
433
|
export interface IMidwayFramework<APP extends IMidwayApplication<CTX>, CTX extends IMidwayContext, CONFIG extends IConfigurationOptions, ResOrNext = unknown, Next = unknown> {
|
|
434
434
|
app: APP;
|
|
@@ -450,7 +450,6 @@ export interface IMidwayFramework<APP extends IMidwayApplication<CTX>, CTX exten
|
|
|
450
450
|
getCoreLogger(): ILogger;
|
|
451
451
|
createLogger(name: string, options: LoggerOptions): ILogger;
|
|
452
452
|
getProjectName(): string;
|
|
453
|
-
getDefaultContextLoggerClass(): any;
|
|
454
453
|
useMiddleware(Middleware: CommonMiddlewareUnion<CTX, ResOrNext, Next>): void;
|
|
455
454
|
getMiddleware(): ContextMiddlewareManager<CTX, ResOrNext, Next>;
|
|
456
455
|
applyMiddleware(lastMiddleware?: CommonMiddleware<CTX, ResOrNext, Next>): Promise<MiddlewareRespond<CTX, ResOrNext, Next>>;
|
package/dist/setup.js
CHANGED
|
@@ -38,14 +38,14 @@ async function initializeGlobalApplicationContext(globalOptions) {
|
|
|
38
38
|
// bind inner service
|
|
39
39
|
applicationContext.bindClass(_1.MidwayEnvironmentService);
|
|
40
40
|
applicationContext.bindClass(_1.MidwayInformationService);
|
|
41
|
+
applicationContext.bindClass(_1.MidwayAspectService);
|
|
41
42
|
applicationContext.bindClass(_1.MidwayDecoratorService);
|
|
42
43
|
applicationContext.bindClass(_1.MidwayConfigService);
|
|
43
|
-
applicationContext.bindClass(_1.MidwayAspectService);
|
|
44
44
|
applicationContext.bindClass(_1.MidwayLoggerService);
|
|
45
|
+
applicationContext.bindClass(_1.MidwayApplicationManager);
|
|
45
46
|
applicationContext.bindClass(_1.MidwayFrameworkService);
|
|
46
47
|
applicationContext.bindClass(_1.MidwayMiddlewareService);
|
|
47
48
|
applicationContext.bindClass(_1.MidwayLifeCycleService);
|
|
48
|
-
applicationContext.bindClass(_1.MidwayApplicationManager);
|
|
49
49
|
// bind preload module
|
|
50
50
|
if (globalOptions.preloadModules && globalOptions.preloadModules.length) {
|
|
51
51
|
for (const preloadModule of globalOptions.preloadModules) {
|
|
@@ -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",
|
|
@@ -21,18 +21,19 @@
|
|
|
21
21
|
],
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@midwayjs/decorator": "^3.0.
|
|
24
|
+
"@midwayjs/decorator": "^3.0.7",
|
|
25
25
|
"koa": "2.13.4",
|
|
26
26
|
"midway-test-component": "*",
|
|
27
27
|
"mm": "3.2.0",
|
|
28
|
-
"
|
|
28
|
+
"raw-body": "2.4.3",
|
|
29
|
+
"sinon": "13.0.1"
|
|
29
30
|
},
|
|
30
31
|
"peerDependencies": {
|
|
31
32
|
"@midwayjs/decorator": "*"
|
|
32
33
|
},
|
|
33
34
|
"dependencies": {
|
|
34
35
|
"@midwayjs/glob": "^1.0.2",
|
|
35
|
-
"@midwayjs/logger": "^2.
|
|
36
|
+
"@midwayjs/logger": "^2.15.0",
|
|
36
37
|
"class-transformer": "^0.5.1",
|
|
37
38
|
"picomatch": "2.3.1"
|
|
38
39
|
},
|
|
@@ -44,5 +45,5 @@
|
|
|
44
45
|
"engines": {
|
|
45
46
|
"node": ">=12"
|
|
46
47
|
},
|
|
47
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "6fbf54fb8840844c229d2cf11b30128c67cbeaf1"
|
|
48
49
|
}
|