@@ -3,13 +3,18 @@ import { HttpService, HttpModule, HttpModuleOptions } from '@nestjs/axios';
3
3
import { Configuration } from './configuration';
4
4
import { Agent } from "https";
5
5
import * as fs from 'fs';
6
+ import { REQUEST} from "@nestjs/core";
7
+
6
8
7
9
{ {#apiInfo} }
8
10
{ {#apis} }
9
11
import { {{classname} } } from './{ {importPath} }';
10
12
{ {/apis} }
11
13
{ {/apiInfo} }
12
14
15
+ let cachedOptions: HttpModuleOptions | undefined = undefined;
16
+ let nextOptionsRefresh: number = 0;
17
+
13
18
@Global()
14
19
@Module({
15
20
imports: [ ],
@@ -29,12 +34,20 @@ export class ApiModule {
29
34
providers: [ { provide: Configuration, useFactory: configurationFactory } ],
30
35
imports: [
31
36
HttpModule.registerAsync({
32
- useFactory: () =>
33
- createHttpOptions(
34
- configurationFactory().certPath,
35
- configurationFactory().certKeyPath,
36
- configurationFactory().caPath
37
- ),
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]
38
51
}),
39
52
],
40
53
exports: [HttpModule]
0 commit comments