requestjs-rails 0.0.11 → 0.0.13

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: fad5960e6459e419a6a684ebc12194003f4b19f83b984004c5a62205ea761ea5
4
- data.tar.gz: 1f1d45bd1e64fda7a075641e00fd78a97f84606ee0843b3663be143a9896295f
3
+ metadata.gz: a452d308a3c2a875b8aac295997c789ba97655aed5b3bd3d28075b12cd494170
4
+ data.tar.gz: b39226e61c2eec6f162a21963ac8a06fa2e800f7232aa8430e16b72a5d6759c2
5
5
  SHA512:
6
- metadata.gz: a55c0defd7b2890825932f7d53f01d6d6388bc4ce8c8bf4949da772a4e4bb8712a4378feae12c0fb75fb1afc3b8221ae32f60ace6f373533cbe962298978eb60
7
- data.tar.gz: a5eb76765f8a41f810d65dc2742138191b37ab7e3bf5bc561cbcd5f706cb3791cc5ca211e4c1be761e2b400fdb5007cd0fcfd0e32088792f05f436e9a1d476ef
6
+ metadata.gz: 16e03876315812f4506c41d2e65329a4f169d57bd290ecdc301424e7d4bcbc5e3387ed9669be12682bc0147940baa3e9073be3ff231c4f490d780d529635907e
7
+ data.tar.gz: '0418aac7b2ee58548edd735bf9b76b7714bcddcca7417e4e19ab5b37dbe23d6283f8556ee84717ac6d7f37bbfb634d7e3134ea7ad87cffff45ab95b348f7fe5f'
@@ -45,6 +45,9 @@ class FetchResponse {
45
45
  get isTurboStream() {
46
46
  return this.contentType.match(/^text\/vnd\.turbo-stream\.html/);
47
47
  }
48
+ get isScript() {
49
+ return this.contentType.match(/\b(?:java|ecma)script\b/);
50
+ }
48
51
  async renderTurboStream() {
49
52
  if (this.isTurboStream) {
50
53
  if (window.Turbo) {
@@ -56,6 +59,22 @@ class FetchResponse {
56
59
  return Promise.reject(new Error(`Expected a Turbo Stream response but got "${this.contentType}" instead`));
57
60
  }
58
61
  }
62
+ async activeScript() {
63
+ if (this.isScript) {
64
+ const script = document.createElement("script");
65
+ const metaTag = document.querySelector("meta[name=csp-nonce]");
66
+ if (metaTag) {
67
+ const nonce = metaTag.nonce === "" ? metaTag.content : metaTag.nonce;
68
+ if (nonce) {
69
+ script.setAttribute("nonce", nonce);
70
+ }
71
+ }
72
+ script.innerHTML = await this.text;
73
+ document.body.appendChild(script);
74
+ } else {
75
+ return Promise.reject(new Error(`Expected a Script response but got "${this.contentType}" instead`));
76
+ }
77
+ }
59
78
  }
60
79
 
61
80
  class RequestInterceptor {
@@ -129,10 +148,14 @@ class FetchRequest {
129
148
  } catch (error) {
130
149
  console.error(error);
131
150
  }
132
- const response = new FetchResponse(await window.fetch(this.url, this.fetchOptions));
151
+ const fetch = this.responseKind === "turbo-stream" && window.Turbo ? window.Turbo.fetch : window.fetch;
152
+ const response = new FetchResponse(await fetch(this.url, this.fetchOptions));
133
153
  if (response.unauthenticated && response.authenticationURL) {
134
154
  return Promise.reject(window.location.href = response.authenticationURL);
135
155
  }
156
+ if (response.isScript) {
157
+ await response.activeScript();
158
+ }
136
159
  const responseStatusIsTurboStreamable = response.ok || response.unprocessableEntity;
137
160
  if (responseStatusIsTurboStreamable && response.isTurboStream) {
138
161
  await response.renderTurboStream();
@@ -145,7 +168,7 @@ class FetchRequest {
145
168
  this.options.headers = headers;
146
169
  }
147
170
  sameHostname() {
148
- if (!this.originalUrl.startsWith("http:")) {
171
+ if (!this.originalUrl.startsWith("http:") && !this.originalUrl.startsWith("https:")) {
149
172
  return true;
150
173
  }
151
174
  try {
@@ -161,7 +184,8 @@ class FetchRequest {
161
184
  body: this.formattedBody,
162
185
  signal: this.signal,
163
186
  credentials: this.credentials,
164
- redirect: this.redirect
187
+ redirect: this.redirect,
188
+ keepalive: this.keepalive
165
189
  };
166
190
  }
167
191
  get headers() {
@@ -199,6 +223,9 @@ class FetchRequest {
199
223
  case "json":
200
224
  return "application/json, application/vnd.api+json";
201
225
 
226
+ case "script":
227
+ return "text/javascript, application/javascript";
228
+
202
229
  default:
203
230
  return "*/*";
204
231
  }
@@ -236,6 +263,9 @@ class FetchRequest {
236
263
  get credentials() {
237
264
  return this.options.credentials || "same-origin";
238
265
  }
266
+ get keepalive() {
267
+ return this.options.keepalive || false;
268
+ }
239
269
  get additionalHeaders() {
240
270
  return this.options.headers || {};
241
271
  }
@@ -1,3 +1,3 @@
1
1
  module Requestjs
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.13"
3
3
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: requestjs-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Lauxen
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2023-11-17 00:00:00.000000000 Z
10
+ date: 2025-04-30 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: railties
@@ -16,15 +15,14 @@ dependencies:
16
15
  requirements:
17
16
  - - ">="
18
17
  - !ruby/object:Gem::Version
19
- version: 6.1.0
18
+ version: 7.1.0
20
19
  type: :runtime
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
23
  - - ">="
25
24
  - !ruby/object:Gem::Version
26
- version: 6.1.0
27
- description:
25
+ version: 7.1.0
28
26
  email: marcelolauxen16@gmail.com
29
27
  executables: []
30
28
  extensions: []
@@ -46,7 +44,6 @@ homepage: https://github.com/marcelolx/requestjs-rails
46
44
  licenses:
47
45
  - MIT
48
46
  metadata: {}
49
- post_install_message:
50
47
  rdoc_options: []
51
48
  require_paths:
52
49
  - lib
@@ -61,8 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
58
  - !ruby/object:Gem::Version
62
59
  version: '0'
63
60
  requirements: []
64
- rubygems_version: 3.4.19
65
- signing_key:
61
+ rubygems_version: 3.6.2
66
62
  specification_version: 4
67
63
  summary: A tiny Fetch API wrapper that allows you to make http requests without need
68
64
  to handle to send the CSRF Token on every request