Skip to content
This repository was archived by the owner on Mar 20, 2022. It is now read-only.

Commit a40e638

Browse files
committed
Add output examples to API Usage docs
Closes gh-202
1 parent e7e0770 commit a40e638

File tree

1 file changed

+85
-5
lines changed

1 file changed

+85
-5
lines changed

docs/api.md

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ Normalizes input data per the schema definition provided.
2020
```js
2121
import { normalize, schema } from 'normalizr';
2222

23-
const myData = { ... };
23+
const myData = { users: [ { id: 1 }, { id: 2 } ] };
2424
const user = new schema.Entity('users');
2525
const mySchema = { users: [ user ] }
26-
const result = normalize(myData, mySchema);
26+
const normalizedData = normalize(myData, mySchema);
2727
```
2828

2929
## `schema`
@@ -56,6 +56,24 @@ const userSchema = new schema.Entity('users');
5656
const userListSchema = new schema.Array(userSchema);
5757
// or use shorthand syntax:
5858
const userListSchema = [ userSchema ];
59+
60+
const normalizedData = normalize(data, userListSchema);
61+
```
62+
63+
#### Output
64+
65+
```js
66+
{
67+
entities: {
68+
users: {
69+
'123': { id: '123', name: 'Jim' },
70+
'456': { id: '456', name: 'Jane' }
71+
}
72+
},
73+
result: {
74+
users: [ '123', '456' ]
75+
}
76+
}
5977
```
6078

6179
If your input data is an array of more than one type of entity, it is necessary to define a schema mapping. For example:
@@ -73,6 +91,21 @@ const myArray = new schema.Array({
7391
const normalizedData = normalize(data, myArray);
7492
```
7593

94+
#### Output
95+
96+
```js
97+
{
98+
entities: {
99+
admins: { '1': { id: 1, type: 'admin' } },
100+
users: { '2': { id: 2, type: 'user' } }
101+
},
102+
result: [
103+
{ id: 1, schema: 'admins' },
104+
{ id: 2, schema: 'users' }
105+
]
106+
}
107+
```
108+
76109
### `Entity(key, definition = {}, options = {})`
77110

78111
* `key`: **required** The key name under which all entities of this type will be listed in the normalized response. Must be a string name.
@@ -101,6 +134,8 @@ You *do not* need to define any keys in your entity other than those that hold n
101134
#### Usage
102135

103136
```js
137+
const data = { id_str: '123', url: 'https://twitter.com', user: { id_str: '456', name: 'Jimmy' } };
138+
104139
const user = new schema.Entity('users', {}, { idAttribute: 'id_str' });
105140
const tweet = new schema.Entity('tweets', { user: user }, {
106141
idAttribute: 'id_str',
@@ -117,6 +152,18 @@ const tweet = new schema.Entity('tweets', { user: user }, {
117152
const normalizedData = normalize(data, tweet);
118153
```
119154

155+
#### Output
156+
157+
```js
158+
{
159+
entities: {
160+
tweets: { '123': { id_str: '123', user: '456' } },
161+
users: { '456': { id_str: '456', name: 'Jimmy' } }
162+
},
163+
result: '123'
164+
}
165+
```
166+
120167
### `Object(definition)`
121168

122169
Define a plain object mapping that has values needing to be normalized into Entities. *Note: The same behavior can be defined with shorthand syntax: `{ ... }`*
@@ -132,16 +179,27 @@ You *do not* need to define any keys in your object other than those that hold o
132179

133180
```js
134181
// Example data response
135-
const data = { users: [ /*...*/ ] };
182+
const data = { users: [ { id: '123', name: 'Beth' } ] };
136183

137-
const user = new schema.Entity('users')
184+
const user = new schema.Entity('users');
138185
const responseSchema = new schema.Object({ users: new schema.Array(user) });
139186
// or shorthand
140187
const responseSchema = { users: new schema.Array(user) };
141188

142189
const normalizedData = normalize(data, responseSchema);
143190
```
144191

192+
#### Output
193+
194+
```js
195+
{
196+
entities: {
197+
users: { '123': { id_str: '123', name: 'Beth' } }
198+
},
199+
result: { users: [ '123' ] }
200+
}
201+
```
202+
145203
### `Union(definition, schemaAttribute)`
146204

147205
Describe a schema which is a union of multiple schemas. This is useful if you need the polymorphic behavior provided by `schema.Array` or `schema.Values` but for non-collection fields.
@@ -160,7 +218,7 @@ Can be a string or a function. If given a function, accepts the following argume
160218
#### Usage
161219

162220
```js
163-
const data = { owner: { id: 1, type: 'user' } };
221+
const data = { owner: { id: 1, type: 'user', name: 'Anne' } };
164222

165223
const user = new schema.Entity('users');
166224
const group = new schema.Entity('groups');
@@ -172,6 +230,17 @@ const unionSchema = new schema.Union({
172230
const normalizedData = normalize(data, { owner: unionSchema });
173231
```
174232

233+
#### Output
234+
235+
```js
236+
{
237+
entities: {
238+
users: { '1': { id: 1, type: 'user', name: 'Anne' } }
239+
},
240+
result: { owner: { id: 1, schema: 'user' } }
241+
}
242+
```
243+
175244
### `Values(definition, schemaAttribute)`
176245

177246
Describes a map whose values follow the given schema.
@@ -197,3 +266,14 @@ const valuesSchema = new schema.Values(item);
197266

198267
const normalizedData = normalize(data, valuesSchema);
199268
```
269+
270+
#### Output
271+
272+
```js
273+
{
274+
entities: {
275+
items: { '1': { id: 1 }, '2': { id: 2 } }
276+
},
277+
result: { firstThing: 1, secondThing: 2 }
278+
}
279+
```

0 commit comments

Comments
 (0)