Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(refactor): simplify the ShortLink types #693

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,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