Skip to content

Instantly share code, notes, and snippets.

@carlrip
carlrip / http.ts
Created January 27, 2019 10:35
async fetch
export const http = async (request: RequestInfo): Promise<any> => {
return new Promise(resolve => {
fetch(request)
.then(response => response.json())
.then(body => {
resolve(body);
});
});
};
@carlrip
carlrip / http.ts
Created January 27, 2019 10:54
async fetch with types
export const http = <T>(request: RequestInfo): Promise<T> => {
return new Promise((resolve) => {
fetch(request)
.then(response => response.json())
.then(body => {
resolve(body);
});
});
};
@carlrip
carlrip / http.ts
Created January 27, 2019 10:57
async fetch with types returning full response
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();
@carlrip
carlrip / http.ts
Created January 27, 2019 11:03
async fetch with types handling errors
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;
@carlrip
carlrip / http.ts
Created January 27, 2019 11:05
POST using http
const response = await http<{ id: number }>(
new Request("https://jsonplaceholder.typicode.com/posts", {
method: "post",
body: JSON.stringify({ title: "my post", body: "some content" })
})
);
@carlrip
carlrip / Store.ts
Created February 3, 2019 10:17
React Redux State with TypeScript
interface IPeopleState {
readonly people: IPerson[];
readonly loading: boolean;
readonly posting: boolean;
}
export interface IAppState {
readonly peopleState: IPeopleState;
}
@carlrip
carlrip / Store.ts
Created February 3, 2019 10:32
React Redux Actions with TypeScript
export interface IGettingPeopleAction extends Action<'GettingPeople'> {}
export interface IGotPeopleAction extends Action<'GotPeople'> {
people: IPerson[];
}
export interface IPostingPersonAction extends Action<'PostingPerson'> {
type: 'PostingPerson';
}
@carlrip
carlrip / Store.ts
Created February 3, 2019 14:25
React Redux Reducer with TypeScript
const peopleReducer: Reducer<IPeopleState, PeopleActions> = (
state = initialPeopleState,
action,
) => {
switch (action.type) {
case 'GettingPeople': {
return {
...state,
loading: true,
};
@carlrip
carlrip / Store.ts
Created February 3, 2019 14:39
React Redux Store with TypeScript
export function configureStore(): Store<IAppState> {
const store = createStore(rootReducer, undefined, applyMiddleware(thunk));
return store;
}
@carlrip
carlrip / App.tsx
Last active February 5, 2019 03:52
React Redux Component with TypeScript
interface IProps {
getPeople: () => Promise<IGotPeopleAction>;
people: IPerson[];
peopleLoading: boolean;
postPerson: (person: IPostPerson) => Promise<IPostedPersonAction>;
personPosting: boolean;
}
const App: FC<IProps> = ({
getPeople,