-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor(getDiscussion): more accurate parsing and safer error handling See guardian/discussion-api for type definitions * feat(result): success and failure wrapper * feat(JSON): wrap in Result type --------- Co-authored-by: Jamie B <[email protected]>
- Loading branch information
Showing
11 changed files
with
128 additions
and
69 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
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
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,21 @@ | ||
import type { Result } from './result'; | ||
|
||
/** | ||
* Safely fetch JSON from a URL. | ||
* | ||
* If successful, the value is typed as `unknown`. | ||
*/ | ||
export const fetchJSON = async ( | ||
...args: Parameters<typeof fetch> | ||
): Promise<Result<'ApiError' | 'NetworkError', unknown>> => { | ||
try { | ||
const response = await fetch(...args); | ||
return { kind: 'ok', value: await response.json() }; | ||
} catch (error) { | ||
if (error instanceof SyntaxError) { | ||
return { kind: 'error', error: 'ApiError' }; | ||
} | ||
|
||
return { kind: 'error', error: 'NetworkError' }; | ||
} | ||
}; |
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,11 @@ | ||
type Result<Err, Value> = | ||
| { | ||
kind: 'ok'; | ||
value: Value; | ||
} | ||
| { | ||
kind: 'error'; | ||
error: Err; | ||
}; | ||
|
||
export type { Result }; |
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