11import Mixin from '@ember/object/mixin' ;
2- import { assign } from '@ember/polyfills'
3- import RSVP from 'rsvp' ;
2+ import { assign } from '@ember/polyfills' ;
3+ import RSVP , { reject } from 'rsvp' ;
44import fetch from 'fetch' ;
55import mungOptionsForFetch from '../utils/mung-options-for-fetch' ;
66import determineBodyPromise from '../utils/determine-body-promise' ;
7+ import DS from 'ember-data' ;
8+ import Mix from '@ember/polyfills/types' ;
9+ import { get } from '@ember/object' ;
10+ import {
11+ PlainObject ,
12+ PlainHeaders ,
13+ Method ,
14+ FetchOptions ,
15+ Nullable
16+ } from 'ember-fetch/types' ;
717
818/**
919 * Helper function to create a plain object from the response's Headers.
1020 * Consumed by the adapter's `handleResponse`.
11- * @param {Headers } headers
12- * @returns {Object }
1321 */
14- export function headersToObject ( headers ) {
15- let headersObject = { } ;
22+ export function headersToObject ( headers : Headers ) : PlainObject {
23+ let headersObject : PlainObject = { } ;
1624
1725 if ( headers ) {
18- headers . forEach ( ( value , key ) => headersObject [ key ] = value ) ;
26+ headers . forEach ( ( value , key ) => ( headersObject [ key ] = value ) ) ;
1927 }
2028
2129 return headersObject ;
2230}
2331
2432export default Mixin . create ( {
2533 /**
26- * @param {String } url
27- * @param {String } type
28- * @param {Object } _options
29- * @returns {Object }
34+ * @property {PlainHeaders } headers
35+ * @public
36+ */
37+ headers : undefined ,
38+
39+ /**
3040 * @override
31- */
32- ajaxOptions ( url , type , options = { } ) {
41+ */
42+ ajaxOptions (
43+ url : string ,
44+ type : Method ,
45+ options : JQueryAjaxSettings = { }
46+ ) : FetchOptions {
3347 options . url = url ;
3448 options . type = type ;
3549
3650 // Add headers set on the Adapter
37- let adapterHeaders = this . get ( 'headers' ) ;
51+ let adapterHeaders = get ( this , 'headers' ) ;
3852 if ( adapterHeaders ) {
39- options . headers = assign ( options . headers || { } , adapterHeaders ) ;
53+ options . headers = assign (
54+ options . headers || { } ,
55+ adapterHeaders as PlainHeaders
56+ ) ;
4057 }
4158
42- const mungedOptions = mungOptionsForFetch ( options ) ;
59+ const mungedOptions = mungOptionsForFetch ( options as Mix <
60+ JQueryAjaxSettings ,
61+ { url : string ; type : Method }
62+ > ) ;
4363
4464 // Mimics the default behavior in Ember Data's `ajaxOptions`, namely to set the
4565 // 'Content-Type' header to application/json if it is not a GET request and it has a body.
@@ -60,65 +80,73 @@ export default Mixin.create({
6080 } ,
6181
6282 /**
63- * @param {String } url
64- * @param {String } type
65- * @param {Object } options
6683 * @override
6784 */
68- ajax ( url , type , options ) {
85+ ajax ( url : string , type : Method , options : object ) {
6986 const requestData = {
7087 url,
71- method : type ,
88+ method : type
7289 } ;
7390
7491 const hash = this . ajaxOptions ( url , type , options ) ;
7592
76- return this . _ajaxRequest ( hash )
77- . catch ( ( error , response , requestData ) => {
78- throw this . ajaxError ( this , response , null , requestData , error ) ;
79- } )
80- . then ( ( response ) => {
81- return RSVP . hash ( {
82- response,
83- payload : determineBodyPromise ( response , requestData )
84- } ) ;
85- } )
86- . then ( ( { response, payload } ) => {
87- if ( response . ok ) {
88- return this . ajaxSuccess ( this , response , payload , requestData ) ;
89- } else {
90- throw this . ajaxError ( this , response , payload , requestData ) ;
91- }
92- } ) ;
93+ return (
94+ this . _ajaxRequest ( hash )
95+ // @ts -ignore
96+ . catch ( ( error , response , requestData ) => {
97+ throw this . ajaxError ( this , response , null , requestData , error ) ;
98+ } )
99+ . then ( ( response : Response ) => {
100+ return RSVP . hash ( {
101+ response,
102+ payload : determineBodyPromise ( response , requestData )
103+ } ) ;
104+ } )
105+ . then (
106+ ( {
107+ response,
108+ payload
109+ } : {
110+ response : Response ;
111+ payload : string | object | undefined ;
112+ } ) => {
113+ if ( response . ok ) {
114+ return this . ajaxSuccess ( this , response , payload , requestData ) ;
115+ } else {
116+ throw this . ajaxError ( this , response , payload , requestData ) ;
117+ }
118+ }
119+ )
120+ ) ;
93121 } ,
94122
95123 /**
96124 * Overrides the `_ajaxRequest` method to use `fetch` instead of jQuery.ajax
97- * @param {Object } options
98125 * @override
99126 */
100- _ajaxRequest ( options ) {
127+ _ajaxRequest (
128+ options : Mix < RequestInit , { url : string } >
129+ ) : RSVP . Promise < Response > {
101130 return this . _fetchRequest ( options . url , options ) ;
102131 } ,
103132
104133 /**
105134 * A hook into where `fetch` is called.
106135 * Useful if you want to override this behavior, for example to multiplex requests.
107- * @param {String } url
108- * @param {Object } options
109136 */
110- _fetchRequest ( url , options ) {
137+ _fetchRequest ( url : string , options : RequestInit ) : RSVP . Promise < Response > {
111138 return fetch ( url , options ) ;
112139 } ,
113140
114141 /**
115- * @param {Object } adapter
116- * @param {Object } response
117- * @param {Object } payload
118- * @param {Object } requestData
119142 * @override
120143 */
121- ajaxSuccess ( adapter , response , payload , requestData ) {
144+ ajaxSuccess (
145+ adapter : any ,
146+ response : Response ,
147+ payload : Nullable < string | object > ,
148+ requestData : { url : string ; method : string }
149+ ) : object | DS . AdapterError | RSVP . Promise < never > {
122150 const returnResponse = adapter . handleResponse (
123151 response . status ,
124152 headersToObject ( response . headers ) ,
@@ -127,36 +155,40 @@ export default Mixin.create({
127155 ) ;
128156
129157 if ( returnResponse && returnResponse . isAdapterError ) {
130- return RSVP . Promise . reject ( returnResponse ) ;
158+ return reject ( returnResponse ) ;
131159 } else {
132160 return returnResponse ;
133161 }
134162 } ,
135163
136-
137164 /**
138165 * Allows for the error to be selected from either the
139166 * response object, or the response data.
140- * @param {Object } response
141- * @param {Object } payload
142167 */
143- parseFetchResponseForError ( response , payload ) {
168+ parseFetchResponseForError (
169+ response : Response ,
170+ payload : object | string
171+ ) : object | string {
144172 return payload || response . statusText ;
145173 } ,
146174
147175 /**
148- * @param {Object } adapter
149- * @param {Object } response
150- * @param {String|Object } payload
151- * @param {Object } requestData
152- * @param {Error } error
153176 * @override
154177 */
155- ajaxError ( adapter , response , payload , requestData , error ) {
178+ ajaxError (
179+ adapter : any ,
180+ response : Response ,
181+ payload : Nullable < string | object > ,
182+ requestData : object ,
183+ error ?: Error
184+ ) : Error | object | DS . AdapterError {
156185 if ( error ) {
157186 return error ;
158187 } else {
159- const parsedResponse = adapter . parseFetchResponseForError ( response , payload ) ;
188+ const parsedResponse = adapter . parseFetchResponseForError (
189+ response ,
190+ payload
191+ ) ;
160192 return adapter . handleResponse (
161193 response . status ,
162194 headersToObject ( response . headers ) ,
0 commit comments