@thoughtbot/superglue 0.53.4 → 1.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.
- package/.babelrc.js +15 -0
- package/.prettierignore +6 -0
- package/.prettierrc +7 -0
- package/.tool-versions +1 -0
- package/LICENSE +21 -0
- package/dist/action_creators.d.mts +3 -0
- package/dist/action_creators.mjs +13 -0
- package/dist/action_creators.mjs.map +1 -0
- package/dist/chunk-LGUVOEZ3.mjs +790 -0
- package/dist/chunk-LGUVOEZ3.mjs.map +1 -0
- package/dist/cjs/action_creators.cjs +640 -0
- package/dist/cjs/action_creators.cjs.map +1 -0
- package/dist/cjs/superglue.cjs +1178 -0
- package/dist/cjs/superglue.cjs.map +1 -0
- package/dist/index-BYr1PoYr.d.mts +232 -0
- package/dist/superglue.d.mts +64 -0
- package/dist/superglue.mjs +499 -0
- package/dist/superglue.mjs.map +1 -0
- package/package.json +64 -30
- package/tsconfig.json +15 -0
- package/tsup.config.ts +29 -0
- package/typedoc.json +32 -0
- package/README.md +0 -126
- package/action_creators/index.js +0 -144
- package/action_creators/requests.js +0 -207
- package/actions.js +0 -17
- package/components/Nav.js +0 -211
- package/config.js +0 -8
- package/index.js +0 -215
- package/middleware.js +0 -63
- package/reducers/index.js +0 -225
- package/utils/helpers.js +0 -33
- package/utils/immutability.js +0 -128
- package/utils/index.js +0 -45
- package/utils/react.js +0 -40
- package/utils/request.js +0 -120
- package/utils/ujs.js +0 -110
- package/utils/url.js +0 -79
- package/utils/window.js +0 -14
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// lib/action_creators/index.ts
|
|
31
|
+
var action_creators_exports = {};
|
|
32
|
+
__export(action_creators_exports, {
|
|
33
|
+
MismatchedComponentError: () => MismatchedComponentError,
|
|
34
|
+
remote: () => remote,
|
|
35
|
+
saveAndProcessPage: () => saveAndProcessPage,
|
|
36
|
+
visit: () => visit
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(action_creators_exports);
|
|
39
|
+
|
|
40
|
+
// lib/utils/url.ts
|
|
41
|
+
var import_url_parse = __toESM(require("url-parse"));
|
|
42
|
+
function pathQuery(url) {
|
|
43
|
+
const { pathname, query } = new import_url_parse.default(url, {});
|
|
44
|
+
return pathname + query;
|
|
45
|
+
}
|
|
46
|
+
function hasPropsAt(url) {
|
|
47
|
+
const parsed = new import_url_parse.default(url, {}, true);
|
|
48
|
+
const query = parsed.query;
|
|
49
|
+
return !!query["props_at"];
|
|
50
|
+
}
|
|
51
|
+
function propsAtParam(url) {
|
|
52
|
+
const parsed = new import_url_parse.default(url, {}, true);
|
|
53
|
+
const query = parsed.query;
|
|
54
|
+
return query["props_at"];
|
|
55
|
+
}
|
|
56
|
+
function withFormatJson(url) {
|
|
57
|
+
const parsed = new import_url_parse.default(url, {}, true);
|
|
58
|
+
parsed.query["format"] = "json";
|
|
59
|
+
return parsed.toString();
|
|
60
|
+
}
|
|
61
|
+
function removePropsAt(url) {
|
|
62
|
+
const parsed = new import_url_parse.default(url, {}, true);
|
|
63
|
+
const query = parsed.query;
|
|
64
|
+
delete query["props_at"];
|
|
65
|
+
parsed.set("query", query);
|
|
66
|
+
return parsed.toString();
|
|
67
|
+
}
|
|
68
|
+
function urlToPageKey(url) {
|
|
69
|
+
const parsed = new import_url_parse.default(url, {}, true);
|
|
70
|
+
const query = parsed.query;
|
|
71
|
+
delete query["props_at"];
|
|
72
|
+
delete query["format"];
|
|
73
|
+
parsed.set("query", query);
|
|
74
|
+
return pathQuery(parsed.toString());
|
|
75
|
+
}
|
|
76
|
+
function withoutHash(url) {
|
|
77
|
+
const parsed = new import_url_parse.default(url, {}, true);
|
|
78
|
+
parsed.set("hash", "");
|
|
79
|
+
return parsed.toString();
|
|
80
|
+
}
|
|
81
|
+
function withoutBusters(url) {
|
|
82
|
+
const parsed = new import_url_parse.default(url, {}, true);
|
|
83
|
+
const query = parsed.query;
|
|
84
|
+
delete query["format"];
|
|
85
|
+
parsed.set("query", query);
|
|
86
|
+
return pathQuery(parsed.toString());
|
|
87
|
+
}
|
|
88
|
+
function formatForXHR(url) {
|
|
89
|
+
const formats = [withoutHash, withFormatJson];
|
|
90
|
+
return formats.reduce((memo, f) => f(memo), url);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// lib/utils/immutability.ts
|
|
94
|
+
var canLookAhead = /^[\da-zA-Z\-_]+=[\da-zA-Z\-_]+$/;
|
|
95
|
+
var KeyPathError = class extends Error {
|
|
96
|
+
constructor(message) {
|
|
97
|
+
super(message);
|
|
98
|
+
this.name = "KeyPathError";
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
function getIn(node, path) {
|
|
102
|
+
const keyPath = normalizeKeyPath(path);
|
|
103
|
+
let result = node;
|
|
104
|
+
let i;
|
|
105
|
+
for (i = 0; i < keyPath.length; i++) {
|
|
106
|
+
const key = keyPath[i];
|
|
107
|
+
if (typeof result === "object" && result !== null) {
|
|
108
|
+
if (!Array.isArray(result) && canLookAhead.test(key)) {
|
|
109
|
+
throw new KeyPathError(
|
|
110
|
+
`Expected to find an Array when using the key: ${key}`
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
result = atKey(result, key);
|
|
114
|
+
} else {
|
|
115
|
+
throw new KeyPathError(
|
|
116
|
+
`Expected to traverse an Array or Obj, got ${JSON.stringify(result)}`
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (i === keyPath.length) {
|
|
121
|
+
return result;
|
|
122
|
+
} else {
|
|
123
|
+
return void 0;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function getKey(node, key) {
|
|
127
|
+
if (Array.isArray(node) && Number.isNaN(Number(key))) {
|
|
128
|
+
const key_parts = Array.from(key.split("="));
|
|
129
|
+
const attr = key_parts[0];
|
|
130
|
+
const id = key_parts[1];
|
|
131
|
+
if (!id || !attr) {
|
|
132
|
+
return key;
|
|
133
|
+
}
|
|
134
|
+
let i;
|
|
135
|
+
let child;
|
|
136
|
+
for (i = 0; i < node.length; i++) {
|
|
137
|
+
child = node[i];
|
|
138
|
+
if (typeof child === "object" && !Array.isArray(child) && child !== null) {
|
|
139
|
+
const val = child[attr];
|
|
140
|
+
if (val && val.toString() === id) {
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
} else {
|
|
144
|
+
throw new KeyPathError(`Could not look ahead ${key} at ${child}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (i === node.length) {
|
|
148
|
+
throw new KeyPathError(`Could not find ${key} while looking ahead`);
|
|
149
|
+
}
|
|
150
|
+
return i;
|
|
151
|
+
} else {
|
|
152
|
+
return key;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function atKey(node, key) {
|
|
156
|
+
const actualKey = getKey(node, key);
|
|
157
|
+
if (Array.isArray(node)) {
|
|
158
|
+
return node[actualKey];
|
|
159
|
+
} else {
|
|
160
|
+
return node[actualKey];
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
function normalizeKeyPath(path) {
|
|
164
|
+
if (typeof path === "string") {
|
|
165
|
+
path = path.replace(/ /g, "");
|
|
166
|
+
if (path === "") {
|
|
167
|
+
return [];
|
|
168
|
+
}
|
|
169
|
+
return path.split(".");
|
|
170
|
+
} else {
|
|
171
|
+
return path;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// lib/utils/request.ts
|
|
176
|
+
var import_url_parse2 = __toESM(require("url-parse"));
|
|
177
|
+
|
|
178
|
+
// lib/config.ts
|
|
179
|
+
var config = {
|
|
180
|
+
baseUrl: "",
|
|
181
|
+
maxPages: 20
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
// lib/utils/request.ts
|
|
185
|
+
function isValidResponse(xhr) {
|
|
186
|
+
return isValidContent(xhr) && !downloadingFile(xhr);
|
|
187
|
+
}
|
|
188
|
+
function isValidContent(rsp) {
|
|
189
|
+
const contentType = rsp.headers.get("content-type");
|
|
190
|
+
const jsContent = /^(?:application\/json)(?:;|$)/;
|
|
191
|
+
return !!(contentType && contentType.match(jsContent));
|
|
192
|
+
}
|
|
193
|
+
function downloadingFile(xhr) {
|
|
194
|
+
const disposition = xhr.headers.get("content-disposition");
|
|
195
|
+
return !!(disposition && disposition.match(/^attachment/) !== null);
|
|
196
|
+
}
|
|
197
|
+
var SuperglueResponseError = class extends Error {
|
|
198
|
+
constructor(message) {
|
|
199
|
+
super(message);
|
|
200
|
+
this.name = "SuperglueResponseError";
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
function validateResponse(args) {
|
|
204
|
+
const { rsp } = args;
|
|
205
|
+
if (isValidResponse(rsp)) {
|
|
206
|
+
return args;
|
|
207
|
+
} else {
|
|
208
|
+
const error = new SuperglueResponseError("Invalid Superglue Response");
|
|
209
|
+
error.response = rsp;
|
|
210
|
+
throw error;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
function handleServerErrors(args) {
|
|
214
|
+
const { rsp } = args;
|
|
215
|
+
if (!rsp.ok && rsp.status !== 422) {
|
|
216
|
+
if (rsp.status === 406) {
|
|
217
|
+
console.error(
|
|
218
|
+
"Superglue encountered a 406 Not Acceptable response. This can happen if you used respond_to and didn't specify format.json in the block. Try adding it to your respond_to. For example:\n\nrespond_to do |format|\n format.html\n format.json\n format.csv\nend"
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
const error = new SuperglueResponseError(rsp.statusText);
|
|
222
|
+
error.response = rsp;
|
|
223
|
+
throw error;
|
|
224
|
+
}
|
|
225
|
+
return args;
|
|
226
|
+
}
|
|
227
|
+
function argsForFetch(getState, pathQuery2, {
|
|
228
|
+
method = "GET",
|
|
229
|
+
headers = {},
|
|
230
|
+
body = "",
|
|
231
|
+
signal,
|
|
232
|
+
...rest
|
|
233
|
+
} = {}) {
|
|
234
|
+
method = method.toUpperCase();
|
|
235
|
+
const currentState = getState().superglue;
|
|
236
|
+
const nextHeaders = { ...headers };
|
|
237
|
+
nextHeaders["x-requested-with"] = "XMLHttpRequest";
|
|
238
|
+
nextHeaders["accept"] = "application/json";
|
|
239
|
+
nextHeaders["x-superglue-request"] = "true";
|
|
240
|
+
if (method != "GET" && method != "HEAD") {
|
|
241
|
+
nextHeaders["content-type"] = "application/json";
|
|
242
|
+
}
|
|
243
|
+
if (body instanceof FormData) {
|
|
244
|
+
delete nextHeaders["content-type"];
|
|
245
|
+
}
|
|
246
|
+
if (currentState.csrfToken) {
|
|
247
|
+
nextHeaders["x-csrf-token"] = currentState.csrfToken;
|
|
248
|
+
}
|
|
249
|
+
const fetchPath = new import_url_parse2.default(
|
|
250
|
+
formatForXHR(pathQuery2),
|
|
251
|
+
config.baseUrl || {},
|
|
252
|
+
true
|
|
253
|
+
);
|
|
254
|
+
const credentials = "same-origin";
|
|
255
|
+
if (!(method == "GET" || method == "HEAD")) {
|
|
256
|
+
nextHeaders["x-http-method-override"] = method;
|
|
257
|
+
method = "POST";
|
|
258
|
+
}
|
|
259
|
+
const options = {
|
|
260
|
+
method,
|
|
261
|
+
headers: nextHeaders,
|
|
262
|
+
body,
|
|
263
|
+
credentials,
|
|
264
|
+
signal
|
|
265
|
+
};
|
|
266
|
+
if (currentState.currentPageKey) {
|
|
267
|
+
const referrer = new import_url_parse2.default(
|
|
268
|
+
currentState.currentPageKey,
|
|
269
|
+
config.baseUrl || {},
|
|
270
|
+
false
|
|
271
|
+
).href;
|
|
272
|
+
options.referrer = referrer;
|
|
273
|
+
}
|
|
274
|
+
if (method == "GET" || method == "HEAD") {
|
|
275
|
+
if (options.body instanceof FormData) {
|
|
276
|
+
const allData = new URLSearchParams(
|
|
277
|
+
options.body
|
|
278
|
+
);
|
|
279
|
+
const nextQuery = { ...fetchPath.query, ...Object.fromEntries(allData) };
|
|
280
|
+
fetchPath.set("query", nextQuery);
|
|
281
|
+
}
|
|
282
|
+
delete options.body;
|
|
283
|
+
}
|
|
284
|
+
return [fetchPath.toString(), { ...options, ...rest }];
|
|
285
|
+
}
|
|
286
|
+
function extractJSON(rsp) {
|
|
287
|
+
return rsp.json().then((json) => {
|
|
288
|
+
return { rsp, json };
|
|
289
|
+
}).catch((e) => {
|
|
290
|
+
e.response = rsp;
|
|
291
|
+
throw e;
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
function parseResponse(prm) {
|
|
295
|
+
return Promise.resolve(prm).then(extractJSON).then(handleServerErrors).then(validateResponse);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// lib/utils/window.ts
|
|
299
|
+
function needsRefresh(prevAssets, newAssets) {
|
|
300
|
+
if (prevAssets && newAssets) {
|
|
301
|
+
const hasNewAssets = !newAssets.every((asset) => prevAssets.includes(asset));
|
|
302
|
+
return hasNewAssets;
|
|
303
|
+
} else {
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// lib/action_creators/index.ts
|
|
309
|
+
var import_url_parse3 = __toESM(require("url-parse"));
|
|
310
|
+
|
|
311
|
+
// lib/actions.ts
|
|
312
|
+
var import_toolkit = require("@reduxjs/toolkit");
|
|
313
|
+
var GRAFTING_ERROR = "@@superglue/GRAFTING_ERROR";
|
|
314
|
+
var GRAFTING_SUCCESS = "@@superglue/GRAFTING_SUCCESS";
|
|
315
|
+
var saveResponse = (0, import_toolkit.createAction)(
|
|
316
|
+
"@@superglue/SAVE_RESPONSE",
|
|
317
|
+
({ pageKey, page }) => {
|
|
318
|
+
pageKey = urlToPageKey(pageKey);
|
|
319
|
+
return {
|
|
320
|
+
payload: {
|
|
321
|
+
pageKey,
|
|
322
|
+
page
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
);
|
|
327
|
+
var handleGraft = (0, import_toolkit.createAction)(
|
|
328
|
+
"@@superglue/HANDLE_GRAFT",
|
|
329
|
+
({ pageKey, page }) => {
|
|
330
|
+
pageKey = urlToPageKey(pageKey);
|
|
331
|
+
return {
|
|
332
|
+
payload: {
|
|
333
|
+
page,
|
|
334
|
+
pageKey
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
);
|
|
339
|
+
var superglueError = (0, import_toolkit.createAction)(
|
|
340
|
+
"@@superglue/ERROR"
|
|
341
|
+
);
|
|
342
|
+
var updateFragments = (0, import_toolkit.createAction)("@@superglue/UPDATE_FRAGMENTS");
|
|
343
|
+
var copyPage = (0, import_toolkit.createAction)(
|
|
344
|
+
"@@superglue/COPY_PAGE"
|
|
345
|
+
);
|
|
346
|
+
var removePage = (0, import_toolkit.createAction)(
|
|
347
|
+
"@@superglue/REMOVE_PAGE"
|
|
348
|
+
);
|
|
349
|
+
var beforeFetch = (0, import_toolkit.createAction)(
|
|
350
|
+
"@@superglue/BEFORE_FETCH"
|
|
351
|
+
);
|
|
352
|
+
var beforeVisit = (0, import_toolkit.createAction)("@@superglue/BEFORE_VISIT");
|
|
353
|
+
var beforeRemote = (0, import_toolkit.createAction)("@@superglue/BEFORE_REMOTE");
|
|
354
|
+
var setCSRFToken = (0, import_toolkit.createAction)("@@superglue/SET_CSRF_TOKEN");
|
|
355
|
+
var historyChange = (0, import_toolkit.createAction)("@@superglue/HISTORY_CHANGE");
|
|
356
|
+
var setActivePage = (0, import_toolkit.createAction)("@@superglue/SET_ACTIVE_PAGE");
|
|
357
|
+
|
|
358
|
+
// lib/action_creators/requests.ts
|
|
359
|
+
function handleFetchErr(err, fetchArgs, dispatch) {
|
|
360
|
+
dispatch(superglueError({ message: err.message }));
|
|
361
|
+
throw err;
|
|
362
|
+
}
|
|
363
|
+
function buildMeta(pageKey, page, state, rsp, fetchArgs) {
|
|
364
|
+
const { assets: prevAssets } = state;
|
|
365
|
+
const { assets: nextAssets } = page;
|
|
366
|
+
return {
|
|
367
|
+
pageKey,
|
|
368
|
+
page,
|
|
369
|
+
redirected: rsp.redirected,
|
|
370
|
+
rsp,
|
|
371
|
+
fetchArgs,
|
|
372
|
+
componentIdentifier: page.componentIdentifier,
|
|
373
|
+
needsRefresh: needsRefresh(prevAssets, nextAssets)
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
var MismatchedComponentError = class extends Error {
|
|
377
|
+
constructor(message) {
|
|
378
|
+
super(message);
|
|
379
|
+
this.name = "MismatchedComponentError";
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
var remote = (path, {
|
|
383
|
+
pageKey: targetPageKey,
|
|
384
|
+
force = false,
|
|
385
|
+
beforeSave = (prevPage, receivedPage) => receivedPage,
|
|
386
|
+
...rest
|
|
387
|
+
} = {}) => {
|
|
388
|
+
path = withoutBusters(path);
|
|
389
|
+
targetPageKey = targetPageKey && urlToPageKey(targetPageKey);
|
|
390
|
+
return (dispatch, getState) => {
|
|
391
|
+
const fetchArgs = argsForFetch(getState, path, rest);
|
|
392
|
+
const currentPageKey = getState().superglue.currentPageKey;
|
|
393
|
+
dispatch(beforeRemote({ currentPageKey, fetchArgs }));
|
|
394
|
+
dispatch(beforeFetch({ fetchArgs }));
|
|
395
|
+
return fetch(...fetchArgs).then(parseResponse).then(({ rsp, json }) => {
|
|
396
|
+
const { superglue, pages = {} } = getState();
|
|
397
|
+
let pageKey;
|
|
398
|
+
if (targetPageKey === void 0) {
|
|
399
|
+
const isGet = fetchArgs[1].method === "GET";
|
|
400
|
+
pageKey = calculatePageKey(rsp, isGet, currentPageKey);
|
|
401
|
+
} else {
|
|
402
|
+
pageKey = targetPageKey;
|
|
403
|
+
}
|
|
404
|
+
const meta = buildMeta(pageKey, json, superglue, rsp, fetchArgs);
|
|
405
|
+
const existingId = pages[pageKey]?.componentIdentifier;
|
|
406
|
+
const receivedId = json.componentIdentifier;
|
|
407
|
+
if (!!existingId && existingId != receivedId && !force) {
|
|
408
|
+
const message = `You cannot replace or update an existing page
|
|
409
|
+
located at pages["${currentPageKey}"] that has a componentIdentifier
|
|
410
|
+
of "${existingId}" with the contents of a page response that has a
|
|
411
|
+
componentIdentifier of "${receivedId}".
|
|
412
|
+
|
|
413
|
+
This can happen if you're using data-sg-remote or remote but your
|
|
414
|
+
response redirected to a page with a different componentIdentifier
|
|
415
|
+
than the target page.
|
|
416
|
+
|
|
417
|
+
This limitation exists because the resulting page shape from grafting
|
|
418
|
+
"${receivedId}"'s "${propsAtParam(path)}" into "${existingId}" may not be
|
|
419
|
+
compatible with the page component associated with "${existingId}".
|
|
420
|
+
|
|
421
|
+
Consider using data-sg-visit, the visit function, or redirect_back to
|
|
422
|
+
the same page. Or if you're sure you want to proceed, use force: true.
|
|
423
|
+
`;
|
|
424
|
+
throw new MismatchedComponentError(message);
|
|
425
|
+
}
|
|
426
|
+
const page = beforeSave(pages[pageKey], json);
|
|
427
|
+
return dispatch(saveAndProcessPage(pageKey, page)).then(() => meta);
|
|
428
|
+
}).catch((e) => handleFetchErr(e, fetchArgs, dispatch));
|
|
429
|
+
};
|
|
430
|
+
};
|
|
431
|
+
var lastVisitController = {
|
|
432
|
+
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
|
|
433
|
+
abort: (_reason) => {
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
var visit = (path, {
|
|
437
|
+
placeholderKey,
|
|
438
|
+
beforeSave = (prevPage, receivedPage) => receivedPage,
|
|
439
|
+
revisit = false,
|
|
440
|
+
...rest
|
|
441
|
+
} = {}) => {
|
|
442
|
+
path = withoutBusters(path);
|
|
443
|
+
return (dispatch, getState) => {
|
|
444
|
+
const currentPageKey = getState().superglue.currentPageKey;
|
|
445
|
+
placeholderKey = placeholderKey && urlToPageKey(placeholderKey) || currentPageKey;
|
|
446
|
+
const hasPlaceholder = placeholderKey in getState().pages;
|
|
447
|
+
if (hasPropsAt(path) && !hasPlaceholder) {
|
|
448
|
+
console.warn(
|
|
449
|
+
`Could not find placeholder with key ${placeholderKey} in state. The props_at param will be ignored`
|
|
450
|
+
);
|
|
451
|
+
path = removePropsAt(path);
|
|
452
|
+
}
|
|
453
|
+
const controller = new AbortController();
|
|
454
|
+
const { signal } = controller;
|
|
455
|
+
const fetchArgs = argsForFetch(getState, path, {
|
|
456
|
+
...rest,
|
|
457
|
+
signal
|
|
458
|
+
});
|
|
459
|
+
dispatch(beforeVisit({ currentPageKey, fetchArgs }));
|
|
460
|
+
dispatch(beforeFetch({ fetchArgs }));
|
|
461
|
+
lastVisitController.abort(
|
|
462
|
+
"Aborting the previous `visit`. There can be one visit at a time. Use `remote` if there is a need for async requests."
|
|
463
|
+
);
|
|
464
|
+
lastVisitController = controller;
|
|
465
|
+
return fetch(...fetchArgs).then(parseResponse).then(({ rsp, json }) => {
|
|
466
|
+
const { superglue, pages = {} } = getState();
|
|
467
|
+
const isGet = fetchArgs[1].method === "GET";
|
|
468
|
+
const pageKey = calculatePageKey(rsp, isGet, currentPageKey);
|
|
469
|
+
if (placeholderKey && hasPropsAt(path) && hasPlaceholder) {
|
|
470
|
+
const existingId = pages[placeholderKey]?.componentIdentifier;
|
|
471
|
+
const receivedId = json.componentIdentifier;
|
|
472
|
+
if (!!existingId && existingId != receivedId) {
|
|
473
|
+
const message = `You received a page response with a
|
|
474
|
+
componentIdentifier "${receivedId}" that is different than the
|
|
475
|
+
componentIdentifier "${existingId}" located at ${placeholderKey}.
|
|
476
|
+
|
|
477
|
+
This can happen if you're using data-sg-visit or visit with a
|
|
478
|
+
props_at param, but the response redirected to a page with a
|
|
479
|
+
different componentIdentifier than the target page.
|
|
480
|
+
|
|
481
|
+
This limitation exists because the resulting page shape from grafting
|
|
482
|
+
"${receivedId}"'s "${propsAtParam(path)}" into "${existingId}" may not be
|
|
483
|
+
compatible with the page component associated with "${existingId}".
|
|
484
|
+
|
|
485
|
+
Check that you're rendering a page with a matching
|
|
486
|
+
componentIdentifier, or consider using redirect_back_with_props_at
|
|
487
|
+
to the same page.
|
|
488
|
+
`;
|
|
489
|
+
throw new MismatchedComponentError(message);
|
|
490
|
+
}
|
|
491
|
+
dispatch(copyPage({ from: placeholderKey, to: pageKey }));
|
|
492
|
+
}
|
|
493
|
+
const meta = buildMeta(pageKey, json, superglue, rsp, fetchArgs);
|
|
494
|
+
const visitMeta = {
|
|
495
|
+
...meta,
|
|
496
|
+
navigationAction: calculateNavAction(
|
|
497
|
+
meta,
|
|
498
|
+
rsp,
|
|
499
|
+
isGet,
|
|
500
|
+
pageKey,
|
|
501
|
+
currentPageKey,
|
|
502
|
+
revisit
|
|
503
|
+
)
|
|
504
|
+
};
|
|
505
|
+
const page = beforeSave(pages[pageKey], json);
|
|
506
|
+
return dispatch(saveAndProcessPage(pageKey, page)).then(() => visitMeta);
|
|
507
|
+
}).catch((e) => handleFetchErr(e, fetchArgs, dispatch));
|
|
508
|
+
};
|
|
509
|
+
};
|
|
510
|
+
function calculateNavAction(meta, rsp, isGet, pageKey, currentPageKey, revisit) {
|
|
511
|
+
let navigationAction = "push";
|
|
512
|
+
if (!rsp.redirected && !isGet) {
|
|
513
|
+
navigationAction = "replace";
|
|
514
|
+
}
|
|
515
|
+
const isSamePage = pageKey == currentPageKey;
|
|
516
|
+
if (isSamePage) {
|
|
517
|
+
navigationAction = "none";
|
|
518
|
+
}
|
|
519
|
+
if (revisit && isGet) {
|
|
520
|
+
if (rsp.redirected) {
|
|
521
|
+
navigationAction = "replace";
|
|
522
|
+
} else {
|
|
523
|
+
navigationAction = "none";
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return navigationAction;
|
|
527
|
+
}
|
|
528
|
+
function calculatePageKey(rsp, isGet, currentPageKey) {
|
|
529
|
+
let pageKey = urlToPageKey(rsp.url);
|
|
530
|
+
if (!isGet && !rsp.redirected) {
|
|
531
|
+
pageKey = currentPageKey;
|
|
532
|
+
}
|
|
533
|
+
const contentLocation = rsp.headers.get("content-location");
|
|
534
|
+
if (contentLocation) {
|
|
535
|
+
pageKey = urlToPageKey(contentLocation);
|
|
536
|
+
}
|
|
537
|
+
return pageKey;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// lib/action_creators/index.ts
|
|
541
|
+
function fetchDeferments(pageKey, defers = []) {
|
|
542
|
+
pageKey = urlToPageKey(pageKey);
|
|
543
|
+
return (dispatch) => {
|
|
544
|
+
const fetches = defers.filter(({ type }) => type === "auto").map(function({
|
|
545
|
+
url,
|
|
546
|
+
successAction = GRAFTING_SUCCESS,
|
|
547
|
+
failAction = GRAFTING_ERROR
|
|
548
|
+
}) {
|
|
549
|
+
const parsedUrl = new import_url_parse3.default(url, true);
|
|
550
|
+
const keyPath = parsedUrl.query.props_at;
|
|
551
|
+
return dispatch(remote(url, { pageKey })).then(() => {
|
|
552
|
+
dispatch({
|
|
553
|
+
type: successAction,
|
|
554
|
+
payload: {
|
|
555
|
+
pageKey,
|
|
556
|
+
keyPath
|
|
557
|
+
}
|
|
558
|
+
});
|
|
559
|
+
}).catch((err) => {
|
|
560
|
+
dispatch({
|
|
561
|
+
type: failAction,
|
|
562
|
+
payload: {
|
|
563
|
+
url,
|
|
564
|
+
err,
|
|
565
|
+
pageKey,
|
|
566
|
+
keyPath
|
|
567
|
+
}
|
|
568
|
+
});
|
|
569
|
+
});
|
|
570
|
+
});
|
|
571
|
+
return Promise.all(fetches);
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
function saveAndProcessPage(pageKey, page) {
|
|
575
|
+
return (dispatch, getState) => {
|
|
576
|
+
pageKey = urlToPageKey(pageKey);
|
|
577
|
+
const { defers = [] } = page;
|
|
578
|
+
if ("action" in page) {
|
|
579
|
+
const prevPage = getState().pages[pageKey];
|
|
580
|
+
dispatch(handleGraft({ pageKey, page }));
|
|
581
|
+
const currentPage = getState().pages[pageKey];
|
|
582
|
+
currentPage.fragments.forEach((fragment) => {
|
|
583
|
+
const { type, path } = fragment;
|
|
584
|
+
const currentFragment = getIn(currentPage, path);
|
|
585
|
+
const prevFragment = getIn(prevPage, path);
|
|
586
|
+
if (!prevFragment) {
|
|
587
|
+
dispatch(
|
|
588
|
+
updateFragments({
|
|
589
|
+
name: type,
|
|
590
|
+
pageKey,
|
|
591
|
+
value: currentFragment,
|
|
592
|
+
path
|
|
593
|
+
})
|
|
594
|
+
);
|
|
595
|
+
} else if (currentFragment !== prevFragment) {
|
|
596
|
+
dispatch(
|
|
597
|
+
updateFragments({
|
|
598
|
+
name: type,
|
|
599
|
+
pageKey,
|
|
600
|
+
value: currentFragment,
|
|
601
|
+
previousValue: prevFragment,
|
|
602
|
+
path
|
|
603
|
+
})
|
|
604
|
+
);
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
} else {
|
|
608
|
+
dispatch(saveResponse({ pageKey, page }));
|
|
609
|
+
const currentPage = getState().pages[pageKey];
|
|
610
|
+
currentPage.fragments.forEach((fragment) => {
|
|
611
|
+
const { type, path } = fragment;
|
|
612
|
+
const currentFragment = getIn(currentPage, path);
|
|
613
|
+
dispatch(
|
|
614
|
+
updateFragments({
|
|
615
|
+
name: type,
|
|
616
|
+
pageKey,
|
|
617
|
+
value: currentFragment,
|
|
618
|
+
path
|
|
619
|
+
})
|
|
620
|
+
);
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
const hasFetch = typeof fetch != "undefined";
|
|
624
|
+
if (hasFetch) {
|
|
625
|
+
return dispatch(fetchDeferments(pageKey, defers)).then(
|
|
626
|
+
() => Promise.resolve()
|
|
627
|
+
);
|
|
628
|
+
} else {
|
|
629
|
+
return Promise.resolve();
|
|
630
|
+
}
|
|
631
|
+
};
|
|
632
|
+
}
|
|
633
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
634
|
+
0 && (module.exports = {
|
|
635
|
+
MismatchedComponentError,
|
|
636
|
+
remote,
|
|
637
|
+
saveAndProcessPage,
|
|
638
|
+
visit
|
|
639
|
+
});
|
|
640
|
+
//# sourceMappingURL=action_creators.cjs.map
|