Last active
July 8, 2016 11:59
-
-
Save andywer/513a4d878de8e04f3ff5fcb97c844330 to your computer and use it in GitHub Desktop.
Resticle usage idea (ID-based, writeable)
This file contains hidden or 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
| import { createApi, Schema } from 'resticle' | |
| import { httpOptions } from 'resticle-middlewares' | |
| const api = createApi() | |
| api.use(httpOptions({ | |
| Authorization: 'Bearer someSuperSecretToken' | |
| })) | |
| const Post = api.createModel('post', { | |
| // allows you to call `myPost.publish()` in order to trigger a POST request to `/api/posts/${myPost.id}/publish` | |
| // ({ publish: Schema.action('/some/path') } would have worked, too, if we just wanted to POST to a static URL) | |
| publish () { | |
| return Schema.action(`/api/posts/${this.id}/publish`, 'POST') | |
| } | |
| }) | |
| createSamplePost() | |
| async function createSamplePost () { | |
| const newPost = await Post.create({ | |
| title: 'This is my first, but awesome post', | |
| text: 'Lorem ipsum sucks. Seriously.' | |
| }) | |
| await newPost.publish() | |
| console.log(`Created new blog post:`, newPost) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Must be able to provide endpoint href on
createModel()for ID-based API. Likeapi.createModel('post', '/api/posts'). Could then do something likePost.fetch('some-id'). Shouldn't be a problem to distinguish an href from an ID passed to fetch().