|
1 | | -import type { |
2 | | - Features, |
3 | | - IClientConfiguration, |
4 | | -} from './types/client-configuration'; |
| 1 | +import type { ClientConfiguration } from './types/client-configuration'; |
5 | 2 | import { $ClientConfiguration } from './schemas/client-configuration'; |
| 3 | +import { catchAxiosError, createAxiosClient } from './axios-client'; |
| 4 | +import { Result } from './types/result'; |
6 | 5 |
|
7 | | -export class ClientConfiguration implements IClientConfiguration { |
8 | | - private constructor( |
9 | | - public readonly features: Readonly<Features>, |
10 | | - public readonly campaignId?: string |
11 | | - ) {} |
12 | | - |
13 | | - featureEnabled(feature: keyof Features) { |
14 | | - return this.features[feature] === true; |
15 | | - } |
| 6 | +export class ClientConfigurationApiService { |
| 7 | + constructor(public readonly client = createAxiosClient()) {} |
16 | 8 |
|
17 | 9 | /** |
18 | 10 | * Gets and validates client configuration via AWS API gateway |
19 | 11 | * @param {string} token user's JWT to authenticate against API gateway |
20 | 12 | * @returns {Promise<ClientConfiguration>} client configuration. |
21 | 13 | */ |
22 | | - static async fetch(token: string): Promise<ClientConfiguration | undefined> { |
23 | | - const client = await ClientConfiguration._fetch(token); |
| 14 | + async fetch(token: string): Promise<Result<ClientConfiguration>> { |
| 15 | + const client = await this._fetch(token); |
24 | 16 |
|
25 | | - return new ClientConfiguration(client.features, client.campaignId); |
| 17 | + return client; |
26 | 18 | } |
27 | 19 |
|
28 | | - // Note: Temporary until we have some sort of API. |
29 | | - private static async _fetch(_: string): Promise<IClientConfiguration> { |
30 | | - return $ClientConfiguration.parse({ |
31 | | - clientId: 'example-clientId', |
32 | | - campaignId: 'example-campaignId', |
33 | | - name: 'example-name', |
34 | | - features: { |
35 | | - proofing: true, |
36 | | - }, |
37 | | - }); |
| 20 | + private async _fetch(token: string): Promise<Result<ClientConfiguration>> { |
| 21 | + const response = await catchAxiosError( |
| 22 | + this.client.get<{ |
| 23 | + statusCode: number; |
| 24 | + clientConfiguration: ClientConfiguration; |
| 25 | + }>('/v1/client-configuration', { |
| 26 | + headers: { |
| 27 | + 'Content-Type': 'application/json', |
| 28 | + Authorization: token, |
| 29 | + }, |
| 30 | + }) |
| 31 | + ); |
| 32 | + |
| 33 | + if (response.error) { |
| 34 | + return { |
| 35 | + error: response.error, |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + return { |
| 40 | + data: $ClientConfiguration.parse(response.data.clientConfiguration), |
| 41 | + }; |
38 | 42 | } |
39 | 43 | } |
0 commit comments