-
Notifications
You must be signed in to change notification settings - Fork 4
/
body-parser.extension.ts
62 lines (54 loc) · 2.6 KB
/
body-parser.extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import {
Extension,
ExtensionsManager,
HTTP_INTERCEPTORS,
PerAppService,
injectable,
InjectionToken,
} from '@ditsmod/core';
import { ROUTES_EXTENSIONS } from '@ditsmod/routing';
import { BodyParserConfig } from './body-parser-config.js';
import { BodyParserInterceptor } from './body-parser.interceptor.js';
import { SingletonBodyParserInterceptor } from './singleton-body-parser.interceptor.js';
export const BODY_PARSER_EXTENSIONS = new InjectionToken<Extension<void>[]>('BODY_PARSER_EXTENSIONS');
@injectable()
export class BodyParserExtension implements Extension<void> {
private inited: boolean;
constructor(
protected extensionManager: ExtensionsManager,
protected perAppService: PerAppService,
) {}
async init() {
if (this.inited) {
return;
}
const aMetadataPerMod2 = await this.extensionManager.init(ROUTES_EXTENSIONS);
aMetadataPerMod2.forEach((metadataPerMod2) => {
const { aControllersMetadata2, providersPerMod } = metadataPerMod2;
aControllersMetadata2.forEach(({ providersPerRou, providersPerReq, httpMethod, isSingleton }) => {
// Merging the providers from a module and a controller
const mergedProvidersPerRou = [...metadataPerMod2.providersPerRou, ...providersPerRou];
const mergedProvidersPerReq = [...metadataPerMod2.providersPerReq, ...providersPerReq];
// Creating a hierarchy of injectors.
const injectorPerApp = this.perAppService.injector;
const injectorPerMod = injectorPerApp.resolveAndCreateChild(providersPerMod);
const injectorPerRou = injectorPerMod.resolveAndCreateChild(mergedProvidersPerRou);
if (isSingleton) {
let bodyParserConfig = injectorPerRou.get(BodyParserConfig, undefined, {}) as BodyParserConfig;
bodyParserConfig = { ...new BodyParserConfig(), ...bodyParserConfig }; // Merge with default.
if (bodyParserConfig.acceptMethods!.includes(httpMethod)) {
providersPerRou.push({ token: HTTP_INTERCEPTORS, useClass: SingletonBodyParserInterceptor, multi: true });
}
} else {
const injectorPerReq = injectorPerRou.resolveAndCreateChild(mergedProvidersPerReq);
let bodyParserConfig = injectorPerReq.get(BodyParserConfig, undefined, {}) as BodyParserConfig;
bodyParserConfig = { ...new BodyParserConfig(), ...bodyParserConfig }; // Merge with default.
if (bodyParserConfig.acceptMethods!.includes(httpMethod)) {
providersPerReq.push({ token: HTTP_INTERCEPTORS, useClass: BodyParserInterceptor, multi: true });
}
}
});
});
this.inited = true;
}
}