-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb8cc3c
commit 9237896
Showing
10 changed files
with
90 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './api/api.configuration'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export class HttpGuard { | ||
static checkStatusOk(response: Response) { | ||
if (!response.ok) { | ||
throw new Error(`${response.status} - ${response.statusText}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './type.guard'; | ||
export * from './http.guard'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export class TypeGuard { | ||
static checkKeyAndValueExists(json: Record<string, unknown>, key: string): void { | ||
if (!(key in json) && json[key]) { | ||
throw new Error(`The ${key} is missing`); | ||
} | ||
} | ||
|
||
static checkNumber(value: unknown): void { | ||
if (typeof value !== 'number') { | ||
throw new Error('Type is not a number'); | ||
} | ||
} | ||
|
||
static checkArray(value: unknown): void { | ||
if (!Array.isArray(value)) { | ||
throw new Error('Type is not an array'); | ||
} | ||
} | ||
|
||
static checkBoolean(value: unknown): void { | ||
if (typeof value !== 'boolean') { | ||
throw new Error('Type is not a boolean'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Envs } from "../types"; | ||
import { TypeGuard } from "../guards"; | ||
|
||
const checkEnvType = (obj: Record<string, unknown>): void => { | ||
TypeGuard.checkKeyAndValueExists(obj, 'FEATURE_TLDRAW_ENABLED'); | ||
TypeGuard.checkKeyAndValueExists(obj, 'TLDRAW__ASSETS_ENABLED'); | ||
TypeGuard.checkKeyAndValueExists(obj, 'TLDRAW__ASSETS_MAX_SIZE'); | ||
TypeGuard.checkKeyAndValueExists(obj, 'TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST'); | ||
TypeGuard.checkBoolean(obj.FEATURE_TLDRAW_ENABLED); | ||
TypeGuard.checkBoolean(obj.FEATURE_TLDRAW_ENABLED); | ||
TypeGuard.checkNumber(obj.TLDRAW__ASSETS_MAX_SIZE); | ||
TypeGuard.checkArray(obj.TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST); | ||
} | ||
|
||
const castToEnv = (obj: Record<string, unknown>): Envs => { | ||
checkEnvType(obj); | ||
const configuration = obj as Envs; | ||
|
||
return configuration; | ||
} | ||
|
||
export class ConfigurationMapper { | ||
static mapToConfigurationFromResponse(obj: Record<string, unknown>): Envs { | ||
const configuration = castToEnv(obj); | ||
|
||
const mappedConfiguration: Envs = { | ||
FEATURE_TLDRAW_ENABLED: configuration.FEATURE_TLDRAW_ENABLED, | ||
TLDRAW__ASSETS_ENABLED: configuration.TLDRAW__ASSETS_ENABLED, | ||
TLDRAW__ASSETS_MAX_SIZE: configuration.TLDRAW__ASSETS_MAX_SIZE, | ||
TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST: configuration.TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST, | ||
} | ||
|
||
return mappedConfiguration; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './configuration.mapper'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Envs'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,24 @@ | ||
import { Envs } from "../types/Envs"; | ||
import { API } from "../configuration/api/api.configuration"; | ||
import { Envs } from "../types"; | ||
import { API } from "../configuration"; | ||
import { ConfigurationMapper } from "../mapper"; | ||
import { HttpGuard } from "../guards"; | ||
|
||
export const getEnvs = async (): Promise<Envs | undefined> => { | ||
// the try catch should not part of getEnvs, the place that use it must handle the errors | ||
// should be part of a store | ||
// Without loading the config the Promise.all should not be finished and proceed. | ||
export const getEnvs = async (): Promise<Envs|undefined> => { | ||
try { | ||
// TODO: check order.. | ||
const response = await fetch(API.ENV_CONFIG); | ||
HttpGuard.checkStatusOk(response); | ||
const responseData = await response.json(); | ||
|
||
const configuration = ConfigurationMapper.mapToConfigurationFromResponse(responseData); | ||
|
||
if (!response.ok) { | ||
throw new Error(`${response.status} - ${response.statusText}`); | ||
} | ||
|
||
return await response.json(); | ||
return configuration; | ||
} catch (error) { | ||
// It should exists one place that execute the console.error in the application. A errorHandler. | ||
// If we want to collect this informations to send it back to us, then we have currently no possibility to implement it. | ||
console.error("Error fetching env config:", error); | ||
} | ||
}; |