alchemy-json_api 0.26.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +11 -0
- data/README.md +48 -8
- data/app/controllers/alchemy/json_api/nodes_controller.rb +54 -0
- data/app/serializers/alchemy/json_api/base_serializer.rb +9 -0
- data/app/serializers/alchemy/json_api/element_serializer.rb +1 -3
- data/app/serializers/alchemy/json_api/essence_audio_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/essence_boolean_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/essence_date_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/essence_file_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/essence_headline_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/essence_html_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/essence_link_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/essence_node_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/essence_page_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/essence_picture_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/essence_richtext_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/essence_select_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/essence_text_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/essence_video_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/ingredient_audio_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/ingredient_boolean_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/ingredient_datetime_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/ingredient_file_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/ingredient_headline_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/ingredient_html_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/ingredient_link_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/ingredient_node_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/ingredient_page_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/ingredient_picture_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/ingredient_richtext_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/ingredient_select_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/ingredient_text_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/ingredient_video_serializer.rb +1 -1
- data/app/serializers/alchemy/json_api/language_serializer.rb +1 -3
- data/app/serializers/alchemy/json_api/node_serializer.rb +10 -3
- data/app/serializers/alchemy/json_api/page_serializer.rb +1 -3
- data/config/routes.rb +1 -0
- data/lib/alchemy/json_api/essence_serializer.rb +0 -1
- data/lib/alchemy/json_api/ingredient_serializer.rb +0 -2
- data/lib/alchemy/json_api/test_support/ingredient_serializer_behaviour.rb +7 -3
- data/lib/alchemy/json_api/version.rb +1 -1
- data/lib/alchemy/json_api.rb +19 -1
- metadata +8 -5
- data/MIT-LICENSE +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 41f18d9d1806fce75786f402f58e430e4679891f9845151f94b08d52e0e4651b
|
4
|
+
data.tar.gz: ad0730650816cd36681954084da739669172dfb83acbce4ba04f096ba517259b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5456eff37375355f7ce9feeae16d6693cb9aef2e329f71431d7972c5d87b8e06e57335a5db5ef88ec4d75b593e3e383d2fef20dbadc71f7fbff4e425fe2023c7
|
7
|
+
data.tar.gz: 63ed7972458c05a952244bb2e6fd6f28aef61b30ba3e204fed881c592ebd30966cc77888aa60ef6b1836bbe5f65da4363509d3f305e364e5fb3aebd61beeb350
|
data/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.
|
data/README.md
CHANGED
@@ -2,15 +2,10 @@
|
|
2
2
|
|
3
3
|
A JSON-API based API for AlchemyCMS
|
4
4
|
|
5
|
-
##
|
6
|
-
|
7
|
-
Mount the engine in your `config/routes.rb` file where you want, like this:
|
5
|
+
## Installation
|
8
6
|
|
9
|
-
|
10
|
-
mount Alchemy::JsonApi::Engine => "/jsonapi/"
|
11
|
-
```
|
7
|
+
### In your Alchemy Rails project
|
12
8
|
|
13
|
-
## Installation
|
14
9
|
Add this line to your application's Gemfile:
|
15
10
|
|
16
11
|
```ruby
|
@@ -27,8 +22,53 @@ Or install it yourself as:
|
|
27
22
|
$ gem install alchemy-json_api
|
28
23
|
```
|
29
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
|
+
## Key transforms
|
61
|
+
|
62
|
+
If you ever want to change how Alchemy serializes attributes you can set
|
63
|
+
|
64
|
+
```rb
|
65
|
+
Alchemy::JsonApi.key_transform = :camel_lower
|
66
|
+
```
|
67
|
+
|
68
|
+
It defaults to `:underscore`.
|
69
|
+
|
30
70
|
## Contributing
|
31
71
|
Contribution directions go here.
|
32
72
|
|
33
73
|
## License
|
34
|
-
The gem is available as open source under the terms of the [
|
74
|
+
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,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Alchemy
|
4
|
+
module JsonApi
|
5
|
+
class NodesController < JsonApi::BaseController
|
6
|
+
def index
|
7
|
+
@nodes = node_scope.select(:id, :updated_at)
|
8
|
+
if stale?(last_modified: @nodes.maximum(:updated_at), etag: @nodes)
|
9
|
+
jsonapi_paginate(node_scope_with_includes) do |paginated|
|
10
|
+
render jsonapi: paginated
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
expires_in cache_duration, { public: true, must_revalidate: true }
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def cache_duration
|
20
|
+
ENV.fetch("ALCHEMY_JSON_API_CACHE_DURATION", 3).to_i.hours
|
21
|
+
end
|
22
|
+
|
23
|
+
def jsonapi_meta(nodes)
|
24
|
+
pagination = jsonapi_pagination_meta(nodes)
|
25
|
+
|
26
|
+
{
|
27
|
+
pagination: pagination.presence,
|
28
|
+
total: node_scope.count,
|
29
|
+
}.compact
|
30
|
+
end
|
31
|
+
|
32
|
+
def node_scope
|
33
|
+
Alchemy::Node.all
|
34
|
+
end
|
35
|
+
|
36
|
+
def node_scope_with_includes
|
37
|
+
if params[:include].present?
|
38
|
+
includes = params[:include].split(",").map do |association|
|
39
|
+
association.split(".").reverse.inject({}) do |value, key|
|
40
|
+
{ key.to_sym => value }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
node_scope.includes(includes)
|
44
|
+
else
|
45
|
+
node_scope
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def jsonapi_serializer_class(*)
|
50
|
+
::Alchemy::JsonApi::NodeSerializer
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -1,9 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
module Alchemy
|
3
3
|
module JsonApi
|
4
|
-
class NodeSerializer
|
5
|
-
include JSONAPI::Serializer
|
6
|
-
|
4
|
+
class NodeSerializer < BaseSerializer
|
7
5
|
attributes :name
|
8
6
|
attribute :link_url, &:url
|
9
7
|
attribute :link_title, &:title
|
@@ -11,6 +9,15 @@ module Alchemy
|
|
11
9
|
|
12
10
|
belongs_to :parent, record_type: :node, serializer: self
|
13
11
|
|
12
|
+
belongs_to(
|
13
|
+
:page,
|
14
|
+
record_type: :page,
|
15
|
+
if: ->(node) { node.page },
|
16
|
+
serializer: ::Alchemy::JsonApi::PageSerializer,
|
17
|
+
) do |node|
|
18
|
+
::Alchemy::JsonApi::Page.new(node.page)
|
19
|
+
end
|
20
|
+
|
14
21
|
has_many :children, record_type: :node, serializer: self
|
15
22
|
end
|
16
23
|
end
|
data/config/routes.rb
CHANGED
@@ -4,6 +4,7 @@ Alchemy::JsonApi::Engine.routes.draw do
|
|
4
4
|
get "pages/*path" => "pages#show", as: :page
|
5
5
|
resources :layout_pages, only: [:index]
|
6
6
|
get "layout_pages/*path" => "layout_pages#show", as: :layout_page
|
7
|
+
resources :nodes, only: [:index]
|
7
8
|
|
8
9
|
namespace :admin do
|
9
10
|
get "pages/*path" => "pages#show", as: :page
|
@@ -4,11 +4,15 @@ RSpec.shared_examples "an ingredient serializer" do
|
|
4
4
|
describe "attributes" do
|
5
5
|
subject { serializer.serializable_hash[:data][:attributes] }
|
6
6
|
|
7
|
-
it "has the right keys and values" do
|
7
|
+
it "has the right transformed keys and values" do
|
8
|
+
transform_method = FastJsonapi::ObjectSerializer::TRANSFORMS_MAPPING[Alchemy::JsonApi.key_transform]
|
9
|
+
transformed_keys = %w[created_at updated_at].map { |k| k.send(*transform_method).to_sym }
|
10
|
+
|
8
11
|
expect(subject).to have_key(:role)
|
9
12
|
expect(subject).to have_key(:value)
|
10
|
-
|
11
|
-
|
13
|
+
transformed_keys.each do |transformed_key|
|
14
|
+
expect(subject).to have_key(transformed_key)
|
15
|
+
end
|
12
16
|
expect(subject[:deprecated]).to be(false)
|
13
17
|
end
|
14
18
|
|
data/lib/alchemy/json_api.rb
CHANGED
@@ -1,10 +1,28 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require "fast_jsonapi"
|
3
4
|
require "alchemy_cms"
|
4
5
|
require "alchemy/json_api/engine"
|
5
6
|
|
6
7
|
module Alchemy
|
7
8
|
module JsonApi
|
8
|
-
#
|
9
|
+
# Set FastJsonapi key_transform
|
10
|
+
#
|
11
|
+
# Supported transformations:
|
12
|
+
#
|
13
|
+
# :camel # "some_key" => "SomeKey"
|
14
|
+
# :camel_lower # "some_key" => "someKey"
|
15
|
+
# :dash # "some_key" => "some-key"
|
16
|
+
# :underscore # "some_key" => "some_key"
|
17
|
+
def self.key_transform=(value)
|
18
|
+
@_key_transform = value
|
19
|
+
end
|
20
|
+
|
21
|
+
# FastJsonapi key_transform
|
22
|
+
#
|
23
|
+
# Default :underscore # "some_key" => "some_key"
|
24
|
+
def self.key_transform
|
25
|
+
@_key_transform || :underscore
|
26
|
+
end
|
9
27
|
end
|
10
28
|
end
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: alchemy-json_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Meyerhoff
|
8
|
+
- Thomas von Deyen
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2022-
|
12
|
+
date: 2022-09-16 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: alchemy_cms
|
@@ -121,15 +122,17 @@ executables: []
|
|
121
122
|
extensions: []
|
122
123
|
extra_rdoc_files: []
|
123
124
|
files:
|
124
|
-
-
|
125
|
+
- LICENSE
|
125
126
|
- README.md
|
126
127
|
- Rakefile
|
127
128
|
- app/controllers/alchemy/json_api/admin/layout_pages_controller.rb
|
128
129
|
- app/controllers/alchemy/json_api/admin/pages_controller.rb
|
129
130
|
- app/controllers/alchemy/json_api/base_controller.rb
|
130
131
|
- app/controllers/alchemy/json_api/layout_pages_controller.rb
|
132
|
+
- app/controllers/alchemy/json_api/nodes_controller.rb
|
131
133
|
- app/controllers/alchemy/json_api/pages_controller.rb
|
132
134
|
- app/models/alchemy/json_api/page.rb
|
135
|
+
- app/serializers/alchemy/json_api/base_serializer.rb
|
133
136
|
- app/serializers/alchemy/json_api/element_serializer.rb
|
134
137
|
- app/serializers/alchemy/json_api/essence_audio_serializer.rb
|
135
138
|
- app/serializers/alchemy/json_api/essence_boolean_serializer.rb
|
@@ -173,7 +176,7 @@ files:
|
|
173
176
|
- lib/tasks/alchemy/json_api_tasks.rake
|
174
177
|
homepage: https://github.com/AlchemyCMS/alchemy-json_api
|
175
178
|
licenses:
|
176
|
-
-
|
179
|
+
- BSD-3-Clause
|
177
180
|
metadata: {}
|
178
181
|
post_install_message:
|
179
182
|
rdoc_options: []
|
@@ -190,7 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
190
193
|
- !ruby/object:Gem::Version
|
191
194
|
version: '0'
|
192
195
|
requirements: []
|
193
|
-
rubygems_version: 3.
|
196
|
+
rubygems_version: 3.3.7
|
194
197
|
signing_key:
|
195
198
|
specification_version: 4
|
196
199
|
summary: A JSONAPI compliant API for AlchemyCMS
|
data/MIT-LICENSE
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
Copyright 2020 Martin Meyerhoff
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|