axios-miniprogram/src/core/flattenHeaders.ts

36 lines
693 B
TypeScript
Raw Normal View History

2021-05-11 10:22:44 +08:00
import { Headers, AxiosRequestConfig } from '../types';
import { isUndefined, omit } from '../helpers/utils';
import { methodToLowercase } from './transformMethod';
/**
*
*
* @param config Axios
*/
export default function flattenHeaders(config: AxiosRequestConfig): Headers {
const { headers } = config;
if (isUndefined(headers)) {
return {};
}
const method = methodToLowercase(config.method);
return {
...(headers.common ?? {}),
...(headers[method] ?? {}),
...omit(
headers,
'common',
'options',
'get',
'head',
'post',
'put',
'delete',
'trace',
'connect'
),
};
}