requestjs-rails 0.0.10 → 0.0.12

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: 3cfa027194b10b71958ba06f76e28d181ab6df698cbe26afd29295aec86146b5
4
- data.tar.gz: 757283047d0579f8e32f8556b90e2b4e24bcda160590cd8de677ba64eb26e88e
3
+ metadata.gz: 20cef8fde0fc81345d4531747a97e20a7b14a74cbc30cbf91777d9e1bb34f1fa
4
+ data.tar.gz: 0ff9c29c05b34c81f9e0a24a05f6e53830095e3e93c45baaf9f0c220d72819dc
5
5
  SHA512:
6
- metadata.gz: 9c12ab260f0c0a403c2c2666e35386f72c7637e5c33d71a312bf335ad02c90a261b544b211c3b30bdf06ba19fed84a458e77ac8cadb165482d4e44f7bc03b0fa
7
- data.tar.gz: c0c5a9d7c6cb5963626672fe2866112fce7a75e11c6baf4edb942d27ff507a23499fda5d3d2f553d942b417ba65ab047ccebac449fbb8357110313de37ed8f54
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,11 +146,16 @@ 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
  }
136
- if (response.ok && response.isTurboStream) {
154
+ if (response.isScript) {
155
+ await response.activeScript();
156
+ }
157
+ const responseStatusIsTurboStreamable = response.ok || response.unprocessableEntity;
158
+ if (responseStatusIsTurboStreamable && response.isTurboStream) {
137
159
  await response.renderTurboStream();
138
160
  }
139
161
  return response;
@@ -159,7 +181,7 @@ class FetchRequest {
159
181
  headers: this.headers,
160
182
  body: this.formattedBody,
161
183
  signal: this.signal,
162
- credentials: "same-origin",
184
+ credentials: this.credentials,
163
185
  redirect: this.redirect
164
186
  };
165
187
  }
@@ -198,6 +220,9 @@ class FetchRequest {
198
220
  case "json":
199
221
  return "application/json, application/vnd.api+json";
200
222
 
223
+ case "script":
224
+ return "text/javascript, application/javascript";
225
+
201
226
  default:
202
227
  return "*/*";
203
228
  }
@@ -232,6 +257,9 @@ class FetchRequest {
232
257
  get redirect() {
233
258
  return this.options.redirect || "follow";
234
259
  }
260
+ get credentials() {
261
+ return this.options.credentials || "same-origin";
262
+ }
235
263
  get additionalHeaders() {
236
264
  return this.options.headers || {};
237
265
  }
@@ -1,3 +1,3 @@
1
1
  module Requestjs
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.12"
3
3
  end
metadata CHANGED
@@ -1,30 +1,30 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: requestjs-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Lauxen
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-15 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
- name: rails
14
+ name: railties
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 6.0.0
19
+ version: 6.1.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 6.0.0
27
- description:
26
+ version: 6.1.0
27
+ description:
28
28
  email: marcelolauxen16@gmail.com
29
29
  executables: []
30
30
  extensions: []
@@ -46,7 +46,7 @@ homepage: https://github.com/marcelolx/requestjs-rails
46
46
  licenses:
47
47
  - MIT
48
48
  metadata: {}
49
- post_install_message:
49
+ post_install_message:
50
50
  rdoc_options: []
51
51
  require_paths:
52
52
  - lib
@@ -61,8 +61,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  - !ruby/object:Gem::Version
62
62
  version: '0'
63
63
  requirements: []
64
- rubygems_version: 3.1.6
65
- signing_key:
64
+ rubygems_version: 3.5.3
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
68
68
  to handle to send the CSRF Token on every request