-
Notifications
You must be signed in to change notification settings - Fork 10
/
FronteggRoutes.ts
58 lines (50 loc) · 1.5 KB
/
FronteggRoutes.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
import axios from 'axios';
import { Request } from 'express';
import { baseUrl } from '../config';
export interface IRoutesConfig {
vendorClientPublicRoutes: Array<{
method: string;
url: string;
description?: string;
withQueryParams?: Array<{
key: string;
value?: string;
}>
}>;
}
class FronteggRoutes {
private routesConfig: undefined | IRoutesConfig;
public async getRoutesConfig() {
if (!this.routesConfig) {
this.routesConfig = await this.fetchRoutesConfig();
}
return this.routesConfig;
}
public async isFronteggPublicRoute(req: Request) {
const { vendorClientPublicRoutes } = await this.getRoutesConfig();
return vendorClientPublicRoutes.some((route) => {
const path = req.path.replace('/', '');
if (path !== route.url) {
return false;
}
if (req.method.toUpperCase() !== route.method.toUpperCase()) {
return false;
}
if (route.withQueryParams) {
const hasAllQueryParams = route.withQueryParams.every(({ key, value }) => {
const queryParamValue = req.query[key];
return queryParamValue && (!value || queryParamValue === value);
});
if (!hasAllQueryParams) {
return false;
}
}
return true;
});
}
private async fetchRoutesConfig(): Promise<IRoutesConfig> {
const response = await axios.get(`${baseUrl}/configs/routes`);
return response.data as IRoutesConfig;
}
}
export const fronteggRoutes = new FronteggRoutes();