Skip to content

Instantly share code, notes, and snippets.

@ntucker
Last active October 16, 2019 20:28
Show Gist options
  • Save ntucker/9f68213417b786c2cfcd81c2d2119089 to your computer and use it in GitHub Desktop.
Save ntucker/9f68213417b786c2cfcd81c2d2119089 to your computer and use it in GitHub Desktop.
base resource definition
class Resource {
constructor(args) {
Object.assign(this, args);
}
static url({ id }: { id: string }) {
return `${this.urlRoot}${id}`;
}
// This generic enables us to establish the correct return value based on the polymorphic call
static detailShape<T extends Resource>(this: T) {
return async ({ id }: { id: string }): T {
const options = {
method: 'GET',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
},
};
const url = this.url({ 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