1
1
import { DynamicModule, Module, Global } from '@nestjs/common';
2
2
import { HttpService, HttpModule, HttpModuleOptions } from '@nestjs/axios';
3
3
import { Configuration } from './configuration';
4
- import { Agent } from "https";
5
- import * as fs from 'fs';
6
- import { REQUEST} from "@nestjs/core";
7
4
8
5
9
6
{ {#apiInfo} }
@@ -12,9 +9,6 @@ import { {{classname}} } from './{{importPath}}';
12
9
{ {/apis} }
13
10
{ {/apiInfo} }
14
11
15
- let cachedOptions: HttpModuleOptions | undefined = undefined;
16
- let nextOptionsRefresh: number = 0;
17
-
18
12
@Global()
19
13
@Module({
20
14
imports: [ ],
@@ -34,48 +28,12 @@ export class ApiModule {
34
28
providers: [ { provide: Configuration, useFactory: configurationFactory } ],
35
29
imports: [
36
30
HttpModule.registerAsync({
37
- useFactory: (request) => {
38
- if (cachedOptions === undefined || nextOptionsRefresh < new Date().getTime()){
39
- //Refresh certs once per hour or on next request. Should work with AutoCert since they renew 8 hours before the 24 hour cert expires
40
- nextOptionsRefresh = new Date(new Date().getTime() + 60 * 60000).getTime();
41
- cachedOptions = createHttpOptions(
42
- configurationFactory().certPath,
43
- configurationFactory().certKeyPath,
44
- configurationFactory().caPath
45
- )}
46
- return cachedOptions;
47
- },
48
- //abusing inject a bit here, we only use it to be able to reload the cert using the factory above.
49
- //in case of unit tests, we don't do this, as it breaks them
50
- inject: process.env.NODE_ENV !== "production" ? [] : [REQUEST]
51
- }),
31
+ useFactory: configurationFactory().httpModuleOptionsFactory
32
+ } )
52
33
],
53
34
exports: [HttpModule]
54
35
};
55
36
}
56
37
57
38
constructor( httpService: HttpService) { }
58
39
}
59
-
60
- const createHttpOptions = (
61
- certPath?: string,
62
- certKeyPath?: string,
63
- caPath?: string
64
- ): HttpModuleOptions => {
65
- return {
66
- timeout: 5001,
67
- httpsAgent: certKeyPath && certPath && caPath ? getAgent(certPath, certKeyPath, caPath) : undefined,
68
- } ;
69
- };
70
-
71
- const getAgent = (
72
- certPath: string,
73
- certKeyPath: string,
74
- caPath: string
75
- ): Agent | undefined => {
76
- return new Agent({
77
- cert: fs.readFileSync(certPath),
78
- key: fs.readFileSync(certKeyPath),
79
- ca: fs.readFileSync(caPath),
80
- } );
81
- };
0 commit comments