forked from nest-cloud/nestcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.explorer.ts
64 lines (58 loc) · 2.46 KB
/
http.explorer.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
63
64
import { Injectable, OnModuleInit } from '@nestjs/common';
import { DiscoveryService } from '@nestjs/core';
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
import { HttpOrchestrator } from './http.orchestrator';
import { HttpMetadataAccessor } from './http-metadata.accessor';
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
import { AxiosRequestConfig } from 'axios';
@Injectable()
export class HttpExplorer implements OnModuleInit {
constructor(
private readonly discoveryService: DiscoveryService,
private readonly metadataAccessor: HttpMetadataAccessor,
private readonly httpOrchestrator: HttpOrchestrator,
private readonly metadataScanner: MetadataScanner,
) {
}
async onModuleInit() {
this.explore();
await this.httpOrchestrator.mountDecoratorRequests();
}
explore() {
const providers: InstanceWrapper[] = this.discoveryService.getProviders();
providers.forEach((wrapper: InstanceWrapper) => {
const { instance } = wrapper;
if (!instance || typeof instance === 'string') {
return;
}
this.metadataScanner.scanFromPrototype(
instance,
Object.getPrototypeOf(instance),
(key: string) => this.lookupRequests(instance, key),
);
});
}
lookupRequests(instance: Function, key: string) {
const target = instance[key];
const options: AxiosRequestConfig = this.metadataAccessor.getOptions(instance, target) || {};
options.url = this.metadataAccessor.getUrl(instance, target);
options.method = this.metadataAccessor.getMethod(instance, target);
const responseConfig = this.metadataAccessor.getResponseConfig(instance, target);
const paramsMetadata = this.metadataAccessor.getParams(instance, key);
const serviceName = this.metadataAccessor.getService(instance, target);
const InterceptorRefs = this.metadataAccessor.getInterceptorRefs(instance, target) || [];
const FallbackRef = this.metadataAccessor.getFallbackRef(instance, target);
if (options.url) {
this.httpOrchestrator.addDecoratorRequests(
instance,
key,
options,
responseConfig,
paramsMetadata,
serviceName,
FallbackRef,
InterceptorRefs,
);
}
}
}