Skip to content

Commit

Permalink
Rename options._pagination to options.pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Mar 25, 2020
1 parent b5680f3 commit 1bfb671
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
20 changes: 10 additions & 10 deletions source/as-promise/core.ts
Expand Up @@ -80,31 +80,31 @@ export default class PromisableRequest extends Request {
);
}

// `options._pagination`
if (is.object(options._pagination)) {
// `options.pagination`
if (is.object(options.pagination)) {
if (defaults) {
(options as Options)._pagination = {
...defaults._pagination,
...options._pagination
(options as Options).pagination = {
...defaults.pagination,
...options.pagination
};
}

const {_pagination: pagination} = options;
const {pagination} = options;

if (!is.function_(pagination.transform)) {
throw new Error('`options._pagination.transform` must be implemented');
throw new Error('`options.pagination.transform` must be implemented');
}

if (!is.function_(pagination.shouldContinue)) {
throw new Error('`options._pagination.shouldContinue` must be implemented');
throw new Error('`options.pagination.shouldContinue` must be implemented');
}

if (!is.function_(pagination.filter)) {
throw new TypeError('`options._pagination.filter` must be implemented');
throw new TypeError('`options.pagination.filter` must be implemented');
}

if (!is.function_(pagination.paginate)) {
throw new Error('`options._pagination.paginate` must be implemented');
throw new Error('`options.pagination.paginate` must be implemented');
}
}

Expand Down
6 changes: 3 additions & 3 deletions source/as-promise/types.ts
Expand Up @@ -69,7 +69,7 @@ export interface Hooks extends RequestHooks {
}

export interface PaginationOptions<T> {
_pagination?: {
pagination?: {
transform?: (response: Response) => Promise<T[]> | T[];
filter?: (item: T, allItems: T[], currentItems: T[]) => boolean;
paginate?: (response: Response, allItems: T[], currentItems: T[]) => Options | false;
Expand All @@ -92,7 +92,7 @@ export interface NormalizedOptions extends RequestNormalizedOptions {
resolveBodyOnly: boolean;
retry: RequiredRetryOptions;
isStream: boolean;
_pagination?: Required<PaginationOptions<unknown>['_pagination']>;
pagination?: Required<PaginationOptions<unknown>['pagination']>;
}

export interface Defaults extends RequestDefaults {
Expand All @@ -101,7 +101,7 @@ export interface Defaults extends RequestDefaults {
resolveBodyOnly: boolean;
retry: RequiredRetryOptions;
isStream: boolean;
_pagination?: Required<PaginationOptions<unknown>['_pagination']>;
pagination?: Required<PaginationOptions<unknown>['pagination']>;
}

export class ParseError extends RequestError {
Expand Down
4 changes: 2 additions & 2 deletions source/create.ts
Expand Up @@ -195,10 +195,10 @@ const create = (defaults: InstanceDefaults): Got => {
got.paginate = (async function * <T>(url: string | URL, options?: Options) {
let normalizedOptions = normalizeArguments(url, options, defaults.options);

const pagination = normalizedOptions._pagination!;
const pagination = normalizedOptions.pagination!;

if (!is.object(pagination)) {
throw new TypeError('`options._pagination` must be implemented');
throw new TypeError('`options.pagination` must be implemented');
}

const all: T[] = [];
Expand Down
4 changes: 2 additions & 2 deletions source/index.ts
Expand Up @@ -66,11 +66,11 @@ const defaults: InstanceDefaults = {
methodRewriting: true,
ignoreInvalidCookies: false,
context: {},
// TODO: Set this to `false` when Got 12 gets released
// TODO: Set this to `true` when Got 12 gets released
http2: false,
allowGetBody: false,
rejectUnauthorized: true,
_pagination: {
pagination: {
transform: (response: Response) => {
if (response.request.options.responseType === 'json') {
return response.body;
Expand Down
40 changes: 20 additions & 20 deletions test/pagination.ts
Expand Up @@ -58,7 +58,7 @@ test('retrieves all elements with JSON responseType', withServer, async (t, serv
t.deepEqual(result, [1, 2]);
});

test('points to defaults when extending Got without custom `_pagination`', withServer, async (t, server, got) => {
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('');
Expand All @@ -70,7 +70,7 @@ test('pagination options can be extended', withServer, async (t, server, got) =>
attachHandler(server, 2);

const result = await got.extend({
_pagination: {
pagination: {
shouldContinue: () => false
}
}).paginate.all('');
Expand All @@ -82,7 +82,7 @@ test('filters elements', withServer, async (t, server, got) => {
attachHandler(server, 3);

const result = await got.paginate.all({
_pagination: {
pagination: {
filter: (element: unknown, allItems: unknown[], currentItems: unknown[]) => {
t.true(Array.isArray(allItems));
t.true(Array.isArray(currentItems));
Expand All @@ -99,7 +99,7 @@ test('parses elements', withServer, async (t, server, got) => {
attachHandler(server, 100);

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

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

const result = await got.paginate.all({
_pagination: {
pagination: {
paginate: (response: Response) => {
const url = new URL(response.url);

Expand All @@ -145,7 +145,7 @@ test('custom paginate function using allItems', withServer, async (t, server, go
attachHandler(server, 3);

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

const result = await got.paginate.all({
_pagination: {
pagination: {
paginate: (_response: Response, _allItems: unknown[], currentItems: unknown[]) => {
if (currentItems[0] === 3) {
return false;
Expand Down Expand Up @@ -193,7 +193,7 @@ test('`shouldContinue` works', withServer, async (t, server, got) => {
attachHandler(server, 2);

const options = {
_pagination: {
pagination: {
shouldContinue: (_item: unknown, allItems: unknown[], currentItems: unknown[]) => {
t.true(Array.isArray(allItems));
t.true(Array.isArray(currentItems));
Expand All @@ -216,7 +216,7 @@ test('`countLimit` works', withServer, async (t, server, got) => {
attachHandler(server, 2);

const options = {
_pagination: {
pagination: {
countLimit: 1
}
};
Expand All @@ -232,44 +232,44 @@ test('`countLimit` works', withServer, async (t, server, got) => {

test('throws if no `pagination` option', async t => {
const iterator = got.extend({
_pagination: false as any
pagination: false as any
}).paginate('', {
prefixUrl: 'https://example.com'
});

await t.throwsAsync(iterator.next(), {
message: '`options._pagination` must be implemented'
message: '`options.pagination` must be implemented'
});
});

test('throws if the `pagination` option does not have `transform` property', async t => {
const iterator = got.paginate('', {
_pagination: {...resetPagination},
pagination: {...resetPagination},
prefixUrl: 'https://example.com'
});

await t.throwsAsync(iterator.next(), {
message: '`options._pagination.transform` must be implemented'
message: '`options.pagination.transform` must be implemented'
});
});

test('throws if the `pagination` option does not have `shouldContinue` property', async t => {
const iterator = got.paginate('', {
_pagination: {
pagination: {
...resetPagination,
transform: thrower
},
prefixUrl: 'https://example.com'
});

await t.throwsAsync(iterator.next(), {
message: '`options._pagination.shouldContinue` must be implemented'
message: '`options.pagination.shouldContinue` must be implemented'
});
});

test('throws if the `pagination` option does not have `filter` property', async t => {
const iterator = got.paginate('', {
_pagination: {
pagination: {
...resetPagination,
transform: thrower,
shouldContinue: thrower,
Expand All @@ -279,13 +279,13 @@ test('throws if the `pagination` option does not have `filter` property', async
});

await t.throwsAsync(iterator.next(), {
message: '`options._pagination.filter` must be implemented'
message: '`options.pagination.filter` must be implemented'
});
});

test('throws if the `pagination` option does not have `paginate` property', async t => {
const iterator = got.paginate('', {
_pagination: {
pagination: {
...resetPagination,
transform: thrower,
shouldContinue: thrower,
Expand All @@ -295,6 +295,6 @@ test('throws if the `pagination` option does not have `paginate` property', asyn
});

await t.throwsAsync(iterator.next(), {
message: '`options._pagination.paginate` must be implemented'
message: '`options.pagination.paginate` must be implemented'
});
});

0 comments on commit 1bfb671

Please sign in to comment.