valyrian.js 7.2.12 → 8.0.1
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/README.md +6 -6
- package/dist/flux-store/index.d.ts +32 -0
- package/dist/flux-store/index.d.ts.map +1 -0
- package/dist/flux-store/index.js +267 -0
- package/dist/flux-store/index.js.map +7 -0
- package/dist/flux-store/index.min.js +1 -0
- package/dist/flux-store/index.min.js.map +1 -0
- package/dist/flux-store/index.mjs +246 -0
- package/dist/flux-store/index.mjs.map +7 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +35 -51
- package/dist/hooks/index.js.map +3 -3
- package/dist/hooks/index.min.js +1 -0
- package/dist/hooks/index.min.js.map +1 -0
- package/dist/hooks/index.mjs +36 -52
- package/dist/hooks/index.mjs.map +3 -3
- package/dist/index.d.ts +18 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +107 -88
- package/dist/index.js.map +3 -3
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/index.mjs +107 -88
- package/dist/index.mjs.map +3 -3
- package/dist/native-store/index.d.ts +14 -0
- package/dist/native-store/index.d.ts.map +1 -0
- package/dist/native-store/index.js +103 -0
- package/dist/native-store/index.js.map +7 -0
- package/dist/native-store/index.min.js +1 -0
- package/dist/native-store/index.min.js.map +1 -0
- package/dist/native-store/index.mjs +82 -0
- package/dist/native-store/index.mjs.map +7 -0
- package/dist/node/index.d.ts +1 -1
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +125 -10
- package/dist/node/index.js.map +4 -4
- package/dist/node/index.mjs +125 -10
- package/dist/node/index.mjs.map +4 -4
- package/dist/node/node.sw.js +152 -0
- package/dist/node/utils/icons.d.ts +4 -5
- package/dist/node/utils/icons.d.ts.map +1 -1
- package/dist/node/utils/inline.d.ts +1 -1
- package/dist/node/utils/inline.d.ts.map +1 -1
- package/dist/node/utils/node.sw.js +152 -0
- package/dist/node/utils/session-storage.d.ts +22 -0
- package/dist/node/utils/session-storage.d.ts.map +1 -0
- package/dist/node/utils/sw.d.ts.map +1 -1
- package/dist/node/utils/tree-adapter.d.ts +5 -1
- package/dist/node/utils/tree-adapter.d.ts.map +1 -1
- package/dist/pulse-store/index.d.ts +16 -0
- package/dist/pulse-store/index.d.ts.map +1 -0
- package/dist/pulse-store/index.js +143 -0
- package/dist/pulse-store/index.js.map +7 -0
- package/dist/pulse-store/index.min.js +1 -0
- package/dist/pulse-store/index.min.js.map +1 -0
- package/dist/pulse-store/index.mjs +122 -0
- package/dist/pulse-store/index.mjs.map +7 -0
- package/dist/request/index.d.ts +11 -11
- package/dist/request/index.d.ts.map +1 -1
- package/dist/request/index.js +63 -84
- package/dist/request/index.js.map +2 -2
- package/dist/request/index.min.js +1 -0
- package/dist/request/index.min.js.map +1 -0
- package/dist/request/index.mjs +63 -84
- package/dist/request/index.mjs.map +2 -2
- package/dist/router/index.d.ts +36 -33
- package/dist/router/index.d.ts.map +1 -1
- package/dist/router/index.js +247 -96
- package/dist/router/index.js.map +3 -3
- package/dist/router/index.min.js +1 -0
- package/dist/router/index.min.js.map +1 -0
- package/dist/router/index.mjs +247 -96
- package/dist/router/index.mjs.map +3 -3
- package/dist/signals/index.d.ts +6 -0
- package/dist/signals/index.d.ts.map +1 -0
- package/dist/signals/index.js +92 -0
- package/dist/signals/index.js.map +7 -0
- package/dist/signals/index.min.js +1 -0
- package/dist/signals/index.min.js.map +1 -0
- package/dist/signals/index.mjs +71 -0
- package/dist/signals/index.mjs.map +7 -0
- package/dist/suspense/index.d.ts +6 -0
- package/dist/suspense/index.d.ts.map +1 -0
- package/dist/suspense/index.js +67 -0
- package/dist/suspense/index.js.map +7 -0
- package/dist/suspense/index.min.js +1 -0
- package/dist/suspense/index.min.js.map +1 -0
- package/dist/suspense/index.mjs +46 -0
- package/dist/suspense/index.mjs.map +7 -0
- package/dist/sw/index.min.js +1 -0
- package/dist/sw/index.min.js.map +1 -0
- package/dist/translate/index.d.ts +19 -0
- package/dist/translate/index.d.ts.map +1 -0
- package/dist/translate/index.js +150 -0
- package/dist/translate/index.js.map +7 -0
- package/dist/translate/index.min.js +1 -0
- package/dist/translate/index.min.js.map +1 -0
- package/dist/translate/index.mjs +129 -0
- package/dist/translate/index.mjs.map +7 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/deep-freeze.d.ts +3 -0
- package/dist/utils/deep-freeze.d.ts.map +1 -0
- package/dist/utils/getter-setter.d.ts +3 -0
- package/dist/utils/getter-setter.d.ts.map +1 -0
- package/dist/utils/has-changed.d.ts +2 -0
- package/dist/utils/has-changed.d.ts.map +1 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +138 -0
- package/dist/utils/index.js.map +7 -0
- package/dist/utils/index.min.js +1 -0
- package/dist/utils/index.min.js.map +1 -0
- package/dist/utils/index.mjs +115 -0
- package/dist/utils/index.mjs.map +7 -0
- package/lib/flux-store/index.ts +312 -0
- package/lib/hooks/index.ts +39 -57
- package/lib/index.ts +135 -118
- package/lib/native-store/index.ts +106 -0
- package/lib/node/index.ts +3 -1
- package/lib/node/utils/icons.ts +4 -4
- package/lib/node/utils/inline.ts +2 -0
- package/lib/node/utils/node.sw.js +152 -0
- package/lib/node/utils/session-storage.ts +117 -0
- package/lib/node/utils/sw.ts +34 -10
- package/lib/node/utils/tree-adapter.ts +19 -1
- package/lib/pulse-store/index.ts +188 -0
- package/lib/request/index.ts +92 -122
- package/lib/router/index.ts +353 -164
- package/lib/signals/index.ts +98 -0
- package/lib/suspense/index.ts +57 -0
- package/lib/translate/index.ts +156 -0
- package/lib/utils/deep-freeze.ts +54 -0
- package/lib/utils/getter-setter.ts +40 -0
- package/lib/utils/has-changed.ts +43 -0
- package/lib/utils/index.ts +3 -0
- package/package.json +40 -57
- package/tsconfig.json +5 -4
- package/dist/dataset/index.d.ts +0 -24
- package/dist/dataset/index.d.ts.map +0 -1
- package/dist/dataset/index.js +0 -178
- package/dist/dataset/index.js.map +0 -7
- package/dist/dataset/index.mjs +0 -157
- package/dist/dataset/index.mjs.map +0 -7
- package/dist/node/node.sw.tpl +0 -133
- package/dist/node/utils/node.sw.tpl +0 -133
- package/dist/proxy-signal/index.d.ts +0 -23
- package/dist/proxy-signal/index.d.ts.map +0 -1
- package/dist/proxy-signal/index.js +0 -138
- package/dist/proxy-signal/index.js.map +0 -7
- package/dist/proxy-signal/index.mjs +0 -117
- package/dist/proxy-signal/index.mjs.map +0 -7
- package/dist/signal/index.d.ts +0 -9
- package/dist/signal/index.d.ts.map +0 -1
- package/dist/signal/index.js +0 -76
- package/dist/signal/index.js.map +0 -7
- package/dist/signal/index.mjs +0 -55
- package/dist/signal/index.mjs.map +0 -7
- package/dist/store/index.d.ts +0 -16
- package/dist/store/index.d.ts.map +0 -1
- package/dist/store/index.js +0 -93
- package/dist/store/index.js.map +0 -7
- package/dist/store/index.mjs +0 -72
- package/dist/store/index.mjs.map +0 -7
- package/lib/dataset/index.ts +0 -193
- package/lib/node/utils/node.sw.tpl +0 -133
- package/lib/proxy-signal/index.ts +0 -187
- package/lib/signal/index.ts +0 -86
- package/lib/store/index.ts +0 -101
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../lib/request/index.ts"],
|
|
4
|
-
"sourcesContent": ["import { isNodeJs } from \"valyrian.js\";\n\ninterface UrlOptions {\n base
|
|
5
|
-
"mappings": ";AAAA,SAAS,gBAAgB;AAqDzB,SAAS,UAAU,KAA0B,SAAiB,
|
|
4
|
+
"sourcesContent": ["import { isNodeJs } from \"valyrian.js\";\nimport { get, set } from \"valyrian.js/utils\";\n\ninterface UrlOptions {\n base?: string; // Used to prefix the url for scoped requests.\n node?: string | null; // Used to redirect local requests to node server for server side rendering.\n api?: string | null; // Used to redirect api requests to node server for server side rendering.\n}\n\ninterface RequestOptions {\n allowedMethods?: string[];\n urls?: UrlOptions;\n [key: string | number | symbol]: any;\n}\n\ninterface RequestOptionsWithUrls extends RequestOptions {\n urls: UrlOptions;\n allowedMethods: string[];\n}\n\ninterface SendOptions extends RequestOptionsWithUrls, RequestInit {\n allowedMethods: string[];\n method: string;\n headers: Record<string, string>;\n resolveWithFullResponse?: boolean;\n}\n\nexport interface RequestInterface {\n // eslint-disable-next-line no-unused-vars\n (method: string, url: string, data?: Record<string, any> | null, options?: Partial<SendOptions>): any | Response;\n // eslint-disable-next-line no-unused-vars\n new: (baseUrl: string, options?: RequestOptions) => RequestInterface;\n // eslint-disable-next-line no-unused-vars\n setOptions: (key: string, value: any) => void;\n // eslint-disable-next-line no-unused-vars\n getOptions: (key?: string) => RequestOptions | void;\n // eslint-disable-next-line no-unused-vars\n get: (url: string, data?: Record<string, any> | null, options?: Record<string, any>) => any | Response;\n // eslint-disable-next-line no-unused-vars\n post: (url: string, data?: Record<string, any> | null, options?: Record<string, any>) => any | Response;\n // eslint-disable-next-line no-unused-vars\n put: (url: string, data?: Record<string, any> | null, options?: Record<string, any>) => any | Response;\n // eslint-disable-next-line no-unused-vars\n patch: (url: string, data?: Record<string, any> | null, options?: Record<string, any>) => any | Response;\n // eslint-disable-next-line no-unused-vars\n delete: (url: string, data?: Record<string, any> | null, options?: Record<string, any>) => any | Response;\n // eslint-disable-next-line no-unused-vars\n head: (url: string, data?: Record<string, any> | null, options?: Record<string, any>) => any | Response;\n // eslint-disable-next-line no-unused-vars\n options: (url: string, data?: Record<string, any> | null, options?: Record<string, any>) => any | Response;\n [key: string | number | symbol]: any;\n}\n\n// This method is used to serialize an object into a query string.\nfunction serialize(obj: Record<string, any>, prefix: string = \"\"): URLSearchParams {\n const params = new URLSearchParams();\n\n Object.keys(obj).forEach((prop: string) => {\n const key = prefix ? `${prefix}[${prop}]` : prop;\n if (typeof obj[prop] === \"object\") {\n params.append(key, serialize(obj[prop], key).toString());\n } else {\n params.append(key, obj[prop]);\n }\n });\n\n return params;\n}\n\nfunction serializeFormData(data: Record<string, any>): FormData {\n return Object.entries(data).reduce((fd, [key, value]) => {\n fd.append(key, value);\n return fd;\n }, new FormData());\n}\n\nfunction parseUrl(url: string, options: RequestOptionsWithUrls) {\n const urlWithoutSlash = url.replace(/\\/+$/, \"\").trim();\n const u = /^https?/gi.test(urlWithoutSlash) ? urlWithoutSlash : `${options.urls.base}${urlWithoutSlash}`;\n\n if (isNodeJs && typeof options.urls.node === \"string\") {\n if (typeof options.urls.api === \"string\") {\n return new URL(u.replace(options.urls.api, options.urls.node));\n }\n\n if (!/^https?/gi.test(u)) {\n return new URL(u, options.urls.node);\n }\n }\n\n if (/^https?/gi.test(u)) {\n return new URL(u);\n }\n\n return new URL(u, options.urls.base);\n}\n\nconst defaultOptions: RequestOptions = { allowedMethods: [\"get\", \"post\", \"put\", \"patch\", \"delete\", \"head\", \"options\"] };\n\n// eslint-disable-next-line sonarjs/cognitive-complexity\nfunction Requester(baseUrl = \"\", options: RequestOptions = defaultOptions) {\n const url = baseUrl.replace(/\\/$/gi, \"\").trim();\n if (!options.urls) {\n options.urls = {\n base: \"\",\n node: null,\n api: null\n };\n }\n\n if (!options.allowedMethods) {\n options.allowedMethods = defaultOptions.allowedMethods;\n }\n\n const opts: RequestOptionsWithUrls = {\n ...(options as RequestOptionsWithUrls),\n urls: {\n node: options.urls.node || null,\n api: options.urls.api || null,\n base: options.urls.base ? options.urls.base + url : url\n }\n };\n\n // eslint-disable-next-line complexity\n const request = async function request(\n method: string,\n url: string,\n data?: Record<string, any> | null,\n options: Record<string, any> = {}\n ) {\n const innerOptions: SendOptions = {\n method: method.toUpperCase(),\n headers: {},\n resolveWithFullResponse: false,\n ...opts,\n ...options\n } as SendOptions;\n\n innerOptions.headers = { ...innerOptions.headers, ...opts.headers, ...options.headers };\n\n if (!innerOptions.headers.Accept) {\n innerOptions.headers.Accept = \"application/json\";\n }\n\n const acceptType = innerOptions.headers.Accept;\n const contentType = innerOptions.headers[\"Content-Type\"] || innerOptions.headers[\"content-type\"] || \"\";\n\n if (!innerOptions.allowedMethods.includes(method)) {\n throw new Error(`Method ${method} not allowed. Allowed methods: ${innerOptions.allowedMethods.join(\", \")}`);\n }\n\n let finalUrl: URL;\n try {\n finalUrl = parseUrl(url, opts);\n } catch (error) {\n throw new Error(`Failed to parse URL: ${url}`);\n }\n\n if (data) {\n const isJson = /json/gi.test(contentType);\n\n if (innerOptions.method === \"GET\" && typeof data === \"object\") {\n finalUrl.search = serialize(data).toString();\n } else if (innerOptions.method !== \"GET\") {\n innerOptions.body = isJson ? JSON.stringify(data) : serializeFormData(data);\n }\n }\n\n const response = await fetch(finalUrl.toString(), innerOptions);\n let body = null;\n if (!response.ok) {\n const err = new Error(`${response.status}: ${response.statusText}`) as Error & { response?: any; body?: any };\n err.response = response;\n if (/text/gi.test(acceptType)) {\n err.body = await response.text();\n }\n\n if (/json/gi.test(acceptType)) {\n try {\n err.body = await response.json();\n } catch (error) {\n err.body = null;\n // eslint-disable-next-line no-console\n console.warn(\"Failed to parse JSON response:\", error);\n }\n }\n\n throw err;\n }\n\n if (innerOptions.resolveWithFullResponse) {\n return response;\n }\n\n if (/text/gi.test(acceptType)) {\n body = await response.text();\n } else if (/json/gi.test(acceptType)) {\n try {\n body = await response.json();\n } catch (error) {\n // eslint-disable-next-line no-console\n console.warn(\"Failed to parse JSON response:\", error);\n body = null;\n }\n } else if (/blob/gi.test(acceptType)) {\n body = await response.blob();\n } else if (/arraybuffer/gi.test(acceptType)) {\n body = await response.arrayBuffer();\n } else {\n body = response;\n }\n\n return body || response;\n } as unknown as RequestInterface;\n\n request.new = (baseUrl: string, options?: RequestOptions) => Requester(baseUrl, { ...opts, ...(options || {}) });\n\n request.setOption = (key: string, value: any) => {\n set(opts, key, value);\n return opts;\n };\n\n request.getOptions = (key?: string) => {\n if (key) {\n return get(opts, key);\n }\n\n return opts;\n };\n\n Object.assign(\n request,\n opts.allowedMethods.reduce((acc: Record<string, any>, method) => {\n acc[method] = (url: string, data?: Record<string, any> | null, options?: Record<string, any>) =>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,KAAK,kCAAkC,KAAK;AACpD,eAAO;AAAA,MACT;AAAA,IACF,WAAW,SAAS,KAAK,UAAU,GAAG;AACpC,aAAO,MAAM,SAAS,KAAK;AAAA,IAC7B,WAAW,gBAAgB,KAAK,UAAU,GAAG;AAC3C,aAAO,MAAM,SAAS,YAAY;AAAA,IACpC,OAAO;AACL,aAAO;AAAA,IACT;AAEA,WAAO,QAAQ;AAAA,EACjB;AAEA,EAAAD,SAAQ,MAAM,CAACG,UAAiBD,aAA6B,UAAUC,UAAS,EAAE,GAAG,MAAM,GAAID,YAAW,CAAC,EAAG,CAAC;AAE/G,EAAAF,SAAQ,YAAY,CAAC,KAAa,UAAe;AAC/C,QAAI,MAAM,KAAK,KAAK;AACpB,WAAO;AAAA,EACT;AAEA,EAAAA,SAAQ,aAAa,CAAC,QAAiB;AACrC,QAAI,KAAK;AACP,aAAO,IAAI,MAAM,GAAG;AAAA,IACtB;AAEA,WAAO;AAAA,EACT;AAEA,SAAO;AAAA,IACLA;AAAA,IACA,KAAK,eAAe,OAAO,CAAC,KAA0B,WAAW;AAC/D,UAAI,MAAM,IAAI,CAACC,MAAa,MAAmCC,aAC7DF,SAAQ,QAAQC,MAAK,MAAMC,QAAO;AACpC,aAAO;AAAA,IACT,GAAG,CAAC,CAAC;AAAA,EACP;AAEA,SAAOF;AACT;AAEO,IAAM,UAAU,UAAU;",
|
|
6
6
|
"names": ["request", "url", "options", "baseUrl"]
|
|
7
7
|
}
|
package/dist/router/index.d.ts
CHANGED
|
@@ -1,41 +1,38 @@
|
|
|
1
1
|
import { Component, POJOComponent, VnodeComponentInterface } from "valyrian.js";
|
|
2
|
-
interface Request {
|
|
2
|
+
export interface Request {
|
|
3
3
|
params: Record<string, any>;
|
|
4
4
|
query: Record<string, any>;
|
|
5
5
|
url: string;
|
|
6
6
|
path: string;
|
|
7
7
|
matches: string[];
|
|
8
|
-
redirect: (path: string
|
|
8
|
+
redirect: (path: string) => Promise<string | void>;
|
|
9
9
|
}
|
|
10
|
-
interface Middleware {
|
|
11
|
-
(req: Request,
|
|
10
|
+
export interface Middleware {
|
|
11
|
+
(req: Request, err?: any): Promise<any | Component | POJOComponent | VnodeComponentInterface> | any | Component | POJOComponent | VnodeComponentInterface;
|
|
12
12
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
export declare class Router implements RouterInterface {
|
|
38
|
-
paths: Path[];
|
|
13
|
+
export declare const RouterError: {
|
|
14
|
+
new (message?: string): {
|
|
15
|
+
status: number | undefined;
|
|
16
|
+
name: string;
|
|
17
|
+
message: string;
|
|
18
|
+
stack?: string;
|
|
19
|
+
cause?: unknown;
|
|
20
|
+
};
|
|
21
|
+
new (message?: string, options?: ErrorOptions): {
|
|
22
|
+
status: number | undefined;
|
|
23
|
+
name: string;
|
|
24
|
+
message: string;
|
|
25
|
+
stack?: string;
|
|
26
|
+
cause?: unknown;
|
|
27
|
+
};
|
|
28
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
29
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
30
|
+
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
|
|
31
|
+
stackTraceLimit: number;
|
|
32
|
+
};
|
|
33
|
+
type RouteParams = string | Middleware | Router | (string | Middleware | Router | RouteParams)[];
|
|
34
|
+
export declare class Router {
|
|
35
|
+
private routeTree;
|
|
39
36
|
container: Element | string | null;
|
|
40
37
|
query: Record<string, string | number>;
|
|
41
38
|
options: Record<string, any>;
|
|
@@ -44,12 +41,18 @@ export declare class Router implements RouterInterface {
|
|
|
44
41
|
params: Record<string, string | number | any>;
|
|
45
42
|
matches: string[];
|
|
46
43
|
pathPrefix: string;
|
|
44
|
+
private errorHandlers;
|
|
47
45
|
constructor(pathPrefix?: string);
|
|
48
|
-
add(
|
|
49
|
-
|
|
46
|
+
add(...args: RouteParams[]): Router;
|
|
47
|
+
catch(...args: (number | string | Error | typeof Error | Middleware)[]): Router;
|
|
50
48
|
routes(): string[];
|
|
51
|
-
go(path: string, parentComponent?: Component | POJOComponent | VnodeComponentInterface
|
|
49
|
+
go(path: string, parentComponent?: Component | POJOComponent | VnodeComponentInterface): Promise<string | void>;
|
|
52
50
|
getOnClickHandler(url: string): (e: MouseEvent) => void;
|
|
51
|
+
private getAllRoutes;
|
|
52
|
+
private createRequest;
|
|
53
|
+
private getErrorConditionMiddlewares;
|
|
54
|
+
private handleError;
|
|
55
|
+
private searchComponent;
|
|
53
56
|
}
|
|
54
57
|
export declare function redirect(url: string, parentComponent?: Component | POJOComponent | VnodeComponentInterface, preventPushState?: boolean): Promise<string | void>;
|
|
55
58
|
export declare function mountRouter(elementContainer: string | any, router: Router): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/router/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/router/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,SAAS,EACT,aAAa,EACb,uBAAuB,EASxB,MAAM,aAAa,CAAC;AAErB,MAAM,WAAW,OAAO;IACtB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC5B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC3B,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,EAAE,CAAC;IAElB,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,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MAAM,IAAI,MAAM,EAAE;IAKZ,EAAE,CACN,IAAI,EAAE,MAAM,EACZ,eAAe,CAAC,EAAE,SAAS,GAAG,aAAa,GAAG,uBAAuB,GACpE,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IA2EzB,iBAAiB,CAAC,GAAG,EAAE,MAAM,OAChB,UAAU;IAYvB,OAAO,CAAC,YAAY;IAepB,OAAO,CAAC,aAAa;IAWrB,OAAO,CAAC,4BAA4B;YAgCtB,WAAW;YAwEX,eAAe;CA4B9B;AAID,wBAAsB,QAAQ,CAC5B,GAAG,EAAE,MAAM,EACX,eAAe,CAAC,EAAE,SAAS,GAAG,aAAa,GAAG,uBAAuB,EACrE,gBAAgB,UAAQ,GACvB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAOxB;AAED,wBAAgB,WAAW,CAAC,gBAAgB,EAAE,MAAM,GAAG,GAAG,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAiBhF"}
|
package/dist/router/index.js
CHANGED
|
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var router_exports = {};
|
|
22
22
|
__export(router_exports, {
|
|
23
23
|
Router: () => Router,
|
|
24
|
+
RouterError: () => RouterError,
|
|
24
25
|
mountRouter: () => mountRouter,
|
|
25
26
|
redirect: () => redirect
|
|
26
27
|
});
|
|
@@ -39,83 +40,85 @@ function getPathWithoutLastSlash(path) {
|
|
|
39
40
|
}
|
|
40
41
|
return pathWithoutLastSlash;
|
|
41
42
|
}
|
|
42
|
-
var addPath = ({
|
|
43
|
-
router,
|
|
44
|
-
method,
|
|
45
|
-
path,
|
|
46
|
-
middlewares
|
|
47
|
-
}) => {
|
|
48
|
-
if (!method || !path || !Array.isArray(middlewares) || middlewares.length === 0) {
|
|
49
|
-
throw new Error(`Invalid route input: ${method} ${path} ${middlewares}`);
|
|
50
|
-
}
|
|
51
|
-
const realpath = path.replace(/(\S)(\/+)$/, "$1");
|
|
52
|
-
const params = (realpath.match(/:(\w+)?/gi) || []).map((param) => param.slice(1));
|
|
53
|
-
const regexpPath = "^" + realpath.replace(/:(\w+)/gi, "([^\\/\\s]+)") + "$";
|
|
54
|
-
router.paths.push({
|
|
55
|
-
method,
|
|
56
|
-
path: realpath,
|
|
57
|
-
middlewares: flat(middlewares),
|
|
58
|
-
params,
|
|
59
|
-
regexp: new RegExp(regexpPath, "i")
|
|
60
|
-
});
|
|
61
|
-
};
|
|
62
43
|
function parseQuery(queryParts) {
|
|
63
44
|
const parts = queryParts ? queryParts.split("&") : [];
|
|
64
45
|
const query = {};
|
|
65
46
|
for (const nameValue of parts) {
|
|
66
47
|
const [name, value] = nameValue.split("=", 2);
|
|
67
|
-
query[name] = value
|
|
48
|
+
query[name] = isNaN(Number(value)) === false ? Number(value) : value === "true" ? true : value === "false" ? false : value;
|
|
68
49
|
}
|
|
69
50
|
return query;
|
|
70
51
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
break;
|
|
52
|
+
var RouteTree = class {
|
|
53
|
+
root = { segment: "", children: /* @__PURE__ */ new Map(), isDynamic: false };
|
|
54
|
+
addRoute(path, middlewares) {
|
|
55
|
+
const segments = path === "/" ? [path] : path.split("/").filter(Boolean);
|
|
56
|
+
let currentNode = this.root;
|
|
57
|
+
for (const segment of segments) {
|
|
58
|
+
const isDynamic = segment.startsWith(":");
|
|
59
|
+
const key = isDynamic ? ":" : segment;
|
|
60
|
+
if (!currentNode.children.has(key)) {
|
|
61
|
+
currentNode.children.set(key, {
|
|
62
|
+
segment,
|
|
63
|
+
children: /* @__PURE__ */ new Map(),
|
|
64
|
+
isDynamic,
|
|
65
|
+
paramKey: isDynamic ? segment.slice(1) : void 0
|
|
66
|
+
});
|
|
87
67
|
}
|
|
68
|
+
currentNode = currentNode.children.get(key);
|
|
88
69
|
}
|
|
70
|
+
currentNode.middlewares = middlewares;
|
|
89
71
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
params
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
72
|
+
// Search for a route in the tree
|
|
73
|
+
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
74
|
+
findRoute(path) {
|
|
75
|
+
const pathWithoutLastSlash = getPathWithoutLastSlash(path);
|
|
76
|
+
const segments = pathWithoutLastSlash === "/" ? [pathWithoutLastSlash] : pathWithoutLastSlash.split("/").filter(Boolean);
|
|
77
|
+
let currentNode = this.root;
|
|
78
|
+
const params = {};
|
|
79
|
+
const wildcardMiddlewares = [];
|
|
80
|
+
const segmentsLength = segments.length;
|
|
81
|
+
for (let i = 0; i < segmentsLength; i++) {
|
|
82
|
+
if (!currentNode) {
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
const segment = segments[i];
|
|
86
|
+
let found = false;
|
|
87
|
+
for (const [key, child] of currentNode.children) {
|
|
88
|
+
if (key === segment) {
|
|
89
|
+
currentNode = child;
|
|
90
|
+
found = true;
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
if (segment !== ".*" && key === ":") {
|
|
94
|
+
currentNode = child;
|
|
95
|
+
params[child.paramKey] = segment;
|
|
96
|
+
found = true;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
if (key === ".*" && !found) {
|
|
100
|
+
wildcardMiddlewares.push(...child.middlewares || []);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (!found) {
|
|
104
|
+
if (currentNode.children.has(".*")) {
|
|
105
|
+
return { middlewares: wildcardMiddlewares, params };
|
|
106
|
+
}
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
114
109
|
}
|
|
110
|
+
const allMiddlewares = [...wildcardMiddlewares, ...currentNode.middlewares || []];
|
|
111
|
+
if (allMiddlewares.length === 0) {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
return { middlewares: allMiddlewares, params };
|
|
115
115
|
}
|
|
116
|
-
}
|
|
116
|
+
};
|
|
117
|
+
var RouterError = class RouterError2 extends Error {
|
|
118
|
+
status = 500;
|
|
119
|
+
};
|
|
117
120
|
var Router = class _Router {
|
|
118
|
-
|
|
121
|
+
routeTree = new RouteTree();
|
|
119
122
|
container = null;
|
|
120
123
|
query = {};
|
|
121
124
|
options = {};
|
|
@@ -124,55 +127,93 @@ var Router = class _Router {
|
|
|
124
127
|
params = {};
|
|
125
128
|
matches = [];
|
|
126
129
|
pathPrefix = "";
|
|
130
|
+
errorHandlers = /* @__PURE__ */ new Map();
|
|
127
131
|
constructor(pathPrefix = "") {
|
|
128
132
|
this.pathPrefix = pathPrefix;
|
|
129
133
|
}
|
|
130
|
-
add(
|
|
131
|
-
const
|
|
132
|
-
addPath({ router: this, method: "add", path: pathWithoutLastSlash, middlewares });
|
|
133
|
-
return this;
|
|
134
|
-
}
|
|
135
|
-
use(...middlewares) {
|
|
134
|
+
add(...args) {
|
|
135
|
+
const flatArgs = flat(args);
|
|
136
136
|
const path = getPathWithoutLastSlash(
|
|
137
|
-
`${this.pathPrefix}${typeof
|
|
137
|
+
`${this.pathPrefix}${typeof flatArgs[0] === "string" ? flatArgs.shift() : "/.*"}`
|
|
138
138
|
);
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
continue;
|
|
139
|
+
if (flatArgs.length === 1 && flatArgs[0] instanceof _Router) {
|
|
140
|
+
const subrouter = flatArgs[0];
|
|
141
|
+
for (const subroute of subrouter.routes()) {
|
|
142
|
+
const subroutePath = `${path}${subroute}`;
|
|
143
|
+
this.routeTree.addRoute(subroutePath, subrouter.routeTree.findRoute(subroute).middlewares || []);
|
|
144
|
+
}
|
|
145
|
+
} else {
|
|
146
|
+
if (flatArgs.some((item) => item instanceof _Router)) {
|
|
147
|
+
throw new RouterError("You cannot add middlewares when adding a subrouter.");
|
|
151
148
|
}
|
|
152
|
-
if (typeof item
|
|
153
|
-
|
|
149
|
+
if (flatArgs.some((item) => typeof item !== "function")) {
|
|
150
|
+
throw new RouterError("All middlewares must be functions.");
|
|
154
151
|
}
|
|
152
|
+
this.routeTree.addRoute(path, flatArgs);
|
|
155
153
|
}
|
|
156
154
|
return this;
|
|
157
155
|
}
|
|
156
|
+
catch(...args) {
|
|
157
|
+
const condition = typeof args[0] === "number" || typeof args[0] === "string" || args[0].name.includes("Error") ? args.shift() : "generic";
|
|
158
|
+
if (typeof condition !== "number" && typeof condition !== "string" && !condition.name.includes("Error")) {
|
|
159
|
+
throw new RouterError("The condition must be a number, string or an instance of Error.");
|
|
160
|
+
}
|
|
161
|
+
if (args.some((item) => typeof item !== "function")) {
|
|
162
|
+
throw new RouterError("All middlewares must be functions.");
|
|
163
|
+
}
|
|
164
|
+
let handlers = this.errorHandlers.get(condition);
|
|
165
|
+
if (!handlers) {
|
|
166
|
+
handlers = [];
|
|
167
|
+
this.errorHandlers.set(condition, handlers);
|
|
168
|
+
}
|
|
169
|
+
handlers.push(...args);
|
|
170
|
+
return this;
|
|
171
|
+
}
|
|
158
172
|
routes() {
|
|
159
|
-
return this.
|
|
173
|
+
return this.getAllRoutes(this.routeTree.root, "");
|
|
160
174
|
}
|
|
161
|
-
|
|
175
|
+
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
176
|
+
async go(path, parentComponent) {
|
|
162
177
|
if (!path) {
|
|
163
|
-
|
|
178
|
+
return this.handleError(new RouterError("The URL is empty."), parentComponent);
|
|
179
|
+
}
|
|
180
|
+
if (/%[^0-9A-Fa-f]{2}/.test(path)) {
|
|
181
|
+
return this.handleError(new RouterError(`The URL ${path} is malformed.`));
|
|
164
182
|
}
|
|
165
183
|
const constructedPath = getPathWithoutLastSlash(`${this.pathPrefix}${path}`);
|
|
166
184
|
const parts = constructedPath.split("?", 2);
|
|
167
185
|
this.url = constructedPath;
|
|
168
186
|
this.query = parseQuery(parts[1]);
|
|
169
|
-
const
|
|
170
|
-
|
|
187
|
+
const finalPath = parts[0].replace(/(.+)\/$/, "$1").split("#")[0];
|
|
188
|
+
this.path = path;
|
|
189
|
+
let route = this.routeTree.findRoute(finalPath);
|
|
190
|
+
if (!route || !route.middlewares) {
|
|
191
|
+
const finalPathParts = finalPath.split("/");
|
|
192
|
+
while (finalPathParts.length > 0) {
|
|
193
|
+
finalPathParts.pop();
|
|
194
|
+
const wildcardRoute = this.routeTree.findRoute(finalPathParts.join("/") + "/.*");
|
|
195
|
+
if (wildcardRoute) {
|
|
196
|
+
route = wildcardRoute;
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
if (!route || !route.middlewares) {
|
|
201
|
+
const error = new RouterError(`The URL ${constructedPath} was not found in the router's registered paths.`);
|
|
202
|
+
error.status = 404;
|
|
203
|
+
return this.handleError(error, parentComponent);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
const { middlewares, params } = route;
|
|
207
|
+
this.params = params;
|
|
208
|
+
let component = await this.searchComponent(middlewares, parentComponent);
|
|
171
209
|
if (component === false) {
|
|
172
210
|
return;
|
|
173
211
|
}
|
|
174
212
|
if (!component) {
|
|
175
|
-
|
|
213
|
+
return this.handleError(
|
|
214
|
+
new RouterError(`The URL ${constructedPath} did not return a valid component.`),
|
|
215
|
+
parentComponent
|
|
216
|
+
);
|
|
176
217
|
}
|
|
177
218
|
if ((0, import_valyrian.isComponent)(parentComponent) || (0, import_valyrian.isVnodeComponent)(parentComponent)) {
|
|
178
219
|
const childComponent = (0, import_valyrian.isVnodeComponent)(component) ? component : (0, import_valyrian.v)(component, {});
|
|
@@ -183,7 +224,7 @@ var Router = class _Router {
|
|
|
183
224
|
component = (0, import_valyrian.v)(parentComponent, {}, childComponent);
|
|
184
225
|
}
|
|
185
226
|
}
|
|
186
|
-
if (!import_valyrian.isNodeJs &&
|
|
227
|
+
if (!import_valyrian.isNodeJs && window.location.pathname + window.location.search !== constructedPath) {
|
|
187
228
|
window.history.pushState(null, "", constructedPath);
|
|
188
229
|
}
|
|
189
230
|
if (this.container) {
|
|
@@ -192,17 +233,128 @@ var Router = class _Router {
|
|
|
192
233
|
}
|
|
193
234
|
getOnClickHandler(url) {
|
|
194
235
|
return (e) => {
|
|
236
|
+
if (e.button !== 0 || e.ctrlKey || e.metaKey || e.shiftKey || e.altKey || e.defaultPrevented) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
195
239
|
if (typeof url === "string" && url.length > 0) {
|
|
196
240
|
this.go(url);
|
|
197
241
|
}
|
|
198
242
|
e.preventDefault();
|
|
199
243
|
};
|
|
200
244
|
}
|
|
245
|
+
getAllRoutes(node, prefix) {
|
|
246
|
+
const routes = [];
|
|
247
|
+
for (const [key, child] of node.children) {
|
|
248
|
+
const newPrefix = `${prefix}/${child.isDynamic ? `:${child.paramKey}` : key}`.replace(/\/$/, "");
|
|
249
|
+
if (child.middlewares) {
|
|
250
|
+
routes.push(newPrefix);
|
|
251
|
+
}
|
|
252
|
+
routes.push(...this.getAllRoutes(child, newPrefix));
|
|
253
|
+
}
|
|
254
|
+
return routes;
|
|
255
|
+
}
|
|
256
|
+
createRequest() {
|
|
257
|
+
return {
|
|
258
|
+
params: this.params,
|
|
259
|
+
query: this.query,
|
|
260
|
+
url: this.url,
|
|
261
|
+
path: this.path,
|
|
262
|
+
matches: this.matches,
|
|
263
|
+
redirect: (path) => this.go(path)
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
getErrorConditionMiddlewares(error) {
|
|
267
|
+
for (const [condition, middlewares] of this.errorHandlers) {
|
|
268
|
+
if (typeof condition !== "number" && typeof condition !== "string" && error instanceof condition && error.name === condition.name) {
|
|
269
|
+
return middlewares;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
for (const [condition, middlewares] of this.errorHandlers) {
|
|
273
|
+
if (typeof condition === "number" && (error.status === condition || error.code === condition)) {
|
|
274
|
+
return middlewares;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
for (const [condition, middlewares] of this.errorHandlers) {
|
|
278
|
+
if (typeof condition === "string" && (error.name === condition || error.message.includes(condition))) {
|
|
279
|
+
return middlewares;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return this.errorHandlers.get("generic") || false;
|
|
283
|
+
}
|
|
284
|
+
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
285
|
+
async handleError(error, parentComponent) {
|
|
286
|
+
const request = this.createRequest();
|
|
287
|
+
let component = null;
|
|
288
|
+
const middlewares = this.getErrorConditionMiddlewares(error);
|
|
289
|
+
if (middlewares === false) {
|
|
290
|
+
throw error;
|
|
291
|
+
}
|
|
292
|
+
let response;
|
|
293
|
+
try {
|
|
294
|
+
for (const middleware of middlewares) {
|
|
295
|
+
response = await middleware(request, error);
|
|
296
|
+
if (response !== void 0 && ((0, import_valyrian.isComponent)(response) || (0, import_valyrian.isVnodeComponent)(response))) {
|
|
297
|
+
component = response;
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
if (response === false) {
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
} catch (err) {
|
|
305
|
+
err.cause = error;
|
|
306
|
+
let errorCauseCount = 0;
|
|
307
|
+
while (err.cause) {
|
|
308
|
+
errorCauseCount++;
|
|
309
|
+
}
|
|
310
|
+
if (errorCauseCount > 20) {
|
|
311
|
+
throw new RouterError("Too many error causes. Possible circular error handling.");
|
|
312
|
+
}
|
|
313
|
+
return this.handleError(err, parentComponent);
|
|
314
|
+
}
|
|
315
|
+
if (component) {
|
|
316
|
+
if ((0, import_valyrian.isComponent)(parentComponent) || (0, import_valyrian.isVnodeComponent)(parentComponent)) {
|
|
317
|
+
const childComponent = (0, import_valyrian.isVnodeComponent)(component) ? component : (0, import_valyrian.v)(component, {});
|
|
318
|
+
if ((0, import_valyrian.isVnodeComponent)(parentComponent)) {
|
|
319
|
+
parentComponent.children.push(childComponent);
|
|
320
|
+
component = parentComponent;
|
|
321
|
+
} else {
|
|
322
|
+
component = (0, import_valyrian.v)(parentComponent, {}, childComponent);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
if (!import_valyrian.isNodeJs && window.location.pathname + window.location.search !== this.url) {
|
|
326
|
+
window.history.pushState(null, "", this.url);
|
|
327
|
+
}
|
|
328
|
+
if (this.container) {
|
|
329
|
+
return (0, import_valyrian.mount)(this.container, component);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
throw error;
|
|
333
|
+
}
|
|
334
|
+
async searchComponent(middlewares, parentComponent) {
|
|
335
|
+
const request = this.createRequest();
|
|
336
|
+
let response;
|
|
337
|
+
for (const middleware of middlewares) {
|
|
338
|
+
try {
|
|
339
|
+
response = await middleware(request);
|
|
340
|
+
} catch (error) {
|
|
341
|
+
return this.handleError(error, parentComponent);
|
|
342
|
+
}
|
|
343
|
+
if (response !== void 0 && ((0, import_valyrian.isComponent)(response) || (0, import_valyrian.isVnodeComponent)(response))) {
|
|
344
|
+
return response;
|
|
345
|
+
}
|
|
346
|
+
if (response === false) {
|
|
347
|
+
return false;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
return response;
|
|
351
|
+
}
|
|
201
352
|
};
|
|
202
353
|
var localRedirect;
|
|
203
|
-
function redirect(url, parentComponent, preventPushState = false) {
|
|
354
|
+
async function redirect(url, parentComponent, preventPushState = false) {
|
|
204
355
|
if (!localRedirect) {
|
|
205
|
-
|
|
356
|
+
console.warn("Redirect function is not initialized. Please mount the router first.");
|
|
357
|
+
return;
|
|
206
358
|
}
|
|
207
359
|
return localRedirect(url, parentComponent, preventPushState);
|
|
208
360
|
}
|
|
@@ -212,14 +364,13 @@ function mountRouter(elementContainer, router) {
|
|
|
212
364
|
if (!import_valyrian.isNodeJs) {
|
|
213
365
|
let onPopStateGoToRoute2 = function() {
|
|
214
366
|
const pathWithoutPrefix = getPathWithoutPrefix(document.location.pathname, router.pathPrefix);
|
|
215
|
-
router.go(pathWithoutPrefix
|
|
367
|
+
router.go(pathWithoutPrefix);
|
|
216
368
|
};
|
|
217
369
|
var onPopStateGoToRoute = onPopStateGoToRoute2;
|
|
218
370
|
window.addEventListener("popstate", onPopStateGoToRoute2, false);
|
|
219
371
|
onPopStateGoToRoute2();
|
|
220
372
|
}
|
|
221
|
-
(0, import_valyrian.directive)("route", (vnode) => {
|
|
222
|
-
const url = vnode.props["v-route"];
|
|
373
|
+
(0, import_valyrian.directive)("route", (url, vnode) => {
|
|
223
374
|
(0, import_valyrian.setAttribute)("href", url, vnode);
|
|
224
375
|
(0, import_valyrian.setAttribute)("onclick", router.getOnClickHandler(url), vnode);
|
|
225
376
|
});
|