axios-miniprogram/src/core/flattenHeaders.ts

30 lines
646 B
TypeScript
Raw Normal View History

2023-03-28 21:32:54 +08:00
import { isPlainObject } from '../helpers/isTypes';
import { ignore } from '../helpers/ignore';
2023-03-28 20:48:02 +08:00
import { AxiosRequestConfig, AxiosRequestHeaders } from './Axios';
2023-03-23 20:09:00 +08:00
export function flattenHeaders(
config: AxiosRequestConfig,
): AxiosRequestHeaders | undefined {
if (!isPlainObject(config.headers)) {
return config.headers;
}
return {
...(config.headers.common ?? {}),
2023-03-28 20:48:02 +08:00
...(config.headers[config.method!.toLowerCase()] ?? {}),
...ignore(
2023-03-23 20:09:00 +08:00
config.headers,
'common',
'options',
'get',
'head',
'post',
'put',
2023-04-14 18:16:39 +08:00
'patch',
2023-03-23 20:09:00 +08:00
'delete',
'trace',
'connect',
),
};
}