This file contains hidden or 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
export const http = async (request: RequestInfo): Promise<any> => { | |
return new Promise(resolve => { | |
fetch(request) | |
.then(response => response.json()) | |
.then(body => { | |
resolve(body); | |
}); | |
}); | |
}; |
This file contains hidden or 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
export const http = <T>(request: RequestInfo): Promise<T> => { | |
return new Promise((resolve) => { | |
fetch(request) | |
.then(response => response.json()) | |
.then(body => { | |
resolve(body); | |
}); | |
}); | |
}; | |
This file contains hidden or 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
interface IHttpResponse<T> extends Response { | |
parsedBody?: T; | |
} | |
export const http = <T>(request: RequestInfo): Promise<IHttpResponse<T>> => { | |
let response: IHttpResponse<T>; | |
return new Promise(resolve => { | |
fetch(request) | |
.then(res => { | |
response = res; | |
return res.json(); |
This file contains hidden or 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
export interface IHttpResponse<T> extends Response { | |
parsedBody?: T; | |
} | |
export const http = <T>(request: RequestInfo): Promise<IHttpResponse<T>> => { | |
return new Promise((resolve, reject) => { | |
let response: IHttpResponse<T>; | |
fetch(request) | |
.then(res => { | |
response = res; |
This file contains hidden or 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
const response = await http<{ id: number }>( | |
new Request("https://jsonplaceholder.typicode.com/posts", { | |
method: "post", | |
body: JSON.stringify({ title: "my post", body: "some content" }) | |
}) | |
); |
This file contains hidden or 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
export const get = async <T>( | |
path: string, | |
args: RequestInit = { method: "get" } | |
): Promise<IHttpResponse<T>> => { | |
return await http<T>(new Request(path, args)); | |
}; | |
export const post = async <T>( | |
path: string, | |
body: any, |
This file contains hidden or 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
interface IPeopleState { | |
readonly people: IPerson[]; | |
readonly loading: boolean; | |
readonly posting: boolean; | |
} | |
export interface IAppState { | |
readonly peopleState: IPeopleState; | |
} |
This file contains hidden or 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
export interface IGettingPeopleAction extends Action<'GettingPeople'> {} | |
export interface IGotPeopleAction extends Action<'GotPeople'> { | |
people: IPerson[]; | |
} | |
export interface IPostingPersonAction extends Action<'PostingPerson'> { | |
type: 'PostingPerson'; | |
} |
This file contains hidden or 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
export const getPeopleActionCreator: ActionCreator< | |
ThunkAction< | |
Promise<IGotPeopleAction>, // The type of the last action to be dispatched - will always be promise<T> for async actions | |
IPerson[], // The type for the data within the last action | |
null, // The type of the parameter for the nested function | |
IGotPeopleAction // The type of the last action to be dispatched | |
> | |
> = () => { | |
return async (dispatch: Dispatch) => { | |
const gettingPeopleAction: IGettingPeopleAction = { |
This file contains hidden or 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
export const postPersonActionCreator: ActionCreator< | |
ThunkAction< | |
Promise<IPostedPersonAction>, // The type of the last action to be dispatched - will always be promise<T> for async actions | |
IPostPersonResult, // The type for the data within the last action | |
IPostPerson, // The type of the parameter for the nested function | |
IPostedPersonAction // The type of the last action to be dispatched | |
> | |
> = (person: IPostPerson) => { | |
return async (dispatch: Dispatch) => { | |
const postingPersonAction: IPostingPersonAction = { |
OlderNewer