From 1aecb24bd2710b27aa9c8c9445c9b6dce0a05c9f Mon Sep 17 00:00:00 2001 From: solufa Date: Sat, 24 Aug 2024 16:27:36 +0900 Subject: [PATCH] chore: define listToDict in constants --- server/common/constants.ts | 7 ++++++- server/common/types/brandedId.ts | 4 ++-- server/service/brandedId.ts | 6 +++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/server/common/constants.ts b/server/common/constants.ts index ea70d88..4be3038 100644 --- a/server/common/constants.ts +++ b/server/common/constants.ts @@ -1,5 +1,10 @@ export const APP_NAME = 'CATAPULT'; -export const BRANDED_ID_NAMES = ['user', 'task'] as const; +export const ID_NAME_LIST = ['user', 'task'] as const; export const IS_PROD = process.env.NODE_ENV === 'production'; + +const listToDict = (list: T): { [U in T[number]]: U } => + list.reduce((dict, type) => ({ ...dict, [type]: type }), {} as { [U in T[number]]: U }); + +export const ID_NAMES = listToDict(ID_NAME_LIST); diff --git a/server/common/types/brandedId.ts b/server/common/types/brandedId.ts index a67b580..c9905fe 100644 --- a/server/common/types/brandedId.ts +++ b/server/common/types/brandedId.ts @@ -1,7 +1,7 @@ -import type { BRANDED_ID_NAMES } from 'common/constants'; +import type { ID_NAME_LIST } from 'common/constants'; import type { z } from 'zod'; -type IdName = (typeof BRANDED_ID_NAMES)[number]; +type IdName = (typeof ID_NAME_LIST)[number]; type Branded = string & z.BRAND; diff --git a/server/service/brandedId.ts b/server/service/brandedId.ts index 55286fd..1354d6f 100644 --- a/server/service/brandedId.ts +++ b/server/service/brandedId.ts @@ -1,14 +1,14 @@ -import { BRANDED_ID_NAMES } from 'common/constants'; +import { ID_NAME_LIST } from 'common/constants'; import type { DtoId, MaybeId } from 'common/types/brandedId'; import { z } from 'zod'; -type IdName = (typeof BRANDED_ID_NAMES)[number]; +type IdName = (typeof ID_NAME_LIST)[number]; type Entity = string & z.BRAND<`${T}EntityId`>; export type EntityId = { [T in IdName]: Entity }; -export const brandedId = BRANDED_ID_NAMES.reduce( +export const brandedId = ID_NAME_LIST.reduce( (dict, current) => ({ ...dict, [current]: { entity: z.string(), maybe: z.string(), dto: z.string() },