mirror of
				https://github.com/microsoft/playwright.git
				synced 2025-06-26 21:40:17 +00:00 
			
		
		
		
	chore: merge fetch params on server side (#32518)
https://github.com/microsoft/playwright-python/pull/2546#discussion_r1750090592
This commit is contained in:
		
							parent
							
								
									9a313eecc9
								
							
						
					
					
						commit
						e5d6ee5bd8
					
				@ -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;
 | 
			
		||||
@ -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,
 | 
			
		||||
@ -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'];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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'))),
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -318,6 +318,7 @@ export interface APIRequestContextChannel extends APIRequestContextEventTarget,
 | 
			
		||||
}
 | 
			
		||||
export type APIRequestContextFetchParams = {
 | 
			
		||||
  url: string,
 | 
			
		||||
  encodedParams?: string,
 | 
			
		||||
  params?: NameValue[],
 | 
			
		||||
  method?: string,
 | 
			
		||||
  headers?: NameValue[],
 | 
			
		||||
@ -332,6 +333,7 @@ export type APIRequestContextFetchParams = {
 | 
			
		||||
  maxRetries?: number,
 | 
			
		||||
};
 | 
			
		||||
export type APIRequestContextFetchOptions = {
 | 
			
		||||
  encodedParams?: string,
 | 
			
		||||
  params?: NameValue[],
 | 
			
		||||
  method?: string,
 | 
			
		||||
  headers?: NameValue[],
 | 
			
		||||
 | 
			
		||||
@ -287,6 +287,7 @@ APIRequestContext:
 | 
			
		||||
    fetch:
 | 
			
		||||
      parameters:
 | 
			
		||||
        url: string
 | 
			
		||||
        encodedParams: string?
 | 
			
		||||
        params:
 | 
			
		||||
          type: array?
 | 
			
		||||
          items: NameValue
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user