|
1 | | -# Normalize-JSON-API |
2 | | -Normalizing a JSON:API styled server response |
| 1 | +# Normalize JSON:API Response |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +[](https://www.npmjs.com/package/normalize-json-api-response) |
| 6 | +[](https://www.npmjs.com/package/normalize-json-api-response) |
| 7 | +[](https://github.com/SinestroWhite/Normalize-JSON-API/blob/master/LICENSE) |
| 8 | + |
| 9 | +> *Normalize JSON:API Response (NJAR) is not only designed to simplify JSON:API responses but also to make them easy developer friendly.* |
| 10 | +> |
| 11 | +> *Created by Sinestro White with :heart: !* |
| 12 | +
|
| 13 | +## Features |
| 14 | + |
| 15 | +- **Easy installation and integration** - No complicated actions |
| 16 | +- **JSON:API simplification** |
| 17 | +- **No need to correct the normalized response** |
| 18 | +- **Front-end design friendly** - Makes response data easy to display with a for-loop |
| 19 | +- **No schema required** |
| 20 | +- **No dependencies** 👌 |
| 21 | + |
| 22 | +## Installation |
| 23 | + |
| 24 | +Using [npm](https://www.npmjs.com/package/normalize-json-api-response): |
| 25 | + |
| 26 | +``` |
| 27 | + $ npm i normalize-json-api-response |
| 28 | +``` |
| 29 | + |
| 30 | +Then, using a module bundler that supports either CommonJS or ES2015 modules, such as [webpack](https://github.com/webpack/webpack): |
| 31 | + |
| 32 | +```js |
| 33 | + import normalize from 'normalize-json-api-response'; |
| 34 | +``` |
| 35 | + |
| 36 | +## Usage |
| 37 | + |
| 38 | +### Basic Example |
| 39 | + |
| 40 | +When working with the JSON:API specification, the response body of any request is optimized and it can get difficult to |
| 41 | +do computations. The main information is contained in the "data" property but if there are relations to other tables they |
| 42 | +are put in the "included" property. |
| 43 | + |
| 44 | +```js |
| 45 | + //Example server response |
| 46 | + const data = { |
| 47 | + data: { |
| 48 | + type: "articles", |
| 49 | + id: "1", |
| 50 | + attributes: { |
| 51 | + title: "JSON:API paints my bikeshed!" |
| 52 | + }, |
| 53 | + relationships: { |
| 54 | + author: { |
| 55 | + data: { type: "people", id: "9" } |
| 56 | + } |
| 57 | + } |
| 58 | + }, |
| 59 | + included: [ |
| 60 | + { |
| 61 | + type: "people", |
| 62 | + id: "9", |
| 63 | + attributes: { |
| 64 | + firstName: "Dan", |
| 65 | + lastName: "Gebhardt", |
| 66 | + twitter: "dgeb" |
| 67 | + } |
| 68 | + } |
| 69 | + ] |
| 70 | + }; |
| 71 | + |
| 72 | +``` |
| 73 | + |
| 74 | +Normalize JSON:API Response (NJAR) solves the problem with the JSON:API response optimization by moving every item from |
| 75 | +"included" to the item from "data" it belongs to. |
| 76 | + |
| 77 | +```js |
| 78 | + import normalize from 'normalize-json-api-response'; |
| 79 | + console.log(normalize(data)); |
| 80 | + |
| 81 | + // Normalized object |
| 82 | + // { |
| 83 | + // "people": [ |
| 84 | + // { |
| 85 | + // "id": "9", |
| 86 | + // "attributes": { |
| 87 | + // "firstName": "Dan", |
| 88 | + // "lastName": "Gebhardt", |
| 89 | + // "twitter": "dgeb" |
| 90 | + // } |
| 91 | + // } |
| 92 | + // ], |
| 93 | + // "articles": [ |
| 94 | + // { |
| 95 | + // "id": "1", |
| 96 | + // "attributes": { |
| 97 | + // "title": "JSON:API paints my bikeshed!" |
| 98 | + // }, |
| 99 | + // "relationships": { |
| 100 | + // "people": [ |
| 101 | + // { |
| 102 | + // "type": "people", |
| 103 | + // "id": "9", |
| 104 | + // "attributes": { |
| 105 | + // "firstName": "Dan", |
| 106 | + // "lastName": "Gebhardt", |
| 107 | + // "twitter": "dgeb" |
| 108 | + // } |
| 109 | + // } |
| 110 | + // ] |
| 111 | + // } |
| 112 | + // } |
| 113 | + // ] |
| 114 | + // } |
| 115 | + |
| 116 | +``` |
| 117 | + |
| 118 | +As you can see the "articles" property is an array of objects so that every item can be accessed easily with a simple for-loop. |
| 119 | +The information from "included" has been moved in the "relationships" property of every item in "articles". |
| 120 | + |
| 121 | +## Why should I use this? |
| 122 | + |
| 123 | +There are already a number of great JSON:API normalizing packages out there (for instance, [json-api-normalizer](https://www.npmjs.com/package/json-api-normalizer) is fantastic). |
| 124 | +However, most of those packages do not provide a simple way to access the included information from every "data" item, |
| 125 | +which has some severe limitations. In this case, you have to create additional functions to correct the normalized response. |
| 126 | + |
| 127 | +# FAQ |
| 128 | + |
| 129 | +## Dependencies |
| 130 | + |
| 131 | +NJAR has no dependencies. |
| 132 | + |
| 133 | +## Reporting Issues |
| 134 | + |
| 135 | +If believe you've found an issue, please [report it](https://github.com/SinestroWhite/Normalize-JSON-API/issues) along with any relevant details to reproduce it. |
| 136 | + |
| 137 | +## Asking for help |
| 138 | + |
| 139 | +Please file an issue for personal support requests. Tag them with `question`. |
| 140 | + |
| 141 | +## Contributions |
| 142 | + |
| 143 | +Yes please! Feature requests / pull requests are welcome. |
| 144 | + |
0 commit comments