|
1 | 1 |
|
2 | | -import type { Middleware, NextHandler, Request, Response } from 'jitar'; |
| 2 | +import type { Middleware, NextHandler, Request } from 'jitar'; |
| 3 | +import { Response } from 'jitar'; |
3 | 4 |
|
| 5 | +import type { Tenant } from '^/domain/tenant'; |
| 6 | +import getByOrigin from '^/domain/tenant/getByOriginConverted'; |
| 7 | + |
| 8 | +const GEY_BY_ORIGIN_FQN = 'domain/tenant/getByOriginConverted'; |
4 | 9 | const TENANT_PARAMETER = '*tenant'; |
5 | 10 |
|
6 | 11 | export default class TenantMiddleware implements Middleware |
7 | 12 | { |
8 | | - readonly #cache = new Map<string, Response>(); |
9 | | - readonly #getTenantPath: string; |
10 | | - |
11 | | - constructor(tenantPath: string) |
12 | | - { |
13 | | - this.#getTenantPath = tenantPath; |
14 | | - } |
| 13 | + readonly #tenants = new Map<string, Tenant>(); |
15 | 14 |
|
16 | 15 | async handle(request: Request, next: NextHandler): Promise<Response> |
17 | 16 | { |
18 | | - return request.fqn === this.#getTenantPath |
19 | | - ? this.#getTenant(request, next) |
| 17 | + return request.fqn === GEY_BY_ORIGIN_FQN |
| 18 | + ? this.#getTenant(request) |
20 | 19 | : this.#handleRequest(request, next); |
21 | 20 | } |
22 | 21 |
|
23 | | - async #getTenant(request: Request, next: NextHandler): Promise<Response> |
| 22 | + async #resolveTenant(request: Request): Promise<Tenant> |
24 | 23 | { |
25 | | - const origin = this.#getOrigin(request); |
26 | | - const cached = this.#cache.get(origin); |
| 24 | + const origin = request.getHeader('origin') as string; |
| 25 | + const tenant = this.#tenants.get(origin); |
27 | 26 |
|
28 | | - if (cached === undefined) |
| 27 | + if (tenant === undefined) |
29 | 28 | { |
30 | | - request.setArgument('origin', origin); |
| 29 | + const tenant = await getByOrigin(origin); |
31 | 30 |
|
32 | | - const response = await next(); |
| 31 | + this.#tenants.set(origin, tenant); |
33 | 32 |
|
34 | | - if (response.status === 200) |
35 | | - { |
36 | | - this.#cache.set(origin, response); |
37 | | - } |
38 | | - |
39 | | - return response; |
| 33 | + return tenant; |
40 | 34 | } |
41 | 35 |
|
42 | | - return cached; |
| 36 | + return tenant; |
43 | 37 | } |
44 | 38 |
|
45 | | - async #handleRequest(request: Request, next: NextHandler): Promise<Response> |
| 39 | + async #getTenant(request: Request): Promise<Response> |
46 | 40 | { |
47 | | - const origin = this.#getOrigin(request); |
48 | | - const cached = this.#cache.get(origin); |
49 | | - |
50 | | - if (cached !== undefined) |
51 | | - { |
52 | | - request.setArgument(TENANT_PARAMETER, cached.result); |
53 | | - } |
| 41 | + const tenant = await this.#resolveTenant(request); |
54 | 42 |
|
55 | | - return next(); |
| 43 | + return new Response(200, tenant); |
56 | 44 | } |
57 | 45 |
|
58 | | - #getOrigin(request: Request): string |
| 46 | + async #handleRequest(request: Request, next: NextHandler): Promise<Response> |
59 | 47 | { |
60 | | - return request.getHeader('origin') as string; |
| 48 | + const tenant = await this.#resolveTenant(request); |
| 49 | + |
| 50 | + request.setArgument(TENANT_PARAMETER, tenant); |
| 51 | + |
| 52 | + return next(); |
61 | 53 | } |
62 | 54 | } |
0 commit comments