Skip to content

Commit

Permalink
🏷️ Better type apiRequestType helper
Browse files Browse the repository at this point in the history
  • Loading branch information
horaklukas committed Feb 25, 2022
1 parent 5c03f21 commit 2e611b5
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/utilities/apiRequestType/apiRequestType.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
interface RequestTypeParams {
types?: string[];
typePrefix?: string;
modulePrefix?: string;
interface RequestTypeParams<TP, MP, T extends readonly string[]> {
types?: T;
typePrefix?: TP;
modulePrefix?: MP;
}

/*
Expand All @@ -19,34 +19,38 @@ interface RequestTypeParams {
});
*/

const DEFAULT_TYPES = ['REQUEST', 'SUCCESS', 'FAILURE', 'CANCEL', 'RESET'];
const DEFAULT_TYPES = ['REQUEST', 'SUCCESS', 'FAILURE', 'CANCEL', 'RESET'] as const;

export function apiRequestType(params: RequestTypeParams = {}): Record<string, string> {
export function apiRequestType<TP extends string = '', MP extends string = '', T extends readonly string[] = typeof DEFAULT_TYPES>(params: RequestTypeParams<TP, MP, T> = {})/* : Record<string, string> */ {
const { types, typePrefix, modulePrefix } = {
typePrefix: '',
modulePrefix: '',
typePrefix: '' as const,
modulePrefix: '' as const,
...params,
types: params.types ?? DEFAULT_TYPES,
};
const actionTypes = {};

types.forEach(type => {
type ActionTypes = { [K in T[number] as `${TP}${K}`]: MP extends '' ? K : `${MP}/${K}`}

const actionTypes = {} as ActionTypes;

types.forEach((type) => {
const prefixedType = `${typePrefix}${type}`;

actionTypes[prefixedType] = modulePrefix ? `${modulePrefix}/${prefixedType}` : prefixedType;
});

return Object.freeze(actionTypes);
return Object.freeze<ActionTypes>(actionTypes);
}

export function createApiRequestType({
export function createApiRequestType<MP extends string, DT extends readonly string[] = typeof DEFAULT_TYPES>({
modulePrefix,
defaultTypes,
}: Pick<RequestTypeParams, 'modulePrefix'> & { defaultTypes?: string[] } = {}) {
return (params: RequestTypeParams) =>
apiRequestType({
}: { modulePrefix?: MP, defaultTypes?: DT } = {}) {
return function apiRequestTypeFactory<TP extends string, T extends readonly string[] = DT>(params: Omit<RequestTypeParams<TP, MP, T>, 'modulePrefix'>) {
return apiRequestType({
types: defaultTypes,
...params,
modulePrefix,
});
}
}

0 comments on commit 2e611b5

Please sign in to comment.