This repository has been archived by the owner on Nov 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for validators that are promise-based or throw errors
- Loading branch information
1 parent
5bd928c
commit 086f94a
Showing
9 changed files
with
152 additions
and
26 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
This is a simple live demo of `restul-react` components primarily used to test features. Run it with `npm run example`. |
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,2 @@ | ||
<div id="app"></div> | ||
<script src="index.tsx"></script> |
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,32 @@ | ||
import * as React from "react"; | ||
import { render } from "react-dom"; | ||
|
||
import { Get, RestfulProvider } from "../src"; | ||
|
||
const wait = timeout => | ||
new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve(); | ||
}, timeout); | ||
}); | ||
|
||
const App: React.SFC<{}> = props => ( | ||
<RestfulProvider base="https://dog.ceo"> | ||
<Get path="/api/breeds/list/all" resolve={res => wait(1500).then(() => Promise.resolve(Object.keys(res.message)))}> | ||
{(breeds, { loading, error }) => { | ||
if (loading) { | ||
return "loading.."; | ||
} | ||
if (error) { | ||
return <code>{JSON.stringify(error)}</code>; | ||
} | ||
if (breeds) { | ||
return <ul>{breeds.map((breed, breedIndex) => <li key={breedIndex}>{breed}</li>)}</ul>; | ||
} | ||
return null; | ||
}} | ||
</Get> | ||
</RestfulProvider> | ||
); | ||
|
||
render(<App />, document.querySelector("#app")); |
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,12 @@ | ||
// Shared types across exported components and utils | ||
// | ||
/** | ||
* A function that resolves returned data from | ||
* a fetch call. | ||
*/ | ||
export type ResolveFunction<T> = ((data: any) => T) | ((data: any) => Promise<T>); | ||
|
||
export interface GetDataError<TError> { | ||
message: string; | ||
data: TError | string; | ||
} |
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,32 @@ | ||
import { GetDataError, ResolveFunction } from "../types"; | ||
|
||
export const resolveData = async <TData, TError>({ | ||
data, | ||
resolve, | ||
}: { | ||
data: any; | ||
resolve?: ResolveFunction<TData>; | ||
}): Promise<{ data: TData | null; error: GetDataError<TError> | null }> => { | ||
let resolvedData: TData | null = null; | ||
let resolveError: GetDataError<TError> | null = null; | ||
try { | ||
if (resolve) { | ||
const resolvedDataOrPromise: TData | Promise<TData> = resolve(data); | ||
resolvedData = (resolvedDataOrPromise as { then?: any }).then | ||
? ((await resolvedDataOrPromise) as TData) | ||
: (resolvedDataOrPromise as TData); | ||
} else { | ||
resolvedData = data; | ||
} | ||
} catch (err) { | ||
resolvedData = null; | ||
resolveError = { | ||
message: "RESOLVE_ERROR", | ||
data: JSON.stringify(err), | ||
}; | ||
} | ||
return { | ||
data: resolvedData, | ||
error: resolveError, | ||
}; | ||
}; |