Last active
October 16, 2019 20:28
-
-
Save ntucker/9f68213417b786c2cfcd81c2d2119089 to your computer and use it in GitHub Desktop.
base resource definition
This file contains 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
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