Skip to content

Commit

Permalink
feat(refactor): simplify the ShortLink types (#693)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReidyT authored Nov 6, 2024
1 parent 3a0f32b commit c6ac831
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 38 deletions.
7 changes: 1 addition & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,7 @@ export * from './websockets/index.js';
/**
* Type utils
*/
export type {
UnionOfConst,
ResultOf,
AnyOf,
AnyOfExcept,
} from './typeUtils.js';
export type { UnionOfConst, ResultOf } from './typeUtils.js';

/**
* Custom types
Expand Down
23 changes: 10 additions & 13 deletions src/shortLink/shortLink.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
import { AnyOfExcept, UnionOfConst } from '@/typeUtils.js';
import { UnionOfConst } from '@/typeUtils.js';

// This const is used to define the allowed Platforms.
// It is used in schema or database enums.
/**
* Represents the allowed platforms for which short links can be generated.
*/
export const ShortLinkPlatform = {
Builder: 'builder',
Player: 'player',
Library: 'library',
} as const;

export type ShortLink = {
itemId: string;
alias: string;
platform: UnionOfConst<typeof ShortLinkPlatform>;
item: { id: string };
createdAt: string;
};

export type ShortLinkItemId = {
itemId: string;
};
export type UpdateShortLink = Pick<ShortLink, 'alias'>;

export type ShortLinkPayload = Omit<ShortLink, 'createdAt' | 'item'> &
ShortLinkItemId;
export type ShortLinkPostPayload = ShortLinkPayload;
export type ShortLinkPatchPayload = AnyOfExcept<ShortLinkPostPayload, 'itemId'>;
export type ShortLinkPutPayload = Omit<ShortLinkPayload, 'itemId'>;
export type ShortLinkAvailable = {
available: boolean;
};

export type ShortLinksOfItem = {
[K in (typeof ShortLinkPlatform)[keyof typeof ShortLinkPlatform]]?: ShortLink['alias'];
};
19 changes: 0 additions & 19 deletions src/typeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,6 @@ export type ResultOf<T> = {
errors: Error[];
};

// TODO: add usage informations
/**
* This allow to define a type with at least one attribute of T.
*/
export type AnyOf<T> = {
[K in keyof T]: Pick<T, K>;
}[keyof T];

/**
* This allow to define a type with at least one attribute of T that is not a key in E.
*
* The following type add usage informations about:
* `type ShortLinkPatchPayload = AnyOfExcept<ShortLinkPostPayload, 'itemId'>;`
* - The `itemId` is not allowed.
* - All other attributes of ShortLinkPostPayload are allowed and optional.
* - At least of attribute is required.
*/
export type AnyOfExcept<T, E extends keyof T> = AnyOf<Omit<T, E>>;

/**
* The type is T or null.
*
Expand Down

0 comments on commit c6ac831

Please sign in to comment.