MCPcopy
hub / github.com/apify/crawlee / getResponse

Method getResponse

packages/impit-client/src/index.ts:117–169  ·  view source on GitHub ↗

* Common implementation for `sendRequest` and `stream` methods. * @param request `HttpRequest` object * @returns `HttpResponse` object

(
        request: HttpRequest<TResponseType>,
        redirects?: {
            redirectCount?: number;
            redirectUrls?: URL[];
        },
    )

Source from the content-addressed store, hash-verified

115 * @returns `HttpResponse` object
116 */
117 private async getResponse<TResponseType extends keyof ResponseTypes>(
118 request: HttpRequest<TResponseType>,
119 redirects?: {
120 redirectCount?: number;
121 redirectUrls?: URL[];
122 },
123 ): Promise<ResponseWithRedirects> {
124 if ((redirects?.redirectCount ?? 0) > this.maxRedirects) {
125 throw new Error(`Too many redirects, maximum is ${this.maxRedirects}.`);
126 }
127
128 const url = typeof request.url === 'string' ? request.url : request.url.href;
129
130 const impit = this.getClient({
131 ...this.impitOptions,
132 ...(request?.cookieJar ? { cookieJar: request.cookieJar as ToughCookieJar } : {}),
133 proxyUrl: request.proxyUrl,
134 followRedirects: false,
135 });
136
137 const response = await impit.fetch(url, {
138 method: request.method as HttpMethod,
139 headers: this.intoHeaders(request.headers),
140 body: this.intoImpitBody(request.body),
141 timeout: (request.timeout as { request?: number })?.request,
142 });
143
144 if (this.followRedirects && response.status >= 300 && response.status < 400) {
145 const location = response.headers.get('location');
146 const redirectUrl = new URL(location ?? '', request.url);
147
148 if (!location) {
149 throw new Error('Redirect response missing location header.');
150 }
151
152 return this.getResponse(
153 {
154 ...request,
155 method: this.shouldRewriteRedirectToGet(response.status, request.method) ? 'GET' : request.method,
156 url: redirectUrl.href,
157 },
158 {
159 redirectCount: (redirects?.redirectCount ?? 0) + 1,
160 redirectUrls: [...(redirects?.redirectUrls ?? []), redirectUrl],
161 },
162 );
163 }
164
165 return {
166 response,
167 redirectUrls: redirects?.redirectUrls ?? [],
168 };
169 }
170
171 /**
172 * @inheritDoc

Callers 2

sendRequestMethod · 0.95
streamMethod · 0.95

Calls 5

getClientMethod · 0.95
intoHeadersMethod · 0.95
intoImpitBodyMethod · 0.95
getMethod · 0.65

Tested by

no test coverage detected