Skip to content

Commit

Permalink
fix(typescript): improve internal MapFunction type (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptomulik committed Mar 11, 2021
1 parent 0db1a8a commit a779629
Showing 1 changed file with 27 additions and 47 deletions.
74 changes: 27 additions & 47 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ type NormalizeResponse<T> = T & { data: GetResultsType<T> };

type DataType<T> = "data" extends keyof T ? T["data"] : unknown;

export interface MapFunction<T = unknown, R = unknown> {
(
response: OctokitTypes.OctokitResponse<PaginationResults<T>>,
done: () => void
): R[];
export interface MapFunction<
T = OctokitTypes.OctokitResponse<PaginationResults<unknown>>,
R = unknown[]
> {
(response: T, done: () => void): R;
}

export type PaginationResults<T = unknown> = T[];
Expand All @@ -74,7 +74,7 @@ export interface PaginateInterface {
*/
<T, R>(
options: OctokitTypes.EndpointOptions,
mapFn: MapFunction<T, R>
mapFn: MapFunction<OctokitTypes.OctokitResponse<PaginationResults<T>>, R[]>
): Promise<PaginationResults<R>>;

/**
Expand All @@ -94,10 +94,7 @@ export interface PaginateInterface {
*/
<R extends keyof PaginatingEndpoints, MR extends unknown[]>(
route: R,
mapFn: (
response: PaginatingEndpoints[R]["response"],
done: () => void
) => MR
mapFn: MapFunction<PaginatingEndpoints[R]["response"], MR>
): Promise<MR>;

/**
Expand All @@ -110,10 +107,7 @@ export interface PaginateInterface {
<R extends keyof PaginatingEndpoints, MR extends unknown[]>(
route: R,
parameters: PaginatingEndpoints[R]["parameters"],
mapFn: (
response: PaginatingEndpoints[R]["response"],
done: () => void
) => MR
mapFn: MapFunction<PaginatingEndpoints[R]["response"], MR>
): Promise<MR>;

/**
Expand Down Expand Up @@ -159,12 +153,10 @@ export interface PaginateInterface {
*/
<R extends OctokitTypes.RequestInterface, MR extends unknown[]>(
request: R,
mapFn: (
response: NormalizeResponse<
OctokitTypes.GetResponseTypeFromEndpointMethod<R>
>,
done: () => void
) => MR
mapFn: MapFunction<
NormalizeResponse<OctokitTypes.GetResponseTypeFromEndpointMethod<R>>,
MR
>
): Promise<MR>;

/**
Expand All @@ -177,12 +169,10 @@ export interface PaginateInterface {
<R extends OctokitTypes.RequestInterface, MR extends unknown[]>(
request: R,
parameters: Parameters<R>[0],
mapFn: (
response: NormalizeResponse<
OctokitTypes.GetResponseTypeFromEndpointMethod<R>
>,
done: () => void
) => MR
mapFn: MapFunction<
NormalizeResponse<OctokitTypes.GetResponseTypeFromEndpointMethod<R>>,
MR
>
): Promise<MR>;

/**
Expand Down Expand Up @@ -276,7 +266,7 @@ export interface ComposePaginateInterface {
<T, R>(
octokit: Octokit,
options: OctokitTypes.EndpointOptions,
mapFn: MapFunction<T, R>
mapFn: MapFunction<OctokitTypes.OctokitResponse<PaginationResults<T>>, R[]>
): Promise<PaginationResults<R>>;

/**
Expand All @@ -301,10 +291,7 @@ export interface ComposePaginateInterface {
<R extends keyof PaginatingEndpoints, MR extends unknown[]>(
octokit: Octokit,
route: R,
mapFn: (
response: PaginatingEndpoints[R]["response"],
done: () => void
) => MR
mapFn: MapFunction<PaginatingEndpoints[R]["response"], MR>
): Promise<MR>;

/**
Expand All @@ -319,10 +306,7 @@ export interface ComposePaginateInterface {
octokit: Octokit,
route: R,
parameters: PaginatingEndpoints[R]["parameters"],
mapFn: (
response: PaginatingEndpoints[R]["response"],
done: () => void
) => MR
mapFn: MapFunction<PaginatingEndpoints[R]["response"], MR>
): Promise<MR>;

/**
Expand Down Expand Up @@ -374,12 +358,10 @@ export interface ComposePaginateInterface {
<R extends OctokitTypes.RequestInterface, MR extends unknown[]>(
octokit: Octokit,
request: R,
mapFn: (
response: NormalizeResponse<
OctokitTypes.GetResponseTypeFromEndpointMethod<R>
>,
done: () => void
) => MR
mapFn: MapFunction<
NormalizeResponse<OctokitTypes.GetResponseTypeFromEndpointMethod<R>>,
MR
>
): Promise<MR>;

/**
Expand All @@ -394,12 +376,10 @@ export interface ComposePaginateInterface {
octokit: Octokit,
request: R,
parameters: Parameters<R>[0],
mapFn: (
response: NormalizeResponse<
OctokitTypes.GetResponseTypeFromEndpointMethod<R>
>,
done: () => void
) => MR
mapFn: MapFunction<
NormalizeResponse<OctokitTypes.GetResponseTypeFromEndpointMethod<R>>,
MR
>
): Promise<MR>;

/**
Expand Down

0 comments on commit a779629

Please sign in to comment.