mirror of
https://github.com/tenrok/axios.git
synced 2026-06-08 17:22:34 +03:00
Fix AxiosRequestHeaders & AxiosHeaders types; (#5482)
This commit is contained in:
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+15
-2
@@ -1,5 +1,8 @@
|
||||
type AxiosHeaderValue = AxiosHeaders | string | string[] | number | boolean | null;
|
||||
type RawAxiosHeaders = Record<string, AxiosHeaderValue>;
|
||||
|
||||
interface RawAxiosHeaders {
|
||||
[key: string]: AxiosHeaderValue;
|
||||
}
|
||||
|
||||
type MethodsHeaders = {
|
||||
[Key in axios.Method as Lowercase<Key>]: AxiosHeaders;
|
||||
@@ -23,6 +26,8 @@ declare class AxiosHeaders {
|
||||
headers?: RawAxiosHeaders | AxiosHeaders
|
||||
);
|
||||
|
||||
[key: string]: any;
|
||||
|
||||
set(headerName?: string, value?: AxiosHeaderValue, rewrite?: boolean | AxiosHeaderMatcher): AxiosHeaders;
|
||||
set(headers?: RawAxiosHeaders | AxiosHeaders, rewrite?: boolean): AxiosHeaders;
|
||||
|
||||
@@ -66,6 +71,8 @@ declare class AxiosHeaders {
|
||||
setContentEncoding: AxiosHeaderSetter;
|
||||
getContentEncoding: AxiosHeaderGetter;
|
||||
hasContentEncoding: AxiosHeaderTester;
|
||||
|
||||
[Symbol.iterator](): IterableIterator<[string, AxiosHeaderValue]>;
|
||||
}
|
||||
|
||||
declare class AxiosError<T = unknown, D = any> extends Error {
|
||||
@@ -194,9 +201,15 @@ type InternalAxiosError<T = unknown, D = any> = AxiosError<T, D>;
|
||||
declare namespace axios {
|
||||
type AxiosError<T = unknown, D = any> = InternalAxiosError<T, D>;
|
||||
|
||||
type CommonRequestHeadersList = 'Accept' | 'Content-Type' | 'Content-Length' | 'User-Agent'| 'Content-Encoding';
|
||||
|
||||
type RawCommonRequestHeaders = {
|
||||
[Key in CommonRequestHeadersList]: AxiosHeaderValue;
|
||||
};
|
||||
|
||||
type RawAxiosRequestHeaders = Partial<RawAxiosHeaders & MethodsHeaders & CommonHeaders>;
|
||||
|
||||
type AxiosRequestHeaders = RawAxiosRequestHeaders & AxiosHeaders;
|
||||
type AxiosRequestHeaders = Partial<RawCommonRequestHeaders & RawAxiosHeaders> & AxiosHeaders;
|
||||
|
||||
type RawAxiosResponseHeaders = Partial<Record<string, string> & {
|
||||
"set-cookie"?: string[]
|
||||
|
||||
Vendored
+15
-2
@@ -1,6 +1,9 @@
|
||||
// TypeScript Version: 4.7
|
||||
type AxiosHeaderValue = AxiosHeaders | string | string[] | number | boolean | null;
|
||||
type RawAxiosHeaders = Record<string, AxiosHeaderValue>;
|
||||
|
||||
interface RawAxiosHeaders {
|
||||
[key: string]: AxiosHeaderValue;
|
||||
}
|
||||
|
||||
type MethodsHeaders = {
|
||||
[Key in Method as Lowercase<Key>]: AxiosHeaders;
|
||||
@@ -24,6 +27,8 @@ export class AxiosHeaders {
|
||||
headers?: RawAxiosHeaders | AxiosHeaders
|
||||
);
|
||||
|
||||
[key: string]: any;
|
||||
|
||||
set(headerName?: string, value?: AxiosHeaderValue, rewrite?: boolean | AxiosHeaderMatcher): AxiosHeaders;
|
||||
set(headers?: RawAxiosHeaders | AxiosHeaders, rewrite?: boolean): AxiosHeaders;
|
||||
|
||||
@@ -67,11 +72,19 @@ export class AxiosHeaders {
|
||||
setContentEncoding: AxiosHeaderSetter;
|
||||
getContentEncoding: AxiosHeaderGetter;
|
||||
hasContentEncoding: AxiosHeaderTester;
|
||||
|
||||
[Symbol.iterator](): IterableIterator<[string, AxiosHeaderValue]>;
|
||||
}
|
||||
|
||||
type CommonRequestHeadersList = 'Accept' | 'Content-Type' | 'Content-Length' | 'User-Agent'| 'Content-Encoding';
|
||||
|
||||
type RawCommonRequestHeaders = {
|
||||
[Key in CommonRequestHeadersList]: AxiosHeaderValue;
|
||||
};
|
||||
|
||||
export type RawAxiosRequestHeaders = Partial<RawAxiosHeaders & MethodsHeaders & CommonHeaders>;
|
||||
|
||||
export type AxiosRequestHeaders = RawAxiosRequestHeaders & AxiosHeaders;
|
||||
export type AxiosRequestHeaders = Partial<RawCommonRequestHeaders & RawAxiosHeaders> & AxiosHeaders;
|
||||
|
||||
export type RawAxiosResponseHeaders = Partial<Record<string, string> & {
|
||||
"set-cookie"?: string[]
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
||||
const remove = async (file) => {
|
||||
console.log(`✓ Remove entry '${file}'...`);
|
||||
try {
|
||||
await sleep(100);
|
||||
await sleep(1000);
|
||||
await fs.remove(file);
|
||||
} catch (err) {
|
||||
console.warn(err.message);
|
||||
|
||||
@@ -441,3 +441,51 @@ axios.get('/user', {
|
||||
console.log(e.rate);
|
||||
}
|
||||
});
|
||||
|
||||
// AxiosHeaders
|
||||
|
||||
// iterator
|
||||
|
||||
const headers = new axios.AxiosHeaders({foo: "bar"})
|
||||
|
||||
for (const [header, value] of headers) {
|
||||
console.log(header, value);
|
||||
}
|
||||
|
||||
// index signature
|
||||
|
||||
(()=>{
|
||||
const headers = new axios.AxiosHeaders({x:1});
|
||||
|
||||
headers.y = 2;
|
||||
})();
|
||||
|
||||
|
||||
// AxiosRequestHeaders
|
||||
|
||||
(()=>{
|
||||
const headers:axios.AxiosRequestHeaders = new axios.AxiosHeaders({x:1});
|
||||
|
||||
headers.y = 2;
|
||||
|
||||
headers.get('x');
|
||||
})();
|
||||
|
||||
// AxiosHeaders instance assigment
|
||||
|
||||
{
|
||||
const requestInterceptorId: number = axios.interceptors.request.use(
|
||||
async (config) => {
|
||||
config.headers.Accept ="foo";
|
||||
config.headers.setAccept("foo");
|
||||
config.headers = new axios.AxiosHeaders({x:1});
|
||||
config.headers.foo = "1";
|
||||
config.headers.set('bar', '2');
|
||||
config.headers.set({myHeader: "myValue"})
|
||||
config.headers = new axios.AxiosHeaders({myHeader: "myValue"});
|
||||
config.headers = {...config.headers} as axios.AxiosRequestHeaders;
|
||||
return config;
|
||||
},
|
||||
(error: any) => Promise.reject(error)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -569,3 +569,50 @@ axios.get('/user', {
|
||||
axios.get('/user', {
|
||||
adapter: ['xhr', 'http']
|
||||
});
|
||||
|
||||
// AxiosHeaders
|
||||
|
||||
// iterator
|
||||
|
||||
const headers = new AxiosHeaders({foo: "bar"})
|
||||
|
||||
for (const [header, value] of headers) {
|
||||
console.log(header, value);
|
||||
}
|
||||
|
||||
// index signature
|
||||
|
||||
(()=>{
|
||||
const headers = new AxiosHeaders({x:1});
|
||||
|
||||
headers.y = 2;
|
||||
})();
|
||||
|
||||
// AxiosRequestHeaders
|
||||
|
||||
(()=>{
|
||||
const headers:AxiosRequestHeaders = new AxiosHeaders({x:1});
|
||||
|
||||
headers.y = 2;
|
||||
|
||||
headers.get('x');
|
||||
})();
|
||||
|
||||
// AxiosHeaders instance assigment
|
||||
|
||||
{
|
||||
const requestInterceptorId: number = axios.interceptors.request.use(
|
||||
async (config) => {
|
||||
config.headers.Accept ="foo";
|
||||
config.headers.setAccept("foo");
|
||||
config.headers = new AxiosHeaders({x:1});
|
||||
config.headers.foo = "1";
|
||||
config.headers.set('bar', '2');
|
||||
config.headers.set({myHeader: "myValue"})
|
||||
config.headers = new axios.AxiosHeaders({myHeader: "myValue"});
|
||||
config.headers = {...config.headers} as AxiosRequestHeaders;
|
||||
return config;
|
||||
},
|
||||
(error: any) => Promise.reject(error)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user