@velajs/vela 0.4.3 → 0.5.1
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/cache/cache.module.d.ts +4 -1
- package/dist/cache/cache.module.js +51 -9
- package/dist/config/config.module.d.ts +4 -1
- package/dist/config/config.module.js +40 -3
- package/dist/config/config.types.d.ts +1 -0
- package/dist/constants.d.ts +8 -2
- package/dist/constants.js +6 -0
- package/dist/container/container.d.ts +2 -0
- package/dist/container/container.js +51 -16
- package/dist/container/decorators.d.ts +3 -1
- package/dist/container/decorators.js +27 -6
- package/dist/container/index.d.ts +4 -2
- package/dist/container/index.js +4 -2
- package/dist/container/mixin.d.ts +24 -0
- package/dist/container/mixin.js +26 -0
- package/dist/container/module-ref.d.ts +21 -0
- package/dist/container/module-ref.js +48 -0
- package/dist/container/types.d.ts +9 -3
- package/dist/container/types.js +9 -0
- package/dist/cors/cors.module.js +2 -2
- package/dist/errors/http-exception.d.ts +20 -20
- package/dist/errors/http-exception.js +47 -41
- package/dist/factory.js +11 -2
- package/dist/fetch/fetch.module.d.ts +6 -0
- package/dist/fetch/fetch.module.js +77 -0
- package/dist/fetch/fetch.service.d.ts +24 -0
- package/dist/fetch/fetch.service.js +145 -0
- package/dist/fetch/fetch.types.d.ts +24 -0
- package/dist/fetch/fetch.types.js +1 -0
- package/dist/fetch/index.d.ts +3 -0
- package/dist/fetch/index.js +2 -0
- package/dist/health/health.service.js +2 -2
- package/dist/http/decorators.d.ts +65 -0
- package/dist/http/decorators.js +91 -2
- package/dist/http/index.d.ts +3 -1
- package/dist/http/index.js +2 -1
- package/dist/http/middleware-consumer.d.ts +36 -0
- package/dist/http/middleware-consumer.js +71 -0
- package/dist/http/route.manager.d.ts +4 -0
- package/dist/http/route.manager.js +91 -6
- package/dist/http/types.d.ts +1 -0
- package/dist/index.d.ts +11 -6
- package/dist/index.js +7 -4
- package/dist/metadata.d.ts +1 -1
- package/dist/module/decorators.d.ts +1 -0
- package/dist/module/decorators.js +24 -8
- package/dist/module/index.d.ts +2 -2
- package/dist/module/index.js +1 -1
- package/dist/module/module-loader.d.ts +5 -1
- package/dist/module/module-loader.js +54 -10
- package/dist/module/types.d.ts +13 -3
- package/dist/pipeline/index.d.ts +3 -2
- package/dist/pipeline/index.js +1 -1
- package/dist/pipeline/pipes.d.ts +22 -0
- package/dist/pipeline/pipes.js +50 -0
- package/dist/pipeline/tokens.d.ts +1 -1
- package/dist/pipeline/tokens.js +1 -1
- package/dist/pipeline/types.d.ts +16 -0
- package/dist/registry/metadata.registry.d.ts +7 -6
- package/dist/registry/metadata.registry.js +13 -0
- package/dist/registry/types.d.ts +1 -0
- package/dist/schedule/schedule.module.d.ts +4 -1
- package/dist/schedule/schedule.module.js +39 -7
- package/dist/testing/testing.builder.js +5 -5
- package/dist/throttler/throttler.module.d.ts +2 -1
- package/dist/throttler/throttler.module.js +47 -9
- package/dist/validation/validation.pipe.js +1 -1
- package/package.json +1 -1
|
@@ -1,61 +1,61 @@
|
|
|
1
1
|
export declare class HttpException extends Error {
|
|
2
2
|
readonly statusCode: number;
|
|
3
|
-
readonly
|
|
4
|
-
constructor(
|
|
3
|
+
private readonly _response;
|
|
4
|
+
constructor(response: string | object, statusCode: number);
|
|
5
5
|
getStatus(): number;
|
|
6
6
|
getResponse(): unknown;
|
|
7
7
|
}
|
|
8
8
|
export declare class BadRequestException extends HttpException {
|
|
9
|
-
constructor(message?: string
|
|
9
|
+
constructor(message?: string | object);
|
|
10
10
|
}
|
|
11
11
|
export declare class UnauthorizedException extends HttpException {
|
|
12
|
-
constructor(message?: string
|
|
12
|
+
constructor(message?: string | object);
|
|
13
13
|
}
|
|
14
14
|
export declare class ForbiddenException extends HttpException {
|
|
15
|
-
constructor(message?: string
|
|
15
|
+
constructor(message?: string | object);
|
|
16
16
|
}
|
|
17
17
|
export declare class NotFoundException extends HttpException {
|
|
18
|
-
constructor(message?: string
|
|
18
|
+
constructor(message?: string | object);
|
|
19
19
|
}
|
|
20
20
|
export declare class MethodNotAllowedException extends HttpException {
|
|
21
|
-
constructor(message?: string
|
|
21
|
+
constructor(message?: string | object);
|
|
22
22
|
}
|
|
23
23
|
export declare class NotAcceptableException extends HttpException {
|
|
24
|
-
constructor(message?: string
|
|
24
|
+
constructor(message?: string | object);
|
|
25
25
|
}
|
|
26
26
|
export declare class RequestTimeoutException extends HttpException {
|
|
27
|
-
constructor(message?: string
|
|
27
|
+
constructor(message?: string | object);
|
|
28
28
|
}
|
|
29
29
|
export declare class ConflictException extends HttpException {
|
|
30
|
-
constructor(message?: string
|
|
30
|
+
constructor(message?: string | object);
|
|
31
31
|
}
|
|
32
32
|
export declare class GoneException extends HttpException {
|
|
33
|
-
constructor(message?: string
|
|
33
|
+
constructor(message?: string | object);
|
|
34
34
|
}
|
|
35
35
|
export declare class PayloadTooLargeException extends HttpException {
|
|
36
|
-
constructor(message?: string
|
|
36
|
+
constructor(message?: string | object);
|
|
37
37
|
}
|
|
38
38
|
export declare class UnsupportedMediaTypeException extends HttpException {
|
|
39
|
-
constructor(message?: string
|
|
39
|
+
constructor(message?: string | object);
|
|
40
40
|
}
|
|
41
41
|
export declare class UnprocessableEntityException extends HttpException {
|
|
42
|
-
constructor(message?: string
|
|
42
|
+
constructor(message?: string | object);
|
|
43
43
|
}
|
|
44
44
|
export declare class TooManyRequestsException extends HttpException {
|
|
45
|
-
constructor(message?: string
|
|
45
|
+
constructor(message?: string | object);
|
|
46
46
|
}
|
|
47
47
|
export declare class InternalServerErrorException extends HttpException {
|
|
48
|
-
constructor(message?: string
|
|
48
|
+
constructor(message?: string | object);
|
|
49
49
|
}
|
|
50
50
|
export declare class NotImplementedException extends HttpException {
|
|
51
|
-
constructor(message?: string
|
|
51
|
+
constructor(message?: string | object);
|
|
52
52
|
}
|
|
53
53
|
export declare class BadGatewayException extends HttpException {
|
|
54
|
-
constructor(message?: string
|
|
54
|
+
constructor(message?: string | object);
|
|
55
55
|
}
|
|
56
56
|
export declare class ServiceUnavailableException extends HttpException {
|
|
57
|
-
constructor(message?: string
|
|
57
|
+
constructor(message?: string | object);
|
|
58
58
|
}
|
|
59
59
|
export declare class GatewayTimeoutException extends HttpException {
|
|
60
|
-
constructor(message?: string
|
|
60
|
+
constructor(message?: string | object);
|
|
61
61
|
}
|
|
@@ -1,128 +1,134 @@
|
|
|
1
1
|
export class HttpException extends Error {
|
|
2
2
|
statusCode;
|
|
3
|
-
|
|
4
|
-
constructor(
|
|
5
|
-
|
|
3
|
+
_response;
|
|
4
|
+
constructor(response, statusCode){
|
|
5
|
+
const message = typeof response === 'string' ? response : JSON.stringify(response);
|
|
6
|
+
super(message);
|
|
6
7
|
this.name = 'HttpException';
|
|
8
|
+
this.statusCode = statusCode;
|
|
9
|
+
this._response = response;
|
|
7
10
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
8
11
|
}
|
|
9
12
|
getStatus() {
|
|
10
13
|
return this.statusCode;
|
|
11
14
|
}
|
|
12
15
|
getResponse() {
|
|
13
|
-
|
|
16
|
+
if (typeof this._response === 'object' && this._response !== null) {
|
|
17
|
+
return this._response;
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
14
20
|
statusCode: this.statusCode,
|
|
15
|
-
message: this.
|
|
21
|
+
message: this._response
|
|
16
22
|
};
|
|
17
23
|
}
|
|
18
24
|
}
|
|
19
25
|
// 4xx
|
|
20
26
|
export class BadRequestException extends HttpException {
|
|
21
|
-
constructor(message = 'Bad Request'
|
|
22
|
-
super(message, 400
|
|
27
|
+
constructor(message = 'Bad Request'){
|
|
28
|
+
super(message, 400);
|
|
23
29
|
this.name = 'BadRequestException';
|
|
24
30
|
}
|
|
25
31
|
}
|
|
26
32
|
export class UnauthorizedException extends HttpException {
|
|
27
|
-
constructor(message = 'Unauthorized'
|
|
28
|
-
super(message, 401
|
|
33
|
+
constructor(message = 'Unauthorized'){
|
|
34
|
+
super(message, 401);
|
|
29
35
|
this.name = 'UnauthorizedException';
|
|
30
36
|
}
|
|
31
37
|
}
|
|
32
38
|
export class ForbiddenException extends HttpException {
|
|
33
|
-
constructor(message = 'Forbidden'
|
|
34
|
-
super(message, 403
|
|
39
|
+
constructor(message = 'Forbidden'){
|
|
40
|
+
super(message, 403);
|
|
35
41
|
this.name = 'ForbiddenException';
|
|
36
42
|
}
|
|
37
43
|
}
|
|
38
44
|
export class NotFoundException extends HttpException {
|
|
39
|
-
constructor(message = 'Not Found'
|
|
40
|
-
super(message, 404
|
|
45
|
+
constructor(message = 'Not Found'){
|
|
46
|
+
super(message, 404);
|
|
41
47
|
this.name = 'NotFoundException';
|
|
42
48
|
}
|
|
43
49
|
}
|
|
44
50
|
export class MethodNotAllowedException extends HttpException {
|
|
45
|
-
constructor(message = 'Method Not Allowed'
|
|
46
|
-
super(message, 405
|
|
51
|
+
constructor(message = 'Method Not Allowed'){
|
|
52
|
+
super(message, 405);
|
|
47
53
|
this.name = 'MethodNotAllowedException';
|
|
48
54
|
}
|
|
49
55
|
}
|
|
50
56
|
export class NotAcceptableException extends HttpException {
|
|
51
|
-
constructor(message = 'Not Acceptable'
|
|
52
|
-
super(message, 406
|
|
57
|
+
constructor(message = 'Not Acceptable'){
|
|
58
|
+
super(message, 406);
|
|
53
59
|
this.name = 'NotAcceptableException';
|
|
54
60
|
}
|
|
55
61
|
}
|
|
56
62
|
export class RequestTimeoutException extends HttpException {
|
|
57
|
-
constructor(message = 'Request Timeout'
|
|
58
|
-
super(message, 408
|
|
63
|
+
constructor(message = 'Request Timeout'){
|
|
64
|
+
super(message, 408);
|
|
59
65
|
this.name = 'RequestTimeoutException';
|
|
60
66
|
}
|
|
61
67
|
}
|
|
62
68
|
export class ConflictException extends HttpException {
|
|
63
|
-
constructor(message = 'Conflict'
|
|
64
|
-
super(message, 409
|
|
69
|
+
constructor(message = 'Conflict'){
|
|
70
|
+
super(message, 409);
|
|
65
71
|
this.name = 'ConflictException';
|
|
66
72
|
}
|
|
67
73
|
}
|
|
68
74
|
export class GoneException extends HttpException {
|
|
69
|
-
constructor(message = 'Gone'
|
|
70
|
-
super(message, 410
|
|
75
|
+
constructor(message = 'Gone'){
|
|
76
|
+
super(message, 410);
|
|
71
77
|
this.name = 'GoneException';
|
|
72
78
|
}
|
|
73
79
|
}
|
|
74
80
|
export class PayloadTooLargeException extends HttpException {
|
|
75
|
-
constructor(message = 'Payload Too Large'
|
|
76
|
-
super(message, 413
|
|
81
|
+
constructor(message = 'Payload Too Large'){
|
|
82
|
+
super(message, 413);
|
|
77
83
|
this.name = 'PayloadTooLargeException';
|
|
78
84
|
}
|
|
79
85
|
}
|
|
80
86
|
export class UnsupportedMediaTypeException extends HttpException {
|
|
81
|
-
constructor(message = 'Unsupported Media Type'
|
|
82
|
-
super(message, 415
|
|
87
|
+
constructor(message = 'Unsupported Media Type'){
|
|
88
|
+
super(message, 415);
|
|
83
89
|
this.name = 'UnsupportedMediaTypeException';
|
|
84
90
|
}
|
|
85
91
|
}
|
|
86
92
|
export class UnprocessableEntityException extends HttpException {
|
|
87
|
-
constructor(message = 'Unprocessable Entity'
|
|
88
|
-
super(message, 422
|
|
93
|
+
constructor(message = 'Unprocessable Entity'){
|
|
94
|
+
super(message, 422);
|
|
89
95
|
this.name = 'UnprocessableEntityException';
|
|
90
96
|
}
|
|
91
97
|
}
|
|
92
98
|
export class TooManyRequestsException extends HttpException {
|
|
93
|
-
constructor(message = 'Too Many Requests'
|
|
94
|
-
super(message, 429
|
|
99
|
+
constructor(message = 'Too Many Requests'){
|
|
100
|
+
super(message, 429);
|
|
95
101
|
this.name = 'TooManyRequestsException';
|
|
96
102
|
}
|
|
97
103
|
}
|
|
98
104
|
// 5xx
|
|
99
105
|
export class InternalServerErrorException extends HttpException {
|
|
100
|
-
constructor(message = 'Internal Server Error'
|
|
101
|
-
super(message, 500
|
|
106
|
+
constructor(message = 'Internal Server Error'){
|
|
107
|
+
super(message, 500);
|
|
102
108
|
this.name = 'InternalServerErrorException';
|
|
103
109
|
}
|
|
104
110
|
}
|
|
105
111
|
export class NotImplementedException extends HttpException {
|
|
106
|
-
constructor(message = 'Not Implemented'
|
|
107
|
-
super(message, 501
|
|
112
|
+
constructor(message = 'Not Implemented'){
|
|
113
|
+
super(message, 501);
|
|
108
114
|
this.name = 'NotImplementedException';
|
|
109
115
|
}
|
|
110
116
|
}
|
|
111
117
|
export class BadGatewayException extends HttpException {
|
|
112
|
-
constructor(message = 'Bad Gateway'
|
|
113
|
-
super(message, 502
|
|
118
|
+
constructor(message = 'Bad Gateway'){
|
|
119
|
+
super(message, 502);
|
|
114
120
|
this.name = 'BadGatewayException';
|
|
115
121
|
}
|
|
116
122
|
}
|
|
117
123
|
export class ServiceUnavailableException extends HttpException {
|
|
118
|
-
constructor(message = 'Service Unavailable'
|
|
119
|
-
super(message, 503
|
|
124
|
+
constructor(message = 'Service Unavailable'){
|
|
125
|
+
super(message, 503);
|
|
120
126
|
this.name = 'ServiceUnavailableException';
|
|
121
127
|
}
|
|
122
128
|
}
|
|
123
129
|
export class GatewayTimeoutException extends HttpException {
|
|
124
|
-
constructor(message = 'Gateway Timeout'
|
|
125
|
-
super(message, 504
|
|
130
|
+
constructor(message = 'Gateway Timeout'){
|
|
131
|
+
super(message, 504);
|
|
126
132
|
this.name = 'GatewayTimeoutException';
|
|
127
133
|
}
|
|
128
134
|
}
|
package/dist/factory.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { VelaApplication } from "./application.js";
|
|
2
2
|
import { Container } from "./container/container.js";
|
|
3
|
+
import { ModuleRef } from "./container/module-ref.js";
|
|
3
4
|
import { RouteManager } from "./http/route.manager.js";
|
|
4
5
|
import { ModuleLoader } from "./module/module-loader.js";
|
|
5
6
|
import { ComponentManager } from "./pipeline/component.manager.js";
|
|
@@ -8,9 +9,16 @@ export const VelaFactory = {
|
|
|
8
9
|
async create (rootModule) {
|
|
9
10
|
const container = new Container();
|
|
10
11
|
container.register({
|
|
11
|
-
|
|
12
|
+
provide: Container,
|
|
12
13
|
useValue: container
|
|
13
14
|
});
|
|
15
|
+
container.register({
|
|
16
|
+
provide: ModuleRef,
|
|
17
|
+
useFactory: (c)=>new ModuleRef(c),
|
|
18
|
+
inject: [
|
|
19
|
+
Container
|
|
20
|
+
]
|
|
21
|
+
});
|
|
14
22
|
const routeManager = new RouteManager(container);
|
|
15
23
|
ComponentManager.init(container);
|
|
16
24
|
const loader = new ModuleLoader(container, routeManager);
|
|
@@ -45,8 +53,9 @@ export const VelaFactory = {
|
|
|
45
53
|
} else if (container.has(APP_MIDDLEWARE)) {
|
|
46
54
|
routeManager.useGlobalMiddlewareTokens(APP_MIDDLEWARE);
|
|
47
55
|
}
|
|
56
|
+
routeManager.registerConsumerMiddleware(loader.getConsumerMiddlewareDefinitions());
|
|
48
57
|
const app = new VelaApplication(container, routeManager);
|
|
49
|
-
const instances = loader.resolveAllInstances();
|
|
58
|
+
const instances = await loader.resolveAllInstances();
|
|
50
59
|
app.setInstances(instances);
|
|
51
60
|
await app.callOnModuleInit();
|
|
52
61
|
await app.callOnApplicationBootstrap();
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { AsyncModuleOptions, DynamicModule } from '../module/types';
|
|
2
|
+
import type { HttpModuleOptions } from './fetch.types';
|
|
3
|
+
export declare class HttpModule {
|
|
4
|
+
static register(options?: HttpModuleOptions): DynamicModule;
|
|
5
|
+
static registerAsync(options: AsyncModuleOptions<HttpModuleOptions>): DynamicModule;
|
|
6
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
}
|
|
7
|
+
import { METADATA_KEYS } from "../constants.js";
|
|
8
|
+
import { defineMetadata } from "../metadata.js";
|
|
9
|
+
import { MetadataRegistry } from "../registry/metadata.registry.js";
|
|
10
|
+
import { Module } from "../module/decorators.js";
|
|
11
|
+
import { HttpService, HTTP_MODULE_OPTIONS } from "./fetch.service.js";
|
|
12
|
+
export class HttpModule {
|
|
13
|
+
static register(options = {}) {
|
|
14
|
+
const moduleClass = class HttpDynamicModule {
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(moduleClass, 'name', {
|
|
17
|
+
value: 'HttpModule'
|
|
18
|
+
});
|
|
19
|
+
defineMetadata(METADATA_KEYS.MODULE, true, moduleClass);
|
|
20
|
+
MetadataRegistry.setModuleOptions(moduleClass, {
|
|
21
|
+
exports: [
|
|
22
|
+
HttpService,
|
|
23
|
+
HTTP_MODULE_OPTIONS
|
|
24
|
+
]
|
|
25
|
+
});
|
|
26
|
+
return {
|
|
27
|
+
module: moduleClass,
|
|
28
|
+
providers: [
|
|
29
|
+
{
|
|
30
|
+
provide: HTTP_MODULE_OPTIONS,
|
|
31
|
+
useValue: options
|
|
32
|
+
},
|
|
33
|
+
HttpService
|
|
34
|
+
]
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
static registerAsync(options) {
|
|
38
|
+
const moduleClass = class HttpAsyncDynamicModule {
|
|
39
|
+
};
|
|
40
|
+
Object.defineProperty(moduleClass, 'name', {
|
|
41
|
+
value: 'HttpModule'
|
|
42
|
+
});
|
|
43
|
+
defineMetadata(METADATA_KEYS.MODULE, true, moduleClass);
|
|
44
|
+
MetadataRegistry.setModuleOptions(moduleClass, {
|
|
45
|
+
imports: options.imports ?? [],
|
|
46
|
+
exports: [
|
|
47
|
+
HttpService,
|
|
48
|
+
HTTP_MODULE_OPTIONS
|
|
49
|
+
]
|
|
50
|
+
});
|
|
51
|
+
return {
|
|
52
|
+
module: moduleClass,
|
|
53
|
+
providers: [
|
|
54
|
+
{
|
|
55
|
+
provide: HTTP_MODULE_OPTIONS,
|
|
56
|
+
useFactory: options.useFactory,
|
|
57
|
+
inject: options.inject ?? []
|
|
58
|
+
},
|
|
59
|
+
HttpService
|
|
60
|
+
]
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
HttpModule = _ts_decorate([
|
|
65
|
+
Module({
|
|
66
|
+
providers: [
|
|
67
|
+
{
|
|
68
|
+
provide: HTTP_MODULE_OPTIONS,
|
|
69
|
+
useValue: {}
|
|
70
|
+
},
|
|
71
|
+
HttpService
|
|
72
|
+
],
|
|
73
|
+
exports: [
|
|
74
|
+
HttpService
|
|
75
|
+
]
|
|
76
|
+
})
|
|
77
|
+
], HttpModule);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { InjectionToken } from '../container/index';
|
|
2
|
+
import type { HttpModuleOptions, HttpRequestConfig, HttpResponse, RequestConfig } from './fetch.types';
|
|
3
|
+
export declare const HTTP_MODULE_OPTIONS: InjectionToken<HttpModuleOptions>;
|
|
4
|
+
export declare class HttpRequestException extends Error {
|
|
5
|
+
readonly status: number;
|
|
6
|
+
readonly statusText: string;
|
|
7
|
+
readonly response: Response;
|
|
8
|
+
constructor(status: number, statusText: string, response: Response);
|
|
9
|
+
}
|
|
10
|
+
export declare class HttpService {
|
|
11
|
+
private readonly baseURL;
|
|
12
|
+
private readonly defaultHeaders;
|
|
13
|
+
private readonly defaultTimeout;
|
|
14
|
+
constructor(options: HttpModuleOptions);
|
|
15
|
+
get<T = unknown>(url: string, config?: RequestConfig): Promise<HttpResponse<T>>;
|
|
16
|
+
post<T = unknown>(url: string, body?: unknown, config?: RequestConfig): Promise<HttpResponse<T>>;
|
|
17
|
+
put<T = unknown>(url: string, body?: unknown, config?: RequestConfig): Promise<HttpResponse<T>>;
|
|
18
|
+
patch<T = unknown>(url: string, body?: unknown, config?: RequestConfig): Promise<HttpResponse<T>>;
|
|
19
|
+
delete<T = unknown>(url: string, config?: RequestConfig): Promise<HttpResponse<T>>;
|
|
20
|
+
head(url: string, config?: RequestConfig): Promise<HttpResponse<void>>;
|
|
21
|
+
request<T = unknown>(config: HttpRequestConfig): Promise<HttpResponse<T>>;
|
|
22
|
+
private buildUrl;
|
|
23
|
+
private parseBody;
|
|
24
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
}
|
|
7
|
+
function _ts_metadata(k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
}
|
|
10
|
+
function _ts_param(paramIndex, decorator) {
|
|
11
|
+
return function(target, key) {
|
|
12
|
+
decorator(target, key, paramIndex);
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
import { Inject, Injectable, InjectionToken, Optional } from "../container/index.js";
|
|
16
|
+
export const HTTP_MODULE_OPTIONS = new InjectionToken('HTTP_MODULE_OPTIONS');
|
|
17
|
+
export class HttpRequestException extends Error {
|
|
18
|
+
status;
|
|
19
|
+
statusText;
|
|
20
|
+
response;
|
|
21
|
+
constructor(status, statusText, response){
|
|
22
|
+
super(`HTTP ${status} ${statusText}`), this.status = status, this.statusText = statusText, this.response = response;
|
|
23
|
+
this.name = 'HttpRequestException';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export class HttpService {
|
|
27
|
+
baseURL;
|
|
28
|
+
defaultHeaders;
|
|
29
|
+
defaultTimeout;
|
|
30
|
+
constructor(options){
|
|
31
|
+
const opts = options ?? {};
|
|
32
|
+
this.baseURL = opts.baseURL ?? '';
|
|
33
|
+
this.defaultHeaders = opts.headers ?? {};
|
|
34
|
+
this.defaultTimeout = opts.timeout;
|
|
35
|
+
}
|
|
36
|
+
get(url, config) {
|
|
37
|
+
return this.request({
|
|
38
|
+
method: 'GET',
|
|
39
|
+
url,
|
|
40
|
+
...config
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
post(url, body, config) {
|
|
44
|
+
return this.request({
|
|
45
|
+
method: 'POST',
|
|
46
|
+
url,
|
|
47
|
+
body,
|
|
48
|
+
...config
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
put(url, body, config) {
|
|
52
|
+
return this.request({
|
|
53
|
+
method: 'PUT',
|
|
54
|
+
url,
|
|
55
|
+
body,
|
|
56
|
+
...config
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
patch(url, body, config) {
|
|
60
|
+
return this.request({
|
|
61
|
+
method: 'PATCH',
|
|
62
|
+
url,
|
|
63
|
+
body,
|
|
64
|
+
...config
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
delete(url, config) {
|
|
68
|
+
return this.request({
|
|
69
|
+
method: 'DELETE',
|
|
70
|
+
url,
|
|
71
|
+
...config
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
head(url, config) {
|
|
75
|
+
return this.request({
|
|
76
|
+
method: 'HEAD',
|
|
77
|
+
url,
|
|
78
|
+
...config
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
async request(config) {
|
|
82
|
+
const fullUrl = this.buildUrl(config.url, config.params);
|
|
83
|
+
const headers = {
|
|
84
|
+
...this.defaultHeaders,
|
|
85
|
+
...config.headers
|
|
86
|
+
};
|
|
87
|
+
const init = {
|
|
88
|
+
method: config.method,
|
|
89
|
+
headers
|
|
90
|
+
};
|
|
91
|
+
if (config.body !== undefined) {
|
|
92
|
+
if (typeof config.body === 'string' || config.body instanceof FormData) {
|
|
93
|
+
init.body = config.body;
|
|
94
|
+
} else {
|
|
95
|
+
init.body = JSON.stringify(config.body);
|
|
96
|
+
if (!headers['Content-Type'] && !headers['content-type']) {
|
|
97
|
+
headers['Content-Type'] = 'application/json';
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const timeout = config.timeout ?? this.defaultTimeout;
|
|
102
|
+
if (timeout !== undefined) {
|
|
103
|
+
init.signal = AbortSignal.timeout(timeout);
|
|
104
|
+
}
|
|
105
|
+
const response = await fetch(fullUrl, init);
|
|
106
|
+
if (!response.ok) {
|
|
107
|
+
throw new HttpRequestException(response.status, response.statusText, response);
|
|
108
|
+
}
|
|
109
|
+
const data = await this.parseBody(response);
|
|
110
|
+
return {
|
|
111
|
+
data,
|
|
112
|
+
status: response.status,
|
|
113
|
+
statusText: response.statusText,
|
|
114
|
+
headers: response.headers
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
buildUrl(url, params) {
|
|
118
|
+
const base = this.baseURL ? `${this.baseURL}${url}` : url;
|
|
119
|
+
if (!params || Object.keys(params).length === 0) return base;
|
|
120
|
+
const searchParams = new URLSearchParams(Object.entries(params).map(([k, v])=>[
|
|
121
|
+
k,
|
|
122
|
+
String(v)
|
|
123
|
+
]));
|
|
124
|
+
return `${base}?${searchParams.toString()}`;
|
|
125
|
+
}
|
|
126
|
+
async parseBody(response) {
|
|
127
|
+
if (response.status === 204 || response.headers.get('content-length') === '0') {
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
const contentType = response.headers.get('content-type') ?? '';
|
|
131
|
+
if (contentType.includes('application/json')) {
|
|
132
|
+
return response.json();
|
|
133
|
+
}
|
|
134
|
+
return response.text();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
HttpService = _ts_decorate([
|
|
138
|
+
Injectable(),
|
|
139
|
+
_ts_param(0, Optional()),
|
|
140
|
+
_ts_param(0, Inject(HTTP_MODULE_OPTIONS)),
|
|
141
|
+
_ts_metadata("design:type", Function),
|
|
142
|
+
_ts_metadata("design:paramtypes", [
|
|
143
|
+
typeof HttpModuleOptions === "undefined" ? Object : HttpModuleOptions
|
|
144
|
+
])
|
|
145
|
+
], HttpService);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface HttpModuleOptions {
|
|
2
|
+
/** Base URL prepended to all requests. */
|
|
3
|
+
baseURL?: string;
|
|
4
|
+
/** Default headers sent with every request. */
|
|
5
|
+
headers?: Record<string, string>;
|
|
6
|
+
/** Default timeout in milliseconds (uses AbortSignal.timeout). */
|
|
7
|
+
timeout?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface RequestConfig {
|
|
10
|
+
headers?: Record<string, string>;
|
|
11
|
+
params?: Record<string, string | number | boolean>;
|
|
12
|
+
timeout?: number;
|
|
13
|
+
}
|
|
14
|
+
export interface HttpRequestConfig extends RequestConfig {
|
|
15
|
+
method: string;
|
|
16
|
+
url: string;
|
|
17
|
+
body?: unknown;
|
|
18
|
+
}
|
|
19
|
+
export interface HttpResponse<T = unknown> {
|
|
20
|
+
data: T;
|
|
21
|
+
status: number;
|
|
22
|
+
statusText: string;
|
|
23
|
+
headers: Headers;
|
|
24
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -19,7 +19,7 @@ export class HealthCheckService {
|
|
|
19
19
|
error: {},
|
|
20
20
|
details: {}
|
|
21
21
|
};
|
|
22
|
-
throw new ServiceUnavailableException(
|
|
22
|
+
throw new ServiceUnavailableException(result);
|
|
23
23
|
}
|
|
24
24
|
const results = await Promise.allSettled(indicators.map((fn)=>fn()));
|
|
25
25
|
const info = {};
|
|
@@ -55,7 +55,7 @@ export class HealthCheckService {
|
|
|
55
55
|
details
|
|
56
56
|
};
|
|
57
57
|
if (status === 'error') {
|
|
58
|
-
throw new ServiceUnavailableException(
|
|
58
|
+
throw new ServiceUnavailableException(checkResult);
|
|
59
59
|
}
|
|
60
60
|
return checkResult;
|
|
61
61
|
}
|