Skip to content

Commit 2ae9a69

Browse files
author
Vlad Balin
committed
Preparing for the release
1 parent 73399d9 commit 2ae9a69

File tree

3 files changed

+160
-98
lines changed

3 files changed

+160
-98
lines changed

docs/chapters/io.md

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,39 @@ An endpoint represents the persistent collection of records serialized to JSON.
3030
3131
All IOEndpoint methods must return abortable promises (created with `createIOPromise()`). An IOEndpoint instance is shared by all of the class instances it's attached to and therefore it's normally *must be stateless*.
3232
33+
### `endpoint` restfulIO( url, options? )
34+
35+
HTTP REST client endpoint. Requires `window.fetch` available natively or through the polyfill. Implements standard BackboneJS REST semantic.
36+
37+
All I/O methods append an optional `options.params` object to the URL parameters translating them to string with `JSON.stringify()`.
38+
39+
- `record.save()` makes:
40+
- `POST url`, if the model has no id. Expects to receive `{ id : recordId }`.
41+
- `PUT url/:id`, if the model has an id.
42+
- `collection.fetch()` makes `GET url`.
43+
- `record.destroy()` makes `DELETE url`.
44+
45+
Supports URI relative to owner (`./relative/url` resolves as `/owner/:id/relative/url/:id` ).
46+
47+
```javascript
48+
import { restfulIO } from 'type-r/endpoints/restful'
49+
50+
@define class Role extends Record {
51+
static endpoint = restfulIO( '/api/roles' );
52+
...
53+
}
54+
55+
@define class User extends Record {
56+
static endpoint = restfulIO( '/api/users' );
57+
58+
static attributes = {
59+
// Roles collection here has relative url /api/users/:user_id/roles/
60+
roles : Role.Collection.has.endpoint( restfulIO( './roles' ) ),
61+
...
62+
}
63+
}
64+
```
65+
3366
### `endpoint` memoryIO( mockData?, delay? )
3467
3568
Endpoint for mock testing. Takes optional array with mock data, and optional `delay` parameter which is the simulated I/O delay in milliseconds.
@@ -77,21 +110,22 @@ const store = new PageStore();
77110
store.fetch().then( () => renderUI() );
78111
```
79112
80-
### `endpoint` httpRestIO( url )
113+
### `endpoint` proxyIO( RecordCtor )
81114
82-
<aside class="warning">
83-
Not implemented yet, is scheduled for v2.1
84-
</aside>
115+
Create IO endpoint from the Record class. This endpoint is designed for use on the server side with a data layer managed by Type-R.
116+
117+
Assuming that you have Type-R records with endpoints working with the database, you can create an endpoint which will use
118+
an existing Record subclass as a transport. This endpoint can be connected to the RESTful endpoint API on the server side which will serve JSON to the restfulIO endpoint on the client.
85119
86-
Simple HTTP REST endpoint.
120+
An advantage of this approach is that JSON schema will be transparently validated on the server side by the Type-R.
87121
88-
- `create()` makes `POST url`, expecting to receive `{ id : recordId }` or other object used to update the record.
89-
- `read()` makes `GET url/:id`.
90-
- `update()` makes `PUT url/:id`.
91-
- `destroy()` makes `DELETE url/:id`.
92-
- `list()` makes `GET url`.
122+
```javascript
123+
import { proxyIO } from 'type-r/endpoint/proxy'
124+
125+
...
93126

94-
All I/O methods append an optional `options.params` object to the URL parameters translating them to string using `JSON.stringify()`.
127+
const usersIO = proxyIO( User );
128+
```
95129
96130
### endpoint.read( id, options, record )
97131

0 commit comments

Comments
 (0)