Skip to content

Commit

Permalink
refactor(query): better extract types from endpoints
Browse files Browse the repository at this point in the history
- fix(query): pick correct `ApiError` type inside `createApi`
- `useCache` will automatically pull success type from endpoint.
  • Loading branch information
neurosnap committed Jan 11, 2024
1 parent f43c20f commit 531fbae
Show file tree
Hide file tree
Showing 6 changed files with 1,316 additions and 287 deletions.
61 changes: 34 additions & 27 deletions api-type-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ ${method}<P>(
): CreateActionWithPayload<Omit<Ctx, 'payload'> & Payload<P>, P>;
${method}<P extends never, ApiSuccess, ApiError = unknown>(
req: { supervisor?: Supervisor }
): CreateAction<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError>>;
): CreateAction<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>, ApiSuccess>;
${method}<P, ApiSuccess, ApiError = unknown>(req: {
supervisor?: Supervisor;
}): CreateActionWithPayload<
Omit<Ctx, 'payload' | 'json'> &
Payload<P> &
FetchJson<ApiSuccess, ApiError>,
P
FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>,
P,
ApiSuccess
>;
/**
Expand All @@ -45,15 +46,16 @@ ${method}<P, Gtx extends Ctx = Ctx>(
fn: MiddlewareApiCo<Gtx>,
): CreateActionWithPayload<Gtx, P>;
${method}<P extends never, ApiSuccess, ApiError = unknown>(
fn: MiddlewareApiCo<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError>>,
): CreateAction<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError>>;
fn: MiddlewareApiCo<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>>,
): CreateAction<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>, ApiSuccess>;
${method}<P, ApiSuccess, ApiError = unknown>(
fn: MiddlewareApiCo<Ctx>,
): CreateActionWithPayload<
Omit<Ctx, 'payload' | 'json'> &
Payload<P> &
FetchJson<ApiSuccess, ApiError>,
P
FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>,
P,
ApiSuccess
>;
/**
Expand All @@ -74,16 +76,17 @@ ${method}<P, Gtx extends Ctx = Ctx>(
): CreateActionWithPayload<Gtx, P>;
${method}<P extends never, ApiSuccess, ApiError = unknown>(
req: { supervisor?: Supervisor },
fn: MiddlewareApiCo<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError>>,
): CreateAction<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError>>;
fn: MiddlewareApiCo<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>>,
): CreateAction<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>, ApiSuccess>;
${method}<P, ApiSuccess, ApiError = unknown>(
req: { supervisor?: Supervisor },
fn: MiddlewareApiCo<Ctx>,
): CreateActionWithPayload<
Omit<Ctx, 'payload' | 'json'> &
Payload<P> &
FetchJson<ApiSuccess, ApiError>,
P
FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>,
P,
ApiSuccess
>;`;
const uriMethods = methods.map((m) => uriTmpl(m)).join("\n\n");

Expand All @@ -97,14 +100,15 @@ ${method}<P>(
): CreateActionWithPayload<Omit<Ctx, 'payload'> & Payload<P>, P>;
${method}<P extends never, ApiSuccess, ApiError = unknown>(
name: ApiName,
): CreateAction<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError>>;
): CreateAction<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>, ApiSuccess>;
${method}<P, ApiSuccess, ApiError = unknown>(
name: ApiName,
): CreateActionWithPayload<
Omit<Ctx, 'payload' | 'json'> &
Payload<P> &
FetchJson<ApiSuccess, ApiError>,
P
FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>,
P,
ApiSuccess
>;
/**
Expand All @@ -122,15 +126,16 @@ ${method}<P, Gtx extends Ctx = Ctx>(
${method}<P extends never, ApiSuccess, ApiError = unknown>(
name: ApiName,
req: { supervisor?: Supervisor }
): CreateAction<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError>>;
): CreateAction<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>, ApiSuccess>;
${method}<P, ApiSuccess, ApiError = unknown>(
name: ApiName,
req: { supervisor?: Supervisor },
): CreateActionWithPayload<
Omit<Ctx, 'payload' | 'json'> &
Payload<P> &
FetchJson<ApiSuccess, ApiError>,
P
FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>,
P,
ApiSuccess
>;
/**
Expand All @@ -151,20 +156,21 @@ ${method}<P, Gtx extends Ctx = Ctx>(
): CreateActionWithPayload<Gtx, P>;
${method}<P extends never, ApiSuccess, ApiError = unknown>(
name: ApiName,
fn: MiddlewareApiCo<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError>>,
): CreateAction<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError>>;
fn: MiddlewareApiCo<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>>,
): CreateAction<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>, ApiSuccess>;
${method}<P, ApiSuccess, ApiError = unknown>(
name: ApiName,
fn: MiddlewareApiCo<
Omit<Ctx, 'payload' | 'json'> &
Payload<P> &
FetchJson<ApiSuccess, ApiError>
FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>,
>,
): CreateActionWithPayload<
Omit<Ctx, 'payload' | 'json'> &
Payload<P> &
FetchJson<ApiSuccess, ApiError>,
P
FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>,
P,
ApiSuccess
>;
/**
Expand Down Expand Up @@ -193,21 +199,22 @@ ${method}<P, Gtx extends Ctx = Ctx>(
${method}<P extends never, ApiSuccess, ApiError = unknown>(
name: ApiName,
req: { supervisor?: Supervisor },
fn: MiddlewareApiCo<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError>>,
): CreateAction<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError>>;
fn: MiddlewareApiCo<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>>,
): CreateAction<Omit<Ctx, 'json'> & FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>, ApiSuccess>;
${method}<P, ApiSuccess, ApiError = unknown>(
name: ApiName,
req: { supervisor?: Supervisor },
fn: MiddlewareApiCo<
Omit<Ctx, 'payload' | 'json'> &
Payload<P> &
FetchJson<ApiSuccess, ApiError>
FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>
>,
): CreateActionWithPayload<
Omit<Ctx, 'payload' | 'json'> &
Payload<P> &
FetchJson<ApiSuccess, ApiError>,
P
FetchJson<ApiSuccess, ApiError extends unknown ? Ctx["_error"] : ApiError>,
P,
ApiSuccess
>;`;
const regMethods = methods.map((m) => methodTmpl(m)).join("\n\n");

Expand Down
Loading

0 comments on commit 531fbae

Please sign in to comment.