-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: The addon now requires a fetch service that handles authentication. See `dummy/app/services/fetch.js` for an example implementation.
- Loading branch information
Showing
6 changed files
with
66 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ constructor(...args) { | |
"session", | ||
"intl", | ||
"notification", | ||
"fetch", | ||
"alexandria-config", | ||
], | ||
}, | ||
|
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
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import Service, { inject as service } from "@ember/service"; | ||
import { isEmpty } from "@ember/utils"; | ||
import fetch from "fetch"; | ||
|
||
const CONTENT_TYPE = "application/vnd.api+json"; | ||
|
||
const cleanObject = (obj) => { | ||
return Object.entries(obj).reduce((clean, [key, value]) => { | ||
return { | ||
...clean, | ||
...(isEmpty(value) ? {} : { [key]: value }), | ||
}; | ||
}, {}); | ||
}; | ||
|
||
export default class FetchService extends Service { | ||
@service session; | ||
|
||
async fetch(resource, init = {}) { | ||
init.headers = cleanObject({ | ||
...this.session.headers, | ||
accept: CONTENT_TYPE, | ||
"content-type": CONTENT_TYPE, | ||
...(init.headers || {}), | ||
}); | ||
|
||
const response = await fetch(resource, init); | ||
|
||
if (!response.ok) { | ||
if (response.status === 401) { | ||
return this.session.handleUnauthorized(); | ||
} | ||
|
||
const contentType = response.headers.map["content-type"]; | ||
let body = ""; | ||
|
||
if ( | ||
["application/json", "application/vnd.api+json"].includes(contentType) | ||
) { | ||
body = (await response.json())?.errors; | ||
} else if (contentType === "text/plain") { | ||
body = await response.text(); | ||
} | ||
|
||
// throw an error containing a human readable message | ||
throw new Error( | ||
`Fetch request to URL ${response.url} returned ${response.status} ${response.statusText}:\n\n${body}`, | ||
{ cause: body }, | ||
); | ||
} | ||
|
||
return response; | ||
} | ||
} |
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