@nhost/nhost-js 3.2.7 → 5.0.0-beta.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/LICENSE +1 -1
- package/README.md +67 -37
- package/dist/nhost-js/auth.cjs.js +2 -0
- package/dist/nhost-js/auth.cjs.js.map +1 -0
- package/dist/nhost-js/auth.es.js +261 -0
- package/dist/nhost-js/auth.es.js.map +1 -0
- package/dist/nhost-js/fetch.cjs.js +2 -0
- package/dist/nhost-js/fetch.cjs.js.map +1 -0
- package/dist/nhost-js/fetch.es.js +10 -0
- package/dist/nhost-js/fetch.es.js.map +1 -0
- package/dist/nhost-js/graphql.cjs.js +2 -0
- package/dist/nhost-js/graphql.cjs.js.map +1 -0
- package/dist/nhost-js/graphql.es.js +35 -0
- package/dist/nhost-js/graphql.es.js.map +1 -0
- package/dist/nhost-js/storage.cjs.js +2 -0
- package/dist/nhost-js/storage.cjs.js.map +1 -0
- package/dist/nhost-js/storage.es.js +205 -0
- package/dist/nhost-js/storage.es.js.map +1 -0
- package/dist/nhost-js.cjs.js +2 -0
- package/dist/nhost-js.cjs.js.map +1 -0
- package/dist/nhost-js.es.js +476 -0
- package/dist/nhost-js.es.js.map +1 -0
- package/dist/nhost-js.umd.js +2 -0
- package/dist/nhost-js.umd.js.map +1 -0
- package/dist/src/auth/client.d.ts +415 -0
- package/dist/src/auth/client.d.ts.map +1 -0
- package/dist/src/auth/client.js +330 -0
- package/dist/src/auth/client.js.map +1 -0
- package/dist/src/auth/index.d.ts +11 -0
- package/dist/src/auth/index.d.ts.map +1 -0
- package/dist/src/auth/index.js +11 -0
- package/dist/src/auth/index.js.map +1 -0
- package/dist/src/auth/interface.d.ts +72 -0
- package/dist/src/auth/interface.d.ts.map +1 -0
- package/dist/src/auth/interface.js +2 -0
- package/dist/src/auth/interface.js.map +1 -0
- package/dist/src/fetch/index.d.ts +53 -0
- package/dist/src/fetch/index.d.ts.map +1 -0
- package/dist/src/fetch/index.js +40 -0
- package/dist/src/fetch/index.js.map +1 -0
- package/dist/src/graphql/client.d.ts +89 -0
- package/dist/src/graphql/client.d.ts.map +1 -0
- package/dist/src/graphql/client.js +49 -0
- package/dist/src/graphql/client.js.map +1 -0
- package/dist/src/graphql/index.d.ts +10 -0
- package/dist/src/graphql/index.d.ts.map +1 -0
- package/dist/src/graphql/index.js +10 -0
- package/dist/src/graphql/index.js.map +1 -0
- package/dist/src/index.d.ts +169 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +237 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/middlewareAttachToken.d.ts +24 -0
- package/dist/src/middlewareAttachToken.d.ts.map +1 -0
- package/dist/src/middlewareAttachToken.js +60 -0
- package/dist/src/middlewareAttachToken.js.map +1 -0
- package/dist/src/middlewareRefreshSession.d.ts +43 -0
- package/dist/src/middlewareRefreshSession.d.ts.map +1 -0
- package/dist/src/middlewareRefreshSession.js +190 -0
- package/dist/src/middlewareRefreshSession.js.map +1 -0
- package/dist/src/middlewareResponseSession.d.ts +26 -0
- package/dist/src/middlewareResponseSession.d.ts.map +1 -0
- package/dist/src/middlewareResponseSession.js +83 -0
- package/dist/src/middlewareResponseSession.js.map +1 -0
- package/dist/src/sessionStorage.d.ts +123 -0
- package/dist/src/sessionStorage.d.ts.map +1 -0
- package/dist/src/sessionStorage.js +165 -0
- package/dist/src/sessionStorage.js.map +1 -0
- package/dist/src/storage/client.d.ts +184 -0
- package/dist/src/storage/client.d.ts.map +1 -0
- package/dist/src/storage/client.js +249 -0
- package/dist/src/storage/client.js.map +1 -0
- package/dist/src/storage/index.d.ts +11 -0
- package/dist/src/storage/index.d.ts.map +1 -0
- package/dist/src/storage/index.js +11 -0
- package/dist/src/storage/index.js.map +1 -0
- package/dist/src/storage/interface.d.ts +52 -0
- package/dist/src/storage/interface.d.ts.map +1 -0
- package/dist/src/storage/interface.js +2 -0
- package/dist/src/storage/interface.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +94 -60
- package/dist/clients/auth.d.ts +0 -8
- package/dist/clients/auth.d.ts.map +0 -1
- package/dist/clients/functions/index.d.ts +0 -107
- package/dist/clients/functions/index.d.ts.map +0 -1
- package/dist/clients/functions/types.d.ts +0 -28
- package/dist/clients/functions/types.d.ts.map +0 -1
- package/dist/clients/graphql.d.ts +0 -8
- package/dist/clients/graphql.d.ts.map +0 -1
- package/dist/clients/index.d.ts +0 -6
- package/dist/clients/index.d.ts.map +0 -1
- package/dist/clients/nhost.d.ts +0 -83
- package/dist/clients/nhost.d.ts.map +0 -1
- package/dist/clients/storage.d.ts +0 -8
- package/dist/clients/storage.d.ts.map +0 -1
- package/dist/index.cjs.js +0 -2
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.d.ts +0 -6
- package/dist/index.d.ts.map +0 -1
- package/dist/index.esm.js +0 -344
- package/dist/index.esm.js.map +0 -1
- package/dist/utils/helpers.d.ts +0 -20
- package/dist/utils/helpers.d.ts.map +0 -1
- package/dist/utils/types.d.ts +0 -57
- package/dist/utils/types.d.ts.map +0 -1
- package/umd/nhost-js.umd.js +0 -22
- package/umd/nhost-js.umd.js.map +0 -1
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview GraphQL client for the Nhost JavaScript SDK.
|
|
3
|
+
*
|
|
4
|
+
* This module provides functionality for executing GraphQL operations against
|
|
5
|
+
* a Hasura GraphQL API, with automatic authentication token handling.
|
|
6
|
+
*/
|
|
7
|
+
import type { ChainFunction } from "../fetch";
|
|
8
|
+
/**
|
|
9
|
+
* Variables object for GraphQL operations.
|
|
10
|
+
* Key-value pairs of variable names and their values.
|
|
11
|
+
*/
|
|
12
|
+
export interface GraphQLVariables {
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* GraphQL request object used for queries and mutations.
|
|
17
|
+
*/
|
|
18
|
+
export interface GraphQLRequest {
|
|
19
|
+
/** The GraphQL query or mutation string */
|
|
20
|
+
query: string;
|
|
21
|
+
/** Optional variables for parameterized queries */
|
|
22
|
+
variables?: GraphQLVariables;
|
|
23
|
+
/** Optional name of the operation to execute */
|
|
24
|
+
operationName?: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Represents a GraphQL error returned from the server.
|
|
28
|
+
*/
|
|
29
|
+
export interface GraphQLError {
|
|
30
|
+
/** Error message */
|
|
31
|
+
message: string;
|
|
32
|
+
/** Source locations in the GraphQL document where the error occurred */
|
|
33
|
+
locations?: {
|
|
34
|
+
line: number;
|
|
35
|
+
column: number;
|
|
36
|
+
}[];
|
|
37
|
+
/** Path in the query where the error occurred */
|
|
38
|
+
path?: string[];
|
|
39
|
+
/** Additional error information specific to the GraphQL implementation */
|
|
40
|
+
extensions?: any;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Standard GraphQL response format as defined by the GraphQL specification.
|
|
44
|
+
*/
|
|
45
|
+
export interface GraphQLResponse<T = any> {
|
|
46
|
+
/** The data returned from successful execution */
|
|
47
|
+
data?: T;
|
|
48
|
+
/** Array of errors if execution was unsuccessful or partially successful */
|
|
49
|
+
errors?: GraphQLError[];
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Response wrapper for GraphQL operations with additional metadata.
|
|
53
|
+
*/
|
|
54
|
+
export interface FetchResponse<T = any> {
|
|
55
|
+
/** The parsed response body */
|
|
56
|
+
body: T;
|
|
57
|
+
/** HTTP status code of the response */
|
|
58
|
+
status: number;
|
|
59
|
+
/** HTTP headers from the response */
|
|
60
|
+
headers: Headers;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* GraphQL client interface providing methods for executing queries and mutations
|
|
64
|
+
*/
|
|
65
|
+
export interface Client {
|
|
66
|
+
/**
|
|
67
|
+
* Execute a GraphQL query operation
|
|
68
|
+
*
|
|
69
|
+
* Queries are used to fetch data and should not modify any data on the server.
|
|
70
|
+
*
|
|
71
|
+
* @param request - GraphQL request object containing query and optional variables
|
|
72
|
+
* @param options - Additional fetch options to apply to the request
|
|
73
|
+
* @returns Promise with the GraphQL response and metadata
|
|
74
|
+
*/
|
|
75
|
+
post: (request: GraphQLRequest, options?: RequestInit) => Promise<FetchResponse<GraphQLResponse>>;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Creates a GraphQL API client for interacting with a GraphQL endpoint.
|
|
79
|
+
*
|
|
80
|
+
* This client provides methods for executing queries and mutations against
|
|
81
|
+
* a GraphQL API, with support for middleware functions to handle authentication,
|
|
82
|
+
* error handling, and other cross-cutting concerns.
|
|
83
|
+
*
|
|
84
|
+
* @param baseURL - Base URL for the GraphQL endpoint
|
|
85
|
+
* @param chainFunctions - Array of middleware functions for the fetch chain
|
|
86
|
+
* @returns GraphQL client with query and mutation methods
|
|
87
|
+
*/
|
|
88
|
+
export declare const createAPIClient: (baseURL: string, chainFunctions?: ChainFunction[]) => Client;
|
|
89
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../src/graphql/client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAE9C;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,2CAA2C;IAC3C,KAAK,EAAE,MAAM,CAAC;IACd,mDAAmD;IACnD,SAAS,CAAC,EAAE,gBAAgB,CAAC;IAC7B,gDAAgD;IAChD,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,oBAAoB;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,wEAAwE;IACxE,SAAS,CAAC,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IAC/C,iDAAiD;IACjD,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,0EAA0E;IAC1E,UAAU,CAAC,EAAE,GAAG,CAAC;CAClB;AAED;;GAEG;AAEH,MAAM,WAAW,eAAe,CAAC,CAAC,GAAG,GAAG;IACtC,kDAAkD;IAClD,IAAI,CAAC,EAAE,CAAC,CAAC;IACT,4EAA4E;IAC5E,MAAM,CAAC,EAAE,YAAY,EAAE,CAAC;CACzB;AAED;;GAEG;AAEH,MAAM,WAAW,aAAa,CAAC,CAAC,GAAG,GAAG;IACpC,+BAA+B;IAC/B,IAAI,EAAE,CAAC,CAAC;IACR,uCAAuC;IACvC,MAAM,EAAE,MAAM,CAAC;IACf,qCAAqC;IACrC,OAAO,EAAE,OAAO,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,MAAM;IACrB;;;;;;;;OAQG;IACH,IAAI,EAAE,CACJ,OAAO,EAAE,cAAc,EACvB,OAAO,CAAC,EAAE,WAAW,KAClB,OAAO,CAAC,aAAa,CAAC,eAAe,CAAC,CAAC,CAAC;CAC9C;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,eAAe,GAC1B,SAAS,MAAM,EACf,iBAAgB,aAAa,EAAO,KA0C/B,MACN,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview GraphQL client for the Nhost JavaScript SDK.
|
|
3
|
+
*
|
|
4
|
+
* This module provides functionality for executing GraphQL operations against
|
|
5
|
+
* a Hasura GraphQL API, with automatic authentication token handling.
|
|
6
|
+
*/
|
|
7
|
+
import { createEnhancedFetch } from "../fetch";
|
|
8
|
+
/**
|
|
9
|
+
* Creates a GraphQL API client for interacting with a GraphQL endpoint.
|
|
10
|
+
*
|
|
11
|
+
* This client provides methods for executing queries and mutations against
|
|
12
|
+
* a GraphQL API, with support for middleware functions to handle authentication,
|
|
13
|
+
* error handling, and other cross-cutting concerns.
|
|
14
|
+
*
|
|
15
|
+
* @param baseURL - Base URL for the GraphQL endpoint
|
|
16
|
+
* @param chainFunctions - Array of middleware functions for the fetch chain
|
|
17
|
+
* @returns GraphQL client with query and mutation methods
|
|
18
|
+
*/
|
|
19
|
+
export const createAPIClient = (baseURL, chainFunctions = []) => {
|
|
20
|
+
const enhancedFetch = createEnhancedFetch(chainFunctions);
|
|
21
|
+
const executeOperation = async (request, options) => {
|
|
22
|
+
const response = await enhancedFetch(`${baseURL}`, {
|
|
23
|
+
method: "POST",
|
|
24
|
+
headers: {
|
|
25
|
+
"Content-Type": "application/json",
|
|
26
|
+
},
|
|
27
|
+
body: JSON.stringify(request),
|
|
28
|
+
...options,
|
|
29
|
+
});
|
|
30
|
+
const body = await response.text();
|
|
31
|
+
const data = body ? JSON.parse(body) : {};
|
|
32
|
+
const resp = {
|
|
33
|
+
body: data,
|
|
34
|
+
status: response.status,
|
|
35
|
+
headers: response.headers,
|
|
36
|
+
};
|
|
37
|
+
if (data.errors) {
|
|
38
|
+
throw resp;
|
|
39
|
+
}
|
|
40
|
+
return resp;
|
|
41
|
+
};
|
|
42
|
+
const post = (request, options) => {
|
|
43
|
+
return executeOperation(request, options);
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
post,
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/graphql/client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAgF/C;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,OAAe,EACf,iBAAkC,EAAE,EACpC,EAAE;IACF,MAAM,aAAa,GAAG,mBAAmB,CAAC,cAAc,CAAC,CAAC;IAE1D,MAAM,gBAAgB,GAAG,KAAK,EAC5B,OAAuB,EACvB,OAAqB,EACoB,EAAE;QAC3C,MAAM,QAAQ,GAAG,MAAM,aAAa,CAAC,GAAG,OAAO,EAAE,EAAE;YACjD,MAAM,EAAE,MAAM;YACd,OAAO,EAAE;gBACP,cAAc,EAAE,kBAAkB;aACnC;YACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC;YAC7B,GAAG,OAAO;SACX,CAAC,CAAC;QAEH,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QACnC,MAAM,IAAI,GAAoB,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAE3D,MAAM,IAAI,GAAG;YACX,IAAI,EAAE,IAAI;YACV,MAAM,EAAE,QAAQ,CAAC,MAAM;YACvB,OAAO,EAAE,QAAQ,CAAC,OAAO;SAC1B,CAAC;QAEF,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YAChB,MAAM,IAAI,CAAC;QACb,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IAEF,MAAM,IAAI,GAAG,CACX,OAAuB,EACvB,OAAqB,EACoB,EAAE;QAC3C,OAAO,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC5C,CAAC,CAAC;IAEF,OAAO;QACL,IAAI;KACK,CAAC;AACd,CAAC,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is the main module to interact with Nhost's GraphQL service.
|
|
3
|
+
* Typically you would use this modula via the main [Nhost client](main#createclient)
|
|
4
|
+
* but you can also use it directly if you have a specific use case.
|
|
5
|
+
*
|
|
6
|
+
* @module graphql
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
export * from "./client";
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/graphql/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,cAAc,UAAU,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is the main module to interact with Nhost's GraphQL service.
|
|
3
|
+
* Typically you would use this modula via the main [Nhost client](main#createclient)
|
|
4
|
+
* but you can also use it directly if you have a specific use case.
|
|
5
|
+
*
|
|
6
|
+
* @module graphql
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
export * from "./client";
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/graphql/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,cAAc,UAAU,CAAC"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Main entry point for the Nhost JavaScript SDK.
|
|
3
|
+
*
|
|
4
|
+
* This package provides a unified client for interacting with Nhost services:
|
|
5
|
+
* - Authentication
|
|
6
|
+
* - Storage
|
|
7
|
+
* - GraphQL
|
|
8
|
+
* - Functions
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* You can import and use this package with:
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { createClient } from "@nhost/nhost-js";
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* and use it like:
|
|
18
|
+
*
|
|
19
|
+
* {@includeCode ./__tests__/docstrings.test.ts#mainExample}
|
|
20
|
+
*
|
|
21
|
+
* ## Error handling
|
|
22
|
+
*
|
|
23
|
+
* The SDK will throw errors on most operations if the request returns a non-2xx status code or
|
|
24
|
+
* if the request fails entirely (i.e., due to network errors). A continuation you can see
|
|
25
|
+
* how you can handle errors thrown by the SDK.
|
|
26
|
+
*
|
|
27
|
+
* ### Auth
|
|
28
|
+
*
|
|
29
|
+
* {@includeCode ./__tests__/docstrings-auth.test.ts#errorHandling}
|
|
30
|
+
*
|
|
31
|
+
* ### Storage
|
|
32
|
+
*
|
|
33
|
+
* {@includeCode ./__tests__/docstrings-storage.test.ts#errorHandling}
|
|
34
|
+
*
|
|
35
|
+
* ### GraphQL
|
|
36
|
+
*
|
|
37
|
+
* {@includeCode ./__tests__/docstrings-graphql.test.ts#errorHandling}
|
|
38
|
+
*
|
|
39
|
+
* @packageDocumentation
|
|
40
|
+
*/
|
|
41
|
+
import { createAPIClient as createAuthClient, type Session } from "./auth";
|
|
42
|
+
import { createAPIClient as createStorageClient } from "./storage";
|
|
43
|
+
import { createAPIClient as createGraphQLClient } from "./graphql";
|
|
44
|
+
import { type SessionStorageInterface } from "./sessionStorage";
|
|
45
|
+
export { type SessionStorageInterface, DEFAULT_SESSION_KEY, LocalStorage, MemoryStorage, CookieStorage, } from "./sessionStorage";
|
|
46
|
+
export { type SessionRefreshOptions, createSessionRefreshMiddleware, } from "./middlewareRefreshSession";
|
|
47
|
+
export { createAttachAccessTokenMiddleware } from "./middlewareAttachToken";
|
|
48
|
+
export { createSessionResponseMiddleware } from "./middlewareResponseSession";
|
|
49
|
+
/**
|
|
50
|
+
* Generates a base URL for a Nhost service based on configuration
|
|
51
|
+
*
|
|
52
|
+
* @param serviceType - Type of service (auth, storage, graphql, functions)
|
|
53
|
+
* @param subdomain - Nhost project subdomain
|
|
54
|
+
* @param region - Nhost region
|
|
55
|
+
* @param customUrl - Custom URL override if provided
|
|
56
|
+
* @returns The base URL for the service
|
|
57
|
+
*
|
|
58
|
+
* @internal
|
|
59
|
+
*/
|
|
60
|
+
export declare const generateServiceUrl: (serviceType: "auth" | "storage" | "graphql" | "functions", subdomain?: string, region?: string, customUrl?: string) => string;
|
|
61
|
+
/**
|
|
62
|
+
* Configuration options for creating an Nhost client
|
|
63
|
+
*/
|
|
64
|
+
export interface NhostClientOptions {
|
|
65
|
+
/**
|
|
66
|
+
* Nhost project subdomain (e.g., 'abcdefgh'). Used to construct the base URL for services for the Nhost cloud.
|
|
67
|
+
*/
|
|
68
|
+
subdomain?: string;
|
|
69
|
+
/**
|
|
70
|
+
* Nhost region (e.g., 'eu-central-1'). Used to construct the base URL for services for the Nhost cloud.
|
|
71
|
+
*/
|
|
72
|
+
region?: string;
|
|
73
|
+
/**
|
|
74
|
+
* Complete base URL for the auth service (overrides subdomain/region)
|
|
75
|
+
*/
|
|
76
|
+
authUrl?: string;
|
|
77
|
+
/**
|
|
78
|
+
* Complete base URL for the storage service (overrides subdomain/region)
|
|
79
|
+
*/
|
|
80
|
+
storageUrl?: string;
|
|
81
|
+
/**
|
|
82
|
+
* Complete base URL for the GraphQL service (overrides subdomain/region)
|
|
83
|
+
*/
|
|
84
|
+
graphqlUrl?: string;
|
|
85
|
+
/**
|
|
86
|
+
* Storage implementation to use for session persistence. If not provided, the SDK will
|
|
87
|
+
* default to localStorage in the browser or memory in other environments.
|
|
88
|
+
*/
|
|
89
|
+
storage?: SessionStorageInterface;
|
|
90
|
+
/**
|
|
91
|
+
* Disable automatic session refresh. If set to true, the SDK will not attempt to refresh
|
|
92
|
+
*/
|
|
93
|
+
disableAutoRefreshToken?: boolean;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Main client class that provides access to all Nhost services
|
|
97
|
+
*/
|
|
98
|
+
export declare class NhostClient {
|
|
99
|
+
/**
|
|
100
|
+
* Authentication client providing methods for user sign-in, sign-up, and session management
|
|
101
|
+
*/
|
|
102
|
+
auth: ReturnType<typeof createAuthClient>;
|
|
103
|
+
/**
|
|
104
|
+
* Storage client providing methods for file operations (upload, download, delete)
|
|
105
|
+
*/
|
|
106
|
+
storage: ReturnType<typeof createStorageClient>;
|
|
107
|
+
/**
|
|
108
|
+
* GraphQL client providing methods for executing GraphQL operations against your Hasura backend
|
|
109
|
+
*/
|
|
110
|
+
graphql: ReturnType<typeof createGraphQLClient>;
|
|
111
|
+
/**
|
|
112
|
+
* Storage implementation used for persisting session information
|
|
113
|
+
*/
|
|
114
|
+
sessionStorage: SessionStorageInterface;
|
|
115
|
+
/**
|
|
116
|
+
* Create a new Nhost client. This constructor is reserved for advanced use cases.
|
|
117
|
+
* For typical usage, use [createClient](#createclient) instead.
|
|
118
|
+
*
|
|
119
|
+
* @param auth - Authentication client
|
|
120
|
+
* @param storage - Storage client
|
|
121
|
+
* @param graphql - GraphQL client
|
|
122
|
+
* @param sessionStorage - Storage implementation for session persistence
|
|
123
|
+
*/
|
|
124
|
+
constructor(auth: ReturnType<typeof createAuthClient>, storage: ReturnType<typeof createStorageClient>, graphql: ReturnType<typeof createGraphQLClient>, sessionStorage: SessionStorageInterface);
|
|
125
|
+
/**
|
|
126
|
+
* Get the current session from storage
|
|
127
|
+
*
|
|
128
|
+
* @returns The current session or null if no session exists
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```ts
|
|
132
|
+
* const session = nhost.getUserSession();
|
|
133
|
+
* if (session) {
|
|
134
|
+
* console.log('User is authenticated:', session.user.id);
|
|
135
|
+
* } else {
|
|
136
|
+
* console.log('No active session');
|
|
137
|
+
* }
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
getUserSession(): Session | null;
|
|
141
|
+
/**
|
|
142
|
+
* Refresh the session using the current refresh token
|
|
143
|
+
* in the storage and update the storage with the new session.
|
|
144
|
+
* @param marginSeconds - The number of seconds before the token expiration to refresh the session. If the token is still valid for this duration, it will not be refreshed. Set to 0 to force the refresh.
|
|
145
|
+
*
|
|
146
|
+
* @returns The new session or null if there is currently no session or if refresh fails
|
|
147
|
+
*/
|
|
148
|
+
refreshSession(marginSeconds?: number): Promise<Session | null>;
|
|
149
|
+
private _refreshSession;
|
|
150
|
+
/**
|
|
151
|
+
* Clear the session from storage
|
|
152
|
+
*/
|
|
153
|
+
clearSession(): Promise<void>;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Creates and configures a new Nhost client instance.
|
|
157
|
+
*
|
|
158
|
+
* This helper method instantiates a fully configured Nhost client by:
|
|
159
|
+
* - Instantiating the various service clients (auth, storage, functions and graphql)
|
|
160
|
+
* - Configuring a session storage if none is provided
|
|
161
|
+
* - Setting up the necessary middleware for automatic session management:
|
|
162
|
+
* - Automatically attaching the authorization token to requests
|
|
163
|
+
* - Refreshing the session when it expires
|
|
164
|
+
*
|
|
165
|
+
* @param options - Configuration options for the client
|
|
166
|
+
* @returns A configured Nhost client
|
|
167
|
+
*/
|
|
168
|
+
export declare function createClient(options?: NhostClientOptions): NhostClient;
|
|
169
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH,OAAO,EACL,eAAe,IAAI,gBAAgB,EACnC,KAAK,OAAO,EAGb,MAAM,QAAQ,CAAC;AAChB,OAAO,EAAE,eAAe,IAAI,mBAAmB,EAAE,MAAM,WAAW,CAAC;AACnE,OAAO,EAAE,eAAe,IAAI,mBAAmB,EAAE,MAAM,WAAW,CAAC;AACnE,OAAO,EAAE,KAAK,uBAAuB,EAAiB,MAAM,kBAAkB,CAAC;AAQ/E,OAAO,EACL,KAAK,uBAAuB,EAC5B,mBAAmB,EACnB,YAAY,EACZ,aAAa,EACb,aAAa,GACd,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EACL,KAAK,qBAAqB,EAC1B,8BAA8B,GAC/B,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,iCAAiC,EAAE,MAAM,yBAAyB,CAAC;AAC5E,OAAO,EAAE,+BAA+B,EAAE,MAAM,6BAA6B,CAAC;AAE9E;;;;;;;;;;GAUG;AACH,eAAO,MAAM,kBAAkB,GAC7B,aAAa,MAAM,GAAG,SAAS,GAAG,SAAS,GAAG,WAAW,EACzD,YAAY,MAAM,EAClB,SAAS,MAAM,EACf,YAAY,MAAM,KACjB,M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0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Main entry point for the Nhost JavaScript SDK.
|
|
3
|
+
*
|
|
4
|
+
* This package provides a unified client for interacting with Nhost services:
|
|
5
|
+
* - Authentication
|
|
6
|
+
* - Storage
|
|
7
|
+
* - GraphQL
|
|
8
|
+
* - Functions
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* You can import and use this package with:
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { createClient } from "@nhost/nhost-js";
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* and use it like:
|
|
18
|
+
*
|
|
19
|
+
* {@includeCode ./__tests__/docstrings.test.ts#mainExample}
|
|
20
|
+
*
|
|
21
|
+
* ## Error handling
|
|
22
|
+
*
|
|
23
|
+
* The SDK will throw errors on most operations if the request returns a non-2xx status code or
|
|
24
|
+
* if the request fails entirely (i.e., due to network errors). A continuation you can see
|
|
25
|
+
* how you can handle errors thrown by the SDK.
|
|
26
|
+
*
|
|
27
|
+
* ### Auth
|
|
28
|
+
*
|
|
29
|
+
* {@includeCode ./__tests__/docstrings-auth.test.ts#errorHandling}
|
|
30
|
+
*
|
|
31
|
+
* ### Storage
|
|
32
|
+
*
|
|
33
|
+
* {@includeCode ./__tests__/docstrings-storage.test.ts#errorHandling}
|
|
34
|
+
*
|
|
35
|
+
* ### GraphQL
|
|
36
|
+
*
|
|
37
|
+
* {@includeCode ./__tests__/docstrings-graphql.test.ts#errorHandling}
|
|
38
|
+
*
|
|
39
|
+
* @packageDocumentation
|
|
40
|
+
*/
|
|
41
|
+
import { createAPIClient as createAuthClient, } from "./auth";
|
|
42
|
+
import { createAPIClient as createStorageClient } from "./storage";
|
|
43
|
+
import { createAPIClient as createGraphQLClient } from "./graphql";
|
|
44
|
+
import { detectStorage } from "./sessionStorage";
|
|
45
|
+
import { extractTokenExpiration, createSessionRefreshMiddleware, } from "./middlewareRefreshSession";
|
|
46
|
+
import { createAttachAccessTokenMiddleware } from "./middlewareAttachToken";
|
|
47
|
+
import { createSessionResponseMiddleware } from "./middlewareResponseSession";
|
|
48
|
+
export { DEFAULT_SESSION_KEY, LocalStorage, MemoryStorage, CookieStorage, } from "./sessionStorage";
|
|
49
|
+
export { createSessionRefreshMiddleware, } from "./middlewareRefreshSession";
|
|
50
|
+
export { createAttachAccessTokenMiddleware } from "./middlewareAttachToken";
|
|
51
|
+
export { createSessionResponseMiddleware } from "./middlewareResponseSession";
|
|
52
|
+
/**
|
|
53
|
+
* Generates a base URL for a Nhost service based on configuration
|
|
54
|
+
*
|
|
55
|
+
* @param serviceType - Type of service (auth, storage, graphql, functions)
|
|
56
|
+
* @param subdomain - Nhost project subdomain
|
|
57
|
+
* @param region - Nhost region
|
|
58
|
+
* @param customUrl - Custom URL override if provided
|
|
59
|
+
* @returns The base URL for the service
|
|
60
|
+
*
|
|
61
|
+
* @internal
|
|
62
|
+
*/
|
|
63
|
+
export const generateServiceUrl = (serviceType, subdomain, region, customUrl) => {
|
|
64
|
+
if (customUrl) {
|
|
65
|
+
return customUrl;
|
|
66
|
+
}
|
|
67
|
+
else if (subdomain && region) {
|
|
68
|
+
return `https://${subdomain}.${serviceType}.${region}.nhost.run/v1`;
|
|
69
|
+
}
|
|
70
|
+
else if (subdomain) {
|
|
71
|
+
return `https://${subdomain}.${serviceType}.nhost.run/v1`;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
return `https://local.${serviceType}.local.nhost.run/v1`;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Main client class that provides access to all Nhost services
|
|
79
|
+
*/
|
|
80
|
+
export class NhostClient {
|
|
81
|
+
/**
|
|
82
|
+
* Authentication client providing methods for user sign-in, sign-up, and session management
|
|
83
|
+
*/
|
|
84
|
+
auth;
|
|
85
|
+
/**
|
|
86
|
+
* Storage client providing methods for file operations (upload, download, delete)
|
|
87
|
+
*/
|
|
88
|
+
storage;
|
|
89
|
+
/**
|
|
90
|
+
* GraphQL client providing methods for executing GraphQL operations against your Hasura backend
|
|
91
|
+
*/
|
|
92
|
+
graphql;
|
|
93
|
+
/**
|
|
94
|
+
* Storage implementation used for persisting session information
|
|
95
|
+
*/
|
|
96
|
+
sessionStorage;
|
|
97
|
+
/**
|
|
98
|
+
* Create a new Nhost client. This constructor is reserved for advanced use cases.
|
|
99
|
+
* For typical usage, use [createClient](#createclient) instead.
|
|
100
|
+
*
|
|
101
|
+
* @param auth - Authentication client
|
|
102
|
+
* @param storage - Storage client
|
|
103
|
+
* @param graphql - GraphQL client
|
|
104
|
+
* @param sessionStorage - Storage implementation for session persistence
|
|
105
|
+
*/
|
|
106
|
+
constructor(auth, storage, graphql, sessionStorage) {
|
|
107
|
+
this.auth = auth;
|
|
108
|
+
this.storage = storage;
|
|
109
|
+
this.graphql = graphql;
|
|
110
|
+
this.sessionStorage = sessionStorage;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Get the current session from storage
|
|
114
|
+
*
|
|
115
|
+
* @returns The current session or null if no session exists
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* const session = nhost.getUserSession();
|
|
120
|
+
* if (session) {
|
|
121
|
+
* console.log('User is authenticated:', session.user.id);
|
|
122
|
+
* } else {
|
|
123
|
+
* console.log('No active session');
|
|
124
|
+
* }
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
getUserSession() {
|
|
128
|
+
return this.sessionStorage.get();
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Refresh the session using the current refresh token
|
|
132
|
+
* in the storage and update the storage with the new session.
|
|
133
|
+
* @param marginSeconds - The number of seconds before the token expiration to refresh the session. If the token is still valid for this duration, it will not be refreshed. Set to 0 to force the refresh.
|
|
134
|
+
*
|
|
135
|
+
* @returns The new session or null if there is currently no session or if refresh fails
|
|
136
|
+
*/
|
|
137
|
+
async refreshSession(marginSeconds = 60) {
|
|
138
|
+
try {
|
|
139
|
+
return await this._refreshSession(marginSeconds);
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
try {
|
|
143
|
+
// we retry the refresh token in case of transient error
|
|
144
|
+
// or race conditions
|
|
145
|
+
console.warn("error refreshing session, retrying:", error);
|
|
146
|
+
return await this._refreshSession(marginSeconds);
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
const errResponse = error;
|
|
150
|
+
if (errResponse?.status === 401) {
|
|
151
|
+
// this probably means the refresh token is invalid
|
|
152
|
+
console.error("session probably expired");
|
|
153
|
+
this.sessionStorage.remove();
|
|
154
|
+
}
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
async _refreshSession(marginSeconds = 60) {
|
|
160
|
+
const session = this.sessionStorage.get();
|
|
161
|
+
if (!session) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
const tokenExpiresAt = extractTokenExpiration(session?.accessToken || "");
|
|
165
|
+
const currentTime = Date.now();
|
|
166
|
+
const sessionExpired = tokenExpiresAt < currentTime;
|
|
167
|
+
if (tokenExpiresAt - currentTime > marginSeconds * 1000) {
|
|
168
|
+
return session; // No need to refresh
|
|
169
|
+
}
|
|
170
|
+
try {
|
|
171
|
+
const response = await this.auth.refreshToken({
|
|
172
|
+
refreshToken: session.refreshToken,
|
|
173
|
+
});
|
|
174
|
+
this.sessionStorage.set(response.body);
|
|
175
|
+
return response.body;
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
if (!sessionExpired) {
|
|
179
|
+
// If the session is not expired, we can still use the current session
|
|
180
|
+
// so there is no need to error for now
|
|
181
|
+
return session;
|
|
182
|
+
}
|
|
183
|
+
// we throw the error so the caller can handle it
|
|
184
|
+
throw error;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Clear the session from storage
|
|
189
|
+
*/
|
|
190
|
+
async clearSession() {
|
|
191
|
+
this.sessionStorage.remove();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
function getMiddlewareChain(auth, storage, autoRefresh) {
|
|
195
|
+
const mwChain = [
|
|
196
|
+
createSessionResponseMiddleware(storage),
|
|
197
|
+
createAttachAccessTokenMiddleware(storage),
|
|
198
|
+
];
|
|
199
|
+
if (autoRefresh) {
|
|
200
|
+
// we need to process this one first to make sure any following middlewares
|
|
201
|
+
// run after the session has been refreshed
|
|
202
|
+
mwChain.unshift(createSessionRefreshMiddleware(auth, storage));
|
|
203
|
+
}
|
|
204
|
+
return mwChain;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Creates and configures a new Nhost client instance.
|
|
208
|
+
*
|
|
209
|
+
* This helper method instantiates a fully configured Nhost client by:
|
|
210
|
+
* - Instantiating the various service clients (auth, storage, functions and graphql)
|
|
211
|
+
* - Configuring a session storage if none is provided
|
|
212
|
+
* - Setting up the necessary middleware for automatic session management:
|
|
213
|
+
* - Automatically attaching the authorization token to requests
|
|
214
|
+
* - Refreshing the session when it expires
|
|
215
|
+
*
|
|
216
|
+
* @param options - Configuration options for the client
|
|
217
|
+
* @returns A configured Nhost client
|
|
218
|
+
*/
|
|
219
|
+
export function createClient(options = {}) {
|
|
220
|
+
const { subdomain, region, authUrl, storageUrl, graphqlUrl, storage = detectStorage(), disableAutoRefreshToken = false, } = options;
|
|
221
|
+
// Determine base URLs for each service
|
|
222
|
+
const authBaseUrl = generateServiceUrl("auth", subdomain, region, authUrl);
|
|
223
|
+
const storageBaseUrl = generateServiceUrl("storage", subdomain, region, storageUrl);
|
|
224
|
+
const graphqlBaseUrl = generateServiceUrl("graphql", subdomain, region, graphqlUrl);
|
|
225
|
+
// Create auth client
|
|
226
|
+
const auth = createAuthClient(authBaseUrl);
|
|
227
|
+
const mwChain = getMiddlewareChain(auth, storage, !disableAutoRefreshToken);
|
|
228
|
+
for (const mw of mwChain) {
|
|
229
|
+
auth.pushChainFunction(mw);
|
|
230
|
+
}
|
|
231
|
+
// Create storage and graphql clients with the refresh and attach token middlewares
|
|
232
|
+
const storageClient = createStorageClient(storageBaseUrl, mwChain);
|
|
233
|
+
const graphqlClient = createGraphQLClient(graphqlBaseUrl, mwChain);
|
|
234
|
+
// Return an initialized NhostClient
|
|
235
|
+
return new NhostClient(auth, storageClient, graphqlClient, storage);
|
|
236
|
+
}
|
|
237
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH,OAAO,EACL,eAAe,IAAI,gBAAgB,GAIpC,MAAM,QAAQ,CAAC;AAChB,OAAO,EAAE,eAAe,IAAI,mBAAmB,EAAE,MAAM,WAAW,CAAC;AACnE,OAAO,EAAE,eAAe,IAAI,mBAAmB,EAAE,MAAM,WAAW,CAAC;AACnE,OAAO,EAAgC,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAC/E,OAAO,EACL,sBAAsB,EACtB,8BAA8B,GAC/B,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,iCAAiC,EAAE,MAAM,yBAAyB,CAAC;AAC5E,OAAO,EAAE,+BAA+B,EAAE,MAAM,6BAA6B,CAAC;AAE9E,OAAO,EAEL,mBAAmB,EACnB,YAAY,EACZ,aAAa,EACb,aAAa,GACd,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAEL,8BAA8B,GAC/B,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,iCAAiC,EAAE,MAAM,yBAAyB,CAAC;AAC5E,OAAO,EAAE,+BAA+B,EAAE,MAAM,6BAA6B,CAAC;AAE9E;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,WAAyD,EACzD,SAAkB,EAClB,MAAe,EACf,SAAkB,EACV,EAAE;IACV,IAAI,SAAS,EAAE,CAAC;Q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ED,mFAAmF;IACnF,MAAM,aAAa,GAAG,mBAAmB,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;IACnE,MAAM,aAAa,GAAG,mBAAmB,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;IAEnE,oCAAoC;IACpC,OAAO,IAAI,WAAW,CAAC,IAAI,EAAE,aAAa,EAAE,aAAa,EAAE,OAAO,CAAC,CAAC;AACtE,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Authorization token attachment middleware for the Nhost SDK.
|
|
3
|
+
*
|
|
4
|
+
* This module provides middleware functionality to automatically attach
|
|
5
|
+
* authorization tokens to outgoing API requests, ensuring the client
|
|
6
|
+
* is properly authenticated.
|
|
7
|
+
*/
|
|
8
|
+
import { type SessionStorageInterface } from "./sessionStorage";
|
|
9
|
+
import { type ChainFunction } from "./fetch";
|
|
10
|
+
/**
|
|
11
|
+
* Creates a fetch middleware that adds the Authorization header with the current access token.
|
|
12
|
+
*
|
|
13
|
+
* This middleware:
|
|
14
|
+
* 1. Gets the current session from storage
|
|
15
|
+
* 2. Adds the authorization header with the access token to outgoing requests
|
|
16
|
+
*
|
|
17
|
+
* This middleware should be used after the refresh middleware in the chain to
|
|
18
|
+
* ensure the most recent token is used.
|
|
19
|
+
*
|
|
20
|
+
* @param storage - Storage implementation for retrieving session data
|
|
21
|
+
* @returns A middleware function that adds Authorization headers
|
|
22
|
+
*/
|
|
23
|
+
export declare const createAttachAccessTokenMiddleware: (storage: SessionStorageInterface) => ChainFunction;
|
|
24
|
+
//# sourceMappingURL=middlewareAttachToken.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middlewareAttachToken.d.ts","sourceRoot":"","sources":["../../src/middlewareAttachToken.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAE,KAAK,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAChE,OAAO,EAAE,KAAK,aAAa,EAAsB,MAAM,SAAS,CAAC;AAEjE;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,iCAAiC,GAC5C,SAAS,uBAAuB,KAC/B,aA+BF,CAAC"}
|