requestjs-rails 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fad5960e6459e419a6a684ebc12194003f4b19f83b984004c5a62205ea761ea5
4
- data.tar.gz: 1f1d45bd1e64fda7a075641e00fd78a97f84606ee0843b3663be143a9896295f
3
+ metadata.gz: 20cef8fde0fc81345d4531747a97e20a7b14a74cbc30cbf91777d9e1bb34f1fa
4
+ data.tar.gz: 0ff9c29c05b34c81f9e0a24a05f6e53830095e3e93c45baaf9f0c220d72819dc
5
5
  SHA512:
6
- metadata.gz: a55c0defd7b2890825932f7d53f01d6d6388bc4ce8c8bf4949da772a4e4bb8712a4378feae12c0fb75fb1afc3b8221ae32f60ace6f373533cbe962298978eb60
7
- data.tar.gz: a5eb76765f8a41f810d65dc2742138191b37ab7e3bf5bc561cbcd5f706cb3791cc5ca211e4c1be761e2b400fdb5007cd0fcfd0e32088792f05f436e9a1d476ef
6
+ metadata.gz: 41b0ab104a2dfc9061c2d40a9adf3530db9f2004a5f3997de9c270ebe82caa8a03e87304cdaaddae71fd82a98359043d81b5fe6799a3a79fc1fd668ec2344abc
7
+ data.tar.gz: e6ce7d0d1bd2fbd31494f0e5eefb6272a05aeb30280c877dfdf4ff4a1add6aadf3be553d665c0584837db8ebfbfc741df96af2b6ed9ceb417e2532f6f5b2fd49
@@ -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,20 @@ 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
+ const nonce = metaTag && metaTag.content;
67
+ if (nonce) {
68
+ script.setAttribute("nonce", nonce);
69
+ }
70
+ script.innerHTML = await this.text;
71
+ document.body.appendChild(script);
72
+ } else {
73
+ return Promise.reject(new Error(`Expected a Script response but got "${this.contentType}" instead`));
74
+ }
75
+ }
59
76
  }
60
77
 
61
78
  class RequestInterceptor {
@@ -129,10 +146,14 @@ class FetchRequest {
129
146
  } catch (error) {
130
147
  console.error(error);
131
148
  }
132
- const response = new FetchResponse(await window.fetch(this.url, this.fetchOptions));
149
+ const fetch = this.responseKind === "turbo-stream" && window.Turbo ? window.Turbo.fetch : window.fetch;
150
+ const response = new FetchResponse(await fetch(this.url, this.fetchOptions));
133
151
  if (response.unauthenticated && response.authenticationURL) {
134
152
  return Promise.reject(window.location.href = response.authenticationURL);
135
153
  }
154
+ if (response.isScript) {
155
+ await response.activeScript();
156
+ }
136
157
  const responseStatusIsTurboStreamable = response.ok || response.unprocessableEntity;
137
158
  if (responseStatusIsTurboStreamable && response.isTurboStream) {
138
159
  await response.renderTurboStream();
@@ -199,6 +220,9 @@ class FetchRequest {
199
220
  case "json":
200
221
  return "application/json, application/vnd.api+json";
201
222
 
223
+ case "script":
224
+ return "text/javascript, application/javascript";
225
+
202
226
  default:
203
227
  return "*/*";
204
228
  }
@@ -1,3 +1,3 @@
1
1
  module Requestjs
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.12"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
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.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Lauxen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-17 00:00:00.000000000 Z
11
+ date: 2024-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -61,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  - !ruby/object:Gem::Version
62
62
  version: '0'
63
63
  requirements: []
64
- rubygems_version: 3.4.19
64
+ rubygems_version: 3.5.3
65
65
  signing_key:
66
66
  specification_version: 4
67
67
  summary: A tiny Fetch API wrapper that allows you to make http requests without need