Skip to content

Instantly share code, notes, and snippets.

@carlrip
Last active August 18, 2019 19:42
Show Gist options
  • Save carlrip/cddaabf14d4b36f29d50accd6837006d to your computer and use it in GitHub Desktop.
Save carlrip/cddaabf14d4b36f29d50accd6837006d to your computer and use it in GitHub Desktop.
Specific functions for different http methods
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,
args: RequestInit = { method: "post", body: JSON.stringify(body) }
): Promise<IHttpResponse<T>> => {
return await http<T>(new Request(path, args));
};
export const put = async <T>(
path: string,
body: any,
args: RequestInit = { method: "put", body: JSON.stringify(body) }
): Promise<IHttpResponse<T>> => {
return await http<T>(new Request(path, args));
};
...
// example consuming code
const response = await post<{ id: number }>(
"https://jsonplaceholder.typicode.com/posts",
{ title: "my post", body: "some content" }
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment