Skip to content

Instantly share code, notes, and snippets.

@ntucker
Last active October 16, 2019 02:23
Show Gist options
  • Save ntucker/4822ec9d027015e3f1ee5f1fa2adbefe to your computer and use it in GitHub Desktop.
Save ntucker/4822ec9d027015e3f1ee5f1fa2adbefe to your computer and use it in GitHub Desktop.
simple Resource class before normalization
class PostResource {
readonly id: number | null = null;
readonly title: string = '';
readonly body: string = '';
constructor(args) {
Object.assign(this, args);
}
static async fetchDetail({ id }: { id: string }): PostResource {
const options = {
method: 'GET',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
},
};
const url = `/post/${id}`;
const response = await (await fetch(url, options)).json();
return new PostResource(response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment