Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Mar 25, 2020
1 parent 1bfb671 commit 61321e6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
10 changes: 6 additions & 4 deletions source/types.ts
Expand Up @@ -22,7 +22,9 @@ import {
} from './as-promise';
import Request from './core';

// `type-fest` utilities
type Except<ObjectType, KeysType extends keyof ObjectType> = Pick<ObjectType, Exclude<keyof ObjectType, KeysType>>;
type Merge<FirstType, SecondType> = Except<FirstType, Extract<keyof FirstType, keyof SecondType>> & SecondType;

export interface InstanceDefaults {
options: DefaultOptions;
Expand All @@ -46,15 +48,15 @@ export type StrictOptions = Except<Options, 'isStream' | 'responseType' | 'resol
type ResponseBodyOnly = {resolveBodyOnly: true};

export interface GotPaginate {
<T>(url: string | URL, options?: Options & PaginationOptions<T>): AsyncIterableIterator<T>;
all<T>(url: string | URL, options?: Options & PaginationOptions<T>): Promise<T[]>;
<T>(url: string | URL, options?: Merge<Options, PaginationOptions<T>>): AsyncIterableIterator<T>;
all<T>(url: string | URL, options?: Merge<Options, PaginationOptions<T>>): Promise<T[]>;

// A bug.
// eslint-disable-next-line @typescript-eslint/adjacent-overload-signatures
<T>(options?: Options & PaginationOptions<T>): AsyncIterableIterator<T>;
<T>(options?: Merge<Options, PaginationOptions<T>>): AsyncIterableIterator<T>;
// A bug.
// eslint-disable-next-line @typescript-eslint/adjacent-overload-signatures
all<T>(options?: Options & PaginationOptions<T>): Promise<T[]>;
all<T>(options?: Merge<Options, PaginationOptions<T>>): Promise<T[]>;
}

export interface GotRequestFunction {
Expand Down
38 changes: 19 additions & 19 deletions test/pagination.ts
Expand Up @@ -43,7 +43,7 @@ test('the link header has no next value', withServer, async (t, server, got) =>
test('retrieves all elements', withServer, async (t, server, got) => {
attachHandler(server, 2);

const result = await got.paginate.all('');
const result = await got.paginate.all<number>('');

t.deepEqual(result, [1, 2]);
});
Expand All @@ -53,15 +53,15 @@ test('retrieves all elements with JSON responseType', withServer, async (t, serv

const result = await got.extend({
responseType: 'json'
}).paginate.all('');
}).paginate.all<number>('');

t.deepEqual(result, [1, 2]);
});

test('points to defaults when extending Got without custom `pagination`', withServer, async (t, server, got) => {
attachHandler(server, 2);

const result = await got.extend().paginate.all('');
const result = await got.extend().paginate.all<number>('');

t.deepEqual(result, [1, 2]);
});
Expand All @@ -73,17 +73,17 @@ test('pagination options can be extended', withServer, async (t, server, got) =>
pagination: {
shouldContinue: () => false
}
}).paginate.all('');
}).paginate.all<number>('');

t.deepEqual(result, []);
});

test('filters elements', withServer, async (t, server, got) => {
attachHandler(server, 3);

const result = await got.paginate.all({
const result = await got.paginate.all<number>({
pagination: {
filter: (element: unknown, allItems: unknown[], currentItems: unknown[]) => {
filter: (element: number, allItems: number[], currentItems: number[]) => {
t.true(Array.isArray(allItems));
t.true(Array.isArray(currentItems));

Expand All @@ -98,7 +98,7 @@ test('filters elements', withServer, async (t, server, got) => {
test('parses elements', withServer, async (t, server, got) => {
attachHandler(server, 100);

const result = await got.paginate.all('?page=100', {
const result = await got.paginate.all<number>('?page=100', {
pagination: {
transform: (response: Response) => [(response as Response<string>).body.length]
}
Expand All @@ -110,7 +110,7 @@ test('parses elements', withServer, async (t, server, got) => {
test('parses elements - async function', withServer, async (t, server, got) => {
attachHandler(server, 100);

const result = await got.paginate.all('?page=100', {
const result = await got.paginate.all<number>('?page=100', {
pagination: {
transform: async (response: Response) => [(response as Response<string>).body.length]
}
Expand All @@ -122,7 +122,7 @@ test('parses elements - async function', withServer, async (t, server, got) => {
test('custom paginate function', withServer, async (t, server, got) => {
attachHandler(server, 3);

const result = await got.paginate.all({
const result = await got.paginate.all<number>({
pagination: {
paginate: (response: Response) => {
const url = new URL(response.url);
Expand All @@ -144,9 +144,9 @@ test('custom paginate function', withServer, async (t, server, got) => {
test('custom paginate function using allItems', withServer, async (t, server, got) => {
attachHandler(server, 3);

const result = await got.paginate.all({
const result = await got.paginate.all<number>({
pagination: {
paginate: (_response: Response, allItems: unknown[]) => {
paginate: (_response: Response, allItems: number[]) => {
if (allItems.length === 2) {
return false;
}
Expand All @@ -162,9 +162,9 @@ test('custom paginate function using allItems', withServer, async (t, server, go
test('custom paginate function using currentItems', withServer, async (t, server, got) => {
attachHandler(server, 3);

const result = await got.paginate.all({
const result = await got.paginate.all<number>({
pagination: {
paginate: (_response: Response, _allItems: unknown[], currentItems: unknown[]) => {
paginate: (_response: Response, _allItems: number[], currentItems: number[]) => {
if (currentItems[0] === 3) {
return false;
}
Expand All @@ -180,9 +180,9 @@ test('custom paginate function using currentItems', withServer, async (t, server
test('iterator works', withServer, async (t, server, got) => {
attachHandler(server, 5);

const results = [];
const results: number[] = [];

for await (const item of got.paginate('')) {
for await (const item of got.paginate<number>('')) {
results.push(item);
}

Expand All @@ -203,9 +203,9 @@ test('`shouldContinue` works', withServer, async (t, server, got) => {
}
};

const results = [];
const results: number[] = [];

for await (const item of got.paginate(options)) {
for await (const item of got.paginate<number>(options)) {
results.push(item);
}

Expand All @@ -221,9 +221,9 @@ test('`countLimit` works', withServer, async (t, server, got) => {
}
};

const results = [];
const results: number[] = [];

for await (const item of got.paginate(options)) {
for await (const item of got.paginate<number>(options)) {
results.push(item);
}

Expand Down

0 comments on commit 61321e6

Please sign in to comment.