forked from directus/directus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improving SDK error/response handling (directus#19539)
* improving error/response handling * updated request function to expose the response on error * updated the composables where needed * Create few-rules-talk.md * ran prettier * undid unintended type change * added missing awaits for onResponse * Update few-rules-talk.md * Mark it as major change * unpack directus errors instead of nesting * ran prettier * Update .changeset/few-rules-talk.md Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch> --------- Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch>
- Loading branch information
1 parent
e2e6747
commit b2cfcdc
Showing
6 changed files
with
50 additions
and
56 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@directus/sdk": minor | ||
--- | ||
|
||
Included the response object in thrown errors and added the request object in the `onResponse` hook |
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,24 @@ | ||
/** | ||
* | ||
* @param {Response} response | ||
* @returns {any} | ||
*/ | ||
export async function extractData(response: Response) { | ||
const type = response.headers.get('Content-Type')?.toLowerCase(); | ||
|
||
if (type?.startsWith('application/json')) { | ||
const result = await response.json(); | ||
if (!response.ok) throw result; | ||
if ('data' in result) return result.data; | ||
return result; | ||
} | ||
|
||
if (type?.startsWith('text/html') || type?.startsWith('text/plain')) { | ||
const result = await response.text(); | ||
if (!response.ok) throw result; | ||
return result; | ||
} | ||
|
||
// empty body fallback | ||
return; | ||
} |
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