@alchemy_cms/json_api 3.0.1 → 5.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 +26 -84
- package/dist/alchemy-json_api.js +11 -21
- package/dist/alchemyApiDeserializer.d.ts +15 -0
- package/dist/deserialize.d.ts +13 -0
- package/dist/deserialize.js +20 -43
- package/dist/main.d.ts +2 -0
- package/package.json +21 -7
- package/LICENSE +0 -11
package/README.md
CHANGED
|
@@ -1,106 +1,48 @@
|
|
|
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
|
-
```bash
|
|
18
|
-
$ bundle
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
Or install it yourself as:
|
|
22
|
-
|
|
23
7
|
```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
|
-
```rb
|
|
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
|
|
15
|
+
`deserialize` converts a JSON:API response into plain JS objects: each
|
|
16
|
+
resource's attributes are flattened with its `id`, and relationships present in
|
|
17
|
+
`included` become nested objects (absent ones become `{ id }` stubs).
|
|
49
18
|
|
|
50
|
-
|
|
19
|
+
Each included resource is materialized **once** and shared by every reference
|
|
20
|
+
to it, so the result stays proportional to the size of the response even when
|
|
21
|
+
resources reference each other heavily. A consequence is that back-references
|
|
22
|
+
(e.g. a menu node's `parent`, which lists the node among its `children`) are
|
|
23
|
+
real references, and the result may contain **cycles**:
|
|
51
24
|
|
|
52
|
-
|
|
25
|
+
- `structuredClone`, [devalue](https://github.com/sveltejs/devalue) (Nuxt
|
|
26
|
+
payloads) and Vue/React state handle this fine.
|
|
27
|
+
- `JSON.stringify` does not. If you need JSON, serialize a projection of the
|
|
28
|
+
fields you need, or use a cycle-aware serializer.
|
|
29
|
+
- Mutating a resource is visible everywhere it is referenced. Copy before
|
|
30
|
+
mutating.
|
|
53
31
|
|
|
54
32
|
```js
|
|
55
|
-
import {
|
|
33
|
+
import { deserialize } from "@alchemy_cms/json_api"
|
|
56
34
|
|
|
57
|
-
const response = await fetch("/jsonapi/pages.json")
|
|
58
|
-
const
|
|
59
|
-
const pages = deserializePages(data)
|
|
35
|
+
const response = await fetch("/jsonapi/pages/homepage.json")
|
|
36
|
+
const page = deserialize(await response.json())
|
|
60
37
|
|
|
61
|
-
console.log(
|
|
38
|
+
console.log(page.name) // => "Homepage"
|
|
62
39
|
```
|
|
63
40
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
Alchemy::JsonApi allows for caching API responses. It respects the caching configuration of your Rails app and of your Alchemy configuration and settings in the pages page layout configuration. Restricted pages are never cached.
|
|
67
|
-
|
|
68
|
-
By default it sets the `max-age` `Cache-Control` header to 10 minutes (`600` seconds). You can change this by configuring the `Alchemy::JsonApi.page_cache_max_age` setting. It is recommended to set this via an environment variable like this:
|
|
69
|
-
|
|
70
|
-
```rb
|
|
71
|
-
# config/initializers/alchemy_json_api.rb
|
|
72
|
-
Alchemy::JsonApi.page_cache_max_age = ENV.fetch("ALCHEMY_JSON_API_CACHE_DURATION", 600).to_i
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
### Edge Caching
|
|
76
|
-
|
|
77
|
-
Alchemy sets the `must-revalidate` directive if caching is enabled. If your CDN supports it, you can change that to use the much more efficient `stale-while-revalidate` directive by changing the `Alchemy::JsonApi.page_caching_options` setting to any integer value.
|
|
78
|
-
|
|
79
|
-
```rb
|
|
80
|
-
# config/initializers/alchemy_json_api.rb
|
|
81
|
-
Alchemy::JsonApi.page_caching_options = {
|
|
82
|
-
stale_while_revalidate: ENV.fetch("ALCHEMY_JSON_API_CACHE_STALE_WHILE_REVALIDATE", 60).to_i
|
|
83
|
-
}
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
> [!TIP]
|
|
87
|
-
> You can set any caching option that [`ActionController::ConditionalGet#expires_in` supports](https://api.rubyonrails.org/classes/ActionController/ConditionalGet.html#method-i-expires_in), like `stale_if_error`, `public` or `immutable`.
|
|
88
|
-
|
|
89
|
-
## Key transforms
|
|
90
|
-
|
|
91
|
-
If you ever want to change how Alchemy serializes attributes you can set
|
|
92
|
-
|
|
93
|
-
```rb
|
|
94
|
-
# config/initializers/alchemy_json_api.rb
|
|
95
|
-
Alchemy::JsonApi.key_transform = :camel_lower
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
It defaults to `:underscore`.
|
|
41
|
+
`deserializePage` and `deserializePages` still exist as thin, **deprecated**
|
|
42
|
+
aliases for `deserialize`; prefer `deserialize`.
|
|
99
43
|
|
|
100
44
|
## Contributing
|
|
101
45
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
The gem is available as open source under the terms of the [BSD-3-Clause license](https://opensource.org/licenses/BSD-3-Clause).
|
|
46
|
+
This package lives in the [`javascript/`](https://github.com/AlchemyCMS/alchemy-json_api/tree/main/javascript)
|
|
47
|
+
directory of the `alchemy-json_api` monorepo. Releases are automated with
|
|
48
|
+
release-please and tagged `package-vX.Y.Z`.
|
package/dist/alchemy-json_api.js
CHANGED
|
@@ -1,24 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
e.nested_elements
|
|
7
|
-
)), e.nestedElements?.length > 0 && (e.nestedElements = n(e.nestedElements)), e.essences?.length > 0 && (e.essences = e.essences.filter((i) => !i.deprecated)), e.deprecated || s.push(e);
|
|
8
|
-
}), 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.`));
|
|
9
6
|
}
|
|
10
|
-
function
|
|
11
|
-
|
|
12
|
-
return s.elements = n(s.elements), s;
|
|
7
|
+
function r(t) {
|
|
8
|
+
return n("deserializePage"), e(t);
|
|
13
9
|
}
|
|
14
|
-
function
|
|
15
|
-
|
|
16
|
-
return s.forEach((e) => {
|
|
17
|
-
e.elements = n(e.elements);
|
|
18
|
-
}), s;
|
|
10
|
+
function i(t) {
|
|
11
|
+
return n("deserializePages"), e(t);
|
|
19
12
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
d as deserializePage,
|
|
23
|
-
f as deserializePages
|
|
24
|
-
};
|
|
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,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deserialize a JSON:API document into plain objects, sharing one object per
|
|
3
|
+
* resource. The result may contain reference cycles.
|
|
4
|
+
*
|
|
5
|
+
* The `document` is a raw API response with no compile-time shape, so the
|
|
6
|
+
* parameter is `unknown`. The caller names the shape it expects out via `T`;
|
|
7
|
+
* this function is the boundary that turns the untyped response into it.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const product = deserialize<Product>(apiResponse)
|
|
11
|
+
*/
|
|
12
|
+
export declare function deserialize<T = unknown>(document: unknown): T;
|
|
13
|
+
export default deserialize;
|
package/dist/deserialize.js
CHANGED
|
@@ -1,44 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
if (r.id = e.id, t.cached[e.type][e.id] = r, e.relationships)
|
|
16
|
-
for (const c of Object.keys(e.relationships)) {
|
|
17
|
-
const n = e.relationships[c];
|
|
18
|
-
if (Array.isArray(n.data)) {
|
|
19
|
-
const p = [];
|
|
20
|
-
n.data.forEach((a) => {
|
|
21
|
-
const h = u(
|
|
22
|
-
t,
|
|
23
|
-
a.type,
|
|
24
|
-
a.id
|
|
25
|
-
);
|
|
26
|
-
p.push(h);
|
|
27
|
-
}), r[c] = p;
|
|
28
|
-
} else n && n.data ? r[c] = u(
|
|
29
|
-
t,
|
|
30
|
-
n.data.type,
|
|
31
|
-
n.data.id
|
|
32
|
-
) : r[c] = null;
|
|
33
|
-
}
|
|
34
|
-
return r;
|
|
35
|
-
}
|
|
36
|
-
function u(e, t, s, o) {
|
|
37
|
-
let i = null;
|
|
38
|
-
return e.forEach((r) => {
|
|
39
|
-
r.type === t && r.id === s && (i = f(r, e, !0));
|
|
40
|
-
}), i || (i = { id: s }), i;
|
|
41
|
-
}
|
|
42
|
-
export {
|
|
43
|
-
y 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 = n.get(e(i));
|
|
4
|
+
return o ? a(n, r, o) : 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 = e(r), o = n.get(a);
|
|
7
|
+
if (o) return o;
|
|
8
|
+
let s = {
|
|
9
|
+
...r.attributes,
|
|
10
|
+
id: r.id
|
|
11
|
+
};
|
|
12
|
+
n.set(a, s);
|
|
13
|
+
for (let [e, a] of Object.entries(r.relationships ?? {})) s[e] = i(t, n, a?.data ?? null);
|
|
14
|
+
return s;
|
|
44
15
|
};
|
|
16
|
+
function o(e) {
|
|
17
|
+
let { data: t = null, included: r = [] } = e == null ? {} : structuredClone(e), i = n(r), o = /* @__PURE__ */ new Map(), s = (e) => a(i, o, e);
|
|
18
|
+
return Array.isArray(t) ? t.map(s) : t ? s(t) : null;
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { o as default, o as deserialize };
|
package/dist/main.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alchemy_cms/json_api",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.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"
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=20.19.0"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [
|
|
14
14
|
"AlchemyCMS",
|
|
@@ -22,18 +22,32 @@
|
|
|
22
22
|
"Thomas von Deyen"
|
|
23
23
|
],
|
|
24
24
|
"main": "dist/alchemy-json_api.js",
|
|
25
|
+
"types": "dist/main.d.ts",
|
|
25
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
|
+
},
|
|
26
37
|
"files": [
|
|
27
38
|
"dist"
|
|
28
39
|
],
|
|
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.
|