alchemy-json_api 2.4.2 → 3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7812a7201b85a39537d23fddd317eda7154aae57d939e3eaa7b687edadf6cc06
4
- data.tar.gz: 3202a669eb9298590b8cb434589dfda055e099002893e3c8034a07dddb996554
3
+ metadata.gz: 40926540b22b0e31f3761a394cd421fa64932b2d3a4067b519a8afe89266abb3
4
+ data.tar.gz: 7a7c5b56562dadc57cb0e4f0ce19ab16554631a44173700740ad4b67904cb6ac
5
5
  SHA512:
6
- metadata.gz: e4c02e1f70dff8d703e65a8800f0568da0afca50ba8b9f6fe6f0da95d864378d7559204b401a89607076f5f7e5f073a12301bd0ea759da8609af0b3cef97f9a3
7
- data.tar.gz: c7043b57ae494aad2e4d4073c4a94063b673f39358e07cc209d5dc5f884b6ff5fffc857b73f13d99f8fbc4b2c92597422af0a0de3d0b9b385cf46416b9b2be5a
6
+ metadata.gz: 20b7dce5cfb2dce79e2eba41bfceb34f7cc8cbf474759dd329e413f5e57f0289492901bb75096ca3d6109bd49c735ebf8a88aadcfba25c4925692b8eea35b7cd
7
+ data.tar.gz: a303f1895af78789b65e3b7d526a0ad8f9074e286f3db795cec2dfbadbbba98849d162ec62ce248ac56eebd9c3bb76cac532c413e62d200dd3a75bce2ccd0d93
data/README.md CHANGED
@@ -13,11 +13,13 @@ gem 'alchemy-json_api'
13
13
  ```
14
14
 
15
15
  And then execute:
16
+
16
17
  ```bash
17
18
  $ bundle
18
19
  ```
19
20
 
20
21
  Or install it yourself as:
22
+
21
23
  ```bash
22
24
  $ gem install alchemy-json_api
23
25
  ```
@@ -27,9 +29,11 @@ $ gem install alchemy-json_api
27
29
  Run this in your application:
28
30
 
29
31
  ```
30
- yarn add "@alchemy_cms/json_api"
32
+ npm install @alchemy_cms/json_api --save
31
33
  ```
32
34
 
35
+ or with the package manager of your choice
36
+
33
37
  ## Usage
34
38
 
35
39
  ### In your Rails app
@@ -57,18 +61,46 @@ const pages = deserializePages(data)
57
61
  console.log(pages[0].name) // => Homepage
58
62
  ```
59
63
 
64
+ ## HTTP Caching
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
+
60
89
  ## Key transforms
61
90
 
62
91
  If you ever want to change how Alchemy serializes attributes you can set
63
92
 
64
93
  ```rb
94
+ # config/initializers/alchemy_json_api.rb
65
95
  Alchemy::JsonApi.key_transform = :camel_lower
66
96
  ```
67
97
 
68
98
  It defaults to `:underscore`.
69
99
 
70
100
  ## Contributing
101
+
71
102
  Contribution directions go here.
72
103
 
73
104
  ## License
105
+
74
106
  The gem is available as open source under the terms of the [BSD-3-Clause license](https://opensource.org/licenses/BSD-3-Clause).
@@ -18,11 +18,7 @@ module Alchemy
18
18
  end
19
19
 
20
20
  def set_current_preview
21
- if Alchemy.const_defined?(:Current)
22
- Alchemy::Current.preview_page = @page
23
- else
24
- Alchemy::Page.current_preview = @page
25
- end
21
+ Alchemy::Current.preview_page = @page
26
22
  end
27
23
 
28
24
  def page_cache_key(page)
@@ -3,7 +3,7 @@
3
3
  module Alchemy
4
4
  module JsonApi
5
5
  class PagesController < JsonApi::BaseController
6
- THREE_HOURS = 10800
6
+ CACHE_DURATION = 600
7
7
  JSONAPI_STALEMAKERS = %i[include fields sort filter page]
8
8
 
9
9
  before_action :load_page_for_cache_key, only: :show
@@ -51,11 +51,11 @@ module Alchemy
51
51
  end
52
52
 
53
53
  def cache_duration
54
- ENV.fetch("ALCHEMY_JSON_API_CACHE_DURATION", THREE_HOURS).to_i
54
+ Alchemy::JsonApi.page_cache_max_age
55
55
  end
56
56
 
57
57
  def caching_options
58
- {must_revalidate: true}
58
+ Alchemy::JsonApi.page_caching_options
59
59
  end
60
60
 
61
61
  # Get page w/o includes to get cache key
@@ -138,11 +138,7 @@ module Alchemy
138
138
  end
139
139
 
140
140
  def current_language
141
- if Alchemy.const_defined?(:Current)
142
- Alchemy::Current.language
143
- else
144
- Alchemy::Language.current
145
- end
141
+ Alchemy::Current.language
146
142
  end
147
143
 
148
144
  def jsonapi_serializer_class(_resource, _is_collection)
@@ -20,7 +20,7 @@ module Alchemy
20
20
  end
21
21
 
22
22
  has_one :page, record_type: :page, serializer: PageSerializer do |ingredient|
23
- ingredient.page&.tap { Page.new(_1) }
23
+ Alchemy::JsonApi::Page.new(ingredient.page) if ingredient.page
24
24
  end
25
25
  end
26
26
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Alchemy
4
4
  module JsonApi
5
- VERSION = "2.4.2"
5
+ VERSION = "3.0.0"
6
6
  end
7
7
  end
@@ -6,6 +6,8 @@ require "alchemy/json_api/engine"
6
6
 
7
7
  module Alchemy
8
8
  module JsonApi
9
+ extend self
10
+
9
11
  # Set FastJsonapi key_transform
10
12
  #
11
13
  # Supported transformations:
@@ -14,15 +16,45 @@ module Alchemy
14
16
  # :camel_lower # "some_key" => "someKey"
15
17
  # :dash # "some_key" => "some-key"
16
18
  # :underscore # "some_key" => "some_key"
17
- def self.key_transform=(value)
19
+ def key_transform=(value)
18
20
  @_key_transform = value
19
21
  end
20
22
 
21
23
  # FastJsonapi key_transform
22
24
  #
23
25
  # Default :underscore # "some_key" => "some_key"
24
- def self.key_transform
26
+ def key_transform
25
27
  @_key_transform || :underscore
26
28
  end
29
+
30
+ # HTTP Cache-Control max-age
31
+ #
32
+ # Can be set as `ALCHEMY_JSON_API_CACHE_DURATION` env var
33
+ #
34
+ # Default 600 (seconds)
35
+ def page_cache_max_age
36
+ @_page_cache_max_age ||= ENV.fetch("ALCHEMY_JSON_API_CACHE_DURATION", 600).to_i
37
+ end
38
+
39
+ def page_cache_max_age=(max_age)
40
+ @_page_cache_max_age = max_age
41
+ end
42
+
43
+ # HTTP Cache-Control options
44
+ #
45
+ # Set any of the ActionController::ConditionalGet#expires_in options
46
+ #
47
+ # See https://api.rubyonrails.org/classes/ActionController/ConditionalGet.html#method-i-expires_in
48
+ #
49
+ # Default `{must_revalidate: true}`
50
+ def page_caching_options
51
+ @_page_caching_options ||= {
52
+ must_revalidate: true
53
+ }
54
+ end
55
+
56
+ def page_caching_options=(options)
57
+ @_page_caching_options = options
58
+ end
27
59
  end
28
60
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alchemy-json_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.2
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Meyerhoff
@@ -16,7 +16,7 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 7.0.0.a
19
+ version: 7.2.0
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
22
  version: '8'
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: 7.0.0.a
29
+ version: 7.2.0
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '8'