-
Notifications
You must be signed in to change notification settings - Fork 0
/
ory-base.module.ts
140 lines (131 loc) · 3.64 KB
/
ory-base.module.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { HttpModule, type HttpModuleOptions, HttpService } from '@nestjs/axios';
import {
type DynamicModule,
Module,
type Provider,
type Type,
} from '@nestjs/common';
import axios from 'axios';
import {
type AxiosExtraRequestConfig,
type IOryBaseModuleOptions,
type OryBaseModuleAsyncOptions,
OryBaseModuleOptions,
type OryBaseModuleOptionsFactory,
} from './ory-base.interfaces';
import { OryBaseService } from './ory-base.service';
declare module 'axios' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface AxiosRequestConfig extends AxiosExtraRequestConfig {}
}
const httpModuleOptionsFactory = (
options: IOryBaseModuleOptions
): HttpModuleOptions => ({
timeout: 5000,
responseType: 'json',
validateStatus(status: number) {
return status >= 200 && status < 300;
},
retries: 3,
retryCondition(error) {
const statusToRetry = [429];
return error.response?.status
? statusToRetry.includes(error.response?.status)
: false;
},
retryDelay(error, retryCount) {
if (error.response?.status === 429) {
const headers = error.response.headers;
const remaining = headers['x-ratelimit-remaining'];
const resetTimestamp = headers['x-ratelimit-reset'];
if (Number(remaining) === 0) {
return Number(resetTimestamp) * 1000 - Date.now();
}
}
return retryCount * 250;
},
...options.axiosConfig,
});
@Module({})
export class OryBaseModule {
static forRoot(
options: IOryBaseModuleOptions,
isGlobal?: boolean
): DynamicModule {
const providers = [
{ provide: OryBaseModuleOptions, useValue: options },
OryBaseService,
];
return {
module: OryBaseModule,
imports: [HttpModule.register(httpModuleOptionsFactory(options))],
providers,
exports: [OryBaseService],
global: isGlobal,
};
}
static forRootAsync(
options: OryBaseModuleAsyncOptions,
isGlobal?: boolean
): DynamicModule {
const providers: Provider[] = [
...this.createAsyncProviders(options),
OryBaseService,
{
provide: HttpService,
useFactory: (options: OryBaseModuleOptions) => {
const axiosInstance = axios.create(httpModuleOptionsFactory(options));
return new HttpService(axiosInstance);
},
inject: [OryBaseModuleOptions],
},
];
return {
module: OryBaseModule,
imports: options.imports ?? [],
providers,
exports: [OryBaseService],
global: isGlobal,
};
}
private static createAsyncProviders(
options: OryBaseModuleAsyncOptions
): Provider[] {
if (options.useExisting || options.useFactory) {
return [this.createAsyncOptionsProvider(options)];
}
if (options.useClass) {
return [
this.createAsyncOptionsProvider(options),
{
provide: options.useClass,
useClass: options.useClass,
},
];
}
throw new Error('Invalid OryBaseModuleAsyncOptions');
}
private static createAsyncOptionsProvider(
options: OryBaseModuleAsyncOptions
): Provider {
if (options.useFactory) {
return {
provide: OryBaseModuleOptions,
useFactory: options.useFactory,
inject: options.inject ?? [],
};
}
if (!options.useExisting && !options.useClass) {
throw new Error('Invalid OryBaseModuleAsyncOptions');
}
return {
provide: OryBaseModuleOptions,
useFactory: (optionsFactory: OryBaseModuleOptionsFactory) =>
optionsFactory.createOptions(),
inject: [
(options.useExisting ??
options.useClass) as Type<OryBaseModuleOptionsFactory>,
],
};
}
}