@hybridly/core 0.4.2 → 0.4.4
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 +18 -18
- package/dist/index.d.cts +495 -0
- package/dist/index.d.mts +495 -0
- package/dist/index.d.ts +24 -32
- package/dist/index.mjs +15 -13
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
const qs = require('qs');
|
|
5
4
|
const utils = require('@hybridly/utils');
|
|
6
5
|
const axios = require('axios');
|
|
7
6
|
const superjson = require('superjson');
|
|
8
|
-
const qs = require('qs');
|
|
9
7
|
|
|
10
|
-
function
|
|
8
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
11
9
|
|
|
12
|
-
const
|
|
13
|
-
const
|
|
10
|
+
const qs__default = /*#__PURE__*/_interopDefaultCompat(qs);
|
|
11
|
+
const axios__default = /*#__PURE__*/_interopDefaultCompat(axios);
|
|
14
12
|
|
|
15
13
|
const STORAGE_EXTERNAL_KEY = "hybridly:external";
|
|
16
14
|
const HYBRIDLY_HEADER = "x-hybrid";
|
|
@@ -27,18 +25,18 @@ const SCROLL_REGION_ATTRIBUTE = "scroll-region";
|
|
|
27
25
|
|
|
28
26
|
const constants = {
|
|
29
27
|
__proto__: null,
|
|
30
|
-
|
|
31
|
-
HYBRIDLY_HEADER: HYBRIDLY_HEADER,
|
|
32
|
-
EXTERNAL_NAVIGATION_HEADER: EXTERNAL_NAVIGATION_HEADER,
|
|
33
|
-
PARTIAL_COMPONENT_HEADER: PARTIAL_COMPONENT_HEADER,
|
|
34
|
-
ONLY_DATA_HEADER: ONLY_DATA_HEADER,
|
|
28
|
+
CONTEXT_HEADER: CONTEXT_HEADER,
|
|
35
29
|
DIALOG_KEY_HEADER: DIALOG_KEY_HEADER,
|
|
36
30
|
DIALOG_REDIRECT_HEADER: DIALOG_REDIRECT_HEADER,
|
|
37
|
-
EXCEPT_DATA_HEADER: EXCEPT_DATA_HEADER,
|
|
38
|
-
CONTEXT_HEADER: CONTEXT_HEADER,
|
|
39
|
-
VERSION_HEADER: VERSION_HEADER,
|
|
40
31
|
ERROR_BAG_HEADER: ERROR_BAG_HEADER,
|
|
41
|
-
|
|
32
|
+
EXCEPT_DATA_HEADER: EXCEPT_DATA_HEADER,
|
|
33
|
+
EXTERNAL_NAVIGATION_HEADER: EXTERNAL_NAVIGATION_HEADER,
|
|
34
|
+
HYBRIDLY_HEADER: HYBRIDLY_HEADER,
|
|
35
|
+
ONLY_DATA_HEADER: ONLY_DATA_HEADER,
|
|
36
|
+
PARTIAL_COMPONENT_HEADER: PARTIAL_COMPONENT_HEADER,
|
|
37
|
+
SCROLL_REGION_ATTRIBUTE: SCROLL_REGION_ATTRIBUTE,
|
|
38
|
+
STORAGE_EXTERNAL_KEY: STORAGE_EXTERNAL_KEY,
|
|
39
|
+
VERSION_HEADER: VERSION_HEADER
|
|
42
40
|
};
|
|
43
41
|
|
|
44
42
|
class NotAHybridResponseError extends Error {
|
|
@@ -245,6 +243,7 @@ function setHistoryState(options = {}) {
|
|
|
245
243
|
utils.debug.history("Setting history state:", {
|
|
246
244
|
method,
|
|
247
245
|
context
|
|
246
|
+
// serialized,
|
|
248
247
|
});
|
|
249
248
|
try {
|
|
250
249
|
window.history[method](serialized, "", context.url);
|
|
@@ -912,6 +911,9 @@ async function navigate(options) {
|
|
|
912
911
|
utils.debug.router(`Target URL is ${context.url}, current window URL is ${window.location.href}.`, { shouldReplaceHistory });
|
|
913
912
|
setHistoryState({ replace: shouldReplaceHistory });
|
|
914
913
|
}
|
|
914
|
+
context.adapter.executeOnMounted(() => {
|
|
915
|
+
runHooks("mounted", {}, context);
|
|
916
|
+
});
|
|
915
917
|
const viewComponent = await context.adapter.resolveComponent(context.view.component);
|
|
916
918
|
utils.debug.router(`Component [${context.view.component}] resolved to:`, viewComponent);
|
|
917
919
|
await context.adapter.onViewSwap({
|
|
@@ -926,9 +928,6 @@ async function navigate(options) {
|
|
|
926
928
|
resetScrollPositions();
|
|
927
929
|
}
|
|
928
930
|
await runHooks("navigated", {}, options, context);
|
|
929
|
-
context.adapter.executeOnMounted(() => {
|
|
930
|
-
runHooks("mounted", {}, context);
|
|
931
|
-
});
|
|
932
931
|
}
|
|
933
932
|
async function performHybridRequest(targetUrl, options, abortController) {
|
|
934
933
|
const context = getInternalRouterContext();
|
|
@@ -980,6 +979,7 @@ async function initializeRouter() {
|
|
|
980
979
|
url: makeUrl(context.url, { hash: window.location.hash }).toString()
|
|
981
980
|
});
|
|
982
981
|
await navigate({
|
|
982
|
+
isInitial: true,
|
|
983
983
|
preserveState: true,
|
|
984
984
|
replace: sameUrls(context.url, window.location.href)
|
|
985
985
|
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
import { RequestData } from '@hybridly/utils';
|
|
2
|
+
import { AxiosResponse, AxiosProgressEvent, Axios } from 'axios';
|
|
3
|
+
|
|
4
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
5
|
+
|
|
6
|
+
interface RequestHooks {
|
|
7
|
+
/**
|
|
8
|
+
* Called before a navigation request is going to happen.
|
|
9
|
+
*/
|
|
10
|
+
before: (options: HybridRequestOptions, context: InternalRouterContext) => MaybePromise<any | boolean>;
|
|
11
|
+
/**
|
|
12
|
+
* Called before the request of a navigation is going to happen.
|
|
13
|
+
*/
|
|
14
|
+
start: (context: InternalRouterContext) => MaybePromise<any>;
|
|
15
|
+
/**
|
|
16
|
+
* Called when progress on the request is being made.
|
|
17
|
+
*/
|
|
18
|
+
progress: (progress: Progress, context: InternalRouterContext) => MaybePromise<any>;
|
|
19
|
+
/**
|
|
20
|
+
* Called when data is received after a request for a navigation.
|
|
21
|
+
*/
|
|
22
|
+
data: (response: AxiosResponse, context: InternalRouterContext) => MaybePromise<any>;
|
|
23
|
+
/**
|
|
24
|
+
* Called when a request is successful and there is no error.
|
|
25
|
+
*/
|
|
26
|
+
success: (payload: HybridPayload, context: InternalRouterContext) => MaybePromise<any>;
|
|
27
|
+
/**
|
|
28
|
+
* Called when a request is successful but there were errors.
|
|
29
|
+
*/
|
|
30
|
+
error: (errors: Errors, context: InternalRouterContext) => MaybePromise<any>;
|
|
31
|
+
/**
|
|
32
|
+
* Called when a request has been aborted.
|
|
33
|
+
*/
|
|
34
|
+
abort: (context: InternalRouterContext) => MaybePromise<any>;
|
|
35
|
+
/**
|
|
36
|
+
* Called when a response to a request is not a valid hybrid response.
|
|
37
|
+
*/
|
|
38
|
+
invalid: (response: AxiosResponse, context: InternalRouterContext) => MaybePromise<any>;
|
|
39
|
+
/**
|
|
40
|
+
* Called when an unknowne exception was triggered.
|
|
41
|
+
*/
|
|
42
|
+
exception: (error: Error, context: InternalRouterContext) => MaybePromise<any>;
|
|
43
|
+
/**
|
|
44
|
+
* Called whenever the request failed, for any reason, in addition to other hooks.
|
|
45
|
+
*/
|
|
46
|
+
fail: (context: InternalRouterContext) => MaybePromise<any>;
|
|
47
|
+
/**
|
|
48
|
+
* Called after a request has been made, even if it didn't succeed.
|
|
49
|
+
*/
|
|
50
|
+
after: (context: InternalRouterContext) => MaybePromise<any>;
|
|
51
|
+
}
|
|
52
|
+
interface Hooks extends RequestHooks {
|
|
53
|
+
/**
|
|
54
|
+
* Called when Hybridly's context is initialized.
|
|
55
|
+
*/
|
|
56
|
+
initialized: (context: InternalRouterContext) => MaybePromise<any>;
|
|
57
|
+
/**
|
|
58
|
+
* Called after Hybridly's initial page load.
|
|
59
|
+
*/
|
|
60
|
+
ready: (context: InternalRouterContext) => MaybePromise<any>;
|
|
61
|
+
/**
|
|
62
|
+
* Called when a back-forward navigation occurs.
|
|
63
|
+
*/
|
|
64
|
+
backForward: (state: any, context: InternalRouterContext) => MaybePromise<any>;
|
|
65
|
+
/**
|
|
66
|
+
* Called when a component navigation is being made.
|
|
67
|
+
*/
|
|
68
|
+
navigating: (options: NavigationOptions, context: InternalRouterContext) => MaybePromise<any>;
|
|
69
|
+
/**
|
|
70
|
+
* Called when a component has been navigated to.
|
|
71
|
+
*/
|
|
72
|
+
navigated: (options: NavigationOptions, context: InternalRouterContext) => MaybePromise<any>;
|
|
73
|
+
/**
|
|
74
|
+
* Called when a component has been navigated to and was mounted by the adapter.
|
|
75
|
+
*/
|
|
76
|
+
mounted: (context: InternalRouterContext) => MaybePromise<any>;
|
|
77
|
+
}
|
|
78
|
+
interface HookOptions {
|
|
79
|
+
/** Executes the hook only once. */
|
|
80
|
+
once?: boolean;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Registers a global hook.
|
|
84
|
+
*/
|
|
85
|
+
declare function registerHook<T extends keyof Hooks>(hook: T, fn: Hooks[T], options?: HookOptions): () => void;
|
|
86
|
+
|
|
87
|
+
interface Plugin extends Partial<Hooks> {
|
|
88
|
+
/** Identifier of the plugin. */
|
|
89
|
+
name: string;
|
|
90
|
+
}
|
|
91
|
+
declare function definePlugin(plugin: Plugin): Plugin;
|
|
92
|
+
|
|
93
|
+
interface CloseDialogOptions extends HybridRequestOptions {
|
|
94
|
+
/**
|
|
95
|
+
* Close the dialog without a round-trip to the server.
|
|
96
|
+
* @default false
|
|
97
|
+
*/
|
|
98
|
+
local?: boolean;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
type UrlResolvable = string | URL | Location;
|
|
102
|
+
type UrlTransformable = BaseUrlTransformable | ((string: URL) => BaseUrlTransformable);
|
|
103
|
+
type BaseUrlTransformable = Partial<Omit<URL, 'searchParams' | 'toJSON' | 'toString'>> & {
|
|
104
|
+
query?: any;
|
|
105
|
+
trailingSlash?: boolean;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Converts an input to an URL, optionally changing its properties after initialization.
|
|
109
|
+
*/
|
|
110
|
+
declare function makeUrl(href: UrlResolvable, transformations?: UrlTransformable): URL;
|
|
111
|
+
/**
|
|
112
|
+
* Checks if the given URLs have the same origin and path.
|
|
113
|
+
*/
|
|
114
|
+
declare function sameUrls(...hrefs: UrlResolvable[]): boolean;
|
|
115
|
+
|
|
116
|
+
type ConditionalNavigationOption<T extends boolean | string> = T | ((payload: NavigationOptions) => T);
|
|
117
|
+
interface ComponentNavigationOptions {
|
|
118
|
+
/** Dialog data. */
|
|
119
|
+
dialog?: Dialog | false;
|
|
120
|
+
/** Name of the component to use. */
|
|
121
|
+
component?: string;
|
|
122
|
+
/** Properties to apply to the component. */
|
|
123
|
+
properties?: Properties;
|
|
124
|
+
/**
|
|
125
|
+
* Whether to replace the current history state instead of adding
|
|
126
|
+
* one. This affects the browser's "back" and "forward" features.
|
|
127
|
+
*/
|
|
128
|
+
replace?: ConditionalNavigationOption<boolean>;
|
|
129
|
+
/** Whether to preserve the current scrollbar position. */
|
|
130
|
+
preserveScroll?: ConditionalNavigationOption<boolean>;
|
|
131
|
+
/** Whether to preserve the current page component state. */
|
|
132
|
+
preserveState?: ConditionalNavigationOption<boolean>;
|
|
133
|
+
}
|
|
134
|
+
interface NavigationOptions {
|
|
135
|
+
/** View to navigate to. */
|
|
136
|
+
payload?: HybridPayload;
|
|
137
|
+
/**
|
|
138
|
+
* Whether to replace the current history state instead of adding
|
|
139
|
+
* one. This affects the browser's "back" and "forward" features.
|
|
140
|
+
*/
|
|
141
|
+
replace?: ConditionalNavigationOption<boolean>;
|
|
142
|
+
/** Whether to preserve the scrollbars positions on the page. */
|
|
143
|
+
preserveScroll?: ConditionalNavigationOption<boolean>;
|
|
144
|
+
/** Whether to preserve the current page component's state. */
|
|
145
|
+
preserveState?: ConditionalNavigationOption<boolean>;
|
|
146
|
+
/** Whether to preserve the current URL. */
|
|
147
|
+
preserveUrl?: ConditionalNavigationOption<boolean>;
|
|
148
|
+
/**
|
|
149
|
+
* Properties of the given URL to override.
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* router.get('/login?redirect=/', {
|
|
153
|
+
* transformUrl: { search: '' }
|
|
154
|
+
* }
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
transformUrl?: UrlTransformable;
|
|
158
|
+
/**
|
|
159
|
+
* Defines whether the history state should be updated.
|
|
160
|
+
* @internal This is an advanced property meant to be used internally.
|
|
161
|
+
*/
|
|
162
|
+
updateHistoryState?: boolean;
|
|
163
|
+
/**
|
|
164
|
+
* Defines whether this navigation is a back/forward navigation from the popstate event.
|
|
165
|
+
* @internal This is an advanced property meant to be used internally.
|
|
166
|
+
*/
|
|
167
|
+
isBackForward?: boolean;
|
|
168
|
+
/**
|
|
169
|
+
* Defines whether this navigation is the first to happen after a direct page load.
|
|
170
|
+
* @internal This is an advanced property meant to be used internally.
|
|
171
|
+
*/
|
|
172
|
+
isInitial?: boolean;
|
|
173
|
+
}
|
|
174
|
+
type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
175
|
+
interface HybridRequestOptions extends Omit<NavigationOptions, 'payload'> {
|
|
176
|
+
/** The URL to navigation. */
|
|
177
|
+
url?: UrlResolvable;
|
|
178
|
+
/** HTTP verb to use for the request. */
|
|
179
|
+
method?: Method | Lowercase<Method>;
|
|
180
|
+
/** Body of the request. */
|
|
181
|
+
data?: RequestData;
|
|
182
|
+
/** Which properties to update for this navigation. Other properties will be ignored. */
|
|
183
|
+
only?: string | string[];
|
|
184
|
+
/** Which properties not to update for this navigation. Other properties will be updated. */
|
|
185
|
+
except?: string | string[];
|
|
186
|
+
/** Specific headers to add to the request. */
|
|
187
|
+
headers?: Record<string, string>;
|
|
188
|
+
/** The bag in which to put potential errors. */
|
|
189
|
+
errorBag?: string;
|
|
190
|
+
/** Hooks for this navigation. */
|
|
191
|
+
hooks?: Partial<RequestHooks>;
|
|
192
|
+
/** If `true`, force the usage of a `FormData` object. */
|
|
193
|
+
useFormData?: boolean;
|
|
194
|
+
/**
|
|
195
|
+
* If `false`, disable automatic form spoofing.
|
|
196
|
+
* @see https://laravel.com/docs/9.x/routing#form-method-spoofing
|
|
197
|
+
*/
|
|
198
|
+
spoof?: boolean;
|
|
199
|
+
/**
|
|
200
|
+
* If `false`, does not trigger the progress bar for this request.
|
|
201
|
+
*/
|
|
202
|
+
progress?: boolean;
|
|
203
|
+
}
|
|
204
|
+
interface NavigationResponse {
|
|
205
|
+
response?: AxiosResponse;
|
|
206
|
+
error?: {
|
|
207
|
+
type: string;
|
|
208
|
+
actual: Error;
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
interface DialogRouter {
|
|
212
|
+
/** Closes the current dialog. */
|
|
213
|
+
close: (options?: CloseDialogOptions) => void;
|
|
214
|
+
}
|
|
215
|
+
interface Router {
|
|
216
|
+
/** Aborts the currently pending navigate, if any. */
|
|
217
|
+
abort: () => Promise<void>;
|
|
218
|
+
/** Checks if there is an active navigate. */
|
|
219
|
+
active: () => boolean;
|
|
220
|
+
/** Makes a navigate with the given options. */
|
|
221
|
+
navigate: (options: HybridRequestOptions) => Promise<NavigationResponse>;
|
|
222
|
+
/** Reloads the current page. */
|
|
223
|
+
reload: (options?: HybridRequestOptions) => Promise<NavigationResponse>;
|
|
224
|
+
/** Makes a request to given named route. The HTTP verb is determined automatically but can be overriden. */
|
|
225
|
+
to: <T extends RouteName>(name: T, parameters?: RouteParameters<T>, options?: Omit<HybridRequestOptions, 'url'>) => Promise<NavigationResponse>;
|
|
226
|
+
/** Makes a GET request to the given URL. */
|
|
227
|
+
get: (url: UrlResolvable, options?: Omit<HybridRequestOptions, 'method' | 'url'>) => Promise<NavigationResponse>;
|
|
228
|
+
/** Makes a POST request to the given URL. */
|
|
229
|
+
post: (url: UrlResolvable, options?: Omit<HybridRequestOptions, 'method' | 'url'>) => Promise<NavigationResponse>;
|
|
230
|
+
/** Makes a PUT request to the given URL. */
|
|
231
|
+
put: (url: UrlResolvable, options?: Omit<HybridRequestOptions, 'method' | 'url'>) => Promise<NavigationResponse>;
|
|
232
|
+
/** Makes a PATCH request to the given URL. */
|
|
233
|
+
patch: (url: UrlResolvable, options?: Omit<HybridRequestOptions, 'method' | 'url'>) => Promise<NavigationResponse>;
|
|
234
|
+
/** Makes a DELETE request to the given URL. */
|
|
235
|
+
delete: (url: UrlResolvable, options?: Omit<HybridRequestOptions, 'method' | 'url'>) => Promise<NavigationResponse>;
|
|
236
|
+
/** Navigates to the given external URL. Convenience method using `document.location.href`. */
|
|
237
|
+
external: (url: UrlResolvable, data?: HybridRequestOptions['data']) => void;
|
|
238
|
+
/** Navigates to the given URL without a server round-trip. */
|
|
239
|
+
local: (url: UrlResolvable, options: ComponentNavigationOptions) => Promise<void>;
|
|
240
|
+
/** Preloads the given URL. The next time this URL is navigated to, it will be loaded from the cache. */
|
|
241
|
+
preload: (url: UrlResolvable, options?: Omit<HybridRequestOptions, 'method' | 'url'>) => Promise<boolean>;
|
|
242
|
+
/** Determines if the given route name and parameters matches the current route. */
|
|
243
|
+
matches: <T extends RouteName>(name: T, parameters?: RouteParameters<T>) => boolean;
|
|
244
|
+
/** Gets the current route name. Returns `undefined` is unknown. */
|
|
245
|
+
current: () => string | undefined;
|
|
246
|
+
/** Access the dialog router. */
|
|
247
|
+
dialog: DialogRouter;
|
|
248
|
+
/** Access the history state. */
|
|
249
|
+
history: {
|
|
250
|
+
/** Remembers a value for the given route. */
|
|
251
|
+
remember: (key: string, value: any) => void;
|
|
252
|
+
/** Gets a remembered value. */
|
|
253
|
+
get: <T = any>(key: string) => T | undefined;
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
/** A navigation being made. */
|
|
257
|
+
interface PendingNavigation {
|
|
258
|
+
/** The URL to which the request is being made. */
|
|
259
|
+
url: URL;
|
|
260
|
+
/** Abort controller associated to this request. */
|
|
261
|
+
controller: AbortController;
|
|
262
|
+
/** Options for the associated hybrid request. */
|
|
263
|
+
options: HybridRequestOptions;
|
|
264
|
+
/** Navigation identifier. */
|
|
265
|
+
id: string;
|
|
266
|
+
/** Current status. */
|
|
267
|
+
status: 'pending' | 'success' | 'error';
|
|
268
|
+
}
|
|
269
|
+
/** A page or dialog component. */
|
|
270
|
+
interface View {
|
|
271
|
+
/** Name of the component to use. */
|
|
272
|
+
component: string;
|
|
273
|
+
/** Properties to apply to the component. */
|
|
274
|
+
properties: Properties;
|
|
275
|
+
}
|
|
276
|
+
interface Dialog extends View {
|
|
277
|
+
/** URL that is the base background page when navigating to the dialog directly. */
|
|
278
|
+
baseUrl: string;
|
|
279
|
+
/** URL to which the dialog should redirect when closed. */
|
|
280
|
+
redirectUrl: string;
|
|
281
|
+
/** Unique identifier for this modal's lifecycle. */
|
|
282
|
+
key: string;
|
|
283
|
+
}
|
|
284
|
+
type Property = null | string | number | boolean | Property[] | {
|
|
285
|
+
[name: string]: Property;
|
|
286
|
+
};
|
|
287
|
+
type Properties = Record<string | number, Property>;
|
|
288
|
+
interface SwapOptions<T> {
|
|
289
|
+
/** The new component. */
|
|
290
|
+
component: T;
|
|
291
|
+
/** The new properties. */
|
|
292
|
+
properties?: any;
|
|
293
|
+
/** Whether to preserve the state of the component. */
|
|
294
|
+
preserveState?: boolean;
|
|
295
|
+
/** Current dialog. */
|
|
296
|
+
dialog?: Dialog;
|
|
297
|
+
}
|
|
298
|
+
type ViewComponent = any;
|
|
299
|
+
type ResolveComponent = (name: string) => Promise<ViewComponent>;
|
|
300
|
+
type SwapView = (options: SwapOptions<ViewComponent>) => Promise<void>;
|
|
301
|
+
/** The payload of a navigation request from the server. */
|
|
302
|
+
interface HybridPayload {
|
|
303
|
+
/** The view to use in this request. */
|
|
304
|
+
view: View;
|
|
305
|
+
/** An optional dialog. */
|
|
306
|
+
dialog?: Dialog;
|
|
307
|
+
/** The current page URL. */
|
|
308
|
+
url: string;
|
|
309
|
+
/** The current asset version. */
|
|
310
|
+
version: string;
|
|
311
|
+
}
|
|
312
|
+
interface Progress {
|
|
313
|
+
/** Base event. */
|
|
314
|
+
event: AxiosProgressEvent;
|
|
315
|
+
/** Computed percentage. */
|
|
316
|
+
percentage: Readonly<number>;
|
|
317
|
+
}
|
|
318
|
+
type Errors = any;
|
|
319
|
+
|
|
320
|
+
interface RoutingConfiguration {
|
|
321
|
+
url: string;
|
|
322
|
+
port?: number;
|
|
323
|
+
defaults: Record<string, any>;
|
|
324
|
+
routes: Record<string, RouteDefinition>;
|
|
325
|
+
}
|
|
326
|
+
interface RouteDefinition {
|
|
327
|
+
uri: string;
|
|
328
|
+
method: Method[];
|
|
329
|
+
bindings: Record<string, string>;
|
|
330
|
+
domain?: string;
|
|
331
|
+
wheres?: Record<string, string>;
|
|
332
|
+
name: string;
|
|
333
|
+
}
|
|
334
|
+
interface GlobalRouteCollection extends RoutingConfiguration {
|
|
335
|
+
}
|
|
336
|
+
type RouteName = keyof GlobalRouteCollection['routes'];
|
|
337
|
+
type RouteParameters<T extends RouteName> = Record<keyof GlobalRouteCollection['routes'][T]['bindings'], any> & Record<string, any>;
|
|
338
|
+
|
|
339
|
+
/** Options for creating a router context. */
|
|
340
|
+
interface RouterContextOptions {
|
|
341
|
+
/** The initial payload served by the browser. */
|
|
342
|
+
payload: HybridPayload;
|
|
343
|
+
/** Adapter-specific functions. */
|
|
344
|
+
adapter: Adapter;
|
|
345
|
+
/** History state serializer. */
|
|
346
|
+
serializer?: Serializer;
|
|
347
|
+
/** List of plugins. */
|
|
348
|
+
plugins?: Plugin[];
|
|
349
|
+
/** The Axios instance. */
|
|
350
|
+
axios?: Axios;
|
|
351
|
+
/** Initial routing configuration. */
|
|
352
|
+
routing?: RoutingConfiguration;
|
|
353
|
+
/** Whether to display response error modals. */
|
|
354
|
+
responseErrorModals?: boolean;
|
|
355
|
+
}
|
|
356
|
+
/** Router context. */
|
|
357
|
+
interface InternalRouterContext {
|
|
358
|
+
/** The current, normalized URL. */
|
|
359
|
+
url: string;
|
|
360
|
+
/** The current view. */
|
|
361
|
+
view: View;
|
|
362
|
+
/** The current, optional dialog. */
|
|
363
|
+
dialog?: Dialog;
|
|
364
|
+
/** The current local asset version. */
|
|
365
|
+
version: string;
|
|
366
|
+
/** The current adapter's functions. */
|
|
367
|
+
adapter: ResolvedAdapter;
|
|
368
|
+
/** Scroll positions of the current page's DOM elements. */
|
|
369
|
+
scrollRegions: ScrollRegion[];
|
|
370
|
+
/** Arbitrary state. */
|
|
371
|
+
memo: Record<string, any>;
|
|
372
|
+
/** Currently pending navigation. */
|
|
373
|
+
pendingNavigation?: PendingNavigation;
|
|
374
|
+
/** History state serializer. */
|
|
375
|
+
serializer: Serializer;
|
|
376
|
+
/** List of plugins. */
|
|
377
|
+
plugins: Plugin[];
|
|
378
|
+
/** Global hooks. */
|
|
379
|
+
hooks: Partial<Record<keyof Hooks, Array<Function>>>;
|
|
380
|
+
/** The Axios instance. */
|
|
381
|
+
axios: Axios;
|
|
382
|
+
/** Routing configuration. */
|
|
383
|
+
routing?: RoutingConfiguration;
|
|
384
|
+
/** Whether to display response error modals. */
|
|
385
|
+
responseErrorModals?: boolean;
|
|
386
|
+
/** Cache of preload requests. */
|
|
387
|
+
preloadCache: Map<string, AxiosResponse>;
|
|
388
|
+
}
|
|
389
|
+
/** Router context. */
|
|
390
|
+
type RouterContext = Readonly<InternalRouterContext>;
|
|
391
|
+
/** Adapter-specific functions. */
|
|
392
|
+
interface Adapter {
|
|
393
|
+
/** Resolves a component from the given name. */
|
|
394
|
+
resolveComponent: ResolveComponent;
|
|
395
|
+
/** Called when the view is swapped. */
|
|
396
|
+
onViewSwap: SwapView;
|
|
397
|
+
/** Called when the context is updated. */
|
|
398
|
+
onContextUpdate?: (context: InternalRouterContext) => void;
|
|
399
|
+
/** Called when a dialog is closed. */
|
|
400
|
+
onDialogClose?: (context: InternalRouterContext) => void;
|
|
401
|
+
/** Called when Hybridly is waiting for a component to be mounted. The given callback should be executed after the view component is mounted. */
|
|
402
|
+
executeOnMounted: (callback: Function) => void;
|
|
403
|
+
}
|
|
404
|
+
interface ResolvedAdapter extends Adapter {
|
|
405
|
+
updateRoutingConfiguration: (routing?: RoutingConfiguration) => void;
|
|
406
|
+
}
|
|
407
|
+
interface ScrollRegion {
|
|
408
|
+
top: number;
|
|
409
|
+
left: number;
|
|
410
|
+
}
|
|
411
|
+
/** Provides methods to serialize the state into the history state. */
|
|
412
|
+
interface Serializer {
|
|
413
|
+
serialize: <T>(view: T) => string;
|
|
414
|
+
unserialize: <T>(state?: string) => T | undefined;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** Gets the current context. */
|
|
418
|
+
declare function getRouterContext(): RouterContext;
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* The hybridly router.
|
|
422
|
+
* This is the core function that you can use to navigate in
|
|
423
|
+
* your application. Make sure the routes you call return a
|
|
424
|
+
* hybrid response, otherwise you need to call `external`.
|
|
425
|
+
*
|
|
426
|
+
* @example
|
|
427
|
+
* router.get('/posts/edit', { post })
|
|
428
|
+
*/
|
|
429
|
+
declare const router: Router;
|
|
430
|
+
/** Creates the hybridly router. */
|
|
431
|
+
declare function createRouter(options: RouterContextOptions): Promise<InternalRouterContext>;
|
|
432
|
+
|
|
433
|
+
interface Authorizable<Authorizations extends Record<string, boolean>> {
|
|
434
|
+
authorization: Authorizations;
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Checks whether the given data has the authorization for the given action.
|
|
438
|
+
* If the data object has no authorization definition corresponding to the given action, this method will return `false`.
|
|
439
|
+
*/
|
|
440
|
+
declare function can<Authorizations extends Record<string, boolean>, Data extends Authorizable<Authorizations>, Action extends keyof Data['authorization']>(resource: Data, action: Action): Authorizations[Action];
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Generates a route from the given route name.
|
|
444
|
+
*/
|
|
445
|
+
declare function route<T extends RouteName>(name: T, parameters?: RouteParameters<T>, absolute?: boolean): string;
|
|
446
|
+
|
|
447
|
+
interface DynamicConfiguration {
|
|
448
|
+
architecture: {
|
|
449
|
+
root: string;
|
|
450
|
+
};
|
|
451
|
+
components: {
|
|
452
|
+
eager?: boolean;
|
|
453
|
+
directories: string[];
|
|
454
|
+
views: Component[];
|
|
455
|
+
layouts: Component[];
|
|
456
|
+
components: Component[];
|
|
457
|
+
};
|
|
458
|
+
routing: RoutingConfiguration;
|
|
459
|
+
}
|
|
460
|
+
interface Component {
|
|
461
|
+
path: string;
|
|
462
|
+
identifier: string;
|
|
463
|
+
namespace: string;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
declare const STORAGE_EXTERNAL_KEY = "hybridly:external";
|
|
467
|
+
declare const HYBRIDLY_HEADER = "x-hybrid";
|
|
468
|
+
declare const EXTERNAL_NAVIGATION_HEADER = "x-hybrid-external";
|
|
469
|
+
declare const PARTIAL_COMPONENT_HEADER = "x-hybrid-partial-component";
|
|
470
|
+
declare const ONLY_DATA_HEADER = "x-hybrid-only-data";
|
|
471
|
+
declare const DIALOG_KEY_HEADER = "x-hybrid-dialog-key";
|
|
472
|
+
declare const DIALOG_REDIRECT_HEADER = "x-hybrid-dialog-redirect";
|
|
473
|
+
declare const EXCEPT_DATA_HEADER = "x-hybrid-except-data";
|
|
474
|
+
declare const CONTEXT_HEADER = "x-hybrid-context";
|
|
475
|
+
declare const VERSION_HEADER = "x-hybrid-version";
|
|
476
|
+
declare const ERROR_BAG_HEADER = "x-hybrid-error-bag";
|
|
477
|
+
declare const SCROLL_REGION_ATTRIBUTE = "scroll-region";
|
|
478
|
+
|
|
479
|
+
declare const constants_CONTEXT_HEADER: typeof CONTEXT_HEADER;
|
|
480
|
+
declare const constants_DIALOG_KEY_HEADER: typeof DIALOG_KEY_HEADER;
|
|
481
|
+
declare const constants_DIALOG_REDIRECT_HEADER: typeof DIALOG_REDIRECT_HEADER;
|
|
482
|
+
declare const constants_ERROR_BAG_HEADER: typeof ERROR_BAG_HEADER;
|
|
483
|
+
declare const constants_EXCEPT_DATA_HEADER: typeof EXCEPT_DATA_HEADER;
|
|
484
|
+
declare const constants_EXTERNAL_NAVIGATION_HEADER: typeof EXTERNAL_NAVIGATION_HEADER;
|
|
485
|
+
declare const constants_HYBRIDLY_HEADER: typeof HYBRIDLY_HEADER;
|
|
486
|
+
declare const constants_ONLY_DATA_HEADER: typeof ONLY_DATA_HEADER;
|
|
487
|
+
declare const constants_PARTIAL_COMPONENT_HEADER: typeof PARTIAL_COMPONENT_HEADER;
|
|
488
|
+
declare const constants_SCROLL_REGION_ATTRIBUTE: typeof SCROLL_REGION_ATTRIBUTE;
|
|
489
|
+
declare const constants_STORAGE_EXTERNAL_KEY: typeof STORAGE_EXTERNAL_KEY;
|
|
490
|
+
declare const constants_VERSION_HEADER: typeof VERSION_HEADER;
|
|
491
|
+
declare namespace constants {
|
|
492
|
+
export { constants_CONTEXT_HEADER as CONTEXT_HEADER, constants_DIALOG_KEY_HEADER as DIALOG_KEY_HEADER, constants_DIALOG_REDIRECT_HEADER as DIALOG_REDIRECT_HEADER, constants_ERROR_BAG_HEADER as ERROR_BAG_HEADER, constants_EXCEPT_DATA_HEADER as EXCEPT_DATA_HEADER, constants_EXTERNAL_NAVIGATION_HEADER as EXTERNAL_NAVIGATION_HEADER, constants_HYBRIDLY_HEADER as HYBRIDLY_HEADER, constants_ONLY_DATA_HEADER as ONLY_DATA_HEADER, constants_PARTIAL_COMPONENT_HEADER as PARTIAL_COMPONENT_HEADER, constants_SCROLL_REGION_ATTRIBUTE as SCROLL_REGION_ATTRIBUTE, constants_STORAGE_EXTERNAL_KEY as STORAGE_EXTERNAL_KEY, constants_VERSION_HEADER as VERSION_HEADER };
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
export { type Authorizable, type DynamicConfiguration, type GlobalRouteCollection, type HybridPayload, type HybridRequestOptions, type MaybePromise, type Method, type NavigationResponse, type Plugin, type Progress, type ResolveComponent, type RouteDefinition, type RouteName, type RouteParameters, type Router, type RouterContext, type RouterContextOptions, type RoutingConfiguration, type UrlResolvable, can, constants, createRouter, definePlugin, getRouterContext, makeUrl, registerHook, route, router, sameUrls };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
import { RequestData } from '@hybridly/utils';
|
|
2
|
+
import { AxiosResponse, AxiosProgressEvent, Axios } from 'axios';
|
|
3
|
+
|
|
4
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
5
|
+
|
|
6
|
+
interface RequestHooks {
|
|
7
|
+
/**
|
|
8
|
+
* Called before a navigation request is going to happen.
|
|
9
|
+
*/
|
|
10
|
+
before: (options: HybridRequestOptions, context: InternalRouterContext) => MaybePromise<any | boolean>;
|
|
11
|
+
/**
|
|
12
|
+
* Called before the request of a navigation is going to happen.
|
|
13
|
+
*/
|
|
14
|
+
start: (context: InternalRouterContext) => MaybePromise<any>;
|
|
15
|
+
/**
|
|
16
|
+
* Called when progress on the request is being made.
|
|
17
|
+
*/
|
|
18
|
+
progress: (progress: Progress, context: InternalRouterContext) => MaybePromise<any>;
|
|
19
|
+
/**
|
|
20
|
+
* Called when data is received after a request for a navigation.
|
|
21
|
+
*/
|
|
22
|
+
data: (response: AxiosResponse, context: InternalRouterContext) => MaybePromise<any>;
|
|
23
|
+
/**
|
|
24
|
+
* Called when a request is successful and there is no error.
|
|
25
|
+
*/
|
|
26
|
+
success: (payload: HybridPayload, context: InternalRouterContext) => MaybePromise<any>;
|
|
27
|
+
/**
|
|
28
|
+
* Called when a request is successful but there were errors.
|
|
29
|
+
*/
|
|
30
|
+
error: (errors: Errors, context: InternalRouterContext) => MaybePromise<any>;
|
|
31
|
+
/**
|
|
32
|
+
* Called when a request has been aborted.
|
|
33
|
+
*/
|
|
34
|
+
abort: (context: InternalRouterContext) => MaybePromise<any>;
|
|
35
|
+
/**
|
|
36
|
+
* Called when a response to a request is not a valid hybrid response.
|
|
37
|
+
*/
|
|
38
|
+
invalid: (response: AxiosResponse, context: InternalRouterContext) => MaybePromise<any>;
|
|
39
|
+
/**
|
|
40
|
+
* Called when an unknowne exception was triggered.
|
|
41
|
+
*/
|
|
42
|
+
exception: (error: Error, context: InternalRouterContext) => MaybePromise<any>;
|
|
43
|
+
/**
|
|
44
|
+
* Called whenever the request failed, for any reason, in addition to other hooks.
|
|
45
|
+
*/
|
|
46
|
+
fail: (context: InternalRouterContext) => MaybePromise<any>;
|
|
47
|
+
/**
|
|
48
|
+
* Called after a request has been made, even if it didn't succeed.
|
|
49
|
+
*/
|
|
50
|
+
after: (context: InternalRouterContext) => MaybePromise<any>;
|
|
51
|
+
}
|
|
52
|
+
interface Hooks extends RequestHooks {
|
|
53
|
+
/**
|
|
54
|
+
* Called when Hybridly's context is initialized.
|
|
55
|
+
*/
|
|
56
|
+
initialized: (context: InternalRouterContext) => MaybePromise<any>;
|
|
57
|
+
/**
|
|
58
|
+
* Called after Hybridly's initial page load.
|
|
59
|
+
*/
|
|
60
|
+
ready: (context: InternalRouterContext) => MaybePromise<any>;
|
|
61
|
+
/**
|
|
62
|
+
* Called when a back-forward navigation occurs.
|
|
63
|
+
*/
|
|
64
|
+
backForward: (state: any, context: InternalRouterContext) => MaybePromise<any>;
|
|
65
|
+
/**
|
|
66
|
+
* Called when a component navigation is being made.
|
|
67
|
+
*/
|
|
68
|
+
navigating: (options: NavigationOptions, context: InternalRouterContext) => MaybePromise<any>;
|
|
69
|
+
/**
|
|
70
|
+
* Called when a component has been navigated to.
|
|
71
|
+
*/
|
|
72
|
+
navigated: (options: NavigationOptions, context: InternalRouterContext) => MaybePromise<any>;
|
|
73
|
+
/**
|
|
74
|
+
* Called when a component has been navigated to and was mounted by the adapter.
|
|
75
|
+
*/
|
|
76
|
+
mounted: (context: InternalRouterContext) => MaybePromise<any>;
|
|
77
|
+
}
|
|
78
|
+
interface HookOptions {
|
|
79
|
+
/** Executes the hook only once. */
|
|
80
|
+
once?: boolean;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Registers a global hook.
|
|
84
|
+
*/
|
|
85
|
+
declare function registerHook<T extends keyof Hooks>(hook: T, fn: Hooks[T], options?: HookOptions): () => void;
|
|
86
|
+
|
|
87
|
+
interface Plugin extends Partial<Hooks> {
|
|
88
|
+
/** Identifier of the plugin. */
|
|
89
|
+
name: string;
|
|
90
|
+
}
|
|
91
|
+
declare function definePlugin(plugin: Plugin): Plugin;
|
|
92
|
+
|
|
93
|
+
interface CloseDialogOptions extends HybridRequestOptions {
|
|
94
|
+
/**
|
|
95
|
+
* Close the dialog without a round-trip to the server.
|
|
96
|
+
* @default false
|
|
97
|
+
*/
|
|
98
|
+
local?: boolean;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
type UrlResolvable = string | URL | Location;
|
|
102
|
+
type UrlTransformable = BaseUrlTransformable | ((string: URL) => BaseUrlTransformable);
|
|
103
|
+
type BaseUrlTransformable = Partial<Omit<URL, 'searchParams' | 'toJSON' | 'toString'>> & {
|
|
104
|
+
query?: any;
|
|
105
|
+
trailingSlash?: boolean;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Converts an input to an URL, optionally changing its properties after initialization.
|
|
109
|
+
*/
|
|
110
|
+
declare function makeUrl(href: UrlResolvable, transformations?: UrlTransformable): URL;
|
|
111
|
+
/**
|
|
112
|
+
* Checks if the given URLs have the same origin and path.
|
|
113
|
+
*/
|
|
114
|
+
declare function sameUrls(...hrefs: UrlResolvable[]): boolean;
|
|
115
|
+
|
|
116
|
+
type ConditionalNavigationOption<T extends boolean | string> = T | ((payload: NavigationOptions) => T);
|
|
117
|
+
interface ComponentNavigationOptions {
|
|
118
|
+
/** Dialog data. */
|
|
119
|
+
dialog?: Dialog | false;
|
|
120
|
+
/** Name of the component to use. */
|
|
121
|
+
component?: string;
|
|
122
|
+
/** Properties to apply to the component. */
|
|
123
|
+
properties?: Properties;
|
|
124
|
+
/**
|
|
125
|
+
* Whether to replace the current history state instead of adding
|
|
126
|
+
* one. This affects the browser's "back" and "forward" features.
|
|
127
|
+
*/
|
|
128
|
+
replace?: ConditionalNavigationOption<boolean>;
|
|
129
|
+
/** Whether to preserve the current scrollbar position. */
|
|
130
|
+
preserveScroll?: ConditionalNavigationOption<boolean>;
|
|
131
|
+
/** Whether to preserve the current page component state. */
|
|
132
|
+
preserveState?: ConditionalNavigationOption<boolean>;
|
|
133
|
+
}
|
|
134
|
+
interface NavigationOptions {
|
|
135
|
+
/** View to navigate to. */
|
|
136
|
+
payload?: HybridPayload;
|
|
137
|
+
/**
|
|
138
|
+
* Whether to replace the current history state instead of adding
|
|
139
|
+
* one. This affects the browser's "back" and "forward" features.
|
|
140
|
+
*/
|
|
141
|
+
replace?: ConditionalNavigationOption<boolean>;
|
|
142
|
+
/** Whether to preserve the scrollbars positions on the page. */
|
|
143
|
+
preserveScroll?: ConditionalNavigationOption<boolean>;
|
|
144
|
+
/** Whether to preserve the current page component's state. */
|
|
145
|
+
preserveState?: ConditionalNavigationOption<boolean>;
|
|
146
|
+
/** Whether to preserve the current URL. */
|
|
147
|
+
preserveUrl?: ConditionalNavigationOption<boolean>;
|
|
148
|
+
/**
|
|
149
|
+
* Properties of the given URL to override.
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* router.get('/login?redirect=/', {
|
|
153
|
+
* transformUrl: { search: '' }
|
|
154
|
+
* }
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
transformUrl?: UrlTransformable;
|
|
158
|
+
/**
|
|
159
|
+
* Defines whether the history state should be updated.
|
|
160
|
+
* @internal This is an advanced property meant to be used internally.
|
|
161
|
+
*/
|
|
162
|
+
updateHistoryState?: boolean;
|
|
163
|
+
/**
|
|
164
|
+
* Defines whether this navigation is a back/forward navigation from the popstate event.
|
|
165
|
+
* @internal This is an advanced property meant to be used internally.
|
|
166
|
+
*/
|
|
167
|
+
isBackForward?: boolean;
|
|
168
|
+
/**
|
|
169
|
+
* Defines whether this navigation is the first to happen after a direct page load.
|
|
170
|
+
* @internal This is an advanced property meant to be used internally.
|
|
171
|
+
*/
|
|
172
|
+
isInitial?: boolean;
|
|
173
|
+
}
|
|
174
|
+
type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
175
|
+
interface HybridRequestOptions extends Omit<NavigationOptions, 'payload'> {
|
|
176
|
+
/** The URL to navigation. */
|
|
177
|
+
url?: UrlResolvable;
|
|
178
|
+
/** HTTP verb to use for the request. */
|
|
179
|
+
method?: Method | Lowercase<Method>;
|
|
180
|
+
/** Body of the request. */
|
|
181
|
+
data?: RequestData;
|
|
182
|
+
/** Which properties to update for this navigation. Other properties will be ignored. */
|
|
183
|
+
only?: string | string[];
|
|
184
|
+
/** Which properties not to update for this navigation. Other properties will be updated. */
|
|
185
|
+
except?: string | string[];
|
|
186
|
+
/** Specific headers to add to the request. */
|
|
187
|
+
headers?: Record<string, string>;
|
|
188
|
+
/** The bag in which to put potential errors. */
|
|
189
|
+
errorBag?: string;
|
|
190
|
+
/** Hooks for this navigation. */
|
|
191
|
+
hooks?: Partial<RequestHooks>;
|
|
192
|
+
/** If `true`, force the usage of a `FormData` object. */
|
|
193
|
+
useFormData?: boolean;
|
|
194
|
+
/**
|
|
195
|
+
* If `false`, disable automatic form spoofing.
|
|
196
|
+
* @see https://laravel.com/docs/9.x/routing#form-method-spoofing
|
|
197
|
+
*/
|
|
198
|
+
spoof?: boolean;
|
|
199
|
+
/**
|
|
200
|
+
* If `false`, does not trigger the progress bar for this request.
|
|
201
|
+
*/
|
|
202
|
+
progress?: boolean;
|
|
203
|
+
}
|
|
204
|
+
interface NavigationResponse {
|
|
205
|
+
response?: AxiosResponse;
|
|
206
|
+
error?: {
|
|
207
|
+
type: string;
|
|
208
|
+
actual: Error;
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
interface DialogRouter {
|
|
212
|
+
/** Closes the current dialog. */
|
|
213
|
+
close: (options?: CloseDialogOptions) => void;
|
|
214
|
+
}
|
|
215
|
+
interface Router {
|
|
216
|
+
/** Aborts the currently pending navigate, if any. */
|
|
217
|
+
abort: () => Promise<void>;
|
|
218
|
+
/** Checks if there is an active navigate. */
|
|
219
|
+
active: () => boolean;
|
|
220
|
+
/** Makes a navigate with the given options. */
|
|
221
|
+
navigate: (options: HybridRequestOptions) => Promise<NavigationResponse>;
|
|
222
|
+
/** Reloads the current page. */
|
|
223
|
+
reload: (options?: HybridRequestOptions) => Promise<NavigationResponse>;
|
|
224
|
+
/** Makes a request to given named route. The HTTP verb is determined automatically but can be overriden. */
|
|
225
|
+
to: <T extends RouteName>(name: T, parameters?: RouteParameters<T>, options?: Omit<HybridRequestOptions, 'url'>) => Promise<NavigationResponse>;
|
|
226
|
+
/** Makes a GET request to the given URL. */
|
|
227
|
+
get: (url: UrlResolvable, options?: Omit<HybridRequestOptions, 'method' | 'url'>) => Promise<NavigationResponse>;
|
|
228
|
+
/** Makes a POST request to the given URL. */
|
|
229
|
+
post: (url: UrlResolvable, options?: Omit<HybridRequestOptions, 'method' | 'url'>) => Promise<NavigationResponse>;
|
|
230
|
+
/** Makes a PUT request to the given URL. */
|
|
231
|
+
put: (url: UrlResolvable, options?: Omit<HybridRequestOptions, 'method' | 'url'>) => Promise<NavigationResponse>;
|
|
232
|
+
/** Makes a PATCH request to the given URL. */
|
|
233
|
+
patch: (url: UrlResolvable, options?: Omit<HybridRequestOptions, 'method' | 'url'>) => Promise<NavigationResponse>;
|
|
234
|
+
/** Makes a DELETE request to the given URL. */
|
|
235
|
+
delete: (url: UrlResolvable, options?: Omit<HybridRequestOptions, 'method' | 'url'>) => Promise<NavigationResponse>;
|
|
236
|
+
/** Navigates to the given external URL. Convenience method using `document.location.href`. */
|
|
237
|
+
external: (url: UrlResolvable, data?: HybridRequestOptions['data']) => void;
|
|
238
|
+
/** Navigates to the given URL without a server round-trip. */
|
|
239
|
+
local: (url: UrlResolvable, options: ComponentNavigationOptions) => Promise<void>;
|
|
240
|
+
/** Preloads the given URL. The next time this URL is navigated to, it will be loaded from the cache. */
|
|
241
|
+
preload: (url: UrlResolvable, options?: Omit<HybridRequestOptions, 'method' | 'url'>) => Promise<boolean>;
|
|
242
|
+
/** Determines if the given route name and parameters matches the current route. */
|
|
243
|
+
matches: <T extends RouteName>(name: T, parameters?: RouteParameters<T>) => boolean;
|
|
244
|
+
/** Gets the current route name. Returns `undefined` is unknown. */
|
|
245
|
+
current: () => string | undefined;
|
|
246
|
+
/** Access the dialog router. */
|
|
247
|
+
dialog: DialogRouter;
|
|
248
|
+
/** Access the history state. */
|
|
249
|
+
history: {
|
|
250
|
+
/** Remembers a value for the given route. */
|
|
251
|
+
remember: (key: string, value: any) => void;
|
|
252
|
+
/** Gets a remembered value. */
|
|
253
|
+
get: <T = any>(key: string) => T | undefined;
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
/** A navigation being made. */
|
|
257
|
+
interface PendingNavigation {
|
|
258
|
+
/** The URL to which the request is being made. */
|
|
259
|
+
url: URL;
|
|
260
|
+
/** Abort controller associated to this request. */
|
|
261
|
+
controller: AbortController;
|
|
262
|
+
/** Options for the associated hybrid request. */
|
|
263
|
+
options: HybridRequestOptions;
|
|
264
|
+
/** Navigation identifier. */
|
|
265
|
+
id: string;
|
|
266
|
+
/** Current status. */
|
|
267
|
+
status: 'pending' | 'success' | 'error';
|
|
268
|
+
}
|
|
269
|
+
/** A page or dialog component. */
|
|
270
|
+
interface View {
|
|
271
|
+
/** Name of the component to use. */
|
|
272
|
+
component: string;
|
|
273
|
+
/** Properties to apply to the component. */
|
|
274
|
+
properties: Properties;
|
|
275
|
+
}
|
|
276
|
+
interface Dialog extends View {
|
|
277
|
+
/** URL that is the base background page when navigating to the dialog directly. */
|
|
278
|
+
baseUrl: string;
|
|
279
|
+
/** URL to which the dialog should redirect when closed. */
|
|
280
|
+
redirectUrl: string;
|
|
281
|
+
/** Unique identifier for this modal's lifecycle. */
|
|
282
|
+
key: string;
|
|
283
|
+
}
|
|
284
|
+
type Property = null | string | number | boolean | Property[] | {
|
|
285
|
+
[name: string]: Property;
|
|
286
|
+
};
|
|
287
|
+
type Properties = Record<string | number, Property>;
|
|
288
|
+
interface SwapOptions<T> {
|
|
289
|
+
/** The new component. */
|
|
290
|
+
component: T;
|
|
291
|
+
/** The new properties. */
|
|
292
|
+
properties?: any;
|
|
293
|
+
/** Whether to preserve the state of the component. */
|
|
294
|
+
preserveState?: boolean;
|
|
295
|
+
/** Current dialog. */
|
|
296
|
+
dialog?: Dialog;
|
|
297
|
+
}
|
|
298
|
+
type ViewComponent = any;
|
|
299
|
+
type ResolveComponent = (name: string) => Promise<ViewComponent>;
|
|
300
|
+
type SwapView = (options: SwapOptions<ViewComponent>) => Promise<void>;
|
|
301
|
+
/** The payload of a navigation request from the server. */
|
|
302
|
+
interface HybridPayload {
|
|
303
|
+
/** The view to use in this request. */
|
|
304
|
+
view: View;
|
|
305
|
+
/** An optional dialog. */
|
|
306
|
+
dialog?: Dialog;
|
|
307
|
+
/** The current page URL. */
|
|
308
|
+
url: string;
|
|
309
|
+
/** The current asset version. */
|
|
310
|
+
version: string;
|
|
311
|
+
}
|
|
312
|
+
interface Progress {
|
|
313
|
+
/** Base event. */
|
|
314
|
+
event: AxiosProgressEvent;
|
|
315
|
+
/** Computed percentage. */
|
|
316
|
+
percentage: Readonly<number>;
|
|
317
|
+
}
|
|
318
|
+
type Errors = any;
|
|
319
|
+
|
|
320
|
+
interface RoutingConfiguration {
|
|
321
|
+
url: string;
|
|
322
|
+
port?: number;
|
|
323
|
+
defaults: Record<string, any>;
|
|
324
|
+
routes: Record<string, RouteDefinition>;
|
|
325
|
+
}
|
|
326
|
+
interface RouteDefinition {
|
|
327
|
+
uri: string;
|
|
328
|
+
method: Method[];
|
|
329
|
+
bindings: Record<string, string>;
|
|
330
|
+
domain?: string;
|
|
331
|
+
wheres?: Record<string, string>;
|
|
332
|
+
name: string;
|
|
333
|
+
}
|
|
334
|
+
interface GlobalRouteCollection extends RoutingConfiguration {
|
|
335
|
+
}
|
|
336
|
+
type RouteName = keyof GlobalRouteCollection['routes'];
|
|
337
|
+
type RouteParameters<T extends RouteName> = Record<keyof GlobalRouteCollection['routes'][T]['bindings'], any> & Record<string, any>;
|
|
338
|
+
|
|
339
|
+
/** Options for creating a router context. */
|
|
340
|
+
interface RouterContextOptions {
|
|
341
|
+
/** The initial payload served by the browser. */
|
|
342
|
+
payload: HybridPayload;
|
|
343
|
+
/** Adapter-specific functions. */
|
|
344
|
+
adapter: Adapter;
|
|
345
|
+
/** History state serializer. */
|
|
346
|
+
serializer?: Serializer;
|
|
347
|
+
/** List of plugins. */
|
|
348
|
+
plugins?: Plugin[];
|
|
349
|
+
/** The Axios instance. */
|
|
350
|
+
axios?: Axios;
|
|
351
|
+
/** Initial routing configuration. */
|
|
352
|
+
routing?: RoutingConfiguration;
|
|
353
|
+
/** Whether to display response error modals. */
|
|
354
|
+
responseErrorModals?: boolean;
|
|
355
|
+
}
|
|
356
|
+
/** Router context. */
|
|
357
|
+
interface InternalRouterContext {
|
|
358
|
+
/** The current, normalized URL. */
|
|
359
|
+
url: string;
|
|
360
|
+
/** The current view. */
|
|
361
|
+
view: View;
|
|
362
|
+
/** The current, optional dialog. */
|
|
363
|
+
dialog?: Dialog;
|
|
364
|
+
/** The current local asset version. */
|
|
365
|
+
version: string;
|
|
366
|
+
/** The current adapter's functions. */
|
|
367
|
+
adapter: ResolvedAdapter;
|
|
368
|
+
/** Scroll positions of the current page's DOM elements. */
|
|
369
|
+
scrollRegions: ScrollRegion[];
|
|
370
|
+
/** Arbitrary state. */
|
|
371
|
+
memo: Record<string, any>;
|
|
372
|
+
/** Currently pending navigation. */
|
|
373
|
+
pendingNavigation?: PendingNavigation;
|
|
374
|
+
/** History state serializer. */
|
|
375
|
+
serializer: Serializer;
|
|
376
|
+
/** List of plugins. */
|
|
377
|
+
plugins: Plugin[];
|
|
378
|
+
/** Global hooks. */
|
|
379
|
+
hooks: Partial<Record<keyof Hooks, Array<Function>>>;
|
|
380
|
+
/** The Axios instance. */
|
|
381
|
+
axios: Axios;
|
|
382
|
+
/** Routing configuration. */
|
|
383
|
+
routing?: RoutingConfiguration;
|
|
384
|
+
/** Whether to display response error modals. */
|
|
385
|
+
responseErrorModals?: boolean;
|
|
386
|
+
/** Cache of preload requests. */
|
|
387
|
+
preloadCache: Map<string, AxiosResponse>;
|
|
388
|
+
}
|
|
389
|
+
/** Router context. */
|
|
390
|
+
type RouterContext = Readonly<InternalRouterContext>;
|
|
391
|
+
/** Adapter-specific functions. */
|
|
392
|
+
interface Adapter {
|
|
393
|
+
/** Resolves a component from the given name. */
|
|
394
|
+
resolveComponent: ResolveComponent;
|
|
395
|
+
/** Called when the view is swapped. */
|
|
396
|
+
onViewSwap: SwapView;
|
|
397
|
+
/** Called when the context is updated. */
|
|
398
|
+
onContextUpdate?: (context: InternalRouterContext) => void;
|
|
399
|
+
/** Called when a dialog is closed. */
|
|
400
|
+
onDialogClose?: (context: InternalRouterContext) => void;
|
|
401
|
+
/** Called when Hybridly is waiting for a component to be mounted. The given callback should be executed after the view component is mounted. */
|
|
402
|
+
executeOnMounted: (callback: Function) => void;
|
|
403
|
+
}
|
|
404
|
+
interface ResolvedAdapter extends Adapter {
|
|
405
|
+
updateRoutingConfiguration: (routing?: RoutingConfiguration) => void;
|
|
406
|
+
}
|
|
407
|
+
interface ScrollRegion {
|
|
408
|
+
top: number;
|
|
409
|
+
left: number;
|
|
410
|
+
}
|
|
411
|
+
/** Provides methods to serialize the state into the history state. */
|
|
412
|
+
interface Serializer {
|
|
413
|
+
serialize: <T>(view: T) => string;
|
|
414
|
+
unserialize: <T>(state?: string) => T | undefined;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** Gets the current context. */
|
|
418
|
+
declare function getRouterContext(): RouterContext;
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* The hybridly router.
|
|
422
|
+
* This is the core function that you can use to navigate in
|
|
423
|
+
* your application. Make sure the routes you call return a
|
|
424
|
+
* hybrid response, otherwise you need to call `external`.
|
|
425
|
+
*
|
|
426
|
+
* @example
|
|
427
|
+
* router.get('/posts/edit', { post })
|
|
428
|
+
*/
|
|
429
|
+
declare const router: Router;
|
|
430
|
+
/** Creates the hybridly router. */
|
|
431
|
+
declare function createRouter(options: RouterContextOptions): Promise<InternalRouterContext>;
|
|
432
|
+
|
|
433
|
+
interface Authorizable<Authorizations extends Record<string, boolean>> {
|
|
434
|
+
authorization: Authorizations;
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Checks whether the given data has the authorization for the given action.
|
|
438
|
+
* If the data object has no authorization definition corresponding to the given action, this method will return `false`.
|
|
439
|
+
*/
|
|
440
|
+
declare function can<Authorizations extends Record<string, boolean>, Data extends Authorizable<Authorizations>, Action extends keyof Data['authorization']>(resource: Data, action: Action): Authorizations[Action];
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Generates a route from the given route name.
|
|
444
|
+
*/
|
|
445
|
+
declare function route<T extends RouteName>(name: T, parameters?: RouteParameters<T>, absolute?: boolean): string;
|
|
446
|
+
|
|
447
|
+
interface DynamicConfiguration {
|
|
448
|
+
architecture: {
|
|
449
|
+
root: string;
|
|
450
|
+
};
|
|
451
|
+
components: {
|
|
452
|
+
eager?: boolean;
|
|
453
|
+
directories: string[];
|
|
454
|
+
views: Component[];
|
|
455
|
+
layouts: Component[];
|
|
456
|
+
components: Component[];
|
|
457
|
+
};
|
|
458
|
+
routing: RoutingConfiguration;
|
|
459
|
+
}
|
|
460
|
+
interface Component {
|
|
461
|
+
path: string;
|
|
462
|
+
identifier: string;
|
|
463
|
+
namespace: string;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
declare const STORAGE_EXTERNAL_KEY = "hybridly:external";
|
|
467
|
+
declare const HYBRIDLY_HEADER = "x-hybrid";
|
|
468
|
+
declare const EXTERNAL_NAVIGATION_HEADER = "x-hybrid-external";
|
|
469
|
+
declare const PARTIAL_COMPONENT_HEADER = "x-hybrid-partial-component";
|
|
470
|
+
declare const ONLY_DATA_HEADER = "x-hybrid-only-data";
|
|
471
|
+
declare const DIALOG_KEY_HEADER = "x-hybrid-dialog-key";
|
|
472
|
+
declare const DIALOG_REDIRECT_HEADER = "x-hybrid-dialog-redirect";
|
|
473
|
+
declare const EXCEPT_DATA_HEADER = "x-hybrid-except-data";
|
|
474
|
+
declare const CONTEXT_HEADER = "x-hybrid-context";
|
|
475
|
+
declare const VERSION_HEADER = "x-hybrid-version";
|
|
476
|
+
declare const ERROR_BAG_HEADER = "x-hybrid-error-bag";
|
|
477
|
+
declare const SCROLL_REGION_ATTRIBUTE = "scroll-region";
|
|
478
|
+
|
|
479
|
+
declare const constants_CONTEXT_HEADER: typeof CONTEXT_HEADER;
|
|
480
|
+
declare const constants_DIALOG_KEY_HEADER: typeof DIALOG_KEY_HEADER;
|
|
481
|
+
declare const constants_DIALOG_REDIRECT_HEADER: typeof DIALOG_REDIRECT_HEADER;
|
|
482
|
+
declare const constants_ERROR_BAG_HEADER: typeof ERROR_BAG_HEADER;
|
|
483
|
+
declare const constants_EXCEPT_DATA_HEADER: typeof EXCEPT_DATA_HEADER;
|
|
484
|
+
declare const constants_EXTERNAL_NAVIGATION_HEADER: typeof EXTERNAL_NAVIGATION_HEADER;
|
|
485
|
+
declare const constants_HYBRIDLY_HEADER: typeof HYBRIDLY_HEADER;
|
|
486
|
+
declare const constants_ONLY_DATA_HEADER: typeof ONLY_DATA_HEADER;
|
|
487
|
+
declare const constants_PARTIAL_COMPONENT_HEADER: typeof PARTIAL_COMPONENT_HEADER;
|
|
488
|
+
declare const constants_SCROLL_REGION_ATTRIBUTE: typeof SCROLL_REGION_ATTRIBUTE;
|
|
489
|
+
declare const constants_STORAGE_EXTERNAL_KEY: typeof STORAGE_EXTERNAL_KEY;
|
|
490
|
+
declare const constants_VERSION_HEADER: typeof VERSION_HEADER;
|
|
491
|
+
declare namespace constants {
|
|
492
|
+
export { constants_CONTEXT_HEADER as CONTEXT_HEADER, constants_DIALOG_KEY_HEADER as DIALOG_KEY_HEADER, constants_DIALOG_REDIRECT_HEADER as DIALOG_REDIRECT_HEADER, constants_ERROR_BAG_HEADER as ERROR_BAG_HEADER, constants_EXCEPT_DATA_HEADER as EXCEPT_DATA_HEADER, constants_EXTERNAL_NAVIGATION_HEADER as EXTERNAL_NAVIGATION_HEADER, constants_HYBRIDLY_HEADER as HYBRIDLY_HEADER, constants_ONLY_DATA_HEADER as ONLY_DATA_HEADER, constants_PARTIAL_COMPONENT_HEADER as PARTIAL_COMPONENT_HEADER, constants_SCROLL_REGION_ATTRIBUTE as SCROLL_REGION_ATTRIBUTE, constants_STORAGE_EXTERNAL_KEY as STORAGE_EXTERNAL_KEY, constants_VERSION_HEADER as VERSION_HEADER };
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
export { type Authorizable, type DynamicConfiguration, type GlobalRouteCollection, type HybridPayload, type HybridRequestOptions, type MaybePromise, type Method, type NavigationResponse, type Plugin, type Progress, type ResolveComponent, type RouteDefinition, type RouteName, type RouteParameters, type Router, type RouterContext, type RouterContextOptions, type RoutingConfiguration, type UrlResolvable, can, constants, createRouter, definePlugin, getRouterContext, makeUrl, registerHook, route, router, sameUrls };
|
package/dist/index.d.ts
CHANGED
|
@@ -165,6 +165,11 @@ interface NavigationOptions {
|
|
|
165
165
|
* @internal This is an advanced property meant to be used internally.
|
|
166
166
|
*/
|
|
167
167
|
isBackForward?: boolean;
|
|
168
|
+
/**
|
|
169
|
+
* Defines whether this navigation is the first to happen after a direct page load.
|
|
170
|
+
* @internal This is an advanced property meant to be used internally.
|
|
171
|
+
*/
|
|
172
|
+
isInitial?: boolean;
|
|
168
173
|
}
|
|
169
174
|
type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
170
175
|
interface HybridRequestOptions extends Omit<NavigationOptions, 'payload'> {
|
|
@@ -460,44 +465,31 @@ interface Component {
|
|
|
460
465
|
|
|
461
466
|
declare const STORAGE_EXTERNAL_KEY = "hybridly:external";
|
|
462
467
|
declare const HYBRIDLY_HEADER = "x-hybrid";
|
|
463
|
-
declare const EXTERNAL_NAVIGATION_HEADER
|
|
464
|
-
declare const PARTIAL_COMPONENT_HEADER
|
|
465
|
-
declare const ONLY_DATA_HEADER
|
|
466
|
-
declare const DIALOG_KEY_HEADER
|
|
467
|
-
declare const DIALOG_REDIRECT_HEADER
|
|
468
|
-
declare const EXCEPT_DATA_HEADER
|
|
469
|
-
declare const CONTEXT_HEADER
|
|
470
|
-
declare const VERSION_HEADER
|
|
471
|
-
declare const ERROR_BAG_HEADER
|
|
468
|
+
declare const EXTERNAL_NAVIGATION_HEADER = "x-hybrid-external";
|
|
469
|
+
declare const PARTIAL_COMPONENT_HEADER = "x-hybrid-partial-component";
|
|
470
|
+
declare const ONLY_DATA_HEADER = "x-hybrid-only-data";
|
|
471
|
+
declare const DIALOG_KEY_HEADER = "x-hybrid-dialog-key";
|
|
472
|
+
declare const DIALOG_REDIRECT_HEADER = "x-hybrid-dialog-redirect";
|
|
473
|
+
declare const EXCEPT_DATA_HEADER = "x-hybrid-except-data";
|
|
474
|
+
declare const CONTEXT_HEADER = "x-hybrid-context";
|
|
475
|
+
declare const VERSION_HEADER = "x-hybrid-version";
|
|
476
|
+
declare const ERROR_BAG_HEADER = "x-hybrid-error-bag";
|
|
472
477
|
declare const SCROLL_REGION_ATTRIBUTE = "scroll-region";
|
|
473
478
|
|
|
474
|
-
declare const
|
|
475
|
-
declare const constants_HYBRIDLY_HEADER: typeof HYBRIDLY_HEADER;
|
|
476
|
-
declare const constants_EXTERNAL_NAVIGATION_HEADER: typeof EXTERNAL_NAVIGATION_HEADER;
|
|
477
|
-
declare const constants_PARTIAL_COMPONENT_HEADER: typeof PARTIAL_COMPONENT_HEADER;
|
|
478
|
-
declare const constants_ONLY_DATA_HEADER: typeof ONLY_DATA_HEADER;
|
|
479
|
+
declare const constants_CONTEXT_HEADER: typeof CONTEXT_HEADER;
|
|
479
480
|
declare const constants_DIALOG_KEY_HEADER: typeof DIALOG_KEY_HEADER;
|
|
480
481
|
declare const constants_DIALOG_REDIRECT_HEADER: typeof DIALOG_REDIRECT_HEADER;
|
|
481
|
-
declare const constants_EXCEPT_DATA_HEADER: typeof EXCEPT_DATA_HEADER;
|
|
482
|
-
declare const constants_CONTEXT_HEADER: typeof CONTEXT_HEADER;
|
|
483
|
-
declare const constants_VERSION_HEADER: typeof VERSION_HEADER;
|
|
484
482
|
declare const constants_ERROR_BAG_HEADER: typeof ERROR_BAG_HEADER;
|
|
483
|
+
declare const constants_EXCEPT_DATA_HEADER: typeof EXCEPT_DATA_HEADER;
|
|
484
|
+
declare const constants_EXTERNAL_NAVIGATION_HEADER: typeof EXTERNAL_NAVIGATION_HEADER;
|
|
485
|
+
declare const constants_HYBRIDLY_HEADER: typeof HYBRIDLY_HEADER;
|
|
486
|
+
declare const constants_ONLY_DATA_HEADER: typeof ONLY_DATA_HEADER;
|
|
487
|
+
declare const constants_PARTIAL_COMPONENT_HEADER: typeof PARTIAL_COMPONENT_HEADER;
|
|
485
488
|
declare const constants_SCROLL_REGION_ATTRIBUTE: typeof SCROLL_REGION_ATTRIBUTE;
|
|
489
|
+
declare const constants_STORAGE_EXTERNAL_KEY: typeof STORAGE_EXTERNAL_KEY;
|
|
490
|
+
declare const constants_VERSION_HEADER: typeof VERSION_HEADER;
|
|
486
491
|
declare namespace constants {
|
|
487
|
-
export {
|
|
488
|
-
constants_STORAGE_EXTERNAL_KEY as STORAGE_EXTERNAL_KEY,
|
|
489
|
-
constants_HYBRIDLY_HEADER as HYBRIDLY_HEADER,
|
|
490
|
-
constants_EXTERNAL_NAVIGATION_HEADER as EXTERNAL_NAVIGATION_HEADER,
|
|
491
|
-
constants_PARTIAL_COMPONENT_HEADER as PARTIAL_COMPONENT_HEADER,
|
|
492
|
-
constants_ONLY_DATA_HEADER as ONLY_DATA_HEADER,
|
|
493
|
-
constants_DIALOG_KEY_HEADER as DIALOG_KEY_HEADER,
|
|
494
|
-
constants_DIALOG_REDIRECT_HEADER as DIALOG_REDIRECT_HEADER,
|
|
495
|
-
constants_EXCEPT_DATA_HEADER as EXCEPT_DATA_HEADER,
|
|
496
|
-
constants_CONTEXT_HEADER as CONTEXT_HEADER,
|
|
497
|
-
constants_VERSION_HEADER as VERSION_HEADER,
|
|
498
|
-
constants_ERROR_BAG_HEADER as ERROR_BAG_HEADER,
|
|
499
|
-
constants_SCROLL_REGION_ATTRIBUTE as SCROLL_REGION_ATTRIBUTE,
|
|
500
|
-
};
|
|
492
|
+
export { constants_CONTEXT_HEADER as CONTEXT_HEADER, constants_DIALOG_KEY_HEADER as DIALOG_KEY_HEADER, constants_DIALOG_REDIRECT_HEADER as DIALOG_REDIRECT_HEADER, constants_ERROR_BAG_HEADER as ERROR_BAG_HEADER, constants_EXCEPT_DATA_HEADER as EXCEPT_DATA_HEADER, constants_EXTERNAL_NAVIGATION_HEADER as EXTERNAL_NAVIGATION_HEADER, constants_HYBRIDLY_HEADER as HYBRIDLY_HEADER, constants_ONLY_DATA_HEADER as ONLY_DATA_HEADER, constants_PARTIAL_COMPONENT_HEADER as PARTIAL_COMPONENT_HEADER, constants_SCROLL_REGION_ATTRIBUTE as SCROLL_REGION_ATTRIBUTE, constants_STORAGE_EXTERNAL_KEY as STORAGE_EXTERNAL_KEY, constants_VERSION_HEADER as VERSION_HEADER };
|
|
501
493
|
}
|
|
502
494
|
|
|
503
|
-
export { Authorizable, DynamicConfiguration, GlobalRouteCollection, HybridPayload, HybridRequestOptions, MaybePromise, Method, NavigationResponse, Plugin, Progress, ResolveComponent, RouteDefinition, RouteName, RouteParameters, Router, RouterContext, RouterContextOptions, RoutingConfiguration, UrlResolvable, can, constants, createRouter, definePlugin, getRouterContext, makeUrl, registerHook, route, router, sameUrls };
|
|
495
|
+
export { type Authorizable, type DynamicConfiguration, type GlobalRouteCollection, type HybridPayload, type HybridRequestOptions, type MaybePromise, type Method, type NavigationResponse, type Plugin, type Progress, type ResolveComponent, type RouteDefinition, type RouteName, type RouteParameters, type Router, type RouterContext, type RouterContextOptions, type RoutingConfiguration, type UrlResolvable, can, constants, createRouter, definePlugin, getRouterContext, makeUrl, registerHook, route, router, sameUrls };
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import qs from 'qs';
|
|
1
2
|
import { debug, merge, removeTrailingSlash, debounce, random, hasFiles, objectToFormData, match, showResponseErrorModal, when } from '@hybridly/utils';
|
|
2
3
|
import axios from 'axios';
|
|
3
4
|
import { stringify, parse } from 'superjson';
|
|
4
|
-
import qs from 'qs';
|
|
5
5
|
|
|
6
6
|
const STORAGE_EXTERNAL_KEY = "hybridly:external";
|
|
7
7
|
const HYBRIDLY_HEADER = "x-hybrid";
|
|
@@ -18,18 +18,18 @@ const SCROLL_REGION_ATTRIBUTE = "scroll-region";
|
|
|
18
18
|
|
|
19
19
|
const constants = {
|
|
20
20
|
__proto__: null,
|
|
21
|
-
|
|
22
|
-
HYBRIDLY_HEADER: HYBRIDLY_HEADER,
|
|
23
|
-
EXTERNAL_NAVIGATION_HEADER: EXTERNAL_NAVIGATION_HEADER,
|
|
24
|
-
PARTIAL_COMPONENT_HEADER: PARTIAL_COMPONENT_HEADER,
|
|
25
|
-
ONLY_DATA_HEADER: ONLY_DATA_HEADER,
|
|
21
|
+
CONTEXT_HEADER: CONTEXT_HEADER,
|
|
26
22
|
DIALOG_KEY_HEADER: DIALOG_KEY_HEADER,
|
|
27
23
|
DIALOG_REDIRECT_HEADER: DIALOG_REDIRECT_HEADER,
|
|
28
|
-
EXCEPT_DATA_HEADER: EXCEPT_DATA_HEADER,
|
|
29
|
-
CONTEXT_HEADER: CONTEXT_HEADER,
|
|
30
|
-
VERSION_HEADER: VERSION_HEADER,
|
|
31
24
|
ERROR_BAG_HEADER: ERROR_BAG_HEADER,
|
|
32
|
-
|
|
25
|
+
EXCEPT_DATA_HEADER: EXCEPT_DATA_HEADER,
|
|
26
|
+
EXTERNAL_NAVIGATION_HEADER: EXTERNAL_NAVIGATION_HEADER,
|
|
27
|
+
HYBRIDLY_HEADER: HYBRIDLY_HEADER,
|
|
28
|
+
ONLY_DATA_HEADER: ONLY_DATA_HEADER,
|
|
29
|
+
PARTIAL_COMPONENT_HEADER: PARTIAL_COMPONENT_HEADER,
|
|
30
|
+
SCROLL_REGION_ATTRIBUTE: SCROLL_REGION_ATTRIBUTE,
|
|
31
|
+
STORAGE_EXTERNAL_KEY: STORAGE_EXTERNAL_KEY,
|
|
32
|
+
VERSION_HEADER: VERSION_HEADER
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
class NotAHybridResponseError extends Error {
|
|
@@ -236,6 +236,7 @@ function setHistoryState(options = {}) {
|
|
|
236
236
|
debug.history("Setting history state:", {
|
|
237
237
|
method,
|
|
238
238
|
context
|
|
239
|
+
// serialized,
|
|
239
240
|
});
|
|
240
241
|
try {
|
|
241
242
|
window.history[method](serialized, "", context.url);
|
|
@@ -903,6 +904,9 @@ async function navigate(options) {
|
|
|
903
904
|
debug.router(`Target URL is ${context.url}, current window URL is ${window.location.href}.`, { shouldReplaceHistory });
|
|
904
905
|
setHistoryState({ replace: shouldReplaceHistory });
|
|
905
906
|
}
|
|
907
|
+
context.adapter.executeOnMounted(() => {
|
|
908
|
+
runHooks("mounted", {}, context);
|
|
909
|
+
});
|
|
906
910
|
const viewComponent = await context.adapter.resolveComponent(context.view.component);
|
|
907
911
|
debug.router(`Component [${context.view.component}] resolved to:`, viewComponent);
|
|
908
912
|
await context.adapter.onViewSwap({
|
|
@@ -917,9 +921,6 @@ async function navigate(options) {
|
|
|
917
921
|
resetScrollPositions();
|
|
918
922
|
}
|
|
919
923
|
await runHooks("navigated", {}, options, context);
|
|
920
|
-
context.adapter.executeOnMounted(() => {
|
|
921
|
-
runHooks("mounted", {}, context);
|
|
922
|
-
});
|
|
923
924
|
}
|
|
924
925
|
async function performHybridRequest(targetUrl, options, abortController) {
|
|
925
926
|
const context = getInternalRouterContext();
|
|
@@ -971,6 +972,7 @@ async function initializeRouter() {
|
|
|
971
972
|
url: makeUrl(context.url, { hash: window.location.hash }).toString()
|
|
972
973
|
});
|
|
973
974
|
await navigate({
|
|
975
|
+
isInitial: true,
|
|
974
976
|
preserveState: true,
|
|
975
977
|
replace: sameUrls(context.url, window.location.href)
|
|
976
978
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hybridly/core",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.4",
|
|
4
4
|
"description": "Core functionality of Hybridly",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"hybridly",
|
|
@@ -37,8 +37,8 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"qs": "^6.11.2",
|
|
40
|
-
"superjson": "^1.
|
|
41
|
-
"@hybridly/utils": "0.4.
|
|
40
|
+
"superjson": "^1.13.1",
|
|
41
|
+
"@hybridly/utils": "0.4.4"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"defu": "^6.1.2"
|