Skip to content

Commit 3f296f9

Browse files
authored
Merge pull request #4 from TassoDigital/refresh-tls-certs
Fix for refreshing short-lived TLS certs. Not configurable in this s…
2 parents 52af079 + b295263 commit 3f296f9

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

modules/openapi-generator/src/main/resources/typescript-nestjs/api.module.mustache

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ import { HttpService, HttpModule, HttpModuleOptions } from '@nestjs/axios';
33
import { Configuration } from './configuration';
44
import { Agent } from "https";
55
import * as fs from 'fs';
6+
import {REQUEST} from "@nestjs/core";
7+
68

79
{{#apiInfo}}
810
{{#apis}}
911
import { {{classname}} } from './{{importPath}}';
1012
{{/apis}}
1113
{{/apiInfo}}
1214

15+
let cachedOptions: HttpModuleOptions | undefined = undefined;
16+
let nextOptionsRefresh: number = 0;
17+
1318
@Global()
1419
@Module({
1520
imports: [ ],
@@ -29,12 +34,20 @@ export class ApiModule {
2934
providers: [ { provide: Configuration, useFactory: configurationFactory } ],
3035
imports: [
3136
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]
3851
}),
3952
],
4053
exports: [HttpModule]

0 commit comments

Comments
 (0)