Skip to content

Commit

Permalink
fix: correct types for got scraping
Browse files Browse the repository at this point in the history
chore: fixes

chore: bump lock file
  • Loading branch information
vladfrangu committed Dec 11, 2023
1 parent c455618 commit a0ff98e
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
4.0.3 / 2023/12/11
====================
- Fixed missing extended types for `gotScraping.stream` and `gotScraping.paginate`
- Fixed general type issues with `got-scraping`, including not reporting incorrect types for known properties like `proxyUrl`

4.0.2 / 2023/11/29
====================
- Fixed runtime exceptions when using `got-scraping` in a project with older versions of node.js 16
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "got-scraping",
"version": "4.0.2",
"version": "4.0.3",
"description": "HTTP client made for scraping based on got.",
"engines": {
"node": ">=16"
Expand Down
8 changes: 6 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import type { OptionsInit as GotOptionsInit } from 'got';

export type { GotOptionsInit };

export interface Context extends Record<string, unknown> {
export interface Context {
proxyUrl?: string;
headerGeneratorOptions?: Record<string, unknown>;
useHeaderGenerator?: boolean;
headerGenerator?: { getHeaders: (options: Record<string, unknown>) => Record<string, string> };
insecureHTTPParser?: boolean;
sessionToken?: object;
/** @private */
sessionData?: unknown;
/** @private */
resolveProtocol?: (data: unknown) => { alpnProtocol: string } | Promise<{ alpnProtocol: string }>;
}

export interface OptionsInit extends Context, GotOptionsInit {}
export type OptionsInit = GotOptionsInit & Context;
2 changes: 1 addition & 1 deletion src/resolve-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const createCaches = () => ({

const defaults = createCaches();

interface ProtocolCache {
export interface ProtocolCache {
protocolCache?: typeof defaults.protocolCache;
resolveAlpnQueue?: typeof defaults.resolveAlpnQueue;
}
Expand Down
88 changes: 80 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { CancelableRequest, ExtendOptions, Got, HTTPAlias, Options, Request, Response } from 'got';
import type { CancelableRequest, ExtendOptions, Got, HTTPAlias, Options, PaginateData, PaginationOptions, Request, Response } from 'got';
import type { OptionsInit } from './context.js';

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 ExtendedGotRequestFunction {
export type ExtendedGotRequestFunction = {
(url: string | URL, options?: ExtendedOptionsOfTextResponseBody): CancelableRequest<Response<string>>;
<T>(url: string | URL, options?: ExtendedOptionsOfJSONResponseBody): CancelableRequest<Response<T>>;
(url: string | URL, options?: ExtendedOptionsOfBufferResponseBody): CancelableRequest<Response<Buffer>>;
Expand All @@ -28,13 +28,13 @@ export interface ExtendedGotRequestFunction {
(url: string | URL, options?: OptionsInit): CancelableRequest | Request;
(options: OptionsInit): CancelableRequest | Request;
(url: undefined, options: undefined, defaults: Options): CancelableRequest | Request;
}
};

export type ExtendedOptionsOfTextResponseBody = Merge<OptionsInit, {
isStream?: false;
resolveBodyOnly?: false;
responseType?: 'text';
}>
}>;

export type ExtendedOptionsOfJSONResponseBody = Merge<OptionsInit, {
isStream?: false;
Expand All @@ -57,11 +57,83 @@ export type ResponseBodyOnly = {
resolveBodyOnly: true;
};

export type ExtendedGotStreamFunction = ((url?: string | URL, options?: Merge<OptionsInit, {
isStream?: true;
}>) => Request) & ((options?: Merge<OptionsInit, {
isStream?: true;
}>) => Request);

export type ExtendedExtendOptions = ExtendOptions & OptionsInit;

export interface GotScraping extends Record<HTTPAlias, ExtendedGotRequestFunction>, ExtendedGotRequestFunction {
stream: Got['stream'];
paginate: Got['paginate'];
export type ExtendedGotStream = ExtendedGotStreamFunction & Record<HTTPAlias, ExtendedGotStreamFunction>;

export type ExtendedPaginationOptions<ElementType, BodyType> = PaginationOptions<ElementType, BodyType> & {
paginate?: (data: PaginateData<BodyType, ElementType>) => OptionsInit | false;
};

export type ExtendedOptionsWithPagination<T = unknown, R = unknown> = Merge<OptionsInit, {
pagination?: ExtendedPaginationOptions<T, R>;
}>;

export type ExtendedGotPaginate = {
/**
Same as `GotPaginate.each`.
*/
<T, R = unknown>(url: string | URL, options?: ExtendedOptionsWithPagination<T, R>): AsyncIterableIterator<T>;
/**
Same as `GotPaginate.each`.
*/
<T, R = unknown>(options?: ExtendedOptionsWithPagination<T, R>): AsyncIterableIterator<T>;
/**
Returns an async iterator.
See pagination.options for more pagination options.
@example
```
import { gotScraping } from 'got-scraping';
const countLimit = 10;
const pagination = gotScraping.paginate('https://api.github.com/repos/sindresorhus/got/commits', {
pagination: { countLimit }
});
console.log(`Printing latest ${countLimit} Got commits (newest to oldest):`);
for await (const commitData of pagination) {
console.log(commitData.commit.message);
}
```
*/
each: (<T, R = unknown>(url: string | URL, options?: ExtendedOptionsWithPagination<T, R>) => AsyncIterableIterator<T>)
& (<T, R = unknown>(options?: ExtendedOptionsWithPagination<T, R>) => AsyncIterableIterator<T>);
/**
Returns a Promise for an array of all results.
See pagination.options for more pagination options.
@example
```
import { gotScraping } from 'got-scraping';
const countLimit = 10;
const results = await gotScraping.paginate.all('https://api.github.com/repos/sindresorhus/got/commits', {
pagination: { countLimit }
});
console.log(`Printing latest ${countLimit} Got commits (newest to oldest):`);
console.log(results);
```
*/
all: (<T, R = unknown>(url: string | URL, options?: ExtendedOptionsWithPagination<T, R>) => Promise<T[]>)
& (<T, R = unknown>(options?: ExtendedOptionsWithPagination<T, R>) => Promise<T[]>);
};

export type GotScraping = {
stream: ExtendedGotStream;
paginate: ExtendedGotPaginate;
defaults: Got['defaults'];
extend: (...instancesOrOptions: Array<GotScraping | ExtendedExtendOptions>) => GotScraping;
}
} & Record<HTTPAlias, ExtendedGotRequestFunction> & ExtendedGotRequestFunction;
4 changes: 0 additions & 4 deletions test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,6 @@ describe('GotScraping', () => {

// FIXME: this should be using a local server instead
test.skip('should order headers with proxyUrl and http1', async () => {
// @ts-expect-error FIXME
const body = await getStream(gotScraping.stream({
url: 'https://api.apify.com/v2/browser-info?rawHeaders=1',
proxyUrl: `http://groups-SHADER:${process.env.APIFY_PROXY_PASSWORD}@proxy.apify.com:8000`,
Expand Down Expand Up @@ -522,7 +521,6 @@ describe('GotScraping', () => {

// FIXME: this should use a local server instead
test.skip('Should allow https target via http proxy when auto downgrading', async () => {
// @ts-expect-error FIXME
const stream = gotScraping.stream({
url: 'https://eshop.coop-box.cz/',
proxyUrl: `http://groups-SHADER:${process.env.APIFY_PROXY_PASSWORD}@proxy.apify.com:8000`,
Expand All @@ -546,7 +544,6 @@ describe('GotScraping', () => {
}
const responseBody = chunks.join();

// @ts-expect-error FIXME
const proxyStream = gotScraping.stream({
url: 'https://api.apify.com/v2/browser-info',
proxyUrl: `http://groups-SHADER:${process.env.APIFY_PROXY_PASSWORD}@proxy.apify.com:8000`,
Expand Down Expand Up @@ -584,7 +581,6 @@ describe('GotScraping', () => {
expect(response.statusCode).toBe(200);
expect(response.request.options).toMatchObject({ http2: true });

// @ts-expect-error FIXME
const proxyStream = gotScraping.stream({
url: 'https://api.apify.com/v2/browser-info',
proxyUrl: `http://groups-SHADER:${process.env.APIFY_PROXY_PASSWORD}@proxy.apify.com:8000`,
Expand Down

0 comments on commit a0ff98e

Please sign in to comment.