1212 */
1313
1414/**
15+ * @public
1516 * @typedef Options
1617 * @property {string } [url] the URL to request
1718 * @property {string } [method="get"] HTTP method, case-insensitive
1819 * @property {Headers } [headers] Request headers
20+ * @property {FormData|string|object } [body] a body, optionally encoded, to send
1921 * @property {'text'|'json'|'stream'|'blob'|'arrayBuffer'|'formData'|'stream' } [responseType="text"] An encoding to use for the response
2022 * @property {string } [auth] Authorization header value to send with the request
2123 * @property {string } [xsrfCookieName] Pass an Cross-site Request Forgery prevention cookie value as a header defined by `xsrfHeaderName`
2527 */
2628
2729/**
30+ * @public
2831 * @typedef Headers
2932 * @type {{[name: string]: string} }
3033 */
3134
3235/**
36+ * @public
3337 * @typedef Response
34- * @property {Options } config the resolved request configuration
35- * @property {FormData|string|object } [body] a body, optionally encoded, to send
38+ * @property {Options } config the request configuration
39+ * @property {any } data the decoded response body
3640 */
3741
42+ /** */
3843export default ( function create ( defaults ) {
3944 defaults = defaults || { } ;
4045
4146 /**
4247 * Creates a request factory bound to the given HTTP method.
48+ * @private
4349 * @param {string } method
44- * @param {boolean } allowBody
45- * @returns {(url: string, config?: Options, body?) => Promise<Response> }
50+ * @returns {(url: string, config?: Options) => Promise<Response> }
4651 */
47- function createMethod ( method , allowBody ) {
48- return ( url , config , alt ) => {
49- let data ;
50- if ( allowBody ) {
51- data = config ;
52- config = alt ;
53- }
54- config = Object . assign ( { method } , config ) ;
55- return axios ( url , data , config ) ;
56- } ;
52+ function createBodylessMethod ( method ) {
53+ return ( url , config ) => redaxios ( url , Object . assign ( { method } , config ) ) ;
5754 }
5855
59- axios . request = axios ;
60- axios . get = createMethod ( 'get' , false ) ;
61- axios . delete = createMethod ( 'delete' , false ) ;
62- axios . options = createMethod ( 'options' , false ) ;
63- axios . post = createMethod ( 'post' , true ) ;
64- axios . put = createMethod ( 'put' , true ) ;
65- axios . patch = createMethod ( 'patch' , true ) ;
56+ /**
57+ * Creates a request factory bound to the given HTTP method.
58+ * @private
59+ * @param {string } method
60+ * @returns {(url: string, body?: any, config?: Options) => Promise<Response> }
61+ */
62+ function createBodyMethod ( method ) {
63+ return ( url , data , config ) => redaxios ( url , Object . assign ( { method, data } , config ) ) ;
64+ }
65+
66+ /**
67+ * @public
68+ * @type {((config?: Options) => Promise<Response>) | ((url: string, config?: Options) => Promise<Response>) }
69+ */
70+ redaxios . request = redaxios ;
71+
72+ /** @public */
73+ redaxios . get = createBodylessMethod ( 'get' ) ;
6674
67- axios . all = Promise . all ;
68- axios . spread = function ( fn ) {
75+ /** @public */
76+ redaxios . delete = createBodylessMethod ( 'delete' ) ;
77+
78+ /** @public */
79+ redaxios . options = createBodylessMethod ( 'options' ) ;
80+
81+ /** @public */
82+ redaxios . post = createBodyMethod ( 'post' ) ;
83+
84+ /** @public */
85+ redaxios . put = createBodyMethod ( 'put' ) ;
86+
87+ /** @public */
88+ redaxios . patch = createBodyMethod ( 'patch' ) ;
89+
90+ /** @public */
91+ redaxios . all = Promise . all ;
92+
93+ /** @public */
94+ redaxios . spread = function ( fn ) {
6995 return function ( results ) {
7096 return fn . apply ( this , results ) ;
7197 } ;
@@ -86,12 +112,7 @@ export default (function create(defaults) {
86112 for ( i in overrides ) {
87113 let key = lowerCase ? i . toLowerCase ( ) : i ;
88114 if ( key === 'headers' ) lowerCase = true ;
89- if ( i in out ) {
90- out [ key ] = deepMerge ( out [ key ] , overrides [ i ] , lowerCase ) ;
91- }
92- else {
93- out [ key ] = overrides [ i ] ;
94- }
115+ out [ key ] = i in out ? deepMerge ( out [ key ] , overrides [ i ] , lowerCase ) : overrides [ i ] ;
95116 }
96117 return out ;
97118 }
@@ -104,22 +125,18 @@ export default (function create(defaults) {
104125
105126 /**
106127 * Issues a request.
107- * @param { string } url the URL to fetch
108- * @param {any } [data] request body to send
109- * @param {Options } [config] configuration for the request
128+ * @public
129+ * @param {string } [url]
130+ * @param {Options } [config]
110131 * @returns {Promise<Response> }
111132 */
112- function axios ( url , data , config ) {
113- let options = config ;
133+ function redaxios ( url , config ) {
114134 if ( typeof url !== 'string' ) {
115- options = url ;
116- url = options . url ;
117- }
118- else if ( config === undefined ) {
119- options = data ;
120- data = undefined ;
135+ config = url ;
136+ url = config . url ;
121137 }
122- options = deepMerge ( defaults , options ) || { } ;
138+ const options = deepMerge ( defaults , config || { } ) ;
139+ let data = options . data ;
123140
124141 if ( options . transformRequest ) {
125142 for ( let i = 0 ; i < options . transformRequest . length ; i ++ ) {
@@ -151,9 +168,10 @@ export default (function create(defaults) {
151168 customHeaders . Authorization = options . auth ;
152169 }
153170
154- const response = {
155- config
156- } ;
171+ /** @type {Response } */
172+ const response = { } ;
173+ response . config = config ;
174+
157175 return fetch ( url , {
158176 method : options . method ,
159177 body : data ,
@@ -176,7 +194,9 @@ export default (function create(defaults) {
176194 } ) ;
177195 }
178196
179- axios . CancelToken = self . AbortController || Object ;
197+ redaxios . CancelToken = self . AbortController || Object ;
198+
199+ redaxios . create = create ;
180200
181- return axios ;
201+ return redaxios ;
182202} ) ( ) ;
0 commit comments