Skip to content

Commit

Permalink
feat: Add possibility to overwrite Endpoint types with custom impleme…
Browse files Browse the repository at this point in the history
…ntation, if function is returned
  • Loading branch information
MattCCC committed Sep 24, 2024
1 parent 58c8a30 commit 8daa185
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/types/api-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,23 @@ export declare type Endpoint<
// Setting 'unknown here lets us infer typings for non-predefined endpoints with dynamically set generic response data
type EndpointDefaults = Endpoint<DefaultResponse>;

type AFunction = (...args: any[]) => any;

/**
* Maps the method names from `EndpointsMethods` to their corresponding `Endpoint` type definitions.
*
* @template EndpointsMethods - The object containing endpoint method definitions.
*/
type EndpointsRecord<EndpointsMethods> = {
[K in keyof EndpointsMethods]: EndpointsMethods[K] extends Endpoint<
infer ResponseData,
infer QueryParams,
infer UrlPathParams
>
? Endpoint<ResponseData, QueryParams, UrlPathParams> // Method exists in a provided interface
: EndpointDefaults; // Method is not defined. Provide defaults
[K in keyof EndpointsMethods]: EndpointsMethods[K] extends AFunction
? EndpointsMethods[K] // Map function signatures directly
: EndpointsMethods[K] extends Endpoint<
infer ResponseData,
infer QueryParams,
infer UrlPathParams
>
? Endpoint<ResponseData, QueryParams, UrlPathParams> // Method is an Endpoint type
: EndpointDefaults; // Fallback to default Endpoint type
};

/**
Expand Down

0 comments on commit 8daa185

Please sign in to comment.