Skip to content

Commit

Permalink
chore: merge fetch params on server side (#32518)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Sep 9, 2024
1 parent 9a313ee commit e5d6ee5
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 27 deletions.
33 changes: 7 additions & 26 deletions packages/playwright-core/src/client/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,12 @@ export class APIRequestContext extends ChannelOwner<channels.APIRequestContextCh
assert(options.maxRedirects === undefined || options.maxRedirects >= 0, `'maxRedirects' must be greater than or equal to '0'`);
assert(options.maxRetries === undefined || options.maxRetries >= 0, `'maxRetries' must be greater than or equal to '0'`);
const url = options.url !== undefined ? options.url : options.request!.url();
const params = mapParamsToArray(options.params);
const method = options.method || options.request?.method();
let encodedParams = undefined;
if (typeof options.params === 'string')
encodedParams = options.params;
else if (options.params instanceof URLSearchParams)
encodedParams = options.params.toString();
// Cannot call allHeaders() here as the request may be paused inside route handler.
const headersObj = options.headers || options.request?.headers();
const headers = headersObj ? headersObjectToArray(headersObj) : undefined;
Expand Down Expand Up @@ -228,7 +232,8 @@ export class APIRequestContext extends ChannelOwner<channels.APIRequestContextCh
};
const result = await this._channel.fetch({
url,
params,
params: typeof options.params === 'object' ? objectToArray(options.params) : undefined,
encodedParams,
method,
headers,
postData: postDataBuffer,
Expand Down Expand Up @@ -407,30 +412,6 @@ function objectToArray(map?: { [key: string]: any }): NameValue[] | undefined {
return result;
}

function queryStringToArray(queryString: string): NameValue[] | undefined {
const searchParams = new URLSearchParams(queryString);
return searchParamsToArray(searchParams);
}

function searchParamsToArray(searchParams: URLSearchParams): NameValue[] | undefined {
if (searchParams.size === 0)
return undefined;

const result: NameValue[] = [];
for (const [name, value] of searchParams.entries())
result.push({ name, value });
return result;
}

function mapParamsToArray(params: FetchOptions['params']): NameValue[] | undefined {
if (params instanceof URLSearchParams)
return searchParamsToArray(params);
if (typeof params === 'string')
return queryStringToArray(params);

return objectToArray(params);
}

function isFilePayload(value: any): boolean {
return typeof value === 'object' && value['name'] && value['mimeType'] && value['buffer'];
}
1 change: 1 addition & 0 deletions packages/playwright-core/src/protocol/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ scheme.APIRequestContextInitializer = tObject({
});
scheme.APIRequestContextFetchParams = tObject({
url: tString,
encodedParams: tOptional(tString),
params: tOptional(tArray(tType('NameValue'))),
method: tOptional(tString),
headers: tOptional(tArray(tType('NameValue'))),
Expand Down
4 changes: 3 additions & 1 deletion packages/playwright-core/src/server/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ export abstract class APIRequestContext extends SdkObject {
}

const requestUrl = new URL(params.url, defaults.baseURL);
if (params.params) {
if (params.encodedParams) {
requestUrl.search = params.encodedParams;
} else if (params.params) {
for (const { name, value } of params.params)
requestUrl.searchParams.append(name, value);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/protocol/src/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ export interface APIRequestContextChannel extends APIRequestContextEventTarget,
}
export type APIRequestContextFetchParams = {
url: string,
encodedParams?: string,
params?: NameValue[],
method?: string,
headers?: NameValue[],
Expand All @@ -332,6 +333,7 @@ export type APIRequestContextFetchParams = {
maxRetries?: number,
};
export type APIRequestContextFetchOptions = {
encodedParams?: string,
params?: NameValue[],
method?: string,
headers?: NameValue[],
Expand Down
1 change: 1 addition & 0 deletions packages/protocol/src/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ APIRequestContext:
fetch:
parameters:
url: string
encodedParams: string?
params:
type: array?
items: NameValue
Expand Down

0 comments on commit e5d6ee5

Please sign in to comment.