@vaadin/hilla-frontend 24.4.0-alpha4

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.
@@ -0,0 +1,6 @@
1
+ import { commands, extensions } from '../../../.lintstagedrc.js';
2
+
3
+ export default {
4
+ [`src/**/*.{${extensions}}`]: commands,
5
+ [`test/**/*.{${extensions}}`]: commands,
6
+ };
@@ -0,0 +1,45 @@
1
+ import type { MiddlewareClass, MiddlewareContext, MiddlewareNext } from './Connect.js';
2
+ export interface LoginResult {
3
+ error: boolean;
4
+ token?: string;
5
+ errorTitle?: string;
6
+ errorMessage?: string;
7
+ redirectUrl?: string;
8
+ defaultUrl?: string;
9
+ }
10
+ export interface LoginOptions {
11
+ loginProcessingUrl?: string;
12
+ }
13
+ export interface LogoutOptions {
14
+ logoutUrl?: string;
15
+ }
16
+ /**
17
+ * A helper method for Spring Security based form login.
18
+ * @param username - username
19
+ * @param password - password
20
+ * @param options - defines additional options, e.g, the loginProcessingUrl etc.
21
+ */
22
+ export declare function login(username: string, password: string, options?: LoginOptions): Promise<LoginResult>;
23
+ /**
24
+ * A helper method for Spring Security based form logout
25
+ * @param options - defines additional options, e.g, the logoutUrl.
26
+ */
27
+ export declare function logout(options?: LogoutOptions): Promise<void>;
28
+ /**
29
+ * It defines what to do when it detects a session is invalid. E.g.,
30
+ * show a login view.
31
+ * It takes an <code>EndpointCallContinue</code> parameter, which can be
32
+ * used to continue the endpoint call.
33
+ */
34
+ export type OnInvalidSessionCallback = () => Promise<LoginResult>;
35
+ /**
36
+ * A helper class for handling invalid sessions during an endpoint call.
37
+ * E.g., you can use this to show user a login page when the session has
38
+ * expired.
39
+ */
40
+ export declare class InvalidSessionMiddleware implements MiddlewareClass {
41
+ private readonly onInvalidSessionCallback;
42
+ constructor(onInvalidSessionCallback: OnInvalidSessionCallback);
43
+ invoke(context: MiddlewareContext, next: MiddlewareNext): Promise<Response>;
44
+ }
45
+ //# sourceMappingURL=Authentication.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Authentication.d.ts","sourceRoot":"","sources":["src/Authentication.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAoDvF,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,OAAO,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,YAAY;IAC3B,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,MAAM,WAAW,aAAa;IAC5B,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;GAKG;AACH,wBAAsB,KAAK,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,WAAW,CAAC,CA2D5G;AAED;;;GAGG;AACH,wBAAsB,MAAM,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAqBnE;AAED;;;;;GAKG;AACH,MAAM,MAAM,wBAAwB,GAAG,MAAM,OAAO,CAAC,WAAW,CAAC,CAAC;AAElE;;;;GAIG;AACH,qBAAa,wBAAyB,YAAW,eAAe;IAC9D,OAAO,CAAC,QAAQ,CAAC,wBAAwB,CAA2B;gBAExD,wBAAwB,EAAE,wBAAwB;IAIxD,MAAM,CAAC,OAAO,EAAE,iBAAiB,EAAE,IAAI,EAAE,cAAc,GAAG,OAAO,CAAC,QAAQ,CAAC;CAalF"}
@@ -0,0 +1,136 @@
1
+ import CookieManager from "./CookieManager.js";
2
+ import { getSpringCsrfInfo, getSpringCsrfTokenHeadersForAuthRequest, VAADIN_CSRF_HEADER } from "./CsrfUtils.js";
3
+ const JWT_COOKIE_NAME = "jwt.headerAndPayload";
4
+ function getSpringCsrfTokenFromResponseBody(body) {
5
+ const doc = new DOMParser().parseFromString(body, "text/html");
6
+ return getSpringCsrfInfo(doc);
7
+ }
8
+ function clearSpringCsrfMetaTags() {
9
+ Array.from(document.head.querySelectorAll('meta[name="_csrf"], meta[name="_csrf_header"]')).forEach(
10
+ (el) => el.remove()
11
+ );
12
+ }
13
+ function updateSpringCsrfMetaTags(springCsrfInfo) {
14
+ clearSpringCsrfMetaTags();
15
+ const headerNameMeta = document.createElement("meta");
16
+ headerNameMeta.name = "_csrf_header";
17
+ headerNameMeta.content = springCsrfInfo._csrf_header;
18
+ document.head.appendChild(headerNameMeta);
19
+ const tokenMeta = document.createElement("meta");
20
+ tokenMeta.name = "_csrf";
21
+ tokenMeta.content = springCsrfInfo._csrf;
22
+ document.head.appendChild(tokenMeta);
23
+ }
24
+ const getVaadinCsrfTokenFromResponseBody = (body) => {
25
+ const match = /window\.Vaadin = \{TypeScript: \{"csrfToken":"([0-9a-zA-Z\\-]{36})"\}\};/iu.exec(body);
26
+ return match ? match[1] : void 0;
27
+ };
28
+ async function updateCsrfTokensBasedOnResponse(response) {
29
+ const responseText = await response.text();
30
+ const token = getVaadinCsrfTokenFromResponseBody(responseText);
31
+ const springCsrfTokenInfo = getSpringCsrfTokenFromResponseBody(responseText);
32
+ updateSpringCsrfMetaTags(springCsrfTokenInfo);
33
+ return token;
34
+ }
35
+ async function doLogout(logoutUrl, headers) {
36
+ const response = await fetch(logoutUrl, { headers, method: "POST" });
37
+ if (!response.ok) {
38
+ throw new Error(`failed to logout with response ${response.status}`);
39
+ }
40
+ await updateCsrfTokensBasedOnResponse(response);
41
+ }
42
+ async function login(username, password, options) {
43
+ try {
44
+ const data = new FormData();
45
+ data.append("username", username);
46
+ data.append("password", password);
47
+ const loginProcessingUrl = options?.loginProcessingUrl ?? "login";
48
+ const headers = getSpringCsrfTokenHeadersForAuthRequest(document);
49
+ headers.source = "typescript";
50
+ const response = await fetch(loginProcessingUrl, {
51
+ body: data,
52
+ headers,
53
+ method: "POST"
54
+ });
55
+ const result = response.headers.get("Result");
56
+ const savedUrl = response.headers.get("Saved-url") ?? void 0;
57
+ const defaultUrl = response.headers.get("Default-url") ?? void 0;
58
+ const loginSuccessful = response.ok && result === "success";
59
+ if (loginSuccessful) {
60
+ const vaadinCsrfToken = response.headers.get("Vaadin-CSRF") ?? void 0;
61
+ const springCsrfHeader = response.headers.get("Spring-CSRF-header") ?? void 0;
62
+ const springCsrfToken = response.headers.get("Spring-CSRF-token") ?? void 0;
63
+ if (springCsrfHeader && springCsrfToken) {
64
+ const springCsrfTokenInfo = {};
65
+ springCsrfTokenInfo._csrf = springCsrfToken;
66
+ springCsrfTokenInfo._csrf_header = springCsrfHeader;
67
+ updateSpringCsrfMetaTags(springCsrfTokenInfo);
68
+ }
69
+ return {
70
+ defaultUrl,
71
+ error: false,
72
+ redirectUrl: savedUrl,
73
+ token: vaadinCsrfToken
74
+ };
75
+ }
76
+ return {
77
+ error: true,
78
+ errorMessage: "Check that you have entered the correct username and password and try again.",
79
+ errorTitle: "Incorrect username or password."
80
+ };
81
+ } catch (e) {
82
+ if (e instanceof Error) {
83
+ return {
84
+ error: true,
85
+ errorMessage: e.message,
86
+ errorTitle: e.name
87
+ };
88
+ }
89
+ throw e;
90
+ }
91
+ }
92
+ async function logout(options) {
93
+ const logoutUrl = options?.logoutUrl ?? "logout";
94
+ try {
95
+ const headers = getSpringCsrfTokenHeadersForAuthRequest(document);
96
+ await doLogout(logoutUrl, headers);
97
+ } catch {
98
+ try {
99
+ const response = await fetch("?nocache");
100
+ const responseText = await response.text();
101
+ const doc = new DOMParser().parseFromString(responseText, "text/html");
102
+ const headers = getSpringCsrfTokenHeadersForAuthRequest(doc);
103
+ await doLogout(logoutUrl, headers);
104
+ } catch (error) {
105
+ clearSpringCsrfMetaTags();
106
+ throw error;
107
+ }
108
+ } finally {
109
+ CookieManager.remove(JWT_COOKIE_NAME);
110
+ }
111
+ }
112
+ class InvalidSessionMiddleware {
113
+ onInvalidSessionCallback;
114
+ constructor(onInvalidSessionCallback) {
115
+ this.onInvalidSessionCallback = onInvalidSessionCallback;
116
+ }
117
+ async invoke(context, next) {
118
+ const clonedContext = { ...context };
119
+ clonedContext.request = context.request.clone();
120
+ const response = await next(context);
121
+ if (response.status === 401) {
122
+ const loginResult = await this.onInvalidSessionCallback();
123
+ if (loginResult.token) {
124
+ clonedContext.request.headers.set(VAADIN_CSRF_HEADER, loginResult.token);
125
+ return next(clonedContext);
126
+ }
127
+ }
128
+ return response;
129
+ }
130
+ }
131
+ export {
132
+ InvalidSessionMiddleware,
133
+ login,
134
+ logout
135
+ };
136
+ //# sourceMappingURL=Authentication.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["src/Authentication.ts"],
4
+ "sourcesContent": ["import type { MiddlewareClass, MiddlewareContext, MiddlewareNext } from './Connect.js';\nimport CookieManager from './CookieManager.js';\nimport { getSpringCsrfInfo, getSpringCsrfTokenHeadersForAuthRequest, VAADIN_CSRF_HEADER } from './CsrfUtils.js';\n\nconst JWT_COOKIE_NAME = 'jwt.headerAndPayload';\n\nfunction getSpringCsrfTokenFromResponseBody(body: string): Record<string, string> {\n const doc = new DOMParser().parseFromString(body, 'text/html');\n return getSpringCsrfInfo(doc);\n}\n\nfunction clearSpringCsrfMetaTags() {\n Array.from(document.head.querySelectorAll('meta[name=\"_csrf\"], meta[name=\"_csrf_header\"]')).forEach((el) =>\n el.remove(),\n );\n}\n\nfunction updateSpringCsrfMetaTags(springCsrfInfo: Record<string, string>) {\n clearSpringCsrfMetaTags();\n const headerNameMeta: HTMLMetaElement = document.createElement('meta');\n headerNameMeta.name = '_csrf_header';\n headerNameMeta.content = springCsrfInfo._csrf_header;\n document.head.appendChild(headerNameMeta);\n const tokenMeta: HTMLMetaElement = document.createElement('meta');\n tokenMeta.name = '_csrf';\n tokenMeta.content = springCsrfInfo._csrf;\n document.head.appendChild(tokenMeta);\n}\n\nconst getVaadinCsrfTokenFromResponseBody = (body: string): string | undefined => {\n const match = /window\\.Vaadin = \\{TypeScript: \\{\"csrfToken\":\"([0-9a-zA-Z\\\\-]{36})\"\\}\\};/iu.exec(body);\n return match ? match[1] : undefined;\n};\n\nasync function updateCsrfTokensBasedOnResponse(response: Response): Promise<string | undefined> {\n const responseText = await response.text();\n const token = getVaadinCsrfTokenFromResponseBody(responseText);\n const springCsrfTokenInfo = getSpringCsrfTokenFromResponseBody(responseText);\n updateSpringCsrfMetaTags(springCsrfTokenInfo);\n\n return token;\n}\n\nasync function doLogout(logoutUrl: string, headers: Record<string, string>) {\n const response = await fetch(logoutUrl, { headers, method: 'POST' });\n if (!response.ok) {\n throw new Error(`failed to logout with response ${response.status}`);\n }\n\n await updateCsrfTokensBasedOnResponse(response);\n}\n\nexport interface LoginResult {\n error: boolean;\n token?: string;\n errorTitle?: string;\n errorMessage?: string;\n redirectUrl?: string;\n defaultUrl?: string;\n}\n\nexport interface LoginOptions {\n loginProcessingUrl?: string;\n}\n\nexport interface LogoutOptions {\n logoutUrl?: string;\n}\n\n/**\n * A helper method for Spring Security based form login.\n * @param username - username\n * @param password - password\n * @param options - defines additional options, e.g, the loginProcessingUrl etc.\n */\nexport async function login(username: string, password: string, options?: LoginOptions): Promise<LoginResult> {\n try {\n const data = new FormData();\n data.append('username', username);\n data.append('password', password);\n\n const loginProcessingUrl = options?.loginProcessingUrl ?? 'login';\n const headers = getSpringCsrfTokenHeadersForAuthRequest(document);\n headers.source = 'typescript';\n const response = await fetch(loginProcessingUrl, {\n body: data,\n headers,\n method: 'POST',\n });\n\n // This code assumes that a VaadinSavedRequestAwareAuthenticationSuccessHandler is used on the server side,\n // setting these header values based on the \"source=typescript\" header set above\n\n const result = response.headers.get('Result');\n const savedUrl = response.headers.get('Saved-url') ?? undefined;\n const defaultUrl = response.headers.get('Default-url') ?? undefined;\n const loginSuccessful = response.ok && result === 'success';\n\n if (loginSuccessful) {\n const vaadinCsrfToken = response.headers.get('Vaadin-CSRF') ?? undefined;\n\n const springCsrfHeader = response.headers.get('Spring-CSRF-header') ?? undefined;\n const springCsrfToken = response.headers.get('Spring-CSRF-token') ?? undefined;\n if (springCsrfHeader && springCsrfToken) {\n const springCsrfTokenInfo: Record<string, string> = {};\n springCsrfTokenInfo._csrf = springCsrfToken;\n // eslint-disable-next-line camelcase\n springCsrfTokenInfo._csrf_header = springCsrfHeader;\n updateSpringCsrfMetaTags(springCsrfTokenInfo);\n }\n\n return {\n defaultUrl,\n error: false,\n redirectUrl: savedUrl,\n token: vaadinCsrfToken,\n };\n }\n return {\n error: true,\n errorMessage: 'Check that you have entered the correct username and password and try again.',\n errorTitle: 'Incorrect username or password.',\n };\n } catch (e: unknown) {\n if (e instanceof Error) {\n return {\n error: true,\n errorMessage: e.message,\n errorTitle: e.name,\n };\n }\n\n throw e;\n }\n}\n\n/**\n * A helper method for Spring Security based form logout\n * @param options - defines additional options, e.g, the logoutUrl.\n */\nexport async function logout(options?: LogoutOptions): Promise<void> {\n // this assumes the default Spring Security logout configuration (handler URL)\n const logoutUrl = options?.logoutUrl ?? 'logout';\n try {\n const headers = getSpringCsrfTokenHeadersForAuthRequest(document);\n await doLogout(logoutUrl, headers);\n } catch {\n try {\n const response = await fetch('?nocache');\n const responseText = await response.text();\n const doc = new DOMParser().parseFromString(responseText, 'text/html');\n const headers = getSpringCsrfTokenHeadersForAuthRequest(doc);\n await doLogout(logoutUrl, headers);\n } catch (error) {\n // clear the token if the call fails\n clearSpringCsrfMetaTags();\n throw error;\n }\n } finally {\n CookieManager.remove(JWT_COOKIE_NAME);\n }\n}\n\n/**\n * It defines what to do when it detects a session is invalid. E.g.,\n * show a login view.\n * It takes an <code>EndpointCallContinue</code> parameter, which can be\n * used to continue the endpoint call.\n */\nexport type OnInvalidSessionCallback = () => Promise<LoginResult>;\n\n/**\n * A helper class for handling invalid sessions during an endpoint call.\n * E.g., you can use this to show user a login page when the session has\n * expired.\n */\nexport class InvalidSessionMiddleware implements MiddlewareClass {\n private readonly onInvalidSessionCallback: OnInvalidSessionCallback;\n\n constructor(onInvalidSessionCallback: OnInvalidSessionCallback) {\n this.onInvalidSessionCallback = onInvalidSessionCallback;\n }\n\n async invoke(context: MiddlewareContext, next: MiddlewareNext): Promise<Response> {\n const clonedContext = { ...context };\n clonedContext.request = context.request.clone();\n const response = await next(context);\n if (response.status === 401) {\n const loginResult = await this.onInvalidSessionCallback();\n if (loginResult.token) {\n clonedContext.request.headers.set(VAADIN_CSRF_HEADER, loginResult.token);\n return next(clonedContext) as Promise<Response>;\n }\n }\n return response;\n }\n}\n"],
5
+ "mappings": "AACA,OAAO,mBAAmB;AAC1B,SAAS,mBAAmB,yCAAyC,0BAA0B;AAE/F,MAAM,kBAAkB;AAExB,SAAS,mCAAmC,MAAsC;AAChF,QAAM,MAAM,IAAI,UAAU,EAAE,gBAAgB,MAAM,WAAW;AAC7D,SAAO,kBAAkB,GAAG;AAC9B;AAEA,SAAS,0BAA0B;AACjC,QAAM,KAAK,SAAS,KAAK,iBAAiB,+CAA+C,CAAC,EAAE;AAAA,IAAQ,CAAC,OACnG,GAAG,OAAO;AAAA,EACZ;AACF;AAEA,SAAS,yBAAyB,gBAAwC;AACxE,0BAAwB;AACxB,QAAM,iBAAkC,SAAS,cAAc,MAAM;AACrE,iBAAe,OAAO;AACtB,iBAAe,UAAU,eAAe;AACxC,WAAS,KAAK,YAAY,cAAc;AACxC,QAAM,YAA6B,SAAS,cAAc,MAAM;AAChE,YAAU,OAAO;AACjB,YAAU,UAAU,eAAe;AACnC,WAAS,KAAK,YAAY,SAAS;AACrC;AAEA,MAAM,qCAAqC,CAAC,SAAqC;AAC/E,QAAM,QAAQ,6EAA6E,KAAK,IAAI;AACpG,SAAO,QAAQ,MAAM,CAAC,IAAI;AAC5B;AAEA,eAAe,gCAAgC,UAAiD;AAC9F,QAAM,eAAe,MAAM,SAAS,KAAK;AACzC,QAAM,QAAQ,mC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}
package/Connect.d.ts ADDED
@@ -0,0 +1,165 @@
1
+ /// <reference types="atmosphere.js" />
2
+ import type { ReactiveControllerHost } from '@lit/reactive-element';
3
+ import { FluxConnection } from './FluxConnection.js';
4
+ export type MaybePromise<T> = Promise<T> | T;
5
+ /**
6
+ * Represents the connection to and endpoint returning a subscription rather than a value.
7
+ */
8
+ export interface Subscription<T> {
9
+ /** Cancels the subscription. No values are made available after calling this. */
10
+ cancel(): void;
11
+ context(context: ReactiveControllerHost): Subscription<T>;
12
+ /** Called when the subscription has completed. No values are made available after calling this. */
13
+ onComplete(callback: () => void): Subscription<T>;
14
+ /** Called when an exception occured in the subscription. */
15
+ onError(callback: () => void): Subscription<T>;
16
+ /** Called when a new value is available. */
17
+ onNext(callback: (value: T) => void): Subscription<T>;
18
+ }
19
+ /**
20
+ * The `ConnectClient` constructor options.
21
+ */
22
+ export interface ConnectClientOptions {
23
+ /**
24
+ * The `middlewares` property value.
25
+ */
26
+ middlewares?: Middleware[];
27
+ /**
28
+ * The `prefix` property value.
29
+ */
30
+ prefix?: string;
31
+ /**
32
+ * The Atmosphere options for the FluxConnection.
33
+ */
34
+ atmosphereOptions?: Partial<Atmosphere.Request>;
35
+ }
36
+ export interface EndpointCallMetaInfo {
37
+ /**
38
+ * The endpoint name.
39
+ */
40
+ endpoint: string;
41
+ /**
42
+ * The method name to call on in the endpoint class.
43
+ */
44
+ method: string;
45
+ /**
46
+ * Optional object with method call arguments.
47
+ */
48
+ params?: Record<string, unknown>;
49
+ }
50
+ /**
51
+ * An object with the call arguments and the related Request instance.
52
+ * See also {@link ConnectClient.call | the call() method in ConnectClient}.
53
+ */
54
+ export interface MiddlewareContext extends EndpointCallMetaInfo {
55
+ /**
56
+ * The Fetch API Request object reflecting the other properties.
57
+ */
58
+ request: Request;
59
+ }
60
+ /**
61
+ * An async middleware callback that invokes the next middleware in the chain
62
+ * or makes the actual request.
63
+ * @param context - The information about the call and request
64
+ */
65
+ export type MiddlewareNext = (context: MiddlewareContext) => MaybePromise<Response>;
66
+ /**
67
+ * An interface that allows defining a middleware as a class.
68
+ */
69
+ export interface MiddlewareClass {
70
+ /**
71
+ * @param context - The information about the call and request
72
+ * @param next - Invokes the next in the call chain
73
+ */
74
+ invoke(context: MiddlewareContext, next: MiddlewareNext): MaybePromise<Response>;
75
+ }
76
+ /**
77
+ * An async callback function that can intercept the request and response
78
+ * of a call.
79
+ */
80
+ export type MiddlewareFunction = (context: MiddlewareContext, next: MiddlewareNext) => MaybePromise<Response>;
81
+ /**
82
+ * An async callback that can intercept the request and response
83
+ * of a call, could be either a function or a class.
84
+ */
85
+ export type Middleware = MiddlewareClass | MiddlewareFunction;
86
+ /**
87
+ * A list of parameters supported by {@link ConnectClient.call | the call() method in ConnectClient}.
88
+ */
89
+ export interface EndpointRequestInit {
90
+ /**
91
+ * An AbortSignal to set request's signal.
92
+ */
93
+ signal?: AbortSignal | null;
94
+ }
95
+ /**
96
+ * A low-level network calling utility. It stores
97
+ * a prefix and facilitates remote calls to endpoint class methods
98
+ * on the Hilla backend.
99
+ *
100
+ * Example usage:
101
+ *
102
+ * ```js
103
+ * const client = new ConnectClient();
104
+ * const responseData = await client.call('MyEndpoint', 'myMethod');
105
+ * ```
106
+ *
107
+ * ### Prefix
108
+ *
109
+ * The client supports an `prefix` constructor option:
110
+ * ```js
111
+ * const client = new ConnectClient({prefix: '/my-connect-prefix'});
112
+ * ```
113
+ *
114
+ * The default prefix is '/connect'.
115
+ *
116
+ */
117
+ export declare class ConnectClient {
118
+ #private;
119
+ /**
120
+ * The array of middlewares that are invoked during a call.
121
+ */
122
+ middlewares: Middleware[];
123
+ /**
124
+ * The Hilla endpoint prefix
125
+ */
126
+ prefix: string;
127
+ /**
128
+ * The Atmosphere options for the FluxConnection.
129
+ */
130
+ atmosphereOptions: Partial<Atmosphere.Request>;
131
+ /**
132
+ * @param options - Constructor options.
133
+ */
134
+ constructor(options?: ConnectClientOptions);
135
+ /**
136
+ * Gets a representation of the underlying persistent network connection used for subscribing to Flux type endpoint
137
+ * methods.
138
+ */
139
+ get fluxConnection(): FluxConnection;
140
+ /**
141
+ * Calls the given endpoint method defined using the endpoint and method
142
+ * parameters with the parameters given as params.
143
+ * Asynchronously returns the parsed JSON response data.
144
+ *
145
+ * @param endpoint - Endpoint name.
146
+ * @param method - Method name to call in the endpoint class.
147
+ * @param params - Optional parameters to pass to the method.
148
+ * @param init - Optional parameters for the request
149
+ * @returns Decoded JSON response data.
150
+ */
151
+ call(endpoint: string, method: string, params?: Record<string, unknown>, init?: EndpointRequestInit): Promise<any>;
152
+ /**
153
+ * Subscribes to the given method defined using the endpoint and method
154
+ * parameters with the parameters given as params. The method must return a
155
+ * compatible type such as a Flux.
156
+ * Returns a subscription that is used to fetch values as they become available.
157
+ *
158
+ * @param endpoint - Endpoint name.
159
+ * @param method - Method name to call in the endpoint class.
160
+ * @param params - Optional parameters to pass to the method.
161
+ * @returns A subscription used to handles values as they become available.
162
+ */
163
+ subscribe(endpoint: string, method: string, params?: any): Subscription<any>;
164
+ }
165
+ //# sourceMappingURL=Connect.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Connect.d.ts","sourceRoot":"","sources":["src/Connect.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAWpE,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAWrD,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AAE7C;;GAEG;AACH,MAAM,WAAW,YAAY,CAAC,CAAC;IAC7B,kFAAkF;IAClF,MAAM,IAAI,IAAI,CAAC;IAKf,OAAO,CAAC,OAAO,EAAE,sBAAsB,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;IAE1D,mGAAmG;IACnG,UAAU,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;IAElD,4DAA4D;IAC5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BAAmB;IAClC;;OAEG;IACH,MAAM,CAAC,EAAE,WAAW,GAAG,IAAI,CAAC;CAC7B;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,aAAa;;IACxB;;OAEG;IACH,WAAW,EAAE,UAAU,EAAE,CAAM;IAC/B;;OAEG;IACH,MAAM,SAAc;IACpB;;OAEG;IACH,iBAAiB,EAAE,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,CAAM;IAIpD;;OAEG;gBACS,OAAO,GAAE,oBAAyB;IA8B9C;;;OAGG;IACH,IAAI,cAAc,IAAI,cAAc,CAKnC;IAED;;;;;;;;;;OAUG;IACG,IAAI,CACR,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,IAAI,CAAC,EAAE,mBAAmB,GACzB,OAAO,CAAC,GAAG,CAAC;IAmFf;;;;;;;;;;OAUG;IACH,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,GAAG,YAAY,CAAC,GAAG,CAAC;CAG7E"}
package/Connect.js ADDED
@@ -0,0 +1,185 @@
1
+ import { ConnectionIndicator, ConnectionState } from "@vaadin/common-frontend";
2
+ import { getCsrfTokenHeadersForEndpointRequest } from "./CsrfUtils.js";
3
+ import {
4
+ EndpointError,
5
+ EndpointResponseError,
6
+ EndpointValidationError,
7
+ ForbiddenResponseError,
8
+ UnauthorizedResponseError
9
+ } from "./EndpointErrors.js";
10
+ import { FluxConnection } from "./FluxConnection.js";
11
+ const $wnd = window;
12
+ $wnd.Vaadin ??= {};
13
+ $wnd.Vaadin.registrations ??= [];
14
+ $wnd.Vaadin.registrations.push({
15
+ is: "endpoint"
16
+ });
17
+ const assertResponseIsOk = async (response) => {
18
+ if (!response.ok) {
19
+ const errorText = await response.text();
20
+ let errorJson;
21
+ try {
22
+ errorJson = JSON.parse(errorText);
23
+ } catch (ignored) {
24
+ errorJson = null;
25
+ }
26
+ const message = errorJson?.message ?? (errorText.length > 0 ? errorText : `expected "200 OK" response, but got ${response.status} ${response.statusText}`);
27
+ const type = errorJson?.type;
28
+ if (errorJson?.validationErrorData) {
29
+ throw new EndpointValidationError(message, errorJson.validationErrorData, type);
30
+ }
31
+ if (type) {
32
+ throw new EndpointError(message, type, errorJson?.detail);
33
+ }
34
+ switch (response.status) {
35
+ case 401:
36
+ throw new UnauthorizedResponseError(message, response);
37
+ case 403:
38
+ throw new ForbiddenResponseError(message, response);
39
+ default:
40
+ throw new EndpointResponseError(message, response);
41
+ }
42
+ }
43
+ };
44
+ function isFlowLoaded() {
45
+ return $wnd.Vaadin?.Flow?.clients?.TypeScript !== void 0;
46
+ }
47
+ class ConnectClient {
48
+ /**
49
+ * The array of middlewares that are invoked during a call.
50
+ */
51
+ middlewares = [];
52
+ /**
53
+ * The Hilla endpoint prefix
54
+ */
55
+ prefix = "/connect";
56
+ /**
57
+ * The Atmosphere options for the FluxConnection.
58
+ */
59
+ atmosphereOptions = {};
60
+ #fluxConnection;
61
+ /**
62
+ * @param options - Constructor options.
63
+ */
64
+ constructor(options = {}) {
65
+ if (options.prefix) {
66
+ this.prefix = options.prefix;
67
+ }
68
+ if (options.middlewares) {
69
+ this.middlewares = options.middlewares;
70
+ }
71
+ if (options.atmosphereOptions) {
72
+ this.atmosphereOptions = options.atmosphereOptions;
73
+ }
74
+ ConnectionIndicator.create();
75
+ addEventListener("online", () => {
76
+ if (!isFlowLoaded() && $wnd.Vaadin?.connectionState) {
77
+ $wnd.Vaadin.connectionState.state = ConnectionState.CONNECTED;
78
+ }
79
+ });
80
+ addEventListener("offline", () => {
81
+ if (!isFlowLoaded() && $wnd.Vaadin?.connectionState) {
82
+ $wnd.Vaadin.connectionState.state = ConnectionState.CONNECTION_LOST;
83
+ }
84
+ });
85
+ }
86
+ /**
87
+ * Gets a representation of the underlying persistent network connection used for subscribing to Flux type endpoint
88
+ * methods.
89
+ */
90
+ get fluxConnection() {
91
+ if (!this.#fluxConnection) {
92
+ this.#fluxConnection = new FluxConnection(this.prefix, this.atmosphereOptions);
93
+ }
94
+ return this.#fluxConnection;
95
+ }
96
+ /**
97
+ * Calls the given endpoint method defined using the endpoint and method
98
+ * parameters with the parameters given as params.
99
+ * Asynchronously returns the parsed JSON response data.
100
+ *
101
+ * @param endpoint - Endpoint name.
102
+ * @param method - Method name to call in the endpoint class.
103
+ * @param params - Optional parameters to pass to the method.
104
+ * @param init - Optional parameters for the request
105
+ * @returns Decoded JSON response data.
106
+ */
107
+ async call(endpoint, method, params, init) {
108
+ if (arguments.length < 2) {
109
+ throw new TypeError(`2 arguments required, but got only ${arguments.length}`);
110
+ }
111
+ const csrfHeaders = getCsrfTokenHeadersForEndpointRequest(document);
112
+ const headers = {
113
+ Accept: "application/json",
114
+ "Content-Type": "application/json",
115
+ ...csrfHeaders
116
+ };
117
+ const request = new Request(`${this.prefix}/${endpoint}/${method}`, {
118
+ body: params !== void 0 ? JSON.stringify(params, (_, value) => value === void 0 ? null : value) : void 0,
119
+ headers,
120
+ method: "POST"
121
+ });
122
+ const initialContext = {
123
+ endpoint,
124
+ method,
125
+ params,
126
+ request
127
+ };
128
+ async function responseHandlerMiddleware(context, next) {
129
+ const response = await next(context);
130
+ await assertResponseIsOk(response);
131
+ const text = await response.text();
132
+ return JSON.parse(text, (_, value) => value === null ? void 0 : value);
133
+ }
134
+ async function fetchNext(context) {
135
+ $wnd.Vaadin?.connectionState?.loadingStarted();
136
+ try {
137
+ const response = await fetch(context.request, { signal: init?.signal });
138
+ $wnd.Vaadin?.connectionState?.loadingFinished();
139
+ return response;
140
+ } catch (error) {
141
+ if (error instanceof Error && error.name === "AbortError") {
142
+ $wnd.Vaadin?.connectionState?.loadingFinished();
143
+ } else {
144
+ $wnd.Vaadin?.connectionState?.loadingFailed();
145
+ }
146
+ return Promise.reject(error);
147
+ }
148
+ }
149
+ const middlewares = [responseHandlerMiddleware, ...this.middlewares];
150
+ const chain = middlewares.reduceRight(
151
+ (next, middleware) => (
152
+ // Compose and return the new chain step, that takes the context and
153
+ // invokes the current middleware with the context and the further chain
154
+ // as the next argument
155
+ async (context) => {
156
+ if (typeof middleware === "function") {
157
+ return middleware(context, next);
158
+ }
159
+ return middleware.invoke(context, next);
160
+ }
161
+ ),
162
+ // Initialize reduceRight the accumulator with `fetchNext`
163
+ fetchNext
164
+ );
165
+ return chain(initialContext);
166
+ }
167
+ /**
168
+ * Subscribes to the given method defined using the endpoint and method
169
+ * parameters with the parameters given as params. The method must return a
170
+ * compatible type such as a Flux.
171
+ * Returns a subscription that is used to fetch values as they become available.
172
+ *
173
+ * @param endpoint - Endpoint name.
174
+ * @param method - Method name to call in the endpoint class.
175
+ * @param params - Optional parameters to pass to the method.
176
+ * @returns A subscription used to handles values as they become available.
177
+ */
178
+ subscribe(endpoint, method, params) {
179
+ return this.fluxConnection.subscribe(endpoint, method, params ? Object.values(params) : []);
180
+ }
181
+ }
182
+ export {
183
+ ConnectClient
184
+ };
185
+ //# sourceMappingURL=Connect.js.map
package/Connect.js.map ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["src/Connect.ts"],
4
+ "sourcesContent": ["import type { ReactiveControllerHost } from '@lit/reactive-element';\nimport { ConnectionIndicator, ConnectionState } from '@vaadin/common-frontend';\nimport { getCsrfTokenHeadersForEndpointRequest } from './CsrfUtils.js';\nimport {\n EndpointError,\n EndpointResponseError,\n EndpointValidationError,\n ForbiddenResponseError,\n UnauthorizedResponseError,\n type ValidationErrorData,\n} from './EndpointErrors.js';\nimport { FluxConnection } from './FluxConnection.js';\nimport type { VaadinWindow } from './types.js';\n\nconst $wnd = window as VaadinWindow;\n\n$wnd.Vaadin ??= {};\n$wnd.Vaadin.registrations ??= [];\n$wnd.Vaadin.registrations.push({\n is: 'endpoint',\n});\n\nexport type MaybePromise<T> = Promise<T> | T;\n\n/**\n * Represents the connection to and endpoint returning a subscription rather than a value.\n */\nexport interface Subscription<T> {\n /** Cancels the subscription. No values are made available after calling this. */\n cancel(): void;\n\n /*\n * Binds to the given context (element) so that when the context is deactivated (element detached), the subscription is closed.\n */\n context(context: ReactiveControllerHost): Subscription<T>;\n\n /** Called when the subscription has completed. No values are made available after calling this. */\n onComplete(callback: () => void): Subscription<T>;\n\n /** Called when an exception occured in the subscription. */\n onError(callback: () => void): Subscription<T>;\n\n /** Called when a new value is available. */\n onNext(callback: (value: T) => void): Subscription<T>;\n}\n\ninterface ConnectExceptionData {\n detail?: any;\n message: string;\n type: string;\n validationErrorData?: ValidationErrorData[];\n}\n\n/**\n * Throws a TypeError if the response is not 200 OK.\n * @param response - The response to assert.\n */\nconst assertResponseIsOk = async (response: Response): Promise<void> => {\n if (!response.ok) {\n const errorText = await response.text();\n let errorJson: ConnectExceptionData | null;\n try {\n errorJson = JSON.parse(errorText);\n } catch (ignored) {\n // not a json\n errorJson = null;\n }\n\n const message =\n errorJson?.message ??\n (errorText.length > 0\n ? errorText\n : `expected \"200 OK\" response, but got ${response.status} ${response.statusText}`);\n const type = errorJson?.type;\n\n if (errorJson?.validationErrorData) {\n throw new EndpointValidationError(message, errorJson.validationErrorData, type);\n }\n\n if (type) {\n throw new EndpointError(message, type, errorJson?.detail);\n }\n\n switch (response.status) {\n case 401:\n throw new UnauthorizedResponseError(message, response);\n case 403:\n throw new ForbiddenResponseError(message, response);\n default:\n throw new EndpointResponseError(message, response);\n }\n }\n};\n\n/**\n * The `ConnectClient` constructor options.\n */\nexport interface ConnectClientOptions {\n /**\n * The `middlewares` property value.\n */\n middlewares?: Middleware[];\n /**\n * The `prefix` property value.\n */\n prefix?: string;\n /**\n * The Atmosphere options for the FluxConnection.\n */\n atmosphereOptions?: Partial<Atmosphere.Request>;\n}\n\nexport interface EndpointCallMetaInfo {\n /**\n * The endpoint name.\n */\n endpoint: string;\n\n /**\n * The method name to call on in the endpoint class.\n */\n method: string;\n\n /**\n * Optional object with method call arguments.\n */\n params?: Record<string, unknown>;\n}\n\n/**\n * An object with the call arguments and the related Request instance.\n * See also {@link ConnectClient.call | the call() method in ConnectClient}.\n */\nexport interface MiddlewareContext extends EndpointCallMetaInfo {\n /**\n * The Fetch API Request object reflecting the other properties.\n */\n request: Request;\n}\n\n/**\n * An async middleware callback that invokes the next middleware in the chain\n * or makes the actual request.\n * @param context - The information about the call and request\n */\nexport type MiddlewareNext = (context: MiddlewareContext) => MaybePromise<Response>;\n\n/**\n * An interface that allows defining a middleware as a class.\n */\nexport interface MiddlewareClass {\n /**\n * @param context - The information about the call and request\n * @param next - Invokes the next in the call chain\n */\n invoke(context: MiddlewareContext, next: MiddlewareNext): MaybePromise<Response>;\n}\n\n/**\n * An async callback function that can intercept the request and response\n * of a call.\n */\nexport type MiddlewareFunction = (context: MiddlewareContext, next: MiddlewareNext) => MaybePromise<Response>;\n\n/**\n * An async callback that can intercept the request and response\n * of a call, could be either a function or a class.\n */\nexport type Middleware = MiddlewareClass | MiddlewareFunction;\n\nfunction isFlowLoaded(): boolean {\n return $wnd.Vaadin?.Flow?.clients?.TypeScript !== undefined;\n}\n\n/**\n * A list of parameters supported by {@link ConnectClient.call | the call() method in ConnectClient}.\n */\nexport interface EndpointRequestInit {\n /**\n * An AbortSignal to set request's signal.\n */\n signal?: AbortSignal | null;\n}\n\n/**\n * A low-level network calling utility. It stores\n * a prefix and facilitates remote calls to endpoint class methods\n * on the Hilla backend.\n *\n * Example usage:\n *\n * ```js\n * const client = new ConnectClient();\n * const responseData = await client.call('MyEndpoint', 'myMethod');\n * ```\n *\n * ### Prefix\n *\n * The client supports an `prefix` constructor option:\n * ```js\n * const client = new ConnectClient({prefix: '/my-connect-prefix'});\n * ```\n *\n * The default prefix is '/connect'.\n *\n */\nexport class ConnectClient {\n /**\n * The array of middlewares that are invoked during a call.\n */\n middlewares: Middleware[] = [];\n /**\n * The Hilla endpoint prefix\n */\n prefix = '/connect';\n /**\n * The Atmosphere options for the FluxConnection.\n */\n atmosphereOptions: Partial<Atmosphere.Request> = {};\n\n #fluxConnection?: FluxConnection;\n\n /**\n * @param options - Constructor options.\n */\n constructor(options: ConnectClientOptions = {}) {\n if (options.prefix) {\n this.prefix = options.prefix;\n }\n\n if (options.middlewares) {\n this.middlewares = options.middlewares;\n }\n\n if (options.atmosphereOptions) {\n this.atmosphereOptions = options.atmosphereOptions;\n }\n\n // add connection indicator to DOM\n ConnectionIndicator.create();\n\n // Listen to browser online/offline events and update the loading indicator accordingly.\n // Note: if Flow.ts is loaded, it instead handles the state transitions.\n addEventListener('online', () => {\n if (!isFlowLoaded() && $wnd.Vaadin?.connectionState) {\n $wnd.Vaadin.connectionState.state = ConnectionState.CONNECTED;\n }\n });\n addEventListener('offline', () => {\n if (!isFlowLoaded() && $wnd.Vaadin?.connectionState) {\n $wnd.Vaadin.connectionState.state = ConnectionState.CONNECTION_LOST;\n }\n });\n }\n\n /**\n * Gets a representation of the underlying persistent network connection used for subscribing to Flux type endpoint\n * methods.\n */\n get fluxConnection(): FluxConnection {\n if (!this.#fluxConnection) {\n this.#fluxConnection = new FluxConnection(this.prefix, this.atmosphereOptions);\n }\n return this.#fluxConnection;\n }\n\n /**\n * Calls the given endpoint method defined using the endpoint and method\n * parameters with the parameters given as params.\n * Asynchronously returns the parsed JSON response data.\n *\n * @param endpoint - Endpoint name.\n * @param method - Method name to call in the endpoint class.\n * @param params - Optional parameters to pass to the method.\n * @param init - Optional parameters for the request\n * @returns Decoded JSON response data.\n */\n async call(\n endpoint: string,\n method: string,\n params?: Record<string, unknown>,\n init?: EndpointRequestInit,\n ): Promise<any> {\n if (arguments.length < 2) {\n throw new TypeError(`2 arguments required, but got only ${arguments.length}`);\n }\n\n const csrfHeaders = getCsrfTokenHeadersForEndpointRequest(document);\n const headers: Record<string, string> = {\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n ...csrfHeaders,\n };\n\n const request = new Request(`${this.prefix}/${endpoint}/${method}`, {\n body:\n params !== undefined ? JSON.stringify(params, (_, value) => (value === undefined ? null : value)) : undefined,\n headers,\n method: 'POST',\n });\n\n // The middleware `context`, includes the call arguments and the request\n // constructed from them\n const initialContext: MiddlewareContext = {\n endpoint,\n method,\n params,\n request,\n };\n\n // The internal middleware to assert and parse the response. The internal\n // response handling should come last after the other middlewares are done\n // with processing the response. That is why this middleware is first\n // in the final middlewares array.\n async function responseHandlerMiddleware(context: MiddlewareContext, next: MiddlewareNext): Promise<Response> {\n const response = await next(context);\n await assertResponseIsOk(response);\n const text = await response.text();\n return JSON.parse(text, (_, value: any) => (value === null ? undefined : value));\n }\n\n // The actual fetch call itself is expressed as a middleware\n // chain item for our convenience. Always having an ending of the chain\n // this way makes the folding down below more concise.\n async function fetchNext(context: MiddlewareContext) {\n $wnd.Vaadin?.connectionState?.loadingStarted();\n try {\n const response = await fetch(context.request, { signal: init?.signal });\n $wnd.Vaadin?.connectionState?.loadingFinished();\n return response;\n } catch (error: unknown) {\n // don't bother about connections aborted by purpose\n if (error instanceof Error && error.name === 'AbortError') {\n $wnd.Vaadin?.connectionState?.loadingFinished();\n } else {\n $wnd.Vaadin?.connectionState?.loadingFailed();\n }\n return Promise.reject(error);\n }\n }\n\n // Assemble the final middlewares array from internal\n // and external middlewares\n const middlewares = [responseHandlerMiddleware as Middleware, ...this.middlewares];\n\n // Fold the final middlewares array into a single function\n const chain = middlewares.reduceRight(\n (next: MiddlewareNext, middleware) =>\n // Compose and return the new chain step, that takes the context and\n // invokes the current middleware with the context and the further chain\n // as the next argument\n async (context) => {\n if (typeof middleware === 'function') {\n return middleware(context, next);\n }\n return middleware.invoke(context, next);\n },\n // Initialize reduceRight the accumulator with `fetchNext`\n fetchNext,\n );\n\n // Invoke all the folded async middlewares and return\n return chain(initialContext);\n }\n\n /**\n * Subscribes to the given method defined using the endpoint and method\n * parameters with the parameters given as params. The method must return a\n * compatible type such as a Flux.\n * Returns a subscription that is used to fetch values as they become available.\n *\n * @param endpoint - Endpoint name.\n * @param method - Method name to call in the endpoint class.\n * @param params - Optional parameters to pass to the method.\n * @returns A subscription used to handles values as they become available.\n */\n subscribe(endpoint: string, method: string, params?: any): Subscription<any> {\n return this.fluxConnection.subscribe(endpoint, method, params ? Object.values(params) : []);\n }\n}\n"],
5
+ "mappings": "AACA,SAAS,qBAAqB,uBAAuB;AACrD,SAAS,6CAA6C;AACtD;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP,SAAS,sBAAsB;AAG/B,MAAM,OAAO;AAEb,KAAK,WAAW,CAAC;AACjB,KAAK,OAAO,kBAAkB,CAAC;AAC/B,KAAK,OAAO,cAAc,KAAK;AAAA,EAC7B,IAAI;AACN,CAAC;AAqCD,MAAM,qBAAqB,OAAO,aAAsC;AACtE,MAAI,CAAC,SAAS,IAAI;AAChB,UAAM,YAAY,MAAM,SAAS,KAAK;AACtC,QAAI;AACJ,QAAI;AACF,kBAAY,KAAK,MAAM,SAAS;AAAA,IAClC,SAAS,SAAS;AAEhB,kBAAY;AAAA,IACd;AAEA,UAAM,UACJ,WAAW,YACV,UAAU,SAAS,IAChB,YACA,uCAAuC,SAAS,MAAM,IAAI,SAAS,UAAU;AACnF,UAAM,OAAO,WAAW;AAExB,QAAI,WAAW,qBAAqB;AAClC,YAAM,IAAI,wBAAwB,SAAS,UAAU,qBAAqB,IAAI;AAAA,IAChF;AAEA,QAAI,MAAM;AACR,YAAM,IAAI,cAAc,SAAS,MAAM,WAAW,MAAM;AAAA,IAC1D;AAEA,YAAQ,SAAS,QAAQ;AAAA,MACvB,KAAK;AACH,cAAM,IAAI,0BAA0B,SAAS,QAAQ;AAAA,MACvD,KAAK;AACH,cAAM,IAAI,uBAAuB,SAAS,QAAQ;AAAA,MACpD;AACE,cAAM,IAAI,sBAAsB,SAAS,QAAQ;AAAA,IACrD;AAAA,EACF;AACF;AA8EA,SAAS,eAAwB;AAC/B,SAAO,KAAK,QAAQ,MAAM,SAAS,eAAe;AACpD;AAkCO,MAAM,cAAc;AAAA;AAAA;AAAA;AAAA,E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cAAc;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAaA,UAAU,UAAkB,QAAgB,QAAiC;AAC3E,WAAO,KAAK,eAAe,UAAU,UAAU,QAAQ,SAAS,OAAO,OAAO,MAAM,IAAI,CAAC,CAAC;AAAA,EAC5F;AACF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,5 @@
1
+ /// <reference types="js-cookie" />
2
+ export declare function calculatePath({ pathname }: URL): string;
3
+ declare const CookieManager: Cookies.CookiesStatic;
4
+ export default CookieManager;
5
+ //# sourceMappingURL=CookieManager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CookieManager.d.ts","sourceRoot":"","sources":["src/CookieManager.ts"],"names":[],"mappings":";AAEA,wBAAgB,aAAa,CAAC,EAAE,QAAQ,EAAE,EAAE,GAAG,GAAG,MAAM,CAEvD;AAED,QAAA,MAAM,aAAa,EAAE,OAAO,CAAC,aAE3B,CAAC;AAEH,eAAe,aAAa,CAAC"}
@@ -0,0 +1,13 @@
1
+ import Cookies from "js-cookie";
2
+ function calculatePath({ pathname }) {
3
+ return pathname.length > 1 && pathname.endsWith("/") ? pathname.slice(0, -1) : pathname;
4
+ }
5
+ const CookieManager = Cookies.withAttributes({
6
+ path: calculatePath(new URL(document.baseURI))
7
+ });
8
+ var CookieManager_default = CookieManager;
9
+ export {
10
+ calculatePath,
11
+ CookieManager_default as default
12
+ };
13
+ //# sourceMappingURL=CookieManager.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["src/CookieManager.ts"],
4
+ "sourcesContent": ["import Cookies from 'js-cookie';\n\nexport function calculatePath({ pathname }: URL): string {\n return pathname.length > 1 && pathname.endsWith('/') ? pathname.slice(0, -1) : pathname;\n}\n\nconst CookieManager: Cookies.CookiesStatic = Cookies.withAttributes({\n path: calculatePath(new URL(document.baseURI)),\n});\n\nexport default CookieManager;\n"],
5
+ "mappings": "AAAA,OAAO,aAAa;AAEb,SAAS,cAAc,EAAE,SAAS,GAAgB;AACvD,SAAO,SAAS,SAAS,KAAK,SAAS,SAAS,GAAG,IAAI,SAAS,MAAM,GAAG,EAAE,IAAI;AACjF;AAEA,MAAM,gBAAuC,QAAQ,eAAe;AAAA,EAClE,MAAM,cAAc,IAAI,IAAI,SAAS,OAAO,CAAC;AAC/C,CAAC;AAED,IAAO,wBAAQ;",
6
+ "names": []
7
+ }