Skip to content

Commit

Permalink
chore: define listToDict in constants
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Aug 24, 2024
1 parent 9cfa575 commit 1aecb24
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
7 changes: 6 additions & 1 deletion server/common/constants.ts
Original file line number Diff line number Diff line change
@@ -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 = <T extends readonly [string, ...string[]]>(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);
4 changes: 2 additions & 2 deletions server/common/types/brandedId.ts
Original file line number Diff line number Diff line change
@@ -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<T extends string> = string & z.BRAND<T>;

Expand Down
6 changes: 3 additions & 3 deletions server/service/brandedId.ts
Original file line number Diff line number Diff line change
@@ -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<T extends IdName> = string & z.BRAND<`${T}EntityId`>;

export type EntityId = { [T in IdName]: Entity<T> };

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() },
Expand Down

0 comments on commit 1aecb24

Please sign in to comment.