10000 Merge branch 'master' into release/0.21.4 · darth-coder-js/axios@66c4602 · GitHub
[go: up one dir, main page]

Skip to content

Commit 66c4602

Browse files
authored
Merge branch 'master' into release/0.21.4
2 parents fc15665 + 90205f8 commit 66c4602

File tree

10 files changed

+81
-39
lines changed

10 files changed

+81
-39
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,15 @@ These are the available config options for making requests. Only the `url` is re
456456
// automatically. If set to `true` will also remove the 'content-encoding' header
457457
// from the responses objects of all decompressed responses
458458
// - Node only (XHR cannot turn off decompression)
459-
decompress: true, // default
459+
decompress: true // default
460+
461+
// `insecureHTTPParser` boolean.
462+
// Indicates where to use an insecure HTTP parser that accepts invalid HTTP headers.
463+
// This may allow interoperability with non-conformant HTTP implementations.
464+
// Using the insecure parser should be avoided.
465+
// see options https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_http_request_url_options_callback
466+
// see also https://nodejs.org/en/blog/vulnerability/february-2020-security-releases/#strict-http-header-parsing-none
467+
insecureHTTPParser: undefined // default
460468

461469
// transitional options for backward compatibility that may be removed in the newer versions
462470
transitional: {

index.d.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export interface AxiosTransformer {
2-
(data: any, headers?: any): any;
2+
(data: any, headers?: Record<string, string>): any;
33
}
44

55
export interface AxiosAdapter {
@@ -47,16 +47,16 @@ export interface TransitionalOptions{
4747
clarifyTimeoutError: boolean;
4848
}
4949

50-
export interface AxiosRequestConfig {
50+
export interface AxiosRequestConfig<T = any> {
5151
url?: string;
5252
method?: Method;
5353
baseURL?: string;
5454
transformRequest?: AxiosTransformer | AxiosTransformer[];
5555
transformResponse?: AxiosTransformer | AxiosTransformer[];
56-
headers?: any;
56+
headers?: Record<string, string>;
5757
params?: any;
5858
paramsSerializer?: (params: any) => string;
59-
data?: any;
59+
data?: T;
6060
timeout?: number;
6161
timeoutErrorMessage?: string;
6262
withCredentials?: boolean;
@@ -80,16 +80,16 @@ export interface AxiosRequestConfig {
8080
transitional?: TransitionalOptions
8181
}
8282

83-
export interface AxiosResponse<T = any> {
83+
export interface AxiosResponse<T = never> {
8484
data: T;
8585
status: number;
8686
statusText: string;
87-
headers: any;
88-
config: AxiosRequestConfig;
87+
headers: Record<string, string>;
88+
config: AxiosRequestConfig<T>;
8989
request?: any;
9090
}
9191

92-
export interface AxiosError<T = any> extends Error {
92+
export interface AxiosError<T = never> extends Error {
9393
config: AxiosRequestConfig;
9494
code?: string;
9595
request?: any;
@@ -98,7 +98,7 @@ export interface AxiosError<T = any> extends Error {
9898
toJSON: () => object;
9999
}
100100

101-
export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {
101+
export interface AxiosPromise<T = never> extends Promise<AxiosResponse<T>> {
102102
}
103103

104104
export interface CancelStatic {
@@ -134,29 +134,34 @@ export interface AxiosInterceptorManager<V> {
134134
eject(id: number): void;
135135
}
136136

137-
export interface AxiosInstance {
138-
(config: AxiosRequestConfig): AxiosPromise;
139-
(url: string, config?: AxiosRequestConfig): AxiosPromise;
137+
export class Axios {
138+
constructor(config?: AxiosRequestConfig);
140139
defaults: AxiosRequestConfig;
141140
interceptors: {
142141
request: AxiosInterceptorManager<AxiosRequestConfig>;
143142
response: AxiosInterceptorManager<AxiosResponse>;
144143
};
145144
getUri(config?: AxiosRequestConfig): string;
146-
request<T = any, R = AxiosResponse<T>> (config: AxiosRequestConfig): Promise<R>;
147-
get<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
148-
delete<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
149-
head<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
150-
options<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
151-
post<T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>;
152-
put<T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>;
153-
patch<T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>;
145+
request<T = never, R = AxiosResponse<T>> (config: AxiosRequestConfig<T>): Promise<R>;
146+
get<T = never, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
147+
delete<T = never, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
148+
head<T = never, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
149+
options<T = never, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
150+
post<T = never, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
151+
put<T = never, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
152+
patch<T = never, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
153+
}
154+
155+
export interface AxiosInstance extends Axios {
156+
(config: AxiosRequestConfig): AxiosPromise;
157+
(url: string, config?: AxiosRequestConfig): AxiosPromise;
154158
}
155159

156160
export interface AxiosStatic extends AxiosInstance {
157161
create(config?: AxiosRequestConfig): AxiosInstance;
158162
Cancel: CancelStatic;
159163
CancelToken: CancelTokenStatic;
164+
Axios: typeof Axios;
160165
isCancel(value: any): boolean;
161166
all<T>(values: (T | Promise<T>)[]): Promise<T[]>;
162167
spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;

lib/adapters/http.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ module.exports = function httpAdapter(config) {
198198
options.maxBodyLength = config.maxBodyLength;
199199
}
200200

201+
if (config.insecureHTTPParser) {
202+
options.insecureHTTPParser = config.insecureHTTPParser;
203+
}
204+
201205
// Create the request
202206
var req = transport.request(options, function handleResponse(res) {
203207
if (req.aborted) return;

lib/axios.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ function createInstance(defaultConfig) {
2222
// Copy context to instance
2323
utils.extend(instance, context);
2424

25+
// Factory for creating new instances
26+
instance.create = function create(instanceConfig) {
27+
return createInstance(mergeConfig(defaultConfig, instanceConfig));
28+
};
29+
2530
return instance;
2631
}
2732

@@ -31,11 +36,6 @@ var axios = createInstance(defaults);
3136
// Expose Axios class to allow class inheritance
3237
axios.Axios = Axios;
3338

34-
// Factory for creating new instances
35-
axios.create = function create(instanceConfig) {
36-
return createInstance(mergeConfig(axios.defaults, instanceConfig));
37-
};
38-
3939
// Expose Cancel & CancelToken
4040
axios.Cancel = require('./cancel/Cancel');
4141
axios.CancelToken = require('./cancel/CancelToken');

lib/core/enhanceError.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ module.exports = function enhanceError(error, config, code, request, response) {
3535
stack: this.stack,
3636
// Axios
3737
config: this.config,
38-
code: this.code
38+
code: this.code,
39+
status: this.response && this.response.status ? this.response.status : null
3940
};
4041
};
4142
return error;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.21.4",
44
"description": "Promise based HTTP client for the browser and node.js",
55
"main": "index.js",
6+
"types": "index.d.ts",
67
"scripts": {
78
"test": "grunt test",
89
"start": "node ./sandbox/server.js",

sandbox/client.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"/>
66
<style type="text/css">
77
pre {
8-
max-height: 200px;
98
min-height: 39px;
109
overflow: auto;
1110
}
@@ -58,6 +57,11 @@ <h3>Response</h3>
5857
<pre id="response">No Data</pre>
5958
</div>
6059

60+
<div class="well">
61+
<h3>Error</h3>
62+
<pre id="error">None</pre>
63+
</div>
64+
6165
<script src="/axios.js"></script>
6266
<script>
6367
(function () {
@@ -81,6 +85,7 @@ <h3>Response</h3>
8185
var submit = document.getElementById('submit');
8286
var request = document.getElementById('request');
8387
var response = document.getElementById('response');
88+
var error = document.getElementById('error');
8489

8590
function acceptsData(method) {
8691
return ['PATCH', 'POST', 'PUT'].indexOf(method) > -1;
@@ -138,8 +143,11 @@ <h3>Response</h3>
138143
axios(options)
139144
.then(function (res) {
140145
response.innerHTML = JSON.stringify(res.data, null, 2);
146+
error.innerHTML = "None";
141147
})
142148
.catch(function (res) {
149+
error.innerHTML = JSON.stringify(res.toJSON(), null, 2)
150+
console.error('Axios caught an error from request', res.toJSON());
143151
response.innerHTML = JSON.stringify(res.data, null, 2);
144152
});
145153
};

test/specs/core/createError.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('core::createError', function() {
2323
expect(json.message).toBe('Boom!');
2424
expect(json.config).toEqual({ foo: 'bar' });
2525
expect(json.code).toBe('ESOMETHING');
26+
expect(json.status).toBe(200);
2627
expect(json.request).toBe(undefined);
2728
expect(json.response).toBe(undefined);
2829
});

test/specs/core/enhanceError.spec.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var enhanceError = require('../../../lib/core/enhanceError');
22

33
describe('core::enhanceError', function() {
4-
it('should add config, config, request and response to error', function() {
4+
it('should add config, code, request, response, and toJSON function to error', function() {
55
var error = new Error('Boom!');
66
var request = { path: '/foo' };
77
var response = { status: 200, data: { foo: 'bar' } };
@@ -11,9 +11,19 @@ describe('core::enhanceError', function() {
1111
expect(error.code).toBe('ESOMETHING');
1212
expect(error.request).toBe(request);
1313
expect(error.response).toBe(response);
14+
expect(typeof error.toJSON).toBe('function');
1415
expect(error.isAxiosError).toBe(true);
1516
});
1617

18+
it('should serialize to JSON with a status of null when there is no response', function() {
19+
var error = new Error('Boom!');
20+
var request = { path: '/foo' };
21+
var response = undefined;
22+
23+
var errorAsJson = enhanceError(error, { foo: 'bar' }, 'ESOMETHING', request, response).toJSON();
24+
expect(errorAsJson.status).toEqual(null);
25+
});
26+
1727
it('should return error', function() {
1828
var error = new Error('Boom!');
1929
expect(enhanceError(error, { foo: 'bar' }, 'ESOMETHING')).toBe(error);

test/typescript/axios.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ axios.patch('/user', { foo: 'bar' })
111111
.catch(handleError);
112112

113113
// Typed methods
114+
interface UserCreationDef {
115+
name: string;
116+
}
117+
114118
interface User {
115119
id: number;
116120
name: string;
@@ -138,7 +142,7 @@ axios.get<User>('/user', { params: { id: 12345 } })
138142
axios.head<User>('/user')
139143
.then(handleUserResponse)
140144
.catch(handleError);
141-
145+
142146
axios.options<User>('/user')
143147
.then(handleUserResponse)
144148
.catch(handleError);
@@ -147,19 +151,19 @@ axios.delete<User>('/user')
147151
.then(handleUserResponse)
148152
.catch(handleError);
149153

150-
axios.post<User>('/user', { foo: 'bar' })
154+
axios.post<User>('/user', { name: 'foo', id: 1 })
151155
.then(handleUserResponse)
152156
.catch(handleError);
153157

154-
axios.post<User>('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } })
158+
axios.post<User>('/user', { name: 'foo', id: 1 }, { headers: { 'X-FOO': 'bar' } })
155159
.then(handleUserResponse)
156160
.catch(handleError);
157161

158-
axios.put<User>('/user', { foo: 'bar' })
162+
axios.put<User>('/user', { name: 'foo', id: 1 })
159163
.then(handleUserResponse)
160164
.catch(handleError);
161165

162-
axios.patch<User>('/user', { foo: 'bar' })
166+
axios.patch<User>('/user', { name: 'foo', id: 1 })
163167
.then(handleUserResponse)
164168
.catch(handleError);
165169

@@ -189,19 +193,19 @@ axios.delete<User, string>('/user')
189193
.then(handleStringResponse)
190194
.catch(handleError);
191195

192-
axios.post<User, string>('/user', { foo: 'bar' })
196+
axios.post<Partial<UserCreationDef>, string>('/user', { name: 'foo' })
193197
.then(handleStringResponse)
194198
.catch(handleError);
195199

196-
axios.post<User, string>('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } })
200+
axios.post<Partial<UserCreationDef>, string>('/user', { name: 'foo' }, { headers: { 'X-FOO': 'bar' } })
197201
.then(handleStringResponse)
198202
.catch(handleError);
199203

200-
axios.put<User, string>('/user', { foo: 'bar' })
204+
axios.put<Partial<UserCreationDef>, string>('/user', { name: 'foo' })
201205
.then(handleStringResponse)
202206
.catch(handleError);
203207

204-
axios.patch<User, string>('/user', { foo: 'bar' })
208+
axios.patch<Partial<UserCreationDef>, string>('/user', { name: 'foo' })
205209
.then(handleStringResponse)
206210
.catch(handleError);
207211

@@ -292,7 +296,7 @@ axios.interceptors.response.use((response: AxiosResponse) => Promise.resolve(res
292296
// Adapters
293297

294298
const adapter: AxiosAdapter = (config: AxiosRequestConfig) => {
295-
const response: AxiosResponse = {
299+
const response: AxiosResponse<any> = {
296300
data: { foo: 'bar' },
297301
status: 200,
298302
statusText: 'OK',

0 commit comments

Comments
 (0)
0