@alchemy_cms/json_api 0.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +11 -0
- package/README.md +64 -0
- package/dist/alchemy-json_api.js +125 -0
- package/dist/deserialize.js +86 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Copyright 2020 Martin Meyerhoff, Thomas von Deyen
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
+
|
|
5
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
+
|
|
7
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
+
|
|
9
|
+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10
|
+
|
|
11
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# Alchemy::JsonApi
|
|
2
|
+
|
|
3
|
+
A JSON-API based API for AlchemyCMS
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
### In your Alchemy Rails project
|
|
8
|
+
|
|
9
|
+
Add this line to your application's Gemfile:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'alchemy-json_api'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
And then execute:
|
|
16
|
+
```bash
|
|
17
|
+
$ bundle
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Or install it yourself as:
|
|
21
|
+
```bash
|
|
22
|
+
$ gem install alchemy-json_api
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### In your JS/Frontend app
|
|
26
|
+
|
|
27
|
+
Run this in your application:
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
yarn add "@alchemy_cms/json_api"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
### In your Rails app
|
|
36
|
+
|
|
37
|
+
Mount the engine in your Alchemy Rails app like this:
|
|
38
|
+
|
|
39
|
+
```rb
|
|
40
|
+
# config/routes.rb
|
|
41
|
+
mount Alchemy::JsonApi::Engine => "/jsonapi/"
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
> __NOTE__ Pick any path you like. This will be the **prefix** of your API URLs
|
|
45
|
+
|
|
46
|
+
### In your frontend app
|
|
47
|
+
|
|
48
|
+
This repo provides an NPM package with deserializers to help you convert the response into JS objects.
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
import { deserializePages } from "@alchemy_cms/json_api"
|
|
52
|
+
|
|
53
|
+
const response = await fetch("/jsonapi/pages.json")
|
|
54
|
+
const data = await response.json()
|
|
55
|
+
const pages = deserializePages(data)
|
|
56
|
+
|
|
57
|
+
console.log(pages[0].name) // => Homepage
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Contributing
|
|
61
|
+
Contribution directions go here.
|
|
62
|
+
|
|
63
|
+
## License
|
|
64
|
+
The gem is available as open source under the terms of the [BSD-3-Clause license](https://opensource.org/licenses/BSD-3-Clause).
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var structuredClone = require('@ungap/structured-clone');
|
|
6
|
+
|
|
7
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
+
|
|
9
|
+
var structuredClone__default = /*#__PURE__*/_interopDefaultLegacy(structuredClone);
|
|
10
|
+
|
|
11
|
+
function deserialize(originalResponse, options = {}) {
|
|
12
|
+
const response = structuredClone__default["default"](originalResponse);
|
|
13
|
+
|
|
14
|
+
if (!options) {
|
|
15
|
+
options = {};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const included = response.included || [];
|
|
19
|
+
|
|
20
|
+
if (Array.isArray(response.data)) {
|
|
21
|
+
return response.data.map(data => {
|
|
22
|
+
return parseJsonApiSimpleResourceData(data, included, false);
|
|
23
|
+
});
|
|
24
|
+
} else {
|
|
25
|
+
return parseJsonApiSimpleResourceData(response.data, included, false);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function parseJsonApiSimpleResourceData(data, included, useCache, options) {
|
|
30
|
+
if (!included.cached) {
|
|
31
|
+
included.cached = {};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!(data.type in included.cached)) {
|
|
35
|
+
included.cached[data.type] = {};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (useCache && data.id in included.cached[data.type]) {
|
|
39
|
+
return included.cached[data.type][data.id];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const attributes = data.attributes || {};
|
|
43
|
+
const resource = attributes;
|
|
44
|
+
resource.id = data.id;
|
|
45
|
+
included.cached[data.type][data.id] = resource;
|
|
46
|
+
|
|
47
|
+
if (data.relationships) {
|
|
48
|
+
for (const relationName of Object.keys(data.relationships)) {
|
|
49
|
+
const relationRef = data.relationships[relationName];
|
|
50
|
+
|
|
51
|
+
if (Array.isArray(relationRef.data)) {
|
|
52
|
+
const items = [];
|
|
53
|
+
relationRef.data.forEach(relationData => {
|
|
54
|
+
const item = findJsonApiIncluded(included, relationData.type, relationData.id);
|
|
55
|
+
items.push(item);
|
|
56
|
+
});
|
|
57
|
+
resource[relationName] = items;
|
|
58
|
+
} else if (relationRef && relationRef.data) {
|
|
59
|
+
resource[relationName] = findJsonApiIncluded(included, relationRef.data.type, relationRef.data.id);
|
|
60
|
+
} else {
|
|
61
|
+
resource[relationName] = null;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return resource;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function findJsonApiIncluded(included, type, id, options) {
|
|
70
|
+
let found = null;
|
|
71
|
+
included.forEach(item => {
|
|
72
|
+
if (item.type === type && item.id === id) {
|
|
73
|
+
found = parseJsonApiSimpleResourceData(item, included, true);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
if (!found) {
|
|
78
|
+
found = {
|
|
79
|
+
id
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return found;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function filterDeprecatedElements(elements) {
|
|
87
|
+
const els = [];
|
|
88
|
+
elements.forEach(element => {
|
|
89
|
+
var _element$nested_eleme, _element$essences;
|
|
90
|
+
|
|
91
|
+
if (((_element$nested_eleme = element.nested_elements) === null || _element$nested_eleme === void 0 ? void 0 : _element$nested_eleme.length) > 0) {
|
|
92
|
+
element.nested_elements = filterDeprecatedElements(element.nested_elements);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (((_element$essences = element.essences) === null || _element$essences === void 0 ? void 0 : _element$essences.length) > 0) {
|
|
96
|
+
element.essences = element.essences.filter(essence => {
|
|
97
|
+
return !essence.deprecated;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!element.deprecated) {
|
|
102
|
+
els.push(element);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
return els;
|
|
106
|
+
} // Returns deserialized page without deprecated content
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
function deserializePage(pageData) {
|
|
110
|
+
const page = deserialize(pageData);
|
|
111
|
+
page.elements = filterDeprecatedElements(page.elements);
|
|
112
|
+
return page;
|
|
113
|
+
} // Returns deserialized pages without deprecated content
|
|
114
|
+
|
|
115
|
+
function deserializePages(pagesData) {
|
|
116
|
+
const pages = deserialize(pagesData);
|
|
117
|
+
pages.forEach(page => {
|
|
118
|
+
page.elements = filterDeprecatedElements(page.elements);
|
|
119
|
+
});
|
|
120
|
+
return pages;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
exports.deserialize = deserialize;
|
|
124
|
+
exports.deserializePage = deserializePage;
|
|
125
|
+
exports.deserializePages = deserializePages;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var structuredClone = require('@ungap/structured-clone');
|
|
6
|
+
|
|
7
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
+
|
|
9
|
+
var structuredClone__default = /*#__PURE__*/_interopDefaultLegacy(structuredClone);
|
|
10
|
+
|
|
11
|
+
function deserialize(originalResponse, options = {}) {
|
|
12
|
+
const response = structuredClone__default["default"](originalResponse);
|
|
13
|
+
|
|
14
|
+
if (!options) {
|
|
15
|
+
options = {};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const included = response.included || [];
|
|
19
|
+
|
|
20
|
+
if (Array.isArray(response.data)) {
|
|
21
|
+
return response.data.map(data => {
|
|
22
|
+
return parseJsonApiSimpleResourceData(data, included, false);
|
|
23
|
+
});
|
|
24
|
+
} else {
|
|
25
|
+
return parseJsonApiSimpleResourceData(response.data, included, false);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function parseJsonApiSimpleResourceData(data, included, useCache, options) {
|
|
30
|
+
if (!included.cached) {
|
|
31
|
+
included.cached = {};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!(data.type in included.cached)) {
|
|
35
|
+
included.cached[data.type] = {};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (useCache && data.id in included.cached[data.type]) {
|
|
39
|
+
return included.cached[data.type][data.id];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const attributes = data.attributes || {};
|
|
43
|
+
const resource = attributes;
|
|
44
|
+
resource.id = data.id;
|
|
45
|
+
included.cached[data.type][data.id] = resource;
|
|
46
|
+
|
|
47
|
+
if (data.relationships) {
|
|
48
|
+
for (const relationName of Object.keys(data.relationships)) {
|
|
49
|
+
const relationRef = data.relationships[relationName];
|
|
50
|
+
|
|
51
|
+
if (Array.isArray(relationRef.data)) {
|
|
52
|
+
const items = [];
|
|
53
|
+
relationRef.data.forEach(relationData => {
|
|
54
|
+
const item = findJsonApiIncluded(included, relationData.type, relationData.id);
|
|
55
|
+
items.push(item);
|
|
56
|
+
});
|
|
57
|
+
resource[relationName] = items;
|
|
58
|
+
} else if (relationRef && relationRef.data) {
|
|
59
|
+
resource[relationName] = findJsonApiIncluded(included, relationRef.data.type, relationRef.data.id);
|
|
60
|
+
} else {
|
|
61
|
+
resource[relationName] = null;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return resource;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function findJsonApiIncluded(included, type, id, options) {
|
|
70
|
+
let found = null;
|
|
71
|
+
included.forEach(item => {
|
|
72
|
+
if (item.type === type && item.id === id) {
|
|
73
|
+
found = parseJsonApiSimpleResourceData(item, included, true);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
if (!found) {
|
|
78
|
+
found = {
|
|
79
|
+
id
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return found;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
exports.deserialize = deserialize;
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@alchemy_cms/json_api",
|
|
3
|
+
"version": "0.26.0",
|
|
4
|
+
"description": "JSON API deserializer for AlchemyCMS",
|
|
5
|
+
"license": "BSD-3-Clause",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/AlchemyCMS/alchemy-json_api.git"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"AlchemyCMS",
|
|
12
|
+
"json:api",
|
|
13
|
+
"cms",
|
|
14
|
+
"headless"
|
|
15
|
+
],
|
|
16
|
+
"authors": [
|
|
17
|
+
"Martin Meyerhoff",
|
|
18
|
+
"Thomas von Deyen"
|
|
19
|
+
],
|
|
20
|
+
"main": "dist/alchemy-json_api.js",
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@ungap/structured-clone": "^1.0.1"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@babel/core": "^7.19.0",
|
|
29
|
+
"@babel/preset-env": "^7.19.0",
|
|
30
|
+
"@rollup/plugin-babel": "^5.3.1",
|
|
31
|
+
"babel-jest": "^29.0.2",
|
|
32
|
+
"jest": "^29.0.2",
|
|
33
|
+
"prettier": "^2.7.1",
|
|
34
|
+
"rollup": "^2.79.0"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "rollup --config",
|
|
38
|
+
"test": "jest"
|
|
39
|
+
}
|
|
40
|
+
}
|