Skip to content

Instantly share code, notes, and snippets.

@andywer
Last active July 8, 2016 11:59
Show Gist options
  • Select an option

  • Save andywer/513a4d878de8e04f3ff5fcb97c844330 to your computer and use it in GitHub Desktop.

Select an option

Save andywer/513a4d878de8e04f3ff5fcb97c844330 to your computer and use it in GitHub Desktop.
Resticle usage idea (ID-based, writeable)
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)
}
@andywer
Copy link
Author

andywer commented Jul 8, 2016

Must be able to provide endpoint href on createModel() for ID-based API. Like api.createModel('post', '/api/posts'). Could then do something like Post.fetch('some-id'). Shouldn't be a problem to distinguish an href from an ID passed to fetch().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment