@alchemy_cms/json_api 3.0.0 → 4.0.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/README.md +17 -60
- package/dist/alchemy-json_api.js +11 -22
- package/dist/alchemyApiDeserializer.d.ts +15 -0
- package/dist/deserialize.d.ts +12 -0
- package/dist/deserialize.js +17 -44
- package/dist/main.d.ts +2 -0
- package/package.json +22 -8
- package/LICENSE +0 -11
package/README.md
CHANGED
|
@@ -1,80 +1,37 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @alchemy_cms/json_api
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The JavaScript/TypeScript deserializer for [AlchemyCMS](https://github.com/AlchemyCMS/alchemy_cms)'s JSON:API.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
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
|
-
|
|
17
7
|
```bash
|
|
18
|
-
$ bundle
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
Or install it yourself as:
|
|
22
|
-
|
|
23
|
-
```bash
|
|
24
|
-
$ gem install alchemy-json_api
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
### In your JS/Frontend app
|
|
28
|
-
|
|
29
|
-
Run this in your application:
|
|
30
|
-
|
|
31
|
-
```
|
|
32
8
|
npm install @alchemy_cms/json_api --save
|
|
33
9
|
```
|
|
34
10
|
|
|
35
|
-
or with the package manager of your choice
|
|
11
|
+
or with the package manager of your choice.
|
|
36
12
|
|
|
37
13
|
## Usage
|
|
38
14
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
# config/routes.rb
|
|
45
|
-
mount Alchemy::JsonApi::Engine => "/jsonapi/"
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
> __NOTE__ Pick any path you like. This will be the **prefix** of your API URLs
|
|
49
|
-
|
|
50
|
-
### In your frontend app
|
|
51
|
-
|
|
52
|
-
This repo provides an NPM package with deserializers to help you convert the response into JS objects.
|
|
15
|
+
`deserialize` converts a JSON:API response into plain, acyclic JS objects:
|
|
16
|
+
each resource's attributes are flattened with its `id`, and relationships
|
|
17
|
+
present in `included` become nested objects (absent ones become `{ id }`
|
|
18
|
+
stubs). Reference cycles are broken automatically, so the result is always
|
|
19
|
+
safe to `JSON.stringify`.
|
|
53
20
|
|
|
54
21
|
```js
|
|
55
|
-
import {
|
|
22
|
+
import { deserialize } from "@alchemy_cms/json_api"
|
|
56
23
|
|
|
57
|
-
const response = await fetch("/jsonapi/pages.json")
|
|
58
|
-
const
|
|
59
|
-
const pages = deserializePages(data)
|
|
24
|
+
const response = await fetch("/jsonapi/pages/homepage.json")
|
|
25
|
+
const page = deserialize(await response.json())
|
|
60
26
|
|
|
61
|
-
console.log(
|
|
27
|
+
console.log(page.name) // => "Homepage"
|
|
62
28
|
```
|
|
63
29
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
If you ever want to change how Alchemy serializes attributes you can set
|
|
67
|
-
|
|
68
|
-
```rb
|
|
69
|
-
Alchemy::JsonApi.key_transform = :camel_lower
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
It defaults to `:underscore`.
|
|
30
|
+
`deserializePage` and `deserializePages` still exist as thin, **deprecated**
|
|
31
|
+
aliases for `deserialize`; prefer `deserialize`.
|
|
73
32
|
|
|
74
33
|
## Contributing
|
|
75
34
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
The gem is available as open source under the terms of the [BSD-3-Clause license](https://opensource.org/licenses/BSD-3-Clause).
|
|
35
|
+
This package lives in the [`javascript/`](https://github.com/AlchemyCMS/alchemy-json_api/tree/main/javascript)
|
|
36
|
+
directory of the `alchemy-json_api` monorepo. Releases are automated with
|
|
37
|
+
release-please and tagged `package-vX.Y.Z`.
|
package/dist/alchemy-json_api.js
CHANGED
|
@@ -1,25 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
((r = e.nested_elements) == null ? void 0 : r.length) > 0 && (e.nested_elements = n(
|
|
7
|
-
e.nested_elements
|
|
8
|
-
)), ((i = e.nestedElements) == null ? void 0 : i.length) > 0 && (e.nestedElements = n(e.nestedElements)), ((c = e.essences) == null ? void 0 : c.length) > 0 && (e.essences = e.essences.filter((f) => !f.deprecated)), e.deprecated || s.push(e);
|
|
9
|
-
}), s;
|
|
1
|
+
import e from "./deserialize.js";
|
|
2
|
+
//#region src/alchemyApiDeserializer.ts
|
|
3
|
+
var t = /* @__PURE__ */ new Set();
|
|
4
|
+
function n(e) {
|
|
5
|
+
t.has(e) || (t.add(e), console.warn(`[@alchemy_cms/json_api] \`${e}\` is deprecated; use \`deserialize\` instead.`));
|
|
10
6
|
}
|
|
11
|
-
function
|
|
12
|
-
|
|
13
|
-
return s.elements = n(s.elements), s;
|
|
7
|
+
function r(t) {
|
|
8
|
+
return n("deserializePage"), e(t);
|
|
14
9
|
}
|
|
15
|
-
function
|
|
16
|
-
|
|
17
|
-
return s.forEach((e) => {
|
|
18
|
-
e.elements = n(e.elements);
|
|
19
|
-
}), s;
|
|
10
|
+
function i(t) {
|
|
11
|
+
return n("deserializePages"), e(t);
|
|
20
12
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
o as deserializePage,
|
|
24
|
-
l as deserializePages
|
|
25
|
-
};
|
|
13
|
+
//#endregion
|
|
14
|
+
export { e as deserialize, r as deserializePage, i as deserializePages };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deserializes a JSON:API page document.
|
|
3
|
+
*
|
|
4
|
+
* @deprecated Use `deserialize` instead. `deserializePage` used to strip
|
|
5
|
+
* deprecated elements, but `deprecated` is an admin-only hint and must not
|
|
6
|
+
* alter the serialized output, so this is now only a thin wrapper around
|
|
7
|
+
* `deserialize`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function deserializePage<T = unknown>(pageData: unknown): T;
|
|
10
|
+
/**
|
|
11
|
+
* Deserializes a collection of JSON:API page documents.
|
|
12
|
+
*
|
|
13
|
+
* @deprecated Use `deserialize` instead; see `deserializePage`.
|
|
14
|
+
*/
|
|
15
|
+
export declare function deserializePages<T = unknown>(pagesData: unknown): T[];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deserialize a JSON:API document into plain, acyclic objects.
|
|
3
|
+
*
|
|
4
|
+
* The `document` is a raw API response with no compile-time shape, so the
|
|
5
|
+
* parameter is `unknown`. The caller names the shape it expects out via `T`;
|
|
6
|
+
* this function is the boundary that turns the untyped response into it.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const product = deserialize<Product>(apiResponse)
|
|
10
|
+
*/
|
|
11
|
+
export declare function deserialize<T = unknown>(document: unknown): T;
|
|
12
|
+
export default deserialize;
|
package/dist/deserialize.js
CHANGED
|
@@ -1,45 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
function f(e, r, s, n) {
|
|
13
|
-
if (r.cached || (r.cached = {}), e.type in r.cached || (r.cached[e.type] = {}), s && e.id in r.cached[e.type])
|
|
14
|
-
return r.cached[e.type][e.id];
|
|
15
|
-
const t = e.attributes || {};
|
|
16
|
-
if (t.id = e.id, r.cached[e.type][e.id] = t, e.relationships)
|
|
17
|
-
for (const c of Object.keys(e.relationships)) {
|
|
18
|
-
const o = e.relationships[c];
|
|
19
|
-
if (Array.isArray(o.data)) {
|
|
20
|
-
const p = [];
|
|
21
|
-
o.data.forEach((a) => {
|
|
22
|
-
const h = u(
|
|
23
|
-
r,
|
|
24
|
-
a.type,
|
|
25
|
-
a.id
|
|
26
|
-
);
|
|
27
|
-
p.push(h);
|
|
28
|
-
}), t[c] = p;
|
|
29
|
-
} else o && o.data ? t[c] = u(
|
|
30
|
-
r,
|
|
31
|
-
o.data.type,
|
|
32
|
-
o.data.id
|
|
33
|
-
) : t[c] = null;
|
|
34
|
-
}
|
|
35
|
-
return t;
|
|
36
|
-
}
|
|
37
|
-
function u(e, r, s, n) {
|
|
38
|
-
let i = null;
|
|
39
|
-
return e.forEach((t) => {
|
|
40
|
-
t.type === r && t.id === s && (i = f(t, e, !0));
|
|
41
|
-
}), i || (i = { id: s }), i;
|
|
42
|
-
}
|
|
43
|
-
export {
|
|
44
|
-
A as deserialize
|
|
1
|
+
//#region src/deserialize.ts
|
|
2
|
+
var e = ({ type: e, id: t }) => `${e}:${t}`, t = ({ id: e }) => ({ id: e }), n = (t) => new Map(t.map((t) => [e(t), t])), r = (n, r, i) => {
|
|
3
|
+
let o = e(i), s = r.has(o) ? void 0 : n.get(o);
|
|
4
|
+
return s ? a(n, r, s) : t(i);
|
|
5
|
+
}, i = (e, t, n) => Array.isArray(n) ? n.map((n) => r(e, t, n)) : n ? r(e, t, n) : null, a = (t, n, r) => {
|
|
6
|
+
let a = new Set(n).add(e(r)), o = Object.entries(r.relationships ?? {}).map(([e, n]) => [e, i(t, a, n?.data ?? null)]);
|
|
7
|
+
return {
|
|
8
|
+
...r.attributes,
|
|
9
|
+
id: r.id,
|
|
10
|
+
...Object.fromEntries(o)
|
|
11
|
+
};
|
|
45
12
|
};
|
|
13
|
+
function o(e) {
|
|
14
|
+
let { data: t = null, included: r = [] } = e == null ? {} : structuredClone(e), i = n(r), o = (e) => a(i, /* @__PURE__ */ new Set(), e);
|
|
15
|
+
return Array.isArray(t) ? t.map(o) : t ? o(t) : null;
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
export { o as default, o as deserialize };
|
package/dist/main.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alchemy_cms/json_api",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "JSON API deserializer for AlchemyCMS",
|
|
5
5
|
"license": "BSD-3-Clause",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/AlchemyCMS/alchemy-json_api.git"
|
|
9
9
|
},
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=20.19.0"
|
|
12
|
+
},
|
|
10
13
|
"keywords": [
|
|
11
14
|
"AlchemyCMS",
|
|
12
15
|
"json:api",
|
|
@@ -19,21 +22,32 @@
|
|
|
19
22
|
"Thomas von Deyen"
|
|
20
23
|
],
|
|
21
24
|
"main": "dist/alchemy-json_api.js",
|
|
25
|
+
"types": "dist/main.d.ts",
|
|
22
26
|
"type": "module",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/main.d.ts",
|
|
30
|
+
"import": "./dist/alchemy-json_api.js"
|
|
31
|
+
},
|
|
32
|
+
"./deserialize": {
|
|
33
|
+
"types": "./dist/deserialize.d.ts",
|
|
34
|
+
"import": "./dist/deserialize.js"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
23
37
|
"files": [
|
|
24
38
|
"dist"
|
|
25
39
|
],
|
|
26
|
-
"dependencies": {
|
|
27
|
-
"@ungap/structured-clone": "^1.0.1"
|
|
28
|
-
},
|
|
29
40
|
"devDependencies": {
|
|
30
41
|
"prettier": "^3.3.3",
|
|
31
|
-
"
|
|
32
|
-
"
|
|
42
|
+
"typescript": "^7.0.2",
|
|
43
|
+
"vite": "^8.0.0",
|
|
44
|
+
"vitest": "^4.0.10"
|
|
33
45
|
},
|
|
34
46
|
"scripts": {
|
|
35
|
-
"build": "vite build",
|
|
36
|
-
"
|
|
47
|
+
"build": "vite build && tsc -p tsconfig.json",
|
|
48
|
+
"prepare": "pnpm run build",
|
|
49
|
+
"lint": "prettier --check 'src/**/*.ts'",
|
|
50
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
37
51
|
"test": "vitest"
|
|
38
52
|
},
|
|
39
53
|
"packageManager": "pnpm@10.12.1"
|
package/LICENSE
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
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.
|