|
2 | 2 |
|
3 | 3 | This library provides an interface to APIs following the JSONAPI spec. Though we're not striving for 1:1 compatibility with Active Record, you'll see it's the same basic usage, from scopes to error handling.
|
4 | 4 |
|
5 |
| -Written in [Typescript](https://www.typescriptlang.org) and distributed in ES5, this library is isomorphic - use it from the browser, or from node. |
6 |
| - |
7 |
| -(Open to better names for this lib :grin:) |
| 5 | +Written in [Typescript](https://www.typescriptlang.org) but works in plain old ES5 as well. This library is isomorphic - use it from the browser, or from the server with NodeJS. |
8 | 6 |
|
9 | 7 | ### Sample Usage
|
10 | 8 |
|
11 |
| -Please see [our documentation page](https://jsonapi-suite.github.io/jsorm/) for full usage. |
| 9 | +Please see [our documentation page](https://jsonapi-suite.github.io/jsonapi_suite/js/home) for full usage. Below is a Typescript sample: |
12 | 10 |
|
13 |
| -```es6 |
14 |
| -import 'isomorphic-fetch'; |
15 |
| -import { Config, Model, attr, hasMany, belongsTo } from 'jsorm'; |
| 11 | +```ts |
| 12 | +import { JSORMBase, Model, Attr, HasMany } from "jsorm" |
16 | 13 |
|
17 |
| -const Person = Model.extend({ |
18 |
| - static { |
19 |
| - baseUrl: 'http://localhost:3000', |
20 |
| - jsonapiType: 'people' |
21 |
| - }, |
| 14 | +class ApplicationRecord extends JSORMBase { |
| 15 | + static baseUrl = "http://localhost:3000" |
| 16 | + static jsonapiType = "people" |
| 17 | +} |
22 | 18 |
|
23 |
| - firstName: attr(), |
24 |
| - lastName: attr(), |
25 |
| - fullName() { |
26 |
| - return this.firstName + ' ' + this.lastName; |
| 19 | +@Model() |
| 20 | +class Person extends ApplicationRecord { |
| 21 | + @Attr() firstName: string |
| 22 | + @Attr() lastName: string |
| 23 | + |
| 24 | + get fullName() { |
| 25 | + return `${this.firstName} ${this.lastName}` |
27 | 26 | }
|
28 |
| -}); |
29 |
| - |
30 |
| -Config.setup(); |
31 |
| - |
32 |
| -Person.where({ name: 'Joe' }).page(2).per(10).sort('name').all().then((people) => { |
33 |
| - let names = people.data.map((p) => { return p.fullName(); }); |
34 |
| - console.log(names); // ['Joe Blow', 'Joe DiMaggio', ...] |
35 |
| -}); |
36 |
| -``` |
37 |
| - |
38 |
| -### Fetch |
39 |
| - |
40 |
| -Depending on your requirements, you may need a `fetch` polyfill for use |
41 |
| -in legacy browsers or NodeJS. There are a variety of polyfills and ways |
42 |
| -to install, so implementation is up to you. Here are two recommendations: |
43 |
| - |
44 |
| -```js |
45 |
| -// node |
46 |
| -require("isomorphic-fetch") |
| 27 | +} |
| 28 | + |
| 29 | +let { data } = await Person |
| 30 | + .where({ name: 'Joe' }) |
| 31 | + .page(2).per(10) |
| 32 | + .sort('name') |
| 33 | + .all() |
| 34 | + |
| 35 | +let names = data.map((p) => { return p.fullName }) |
| 36 | +console.log(names) // ['Joe Blow', 'Joe DiMaggio', ...] |
47 | 37 | ```
|
48 |
| - |
49 |
| -```js |
50 |
| -// browser |
51 |
| -import "whatwg-fetch" |
52 |
| -``` |
53 |
| - |
54 |
| -### JSON Web Tokens |
55 |
| - |
56 |
| -jsorm supports setting a JWT and using it for all requests. Set it |
57 |
| -during `Config.setup` and all subsequents will pass it using the |
58 |
| -`Authorization` header: |
59 |
| - |
60 |
| -```es6 |
61 |
| -const ApplicationRecord = Model.extend({ |
62 |
| - // code |
63 |
| -}) |
64 |
| - |
65 |
| -const Person = ApplicationRecord.extend({ |
66 |
| - // code |
67 |
| -}) |
68 |
| - |
69 |
| -const Author = ApplicationRecord.extend({ |
70 |
| - // code |
71 |
| -}) |
72 |
| - |
73 |
| -Config.setup({ jwtOwners: [ApplicationRecord] }) |
74 |
| - |
75 |
| -ApplicationRecord.jwt = "s0m3t0k3n" |
76 |
| -Author.all() // sends JWT in Authorization header |
77 |
| -Author.getJWT() // grabs from ApplicationRecord |
78 |
| -Author.setJWT("t0k3n") // sets on ApplicationRecord |
79 |
| -``` |
80 |
| - |
81 |
| -This means you could define `OtherApplicationRecord`, whose |
82 |
| -subclasses could use an alternate JWT for an alternate website. |
83 |
| - |
84 |
| -The token is sent in the following format: |
85 |
| - |
86 |
| -``` |
87 |
| -Authorization: Token token="s0m3t0k3n" |
88 |
| -``` |
89 |
| - |
90 |
| -If your application responds with `X-JWT` in the headers, jsorm will |
91 |
| -use this JWT for all subsequent requests (helpful when |
92 |
| -implementing token expiry). |
93 |
| - |
94 |
| -### Roadmap |
95 |
| - |
96 |
| -* Attribute transforms (type coercion) |
97 |
| -* Improved Error / Validation handling |
0 commit comments