-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BUGFIX release] Remove ember-fetch dependency.
This change removes ember-fetch as a direct dependency of ember-data, and allows for this dependency to be provided by the host application or for users to directly use native fetch. Having `ember-fetch` as a direct dependency of ember-data has proven troublesome. There are issues using ember-fetch as a nested dependency (these are documented in the ember-fetch README), but it is also quite difficult for ember-data to ensure that the versions of `ember-fetch` provided will actually include the correct version (duplicated addon merging in the ember-cli addon space is complicated and error prone). This change moves the `determineBodyPromise` and `serializeQueryParams` helper functions directly into `@ember-data/adapter`. A future version of ember-fetch will drop these utilities (as well as the ember-data adapter mixin). In addition, this also enables usage of the global `fetch` if a `fetch` module is not provided.
- Loading branch information
Showing
7 changed files
with
146 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
export { default as parseResponseHeaders } from './utils/parse-response-headers'; | ||
export { determineBodyPromise } from './utils/determine-body-promise'; | ||
export { serializeQueryParams } from './utils/serialize-query-params'; | ||
export { default as fetch } from './utils/fetch'; |
30 changes: 30 additions & 0 deletions
30
packages/adapter/addon/-private/utils/determine-body-promise.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Function that always attempts to parse the response as json, and if an error is thrown, | ||
* returns `undefined` if the response is successful and has a status code of 204 (No Content), | ||
* or 205 (Reset Content) or if the request method was 'HEAD', and the plain payload otherwise. | ||
*/ | ||
export function determineBodyPromise( | ||
response: Response, | ||
requestData: JQueryAjaxSettings | ||
): Promise<object | string | undefined> { | ||
return response.text().then(function(payload) { | ||
let ret: string | object | undefined = payload; | ||
try { | ||
ret = JSON.parse(payload); | ||
} catch (error) { | ||
if (!(error instanceof SyntaxError)) { | ||
throw error; | ||
} | ||
const status = response.status; | ||
if ( | ||
response.ok && | ||
(status === 204 || status === 205 || requestData.method === 'HEAD') | ||
) { | ||
ret = undefined; | ||
} else { | ||
console.warn('This response was unable to be parsed as json.', payload); | ||
} | ||
} | ||
return ret; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import require, { has } from 'require'; | ||
|
||
type MaybeFetch = { | ||
(input: RequestInfo, init?: RequestInit | undefined): Promise<Response>; | ||
} | null; | ||
|
||
let _fetch: MaybeFetch = null; | ||
|
||
if (has('fetch')) { | ||
// use `fetch` module by default, this is commonly provided by ember-fetch | ||
_fetch = require('fetch').default; | ||
} else if (typeof fetch === 'function') { | ||
// fallback to using global fetch | ||
_fetch = fetch; | ||
} | ||
|
||
export default _fetch; |
69 changes: 69 additions & 0 deletions
69
packages/adapter/addon/-private/utils/serialize-query-params.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const RBRACKET = /\[\]$/; | ||
|
||
function isPlainObject(obj: any): boolean { | ||
return Object.prototype.toString.call(obj) === '[object Object]'; | ||
} | ||
|
||
/** | ||
* Helper function that turns the data/body of a request into a query param string. | ||
* This is directly copied from jQuery.param. | ||
*/ | ||
export function serializeQueryParams( | ||
queryParamsObject: object | string | ||
): string { | ||
var s: any[] = []; | ||
|
||
function buildParams(prefix: string, obj: any) { | ||
var i, len, key; | ||
|
||
if (prefix) { | ||
if (Array.isArray(obj)) { | ||
for (i = 0, len = obj.length; i < len; i++) { | ||
if (RBRACKET.test(prefix)) { | ||
add(s, prefix, obj[i]); | ||
} else { | ||
buildParams( | ||
prefix + '[' + (typeof obj[i] === 'object' ? i : '') + ']', | ||
obj[i] | ||
); | ||
} | ||
} | ||
} else if (isPlainObject(obj)) { | ||
for (key in obj) { | ||
buildParams(prefix + '[' + key + ']', obj[key]); | ||
} | ||
} else { | ||
add(s, prefix, obj); | ||
} | ||
} else if (Array.isArray(obj)) { | ||
for (i = 0, len = obj.length; i < len; i++) { | ||
add(s, obj[i].name, obj[i].value); | ||
} | ||
} else { | ||
for (key in obj) { | ||
buildParams(key, obj[key]); | ||
} | ||
} | ||
return s; | ||
} | ||
|
||
return buildParams('', queryParamsObject) | ||
.join('&') | ||
.replace(/%20/g, '+'); | ||
} | ||
|
||
/** | ||
* Part of the `serializeQueryParams` helper function. | ||
*/ | ||
function add(s: Array<any>, k: string, v?: string | (() => string)) { | ||
// Strip out keys with undefined value and replace null values with | ||
// empty strings (mimics jQuery.ajax) | ||
if (v === undefined) { | ||
return; | ||
} else if (v === null) { | ||
v = ''; | ||
} | ||
|
||
v = typeof v === 'function' ? v() : v; | ||
s[s.length] = `${encodeURIComponent(k)}=${encodeURIComponent(v)}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters