diff --git a/types/x-ray/index.d.ts b/types/x-ray/index.d.ts index 345059a8eb..9554cc8753 100644 --- a/types/x-ray/index.d.ts +++ b/types/x-ray/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/lapwinglabs/x-ray#readme // Definitions by: Matt Traynham // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.8 +// TypeScript Version: 3.3 /// @@ -31,6 +31,8 @@ declare namespace XRay { } type Selector = ScalarSelector | SelectorArray; + type AbortHandler = (data: unknown[], url: string) => boolean; + interface Instance extends XRayCrawler.Instance { ( source: string, @@ -53,7 +55,7 @@ declare namespace XRay { interface InstanceInvocation { (callback: Callback): void; (source: string, callback: Callback): void; - abort(): this; + abort(arg: AbortHandler): this; paginate(selector: Selector): this; limit(n: number): this; stream(): NodeJS.ReadStream; diff --git a/types/x-ray/x-ray-tests.ts b/types/x-ray/x-ray-tests.ts index 8bc056b2f6..7339e32fc2 100644 --- a/types/x-ray/x-ray-tests.ts +++ b/types/x-ray/x-ray-tests.ts @@ -110,3 +110,24 @@ x({ first: x('a@href', 'title') }) })(fn); + +const pagedUrl = 'https://github.com/matthewmueller/x-ray/issues?q=is%3Aissue%20sort%3Acreated-asc%20'; + +x(pagedUrl, '.js-issue-row', [{ id: '@id', title: 'a.h4' }]) + .paginate('.next_page@href') + .abort((_, url) => url.includes('page=3')); + +const hasStringId = (obj: any): obj is {id: string} => { + if (!!obj) return false; + if (typeof obj !== 'object') return false; + if (!('id' in obj)) return false; + if (typeof obj.id !== 'string') return false; + return true; +}; + +x(pagedUrl, '.js-issue-row', [{ id: '@id', title: 'a.h4' }]) + .paginate('.next_page@href') + .abort((results) => results.some(result => { + if (hasStringId(result) && result.id === 'issue_40') return true; + return false; + }));