crelte 0.1.0
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.md +41 -0
- package/dist/Crelte.d.ts +55 -0
- package/dist/Crelte.d.ts.map +1 -0
- package/dist/Crelte.js +106 -0
- package/dist/CrelteBase.d.ts +16 -0
- package/dist/CrelteBase.d.ts.map +1 -0
- package/dist/CrelteBase.js +1 -0
- package/dist/CrelteRouted.d.ts +50 -0
- package/dist/CrelteRouted.d.ts.map +1 -0
- package/dist/CrelteRouted.js +88 -0
- package/dist/blocks/Blocks.d.ts +35 -0
- package/dist/blocks/Blocks.d.ts.map +1 -0
- package/dist/blocks/Blocks.js +100 -0
- package/dist/blocks/Blocks.svelte +21 -0
- package/dist/blocks/Blocks.svelte.d.ts +24 -0
- package/dist/blocks/Blocks.svelte.d.ts.map +1 -0
- package/dist/blocks/index.d.ts +5 -0
- package/dist/blocks/index.d.ts.map +1 -0
- package/dist/blocks/index.js +3 -0
- package/dist/cookies/ClientCookies.d.ts +9 -0
- package/dist/cookies/ClientCookies.d.ts.map +1 -0
- package/dist/cookies/ClientCookies.js +22 -0
- package/dist/cookies/ServerCookies.d.ts +13 -0
- package/dist/cookies/ServerCookies.d.ts.map +1 -0
- package/dist/cookies/ServerCookies.js +31 -0
- package/dist/cookies/index.d.ts +20 -0
- package/dist/cookies/index.d.ts.map +1 -0
- package/dist/cookies/index.js +1 -0
- package/dist/cookies/utils.d.ts +12 -0
- package/dist/cookies/utils.d.ts.map +1 -0
- package/dist/cookies/utils.js +32 -0
- package/dist/graphql/GraphQl.d.ts +60 -0
- package/dist/graphql/GraphQl.d.ts.map +1 -0
- package/dist/graphql/GraphQl.js +197 -0
- package/dist/graphql/gql.test.d.ts +2 -0
- package/dist/graphql/gql.test.d.ts.map +1 -0
- package/dist/graphql/gql.test.js +80 -0
- package/dist/graphql/index.d.ts +3 -0
- package/dist/graphql/index.d.ts.map +1 -0
- package/dist/graphql/index.js +2 -0
- package/dist/index.d.ts +67 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +83 -0
- package/dist/init/client.d.ts +13 -0
- package/dist/init/client.d.ts.map +1 -0
- package/dist/init/client.js +129 -0
- package/dist/init/server.d.ts +38 -0
- package/dist/init/server.d.ts.map +1 -0
- package/dist/init/server.js +95 -0
- package/dist/init/shared.d.ts +29 -0
- package/dist/init/shared.d.ts.map +1 -0
- package/dist/init/shared.js +154 -0
- package/dist/loadData/Globals.d.ts +33 -0
- package/dist/loadData/Globals.d.ts.map +1 -0
- package/dist/loadData/Globals.js +119 -0
- package/dist/loadData/index.d.ts +25 -0
- package/dist/loadData/index.d.ts.map +1 -0
- package/dist/loadData/index.js +39 -0
- package/dist/plugins/Events.d.ts +11 -0
- package/dist/plugins/Events.d.ts.map +1 -0
- package/dist/plugins/Events.js +29 -0
- package/dist/plugins/Plugins.d.ts +12 -0
- package/dist/plugins/Plugins.d.ts.map +1 -0
- package/dist/plugins/Plugins.js +12 -0
- package/dist/plugins/index.d.ts +5 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/index.js +2 -0
- package/dist/routing/History.d.ts +22 -0
- package/dist/routing/History.d.ts.map +1 -0
- package/dist/routing/History.js +36 -0
- package/dist/routing/InnerRouter.d.ts +111 -0
- package/dist/routing/InnerRouter.d.ts.map +1 -0
- package/dist/routing/InnerRouter.js +397 -0
- package/dist/routing/PageLoader.d.ts +37 -0
- package/dist/routing/PageLoader.d.ts.map +1 -0
- package/dist/routing/PageLoader.js +72 -0
- package/dist/routing/Route.d.ts +82 -0
- package/dist/routing/Route.d.ts.map +1 -0
- package/dist/routing/Route.js +134 -0
- package/dist/routing/Router.d.ts +162 -0
- package/dist/routing/Router.d.ts.map +1 -0
- package/dist/routing/Router.js +333 -0
- package/dist/routing/Site.d.ts +47 -0
- package/dist/routing/Site.d.ts.map +1 -0
- package/dist/routing/Site.js +48 -0
- package/dist/routing/index.d.ts +5 -0
- package/dist/routing/index.d.ts.map +1 -0
- package/dist/routing/index.js +4 -0
- package/dist/ssr/SsrCache.d.ts +12 -0
- package/dist/ssr/SsrCache.d.ts.map +1 -0
- package/dist/ssr/SsrCache.js +50 -0
- package/dist/ssr/SsrComponents.d.ts +7 -0
- package/dist/ssr/SsrComponents.d.ts.map +1 -0
- package/dist/ssr/SsrComponents.js +30 -0
- package/dist/ssr/index.d.ts +4 -0
- package/dist/ssr/index.d.ts.map +1 -0
- package/dist/ssr/index.js +3 -0
- package/package.json +79 -0
- package/src/Crelte.ts +135 -0
- package/src/CrelteBase.ts +24 -0
- package/src/CrelteRouted.ts +128 -0
- package/src/blocks/Blocks.svelte +68 -0
- package/src/blocks/Blocks.ts +155 -0
- package/src/blocks/index.ts +14 -0
- package/src/cookies/ClientCookies.ts +30 -0
- package/src/cookies/ServerCookies.ts +42 -0
- package/src/cookies/index.ts +24 -0
- package/src/cookies/utils.ts +53 -0
- package/src/graphql/GraphQl.ts +281 -0
- package/src/graphql/gql.test.ts +123 -0
- package/src/graphql/index.ts +8 -0
- package/src/index.ts +109 -0
- package/src/init/client.ts +190 -0
- package/src/init/server.ts +177 -0
- package/src/init/shared.ts +221 -0
- package/src/loadData/Globals.ts +150 -0
- package/src/loadData/index.ts +67 -0
- package/src/plugins/Events.ts +50 -0
- package/src/plugins/Plugins.ts +23 -0
- package/src/plugins/index.ts +5 -0
- package/src/routing/History.ts +52 -0
- package/src/routing/InnerRouter.ts +469 -0
- package/src/routing/PageLoader.ts +112 -0
- package/src/routing/Route.ts +184 -0
- package/src/routing/Router.ts +476 -0
- package/src/routing/Site.ts +65 -0
- package/src/routing/index.ts +5 -0
- package/src/ssr/SsrCache.ts +61 -0
- package/src/ssr/SsrComponents.ts +34 -0
- package/src/ssr/index.ts +4 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { parseCookies, setCookieToString } from './utils.js';
|
|
2
|
+
export default class ServerCookies {
|
|
3
|
+
requestCookies;
|
|
4
|
+
setCookies;
|
|
5
|
+
constructor() {
|
|
6
|
+
this.requestCookies = new Map();
|
|
7
|
+
this.setCookies = new Map();
|
|
8
|
+
}
|
|
9
|
+
_init(cookies) {
|
|
10
|
+
// parse the cookies
|
|
11
|
+
this.requestCookies = parseCookies(cookies);
|
|
12
|
+
}
|
|
13
|
+
/// Rethrns the value of the cookie with the given name, or null if it doesn't exist.
|
|
14
|
+
get(name) {
|
|
15
|
+
const setCookie = this.setCookies.get(name);
|
|
16
|
+
// js allows undefined > 0
|
|
17
|
+
if (setCookie && setCookie.maxAge > 0) {
|
|
18
|
+
return setCookie.value;
|
|
19
|
+
}
|
|
20
|
+
return this.requestCookies.get(name) ?? null;
|
|
21
|
+
}
|
|
22
|
+
set(name, value, opts) {
|
|
23
|
+
this.setCookies.set(name, { name, value, ...opts });
|
|
24
|
+
}
|
|
25
|
+
remove(name) {
|
|
26
|
+
this.set(name, '', { maxAge: 0 });
|
|
27
|
+
}
|
|
28
|
+
_getSetCookiesHeaders() {
|
|
29
|
+
return Array.from(this.setCookies.values()).map(setCookie => setCookieToString(setCookie));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type SetOptions = {
|
|
2
|
+
maxAge?: number;
|
|
3
|
+
path?: string;
|
|
4
|
+
domain?: string;
|
|
5
|
+
secure?: boolean;
|
|
6
|
+
httpOnly?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export interface Cookies {
|
|
9
|
+
/**
|
|
10
|
+
* returns the value of the cookie
|
|
11
|
+
*/
|
|
12
|
+
get(name: string): string | null;
|
|
13
|
+
/**
|
|
14
|
+
* sets the value of the cookie
|
|
15
|
+
*/
|
|
16
|
+
set(name: string, value: string, opts?: SetOptions): void;
|
|
17
|
+
remove(name: string): void;
|
|
18
|
+
_init(cookies: string): void;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/cookies/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,GAAG;IACxB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF,MAAM,WAAW,OAAO;IACvB;;OAEG;IACH,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IAEjC;;OAEG;IACH,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,UAAU,GAAG,IAAI,CAAC;IAE1D,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAG3B,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CAC7B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare function parseCookies(cookies: string): Map<string, string>;
|
|
2
|
+
export type SetCookie = {
|
|
3
|
+
name: string;
|
|
4
|
+
value: string;
|
|
5
|
+
maxAge?: number;
|
|
6
|
+
path?: string;
|
|
7
|
+
domain?: string;
|
|
8
|
+
secure?: boolean;
|
|
9
|
+
httpOnly?: boolean;
|
|
10
|
+
};
|
|
11
|
+
export declare function setCookieToString(cookie: SetCookie): string;
|
|
12
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../../src/cookies/utils.ts"],"names":[],"mappings":"AAAA,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAgBjE;AAED,MAAM,MAAM,SAAS,GAAG;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAwB3D"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export function parseCookies(cookies) {
|
|
2
|
+
return new Map(cookies.split(';').map(cookie => {
|
|
3
|
+
let [name, value] = cookie.split('=');
|
|
4
|
+
/// this is the behaviour of chrome
|
|
5
|
+
if (typeof value === 'undefined') {
|
|
6
|
+
value = name;
|
|
7
|
+
name = '';
|
|
8
|
+
}
|
|
9
|
+
name = name.trim();
|
|
10
|
+
value = decodeURIComponent(value.trim());
|
|
11
|
+
return [name, value];
|
|
12
|
+
}));
|
|
13
|
+
}
|
|
14
|
+
export function setCookieToString(cookie) {
|
|
15
|
+
let s = `${cookie.name}=${encodeURIComponent(cookie.value)}`;
|
|
16
|
+
if (typeof cookie.maxAge === 'number') {
|
|
17
|
+
s += `; Max-Age=${cookie.maxAge}`;
|
|
18
|
+
}
|
|
19
|
+
if (cookie.path) {
|
|
20
|
+
s += `; Path=${cookie.path}`;
|
|
21
|
+
}
|
|
22
|
+
if (cookie.domain) {
|
|
23
|
+
s += `; Domain=${cookie.domain}`;
|
|
24
|
+
}
|
|
25
|
+
if (cookie.secure) {
|
|
26
|
+
s += `; Secure`;
|
|
27
|
+
}
|
|
28
|
+
if (cookie.httpOnly) {
|
|
29
|
+
s += `; HttpOnly`;
|
|
30
|
+
}
|
|
31
|
+
return s;
|
|
32
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import SsrCache from '../ssr/SsrCache.js';
|
|
2
|
+
export type GraphQlErrorResponse = {
|
|
3
|
+
status?: number;
|
|
4
|
+
body?: string;
|
|
5
|
+
};
|
|
6
|
+
export declare class GraphQlError extends Error {
|
|
7
|
+
resp: GraphQlErrorResponse;
|
|
8
|
+
ctx: any;
|
|
9
|
+
constructor(resp: GraphQlErrorResponse, ctx?: any);
|
|
10
|
+
status(): number;
|
|
11
|
+
__isGraphQlError__(): void;
|
|
12
|
+
get message(): string;
|
|
13
|
+
}
|
|
14
|
+
export type GraphQlOptions = {
|
|
15
|
+
debug?: boolean;
|
|
16
|
+
debugTiming?: boolean;
|
|
17
|
+
};
|
|
18
|
+
export type GraphQlRequestOptions = {
|
|
19
|
+
path?: string;
|
|
20
|
+
ignoreStatusCode?: boolean;
|
|
21
|
+
previewToken?: string;
|
|
22
|
+
siteToken?: string;
|
|
23
|
+
caching?: boolean;
|
|
24
|
+
headers?: Record<string, string>;
|
|
25
|
+
status?: number;
|
|
26
|
+
};
|
|
27
|
+
export default class GraphQl {
|
|
28
|
+
private endpoint;
|
|
29
|
+
private ssrCache;
|
|
30
|
+
private listeners;
|
|
31
|
+
private loggingRequests;
|
|
32
|
+
private loggingTiming;
|
|
33
|
+
/**
|
|
34
|
+
* Create a new GraphQL
|
|
35
|
+
*
|
|
36
|
+
* @param {Object} [opts={}] opts `{ debug: false, debugTiming: false }`
|
|
37
|
+
*/
|
|
38
|
+
constructor(endpoint: string, ssrCache: SsrCache, opts?: GraphQlOptions);
|
|
39
|
+
request(query: string, variables?: Record<string, unknown>, options?: GraphQlRequestOptions): Promise<unknown>;
|
|
40
|
+
private rawRequest;
|
|
41
|
+
}
|
|
42
|
+
export declare class GraphQlQuery {
|
|
43
|
+
query: string;
|
|
44
|
+
path: string;
|
|
45
|
+
constructor(query: string, path: string);
|
|
46
|
+
__GraphQlQuery__(): boolean;
|
|
47
|
+
}
|
|
48
|
+
/** Returns true if the passed object is a GraphQlQuery */
|
|
49
|
+
export declare function isGraphQlQuery(obj: any): obj is GraphQlQuery;
|
|
50
|
+
/**
|
|
51
|
+
* @description Create a GraphQL query string with variables.
|
|
52
|
+
* @param strings
|
|
53
|
+
* @param keys
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
*
|
|
57
|
+
* const query = gql`query ($id: ID!) { page(id: $id) { id } }`
|
|
58
|
+
*/
|
|
59
|
+
export declare function gql(strings: TemplateStringsArray | string[] | string, ...keys: unknown[]): GraphQlQuery;
|
|
60
|
+
//# sourceMappingURL=GraphQl.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GraphQl.d.ts","sourceRoot":"","sources":["../../../../src/graphql/GraphQl.ts"],"names":[],"mappings":"AAAA,OAAO,QAAwC,MAAM,oBAAoB,CAAC;AAE1E,MAAM,MAAM,oBAAoB,GAAG;IAClC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;CACd,CAAC;AAGF,qBAAa,YAAa,SAAQ,KAAK;IACtC,IAAI,EAAE,oBAAoB,CAAC;IAC3B,GAAG,EAAE,GAAG,CAAC;gBAGG,IAAI,EAAE,oBAAoB,EAAE,GAAG,GAAE,GAAU;IAOvD,MAAM,IAAI,MAAM;IAIhB,kBAAkB;IAElB,IAAI,OAAO,IAAI,MAAM,CAEpB;CACD;AAED,MAAM,MAAM,cAAc,GAAG;IAC5B,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,WAAW,CAAC,EAAE,OAAO,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IACnC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEjC,MAAM,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,CAAC,OAAO,OAAO,OAAO;IAC3B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,QAAQ,CAAW;IAC3B,OAAO,CAAC,SAAS,CAGf;IAEF,OAAO,CAAC,eAAe,CAAU;IACjC,OAAO,CAAC,aAAa,CAAU;IAE/B;;;;OAIG;gBAEF,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,QAAQ,EAClB,IAAI,GAAE,cAAmB;IAYpB,OAAO,CACZ,KAAK,EAAE,MAAM,EACb,SAAS,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM,EACvC,OAAO,GAAE,qBAA0B,GACjC,OAAO,CAAC,OAAO,CAAC;YAsDL,UAAU;CAmFxB;AAED,qBAAa,YAAY;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;gBAED,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;IAMvC,gBAAgB;CAGhB;AAED,0DAA0D;AAC1D,wBAAgB,cAAc,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,IAAI,YAAY,CAM5D;AAED;;;;;;;;GAQG;AACH,wBAAgB,GAAG,CAClB,OAAO,EAAE,oBAAoB,GAAG,MAAM,EAAE,GAAG,MAAM,EACjD,GAAG,IAAI,EAAE,OAAO,EAAE,GAChB,YAAY,CAuBd"}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { calcKey as ssrCacheCalcKey } from '../ssr/SsrCache.js';
|
|
2
|
+
// todo improve this
|
|
3
|
+
export class GraphQlError extends Error {
|
|
4
|
+
resp;
|
|
5
|
+
ctx;
|
|
6
|
+
// ctx might be anything
|
|
7
|
+
constructor(resp, ctx = null) {
|
|
8
|
+
super();
|
|
9
|
+
this.resp = resp;
|
|
10
|
+
this.ctx = ctx;
|
|
11
|
+
}
|
|
12
|
+
status() {
|
|
13
|
+
return this.resp?.status ?? 500;
|
|
14
|
+
}
|
|
15
|
+
__isGraphQlError__() { }
|
|
16
|
+
get message() {
|
|
17
|
+
return 'GraphqlError: ' + JSON.stringify(this.resp);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export default class GraphQl {
|
|
21
|
+
endpoint;
|
|
22
|
+
ssrCache;
|
|
23
|
+
listeners;
|
|
24
|
+
loggingRequests;
|
|
25
|
+
loggingTiming;
|
|
26
|
+
/**
|
|
27
|
+
* Create a new GraphQL
|
|
28
|
+
*
|
|
29
|
+
* @param {Object} [opts={}] opts `{ debug: false, debugTiming: false }`
|
|
30
|
+
*/
|
|
31
|
+
constructor(endpoint, ssrCache, opts = {}) {
|
|
32
|
+
this.endpoint = endpoint;
|
|
33
|
+
this.ssrCache = ssrCache;
|
|
34
|
+
this.listeners = new Map();
|
|
35
|
+
this.loggingRequests = opts?.debug ?? false;
|
|
36
|
+
this.loggingTiming = opts?.debugTiming ?? false;
|
|
37
|
+
}
|
|
38
|
+
// returns {} or throws
|
|
39
|
+
// options: {ignoreStatusCode, previewToken, caching, headers}
|
|
40
|
+
async request(query, variables = {}, options = {}) {
|
|
41
|
+
let key;
|
|
42
|
+
if (options?.caching ?? true) {
|
|
43
|
+
key = await ssrCacheCalcKey({ query, variables });
|
|
44
|
+
const inCache = this.ssrCache.get(key);
|
|
45
|
+
if (inCache)
|
|
46
|
+
return inCache;
|
|
47
|
+
// check if a listeners exists meaning the same request is already
|
|
48
|
+
// in progress
|
|
49
|
+
const listeners = this.listeners.get(key);
|
|
50
|
+
if (listeners) {
|
|
51
|
+
return new Promise((resolve, error) => {
|
|
52
|
+
listeners.push([resolve, error]);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
// else setup the listener
|
|
56
|
+
this.listeners.set(key, []);
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
const resp = await this.rawRequest(query, variables, options);
|
|
60
|
+
if (key) {
|
|
61
|
+
this.ssrCache.set(key, resp);
|
|
62
|
+
// ! because the listeners get's in the previous if statement and will
|
|
63
|
+
// always be set when the key is set
|
|
64
|
+
const listeners = this.listeners.get(key);
|
|
65
|
+
listeners.forEach(([resolve, _error]) => {
|
|
66
|
+
resolve(resp);
|
|
67
|
+
});
|
|
68
|
+
this.listeners.delete(key);
|
|
69
|
+
}
|
|
70
|
+
return resp;
|
|
71
|
+
}
|
|
72
|
+
catch (e) {
|
|
73
|
+
if (key) {
|
|
74
|
+
// ! because the listeners get's in the previous if statement and will
|
|
75
|
+
// always be set when the key is set
|
|
76
|
+
const listeners = this.listeners.get(key);
|
|
77
|
+
listeners.forEach(([_resolve, error]) => {
|
|
78
|
+
error(e);
|
|
79
|
+
});
|
|
80
|
+
this.listeners.delete(key);
|
|
81
|
+
}
|
|
82
|
+
throw e;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// status will be set in opts
|
|
86
|
+
async rawRequest(query, variables = {}, opts = {}) {
|
|
87
|
+
opts.ignoreStatusCode = opts.ignoreStatusCode ?? false;
|
|
88
|
+
let url = this.endpoint;
|
|
89
|
+
if (opts?.previewToken)
|
|
90
|
+
url += '?token=' + opts.previewToken;
|
|
91
|
+
else if (opts?.siteToken)
|
|
92
|
+
url += '?siteToken=' + opts.siteToken;
|
|
93
|
+
const headers = opts?.headers ?? {};
|
|
94
|
+
headers['Content-Type'] = 'application/json';
|
|
95
|
+
if (this.loggingRequests) {
|
|
96
|
+
console.log('query to ', url, variables, opts);
|
|
97
|
+
headers['X-Debug'] = 'enable';
|
|
98
|
+
}
|
|
99
|
+
let timing;
|
|
100
|
+
if (this.loggingTiming)
|
|
101
|
+
timing = Date.now();
|
|
102
|
+
const resp = await fetch(url, {
|
|
103
|
+
method: 'POST',
|
|
104
|
+
headers,
|
|
105
|
+
body: JSON.stringify({
|
|
106
|
+
query,
|
|
107
|
+
variables,
|
|
108
|
+
}),
|
|
109
|
+
});
|
|
110
|
+
if (opts.ignoreStatusCode) {
|
|
111
|
+
opts.status = resp.status;
|
|
112
|
+
}
|
|
113
|
+
else if (!resp.ok) {
|
|
114
|
+
throw new GraphQlError({
|
|
115
|
+
status: resp.status,
|
|
116
|
+
body: await resp.text(),
|
|
117
|
+
}, 'resp not ok');
|
|
118
|
+
}
|
|
119
|
+
if (resp.headers.get('x-debug-link'))
|
|
120
|
+
console.log('Debug link', resp.headers.get('x-debug-link'));
|
|
121
|
+
if (timing) {
|
|
122
|
+
console.log('request ' +
|
|
123
|
+
opts.path +
|
|
124
|
+
' vars: ' +
|
|
125
|
+
JSON.stringify(variables) +
|
|
126
|
+
' took ' +
|
|
127
|
+
(Date.now() - timing) +
|
|
128
|
+
'ms');
|
|
129
|
+
}
|
|
130
|
+
let jsonResp;
|
|
131
|
+
try {
|
|
132
|
+
jsonResp = await resp.json();
|
|
133
|
+
}
|
|
134
|
+
catch (e) {
|
|
135
|
+
throw new GraphQlError({
|
|
136
|
+
status: resp.status,
|
|
137
|
+
}, e);
|
|
138
|
+
}
|
|
139
|
+
if ('errors' in jsonResp) {
|
|
140
|
+
console.log('graphql errors', jsonResp.errors);
|
|
141
|
+
throw new GraphQlError({
|
|
142
|
+
status: resp.status,
|
|
143
|
+
}, jsonResp.errors);
|
|
144
|
+
}
|
|
145
|
+
return jsonResp.data ?? null;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
export class GraphQlQuery {
|
|
149
|
+
query;
|
|
150
|
+
path;
|
|
151
|
+
constructor(query, path) {
|
|
152
|
+
this.query = query;
|
|
153
|
+
this.path = path;
|
|
154
|
+
}
|
|
155
|
+
/// doc hidden
|
|
156
|
+
__GraphQlQuery__() {
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/** Returns true if the passed object is a GraphQlQuery */
|
|
161
|
+
export function isGraphQlQuery(obj) {
|
|
162
|
+
return (typeof obj === 'object' &&
|
|
163
|
+
obj !== null &&
|
|
164
|
+
typeof obj.__GraphQlQuery__ === 'function');
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* @description Create a GraphQL query string with variables.
|
|
168
|
+
* @param strings
|
|
169
|
+
* @param keys
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
*
|
|
173
|
+
* const query = gql`query ($id: ID!) { page(id: $id) { id } }`
|
|
174
|
+
*/
|
|
175
|
+
export function gql(strings, ...keys) {
|
|
176
|
+
if (typeof strings === 'string')
|
|
177
|
+
strings = [strings];
|
|
178
|
+
let query = '';
|
|
179
|
+
strings.forEach((string, i) => {
|
|
180
|
+
query += string;
|
|
181
|
+
if (typeof keys[i] !== 'undefined') {
|
|
182
|
+
const variable = keys[i];
|
|
183
|
+
// nesting support
|
|
184
|
+
if (isGraphQlQuery(variable)) {
|
|
185
|
+
query += variable.query;
|
|
186
|
+
}
|
|
187
|
+
else if (typeof variable === 'string') {
|
|
188
|
+
query += variable;
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
console.error('invalid key', variable);
|
|
192
|
+
throw new Error('Invalid key: ' + typeof variable);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
return new GraphQlQuery(query, import.meta.url);
|
|
197
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gql.test.d.ts","sourceRoot":"","sources":["../../../../src/graphql/gql.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { test, expect, describe } from 'vitest';
|
|
2
|
+
import { gql } from './GraphQl.js';
|
|
3
|
+
// tests for the gql function
|
|
4
|
+
// testing the query property
|
|
5
|
+
describe('gql: query property', () => {
|
|
6
|
+
test('gql: without variables', () => {
|
|
7
|
+
const query = gql `a bc`;
|
|
8
|
+
expect(query).toHaveProperty('query', 'a bc');
|
|
9
|
+
expect(query).toHaveProperty('path');
|
|
10
|
+
});
|
|
11
|
+
test('gql: with one variable', () => {
|
|
12
|
+
const variable = 'var';
|
|
13
|
+
const query = gql `a ${variable} bc`;
|
|
14
|
+
expect(query).toHaveProperty('query', `a ${variable} bc`);
|
|
15
|
+
expect(query).toHaveProperty('path');
|
|
16
|
+
});
|
|
17
|
+
test('gql: with multiple variables', () => {
|
|
18
|
+
const variable1 = 'var1';
|
|
19
|
+
const variable2 = 'var2';
|
|
20
|
+
const variable3 = 'var3';
|
|
21
|
+
const query = gql `a ${variable1} bc ${variable2} def ${variable3}`;
|
|
22
|
+
expect(query).toHaveProperty('query', `a ${variable1} bc ${variable2} def ${variable3}`);
|
|
23
|
+
expect(query).toHaveProperty('path');
|
|
24
|
+
});
|
|
25
|
+
test('gql: beginning with variable', () => {
|
|
26
|
+
const variable1 = 'var1';
|
|
27
|
+
const variable2 = 'var2';
|
|
28
|
+
const variable3 = 'var3';
|
|
29
|
+
const query = gql `${variable1} abc ${variable2} def ${variable3}`;
|
|
30
|
+
expect(query).toHaveProperty('query', `${variable1} abc ${variable2} def ${variable3}`);
|
|
31
|
+
expect(query).toHaveProperty('path');
|
|
32
|
+
});
|
|
33
|
+
test('gql: ending with variable', () => {
|
|
34
|
+
const variable1 = 'var1';
|
|
35
|
+
const query = gql `abcdef ${variable1}`;
|
|
36
|
+
expect(query.query.trim()).toBe(`abcdef ${variable1}`);
|
|
37
|
+
expect(query).toHaveProperty('path');
|
|
38
|
+
});
|
|
39
|
+
test('gql: exclusive variable', () => {
|
|
40
|
+
const variable1 = 'var1';
|
|
41
|
+
const query = gql `
|
|
42
|
+
${variable1}
|
|
43
|
+
`;
|
|
44
|
+
expect(query.query.trim()).toBe(`${variable1}`);
|
|
45
|
+
expect(query).toHaveProperty('path');
|
|
46
|
+
});
|
|
47
|
+
test('gql: empty string', () => {
|
|
48
|
+
const query = gql ``;
|
|
49
|
+
expect(query).toHaveProperty('query', ``);
|
|
50
|
+
expect(query).toHaveProperty('path');
|
|
51
|
+
});
|
|
52
|
+
test('gql: only variables', () => {
|
|
53
|
+
const variable1 = 'var1';
|
|
54
|
+
const variable2 = 'var2';
|
|
55
|
+
const variable3 = 'var3';
|
|
56
|
+
// prettier-ignore
|
|
57
|
+
const query = gql `${variable1}${variable2}${variable3}`;
|
|
58
|
+
expect(query).toHaveProperty('query', `${variable1}${variable2}${variable3}`);
|
|
59
|
+
expect(query).toHaveProperty('path');
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
describe('gql: path property', () => {
|
|
63
|
+
const root = process.cwd();
|
|
64
|
+
test('gql: path is graphql.js file', () => {
|
|
65
|
+
const query = gql ``;
|
|
66
|
+
expect(query).toHaveProperty('query');
|
|
67
|
+
expect(query).toHaveProperty('path', 'file://' + root + '/src/graphql/GraphQl.ts');
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
describe('gql: nesting', () => {
|
|
71
|
+
test('gql: nesting', () => {
|
|
72
|
+
const variable1 = 'var1';
|
|
73
|
+
const variable2 = 'var2';
|
|
74
|
+
const variable3 = 'var3';
|
|
75
|
+
const query = gql `a ${variable1} bc ${gql `a ${variable2} bc ${variable3}`}`;
|
|
76
|
+
expect(query).toHaveProperty('query', `a ${variable1} bc a ${variable2} bc ${variable3}`);
|
|
77
|
+
expect(query).toHaveProperty('path');
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
describe.todo('gql: variable types. Handling of objects and arrays');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/graphql/index.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,EAAE,EACf,GAAG,EACH,YAAY,EACZ,YAAY,EACZ,qBAAqB,EACrB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,YAAY,EAAE,YAAY,EAAE,KAAK,qBAAqB,EAAE,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type Route from './routing/Route.js';
|
|
2
|
+
import type Router from './routing/Router.js';
|
|
3
|
+
import type SsrCache from './ssr/SsrCache.js';
|
|
4
|
+
import type Site from './routing/Site.js';
|
|
5
|
+
import type GraphQl from './graphql/GraphQl.js';
|
|
6
|
+
import Crelte from './Crelte.js';
|
|
7
|
+
import CrelteRouted from './CrelteRouted.js';
|
|
8
|
+
import CrelteBase from './CrelteBase.js';
|
|
9
|
+
import { Global, GlobalData } from './loadData/Globals.js';
|
|
10
|
+
import { Cookies } from './cookies/index.js';
|
|
11
|
+
import { Readable } from 'crelte-std/stores';
|
|
12
|
+
export { Crelte, CrelteRouted };
|
|
13
|
+
export type { CrelteBase };
|
|
14
|
+
/**
|
|
15
|
+
* Get the Crelte from the current context
|
|
16
|
+
*/
|
|
17
|
+
export declare function getCrelte(): Crelte;
|
|
18
|
+
/**
|
|
19
|
+
* Get the router from the current context
|
|
20
|
+
*/
|
|
21
|
+
export declare function getRouter(): Router;
|
|
22
|
+
/**
|
|
23
|
+
* Get the SSR cache from the current context
|
|
24
|
+
*/
|
|
25
|
+
export declare function getSsrCache(): SsrCache;
|
|
26
|
+
/**
|
|
27
|
+
* Get the GraphQl from the current context
|
|
28
|
+
*/
|
|
29
|
+
export declare function getGraphQl(): GraphQl;
|
|
30
|
+
/**
|
|
31
|
+
* Get the current route
|
|
32
|
+
*/
|
|
33
|
+
export declare function getRoute(): Readable<Route>;
|
|
34
|
+
/**
|
|
35
|
+
* Get the current site
|
|
36
|
+
*/
|
|
37
|
+
export declare function getSite(): Readable<Site>;
|
|
38
|
+
/**
|
|
39
|
+
* Get the next route
|
|
40
|
+
*/
|
|
41
|
+
export declare function getNextRoute(): Readable<Route>;
|
|
42
|
+
/**
|
|
43
|
+
* Get the next site
|
|
44
|
+
*/
|
|
45
|
+
export declare function getNextSite(): Readable<Site>;
|
|
46
|
+
/**
|
|
47
|
+
* returns an env Variables, always needs to be prefixed VITE_
|
|
48
|
+
* Except ENDPOINT_URL and CRAFT_WEB_URL
|
|
49
|
+
*/
|
|
50
|
+
export declare function getEnv(name: string): string | null;
|
|
51
|
+
/**
|
|
52
|
+
* returns a store which indicates if the a page is loading
|
|
53
|
+
*/
|
|
54
|
+
export declare function getLoading(): Readable<boolean>;
|
|
55
|
+
/**
|
|
56
|
+
* returns a store which indicates the loading progress
|
|
57
|
+
*/
|
|
58
|
+
export declare function getLoadingProgress(): Readable<number>;
|
|
59
|
+
/**
|
|
60
|
+
* returns a store which contains a globalSet
|
|
61
|
+
*/
|
|
62
|
+
export declare function getGlobal<T extends GlobalData>(name: string): Global<T> | null;
|
|
63
|
+
/**
|
|
64
|
+
* returns the cookies instance
|
|
65
|
+
*/
|
|
66
|
+
export declare function getCookies(): Cookies;
|
|
67
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,MAAM,oBAAoB,CAAC;AAC5C,OAAO,KAAK,MAAM,MAAM,qBAAqB,CAAC;AAC9C,OAAO,KAAK,QAAQ,MAAM,mBAAmB,CAAC;AAC9C,OAAO,KAAK,IAAI,MAAM,mBAAmB,CAAC;AAC1C,OAAO,KAAK,OAAO,MAAM,sBAAsB,CAAC;AAChD,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,YAAY,MAAM,mBAAmB,CAAC;AAC7C,OAAO,UAAU,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE7C,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC;AAChC,YAAY,EAAE,UAAU,EAAE,CAAC;AAE3B;;GAEG;AACH,wBAAgB,SAAS,IAAI,MAAM,CAElC;AAED;;GAEG;AACH,wBAAgB,SAAS,IAAI,MAAM,CAElC;AAED;;GAEG;AACH,wBAAgB,WAAW,IAAI,QAAQ,CAEtC;AAED;;GAEG;AACH,wBAAgB,UAAU,IAAI,OAAO,CAEpC;AAED;;GAEG;AACH,wBAAgB,QAAQ,IAAI,QAAQ,CAAC,KAAK,CAAC,CAE1C;AAED;;GAEG;AACH,wBAAgB,OAAO,IAAI,QAAQ,CAAC,IAAI,CAAC,CAExC;AAED;;GAEG;AACH,wBAAgB,YAAY,IAAI,QAAQ,CAAC,KAAK,CAAC,CAE9C;AAED;;GAEG;AACH,wBAAgB,WAAW,IAAI,QAAQ,CAAC,IAAI,CAAC,CAE5C;AAED;;;GAGG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAElD;AAED;;GAEG;AACH,wBAAgB,UAAU,IAAI,QAAQ,CAAC,OAAO,CAAC,CAE9C;AAED;;GAEG;AACH,wBAAgB,kBAAkB,IAAI,QAAQ,CAAC,MAAM,CAAC,CAErD;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,UAAU,EAC7C,IAAI,EAAE,MAAM,GACV,MAAM,CAAC,CAAC,CAAC,GAAG,IAAI,CAElB;AAED;;GAEG;AACH,wBAAgB,UAAU,IAAI,OAAO,CAEpC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { getContext } from 'svelte';
|
|
2
|
+
import Crelte from './Crelte.js';
|
|
3
|
+
import CrelteRouted from './CrelteRouted.js';
|
|
4
|
+
export { Crelte, CrelteRouted };
|
|
5
|
+
/**
|
|
6
|
+
* Get the Crelte from the current context
|
|
7
|
+
*/
|
|
8
|
+
export function getCrelte() {
|
|
9
|
+
return getContext('crelte');
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Get the router from the current context
|
|
13
|
+
*/
|
|
14
|
+
export function getRouter() {
|
|
15
|
+
return getCrelte().router;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Get the SSR cache from the current context
|
|
19
|
+
*/
|
|
20
|
+
export function getSsrCache() {
|
|
21
|
+
return getCrelte().ssrCache;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Get the GraphQl from the current context
|
|
25
|
+
*/
|
|
26
|
+
export function getGraphQl() {
|
|
27
|
+
return getCrelte().graphQl;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Get the current route
|
|
31
|
+
*/
|
|
32
|
+
export function getRoute() {
|
|
33
|
+
return getRouter().route;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Get the current site
|
|
37
|
+
*/
|
|
38
|
+
export function getSite() {
|
|
39
|
+
return getRouter().site;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Get the next route
|
|
43
|
+
*/
|
|
44
|
+
export function getNextRoute() {
|
|
45
|
+
return getRouter().nextRoute;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Get the next site
|
|
49
|
+
*/
|
|
50
|
+
export function getNextSite() {
|
|
51
|
+
return getRouter().nextSite;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* returns an env Variables, always needs to be prefixed VITE_
|
|
55
|
+
* Except ENDPOINT_URL and CRAFT_WEB_URL
|
|
56
|
+
*/
|
|
57
|
+
export function getEnv(name) {
|
|
58
|
+
return getCrelte().getEnv(name);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* returns a store which indicates if the a page is loading
|
|
62
|
+
*/
|
|
63
|
+
export function getLoading() {
|
|
64
|
+
return getRouter().loading;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* returns a store which indicates the loading progress
|
|
68
|
+
*/
|
|
69
|
+
export function getLoadingProgress() {
|
|
70
|
+
return getRouter().loadingProgress;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* returns a store which contains a globalSet
|
|
74
|
+
*/
|
|
75
|
+
export function getGlobal(name) {
|
|
76
|
+
return getCrelte().globals.get(name) ?? null;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* returns the cookies instance
|
|
80
|
+
*/
|
|
81
|
+
export function getCookies() {
|
|
82
|
+
return getCrelte().cookies;
|
|
83
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type MainData = {
|
|
2
|
+
app: any;
|
|
3
|
+
errorPage: any;
|
|
4
|
+
entryQuery: any;
|
|
5
|
+
globalQuery?: any;
|
|
6
|
+
preloadOnMouseOver?: boolean;
|
|
7
|
+
viewTransition?: boolean;
|
|
8
|
+
playIntro?: boolean;
|
|
9
|
+
graphQlDebug?: boolean;
|
|
10
|
+
debugTiming?: boolean;
|
|
11
|
+
};
|
|
12
|
+
export declare function main(data: MainData): void;
|
|
13
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../../src/init/client.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,QAAQ,GAAG;IAEtB,GAAG,EAAE,GAAG,CAAC;IAET,SAAS,EAAE,GAAG,CAAC;IACf,UAAU,EAAE,GAAG,CAAC;IAChB,WAAW,CAAC,EAAE,GAAG,CAAC;IAGlB,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,SAAS,CAAC,EAAE,OAAO,CAAC;IAGpB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,WAAW,CAAC,EAAE,OAAO,CAAC;CACtB,CAAC;AAYF,wBAAgB,IAAI,CAAC,IAAI,EAAE,QAAQ,QAsIlC"}
|