Skip to content

Commit d399bcc

Browse files
authored
Merge pull request #5 from TassoDigital/add-keep-alive
Pass lambda to configure http options from outside.
2 parents 3f296f9 + 1e5760e commit d399bcc

File tree

2 files changed

+6
-54
lines changed

2 files changed

+6
-54
lines changed
Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { DynamicModule, Module, Global } from '@nestjs/common';
22
import { HttpService, HttpModule, HttpModuleOptions } from '@nestjs/axios';
33
import { Configuration } from './configuration';
4-
import { Agent } from "https";
5-
import * as fs from 'fs';
6-
import {REQUEST} from "@nestjs/core";
74

85

96
{{#apiInfo}}
@@ -12,9 +9,6 @@ import { {{classname}} } from './{{importPath}}';
129
{{/apis}}
1310
{{/apiInfo}}
1411

15-
let cachedOptions: HttpModuleOptions | undefined = undefined;
16-
let nextOptionsRefresh: number = 0;
17-
1812
@Global()
1913
@Module({
2014
imports: [ ],
@@ -34,48 +28,12 @@ export class ApiModule {
3428
providers: [ { provide: Configuration, useFactory: configurationFactory } ],
3529
imports: [
3630
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+
})
5233
],
5334
exports: [HttpModule]
5435
};
5536
}
5637

5738
constructor( httpService: HttpService) { }
5839
}
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-
};

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpModuleOptionsFactory } from "@nestjs/axios";
1+
import {HttpModuleOptions} from "@nestjs/axios";
22

33
export interface ConfigurationParameters {
44
apiKeys?: {[ key: string ]: string};
@@ -7,9 +7,7 @@ export interface ConfigurationParameters {
77
accessToken?: string | (() => string);
88
basePath?: string;
99
withCredentials?: boolean;
10-
certPath?: string;
11-
certKeyPath?: string;
12-
caPath?:string;
10+
httpModuleOptionsFactory?: ()=> Promise<HttpModuleOptions>;
1311
}
1412

1513
export class Configuration {
@@ -19,9 +17,7 @@ export class Configuration {
1917
accessToken?: string | (() => string);
2018
basePath?: string;
2119
withCredentials?: boolean;
22-
certPath?: string;
23-
certKeyPath?: string;
24-
caPath?:string;
20+
httpModuleOptionsFactory?: () => Promise<HttpModuleOptions>;
2521

2622
constructor(configurationParameters: ConfigurationParameters = {}) {
2723
this.apiKeys = configurationParameters.apiKeys;
@@ -30,9 +26,7 @@ export class Configuration {
3026
this.accessToken = configurationParameters.accessToken;
3127
this.basePath = configurationParameters.basePath;
3228
this.withCredentials = configurationParameters.withCredentials;
33-
this.certPath = configurationParameters.certPath;
34-
this.certKeyPath = configurationParameters.certKeyPath;
35-
this.caPath = configurationParameters.caPath;
29+
this.httpModuleOptionsFactory = configurationParameters.httpModuleOptionsFactory;
3630
}
3731

3832
/**

0 commit comments

Comments
 (0)