@hybridly/core 0.0.1-alpha.10 → 0.0.1-alpha.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.
- package/dist/index.cjs +23 -3
- package/dist/index.d.ts +19 -13
- package/dist/index.mjs +23 -3
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
const utils = require('@hybridly/utils');
|
|
6
5
|
const qs = require('qs');
|
|
6
|
+
const utils = require('@hybridly/utils');
|
|
7
7
|
const axios = require('axios');
|
|
8
8
|
|
|
9
9
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
|
|
@@ -165,7 +165,16 @@ function makeUrl(href, transformations = {}) {
|
|
|
165
165
|
try {
|
|
166
166
|
const base = document?.location?.href === "//" ? void 0 : document.location.href;
|
|
167
167
|
const url = new URL(String(href), base);
|
|
168
|
-
Object.entries(transformations ?? {}).forEach(([key, value]) =>
|
|
168
|
+
Object.entries(transformations ?? {}).forEach(([key, value]) => {
|
|
169
|
+
if (key === "query") {
|
|
170
|
+
key = "search";
|
|
171
|
+
value = qs__default.stringify(utils.merge(qs__default.parse(url.search, { ignoreQueryPrefix: true }), value), {
|
|
172
|
+
encodeValuesOnly: true,
|
|
173
|
+
arrayFormat: "brackets"
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
Reflect.set(url, key, value);
|
|
177
|
+
});
|
|
169
178
|
return url;
|
|
170
179
|
} catch (error) {
|
|
171
180
|
throw new TypeError(`${href} is not resolvable to a valid URL.`);
|
|
@@ -423,6 +432,17 @@ async function performHybridNavigation(options) {
|
|
|
423
432
|
options.method = "POST";
|
|
424
433
|
}
|
|
425
434
|
}
|
|
435
|
+
if (!options.method) {
|
|
436
|
+
utils.debug.router("Setting method to GET because none was provided.");
|
|
437
|
+
options.method = "GET";
|
|
438
|
+
}
|
|
439
|
+
if (!(options.data instanceof FormData) && options.method === "GET" && Object.keys(options.data ?? {}).length) {
|
|
440
|
+
utils.debug.router("Transforming data to query parameters.", options.data);
|
|
441
|
+
options.url = makeUrl(options.url ?? context.url, {
|
|
442
|
+
query: options.data
|
|
443
|
+
});
|
|
444
|
+
options.data = {};
|
|
445
|
+
}
|
|
426
446
|
if (!await runHooks("before", options.hooks, options, context)) {
|
|
427
447
|
utils.debug.router('"before" event returned false, aborting the navigation.');
|
|
428
448
|
throw new NavigationCancelledError('The navigation was cancelled by the "before" event.');
|
|
@@ -448,7 +468,7 @@ async function performHybridNavigation(options) {
|
|
|
448
468
|
utils.debug.router("Making request with axios.");
|
|
449
469
|
const response = await context.axios.request({
|
|
450
470
|
url: context.pendingNavigation.url.toString(),
|
|
451
|
-
method: options.method
|
|
471
|
+
method: options.method,
|
|
452
472
|
data: options.method === "GET" ? {} : options.data,
|
|
453
473
|
params: options.method === "GET" ? options.data : {},
|
|
454
474
|
signal: context.pendingNavigation.controller.signal,
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { RequestData } from '@hybridly/utils';
|
|
2
2
|
import { AxiosResponse, Axios, AxiosProgressEvent } from 'axios';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
5
5
|
|
|
6
6
|
interface Hooks {
|
|
7
7
|
/**
|
|
@@ -113,7 +113,7 @@ interface InternalRouterContext {
|
|
|
113
113
|
axios: Axios;
|
|
114
114
|
}
|
|
115
115
|
/** Router context. */
|
|
116
|
-
|
|
116
|
+
type RouterContext = Readonly<InternalRouterContext>;
|
|
117
117
|
/** Adapter-specific functions. */
|
|
118
118
|
interface Adapter {
|
|
119
119
|
/** Resolves a component from the given name. */
|
|
@@ -138,8 +138,10 @@ interface Serializer {
|
|
|
138
138
|
/** Gets the current context. */
|
|
139
139
|
declare function getRouterContext(): RouterContext;
|
|
140
140
|
|
|
141
|
-
|
|
142
|
-
|
|
141
|
+
type UrlResolvable = string | URL | Location;
|
|
142
|
+
type UrlTransformable = Partial<Omit<URL, 'searchParams' | 'toJSON' | 'toString'>> & {
|
|
143
|
+
query?: any;
|
|
144
|
+
};
|
|
143
145
|
/**
|
|
144
146
|
* Converts an input to an URL, optionally changing its properties after initialization.
|
|
145
147
|
*/
|
|
@@ -149,7 +151,7 @@ declare function makeUrl(href: UrlResolvable, transformations?: UrlTransformable
|
|
|
149
151
|
*/
|
|
150
152
|
declare function sameUrls(...hrefs: UrlResolvable[]): boolean;
|
|
151
153
|
|
|
152
|
-
|
|
154
|
+
type ConditionalNavigationOption = boolean | ((payload: HybridPayload) => boolean);
|
|
153
155
|
interface ComponentNavigationOptions {
|
|
154
156
|
/** Name of the component to use. */
|
|
155
157
|
component?: string;
|
|
@@ -200,7 +202,7 @@ interface NavigationOptions {
|
|
|
200
202
|
*/
|
|
201
203
|
isBackForward?: boolean;
|
|
202
204
|
}
|
|
203
|
-
|
|
205
|
+
type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
204
206
|
interface HybridRequestOptions extends Omit<NavigationOptions, 'payload'> {
|
|
205
207
|
/** The URL to navigation. */
|
|
206
208
|
url?: UrlResolvable;
|
|
@@ -225,6 +227,10 @@ interface HybridRequestOptions extends Omit<NavigationOptions, 'payload'> {
|
|
|
225
227
|
* @see https://laravel.com/docs/9.x/routing#form-method-spoofing
|
|
226
228
|
*/
|
|
227
229
|
spoof?: boolean;
|
|
230
|
+
/**
|
|
231
|
+
* If `false`, does not trigger the progress bar for this request.
|
|
232
|
+
*/
|
|
233
|
+
progress?: boolean;
|
|
228
234
|
}
|
|
229
235
|
interface NavigationResponse {
|
|
230
236
|
response?: AxiosResponse;
|
|
@@ -282,21 +288,21 @@ interface View {
|
|
|
282
288
|
/** Properties to apply to the component. */
|
|
283
289
|
properties: Properties;
|
|
284
290
|
}
|
|
285
|
-
|
|
291
|
+
type Property = null | string | number | boolean | Property[] | {
|
|
286
292
|
[name: string]: Property;
|
|
287
293
|
};
|
|
288
|
-
|
|
294
|
+
type Properties = Record<string | number, Property>;
|
|
289
295
|
interface SwapOptions<T> {
|
|
290
296
|
/** The new component. */
|
|
291
297
|
component: T;
|
|
292
298
|
/** Whether to preserve the state of the component. */
|
|
293
299
|
preserveState?: boolean;
|
|
294
300
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
301
|
+
type ViewComponent = any;
|
|
302
|
+
type DialogComponent = any;
|
|
303
|
+
type ResolveComponent = (name: string) => Promise<ViewComponent>;
|
|
304
|
+
type SwapView = (options: SwapOptions<ViewComponent>) => Promise<void>;
|
|
305
|
+
type SwapDialog = (options: SwapOptions<DialogComponent>) => Promise<void>;
|
|
300
306
|
/** The payload of a navigation request from the server. */
|
|
301
307
|
interface HybridPayload {
|
|
302
308
|
/** The view to use in this request. */
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { debug, debounce, random, hasFiles, objectToFormData, when, merge, match, showResponseErrorModal } from '@hybridly/utils';
|
|
2
1
|
import qs from 'qs';
|
|
2
|
+
import { debug, merge, debounce, random, hasFiles, objectToFormData, when, match, showResponseErrorModal } from '@hybridly/utils';
|
|
3
3
|
import axios from 'axios';
|
|
4
4
|
|
|
5
5
|
const STORAGE_EXTERNAL_KEY = "hybridly:external";
|
|
@@ -156,7 +156,16 @@ function makeUrl(href, transformations = {}) {
|
|
|
156
156
|
try {
|
|
157
157
|
const base = document?.location?.href === "//" ? void 0 : document.location.href;
|
|
158
158
|
const url = new URL(String(href), base);
|
|
159
|
-
Object.entries(transformations ?? {}).forEach(([key, value]) =>
|
|
159
|
+
Object.entries(transformations ?? {}).forEach(([key, value]) => {
|
|
160
|
+
if (key === "query") {
|
|
161
|
+
key = "search";
|
|
162
|
+
value = qs.stringify(merge(qs.parse(url.search, { ignoreQueryPrefix: true }), value), {
|
|
163
|
+
encodeValuesOnly: true,
|
|
164
|
+
arrayFormat: "brackets"
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
Reflect.set(url, key, value);
|
|
168
|
+
});
|
|
160
169
|
return url;
|
|
161
170
|
} catch (error) {
|
|
162
171
|
throw new TypeError(`${href} is not resolvable to a valid URL.`);
|
|
@@ -414,6 +423,17 @@ async function performHybridNavigation(options) {
|
|
|
414
423
|
options.method = "POST";
|
|
415
424
|
}
|
|
416
425
|
}
|
|
426
|
+
if (!options.method) {
|
|
427
|
+
debug.router("Setting method to GET because none was provided.");
|
|
428
|
+
options.method = "GET";
|
|
429
|
+
}
|
|
430
|
+
if (!(options.data instanceof FormData) && options.method === "GET" && Object.keys(options.data ?? {}).length) {
|
|
431
|
+
debug.router("Transforming data to query parameters.", options.data);
|
|
432
|
+
options.url = makeUrl(options.url ?? context.url, {
|
|
433
|
+
query: options.data
|
|
434
|
+
});
|
|
435
|
+
options.data = {};
|
|
436
|
+
}
|
|
417
437
|
if (!await runHooks("before", options.hooks, options, context)) {
|
|
418
438
|
debug.router('"before" event returned false, aborting the navigation.');
|
|
419
439
|
throw new NavigationCancelledError('The navigation was cancelled by the "before" event.');
|
|
@@ -439,7 +459,7 @@ async function performHybridNavigation(options) {
|
|
|
439
459
|
debug.router("Making request with axios.");
|
|
440
460
|
const response = await context.axios.request({
|
|
441
461
|
url: context.pendingNavigation.url.toString(),
|
|
442
|
-
method: options.method
|
|
462
|
+
method: options.method,
|
|
443
463
|
data: options.method === "GET" ? {} : options.data,
|
|
444
464
|
params: options.method === "GET" ? options.data : {},
|
|
445
465
|
signal: context.pendingNavigation.controller.signal,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hybridly/core",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
3
|
+
"version": "0.0.1-alpha.12",
|
|
4
4
|
"description": "A solution to develop server-driven, client-rendered applications",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"hybridly",
|
|
@@ -36,11 +36,11 @@
|
|
|
36
36
|
"axios": "^1"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@hybridly/utils": "0.0.1-alpha.
|
|
39
|
+
"@hybridly/utils": "0.0.1-alpha.12",
|
|
40
40
|
"qs": "^6.11.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"defu": "^6.1.
|
|
43
|
+
"defu": "^6.1.1"
|
|
44
44
|
},
|
|
45
45
|
"scripts": {
|
|
46
46
|
"build": "unbuild",
|