crelte 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Crelte.d.ts +89 -25
- package/dist/Crelte.d.ts.map +1 -1
- package/dist/Crelte.js +79 -38
- package/dist/CrelteRequest.d.ts +63 -0
- package/dist/CrelteRequest.d.ts.map +1 -0
- package/dist/CrelteRequest.js +94 -0
- package/dist/blocks/Blocks.d.ts +27 -3
- package/dist/blocks/Blocks.d.ts.map +1 -1
- package/dist/blocks/Blocks.js +8 -0
- package/dist/blocks/Blocks.svelte +1 -1
- package/dist/blocks/Blocks.svelte.d.ts +36 -4
- package/dist/blocks/Blocks.svelte.d.ts.map +1 -1
- package/dist/blocks/index.d.ts +6 -3
- package/dist/blocks/index.d.ts.map +1 -1
- package/dist/blocks/index.js +3 -2
- package/dist/cookies/ServerCookies.d.ts +1 -1
- package/dist/cookies/ServerCookies.d.ts.map +1 -1
- package/dist/cookies/index.d.ts +7 -0
- package/dist/cookies/index.d.ts.map +1 -1
- package/dist/graphql/GraphQl.d.ts +53 -14
- package/dist/graphql/GraphQl.d.ts.map +1 -1
- package/dist/graphql/GraphQl.js +44 -23
- package/dist/graphql/index.d.ts +1 -1
- package/dist/graphql/index.d.ts.map +1 -1
- package/dist/graphql/index.js +2 -2
- package/dist/index.d.ts +55 -22
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +50 -23
- package/dist/init/client.d.ts +43 -2
- package/dist/init/client.d.ts.map +1 -1
- package/dist/init/client.js +26 -6
- package/dist/init/server.d.ts +49 -2
- package/dist/init/server.d.ts.map +1 -1
- package/dist/init/server.js +56 -6
- package/dist/init/shared.d.ts +5 -4
- package/dist/init/shared.d.ts.map +1 -1
- package/dist/init/shared.js +3 -3
- package/dist/loadData/Globals.d.ts +41 -2
- package/dist/loadData/Globals.d.ts.map +1 -1
- package/dist/loadData/Globals.js +33 -5
- package/dist/loadData/index.d.ts +74 -11
- package/dist/loadData/index.d.ts.map +1 -1
- package/dist/loadData/index.js +5 -8
- package/dist/plugins/Events.d.ts +18 -5
- package/dist/plugins/Events.d.ts.map +1 -1
- package/dist/plugins/Events.js +3 -0
- package/dist/plugins/Plugins.d.ts +28 -2
- package/dist/plugins/Plugins.d.ts.map +1 -1
- package/dist/plugins/Plugins.js +8 -0
- package/dist/routing/History.d.ts +6 -3
- package/dist/routing/History.d.ts.map +1 -1
- package/dist/routing/History.js +7 -1
- package/dist/routing/InnerRouter.d.ts +10 -9
- package/dist/routing/InnerRouter.d.ts.map +1 -1
- package/dist/routing/InnerRouter.js +47 -35
- package/dist/routing/PageLoader.d.ts +5 -5
- package/dist/routing/PageLoader.d.ts.map +1 -1
- package/dist/routing/PageLoader.js +7 -7
- package/dist/routing/Request.d.ts +102 -0
- package/dist/routing/Request.d.ts.map +1 -0
- package/dist/routing/Request.js +128 -0
- package/dist/routing/Route.d.ts +73 -5
- package/dist/routing/Route.d.ts.map +1 -1
- package/dist/routing/Route.js +71 -6
- package/dist/routing/Router.d.ts +59 -52
- package/dist/routing/Router.d.ts.map +1 -1
- package/dist/routing/Router.js +95 -116
- package/dist/routing/Site.d.ts +1 -1
- package/dist/routing/Site.js +1 -1
- package/dist/routing/index.d.ts +4 -3
- package/dist/routing/index.d.ts.map +1 -1
- package/dist/routing/index.js +2 -1
- package/dist/ssr/SsrCache.d.ts +17 -0
- package/dist/ssr/SsrCache.d.ts.map +1 -1
- package/dist/ssr/SsrCache.js +17 -4
- package/dist/ssr/SsrComponents.d.ts +1 -0
- package/dist/ssr/SsrComponents.d.ts.map +1 -1
- package/dist/ssr/SsrComponents.js +1 -0
- package/package.json +6 -6
- package/src/Crelte.ts +124 -52
- package/src/CrelteRequest.ts +124 -0
- package/src/blocks/Blocks.svelte +35 -18
- package/src/blocks/Blocks.ts +38 -6
- package/src/blocks/index.ts +19 -10
- package/src/cookies/ServerCookies.ts +1 -1
- package/src/cookies/index.ts +7 -1
- package/src/graphql/GraphQl.ts +79 -27
- package/src/graphql/index.ts +7 -1
- package/src/index.ts +65 -29
- package/src/init/client.ts +55 -10
- package/src/init/server.ts +70 -10
- package/src/init/shared.ts +8 -7
- package/src/loadData/Globals.ts +43 -7
- package/src/loadData/index.ts +76 -13
- package/src/plugins/Events.ts +16 -5
- package/src/plugins/Plugins.ts +28 -2
- package/src/routing/History.ts +13 -4
- package/src/routing/InnerRouter.ts +55 -39
- package/src/routing/PageLoader.ts +10 -10
- package/src/routing/Request.ts +175 -0
- package/src/routing/Route.ts +73 -8
- package/src/routing/Router.ts +108 -162
- package/src/routing/Site.ts +1 -1
- package/src/routing/index.ts +12 -3
- package/src/ssr/SsrCache.ts +17 -4
- package/src/ssr/SsrComponents.ts +1 -0
- package/dist/CrelteBase.d.ts +0 -16
- package/dist/CrelteBase.d.ts.map +0 -1
- package/dist/CrelteBase.js +0 -1
- package/dist/CrelteRouted.d.ts +0 -50
- package/dist/CrelteRouted.d.ts.map +0 -1
- package/dist/CrelteRouted.js +0 -88
- package/src/CrelteBase.ts +0 -24
- package/src/CrelteRouted.ts +0 -128
package/src/Crelte.ts
CHANGED
|
@@ -1,18 +1,12 @@
|
|
|
1
1
|
import ClientCookies from './cookies/ClientCookies.js';
|
|
2
2
|
import { Cookies } from './cookies/index.js';
|
|
3
3
|
import ServerCookies from './cookies/ServerCookies.js';
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import GraphQl, {
|
|
7
|
-
GraphQlOptions,
|
|
8
|
-
GraphQlRequestOptions,
|
|
9
|
-
} from './graphql/GraphQl.js';
|
|
10
|
-
import Globals from './loadData/Globals.js';
|
|
4
|
+
import GraphQl, { GraphQlOptions, GraphQlQuery } from './graphql/GraphQl.js';
|
|
5
|
+
import Globals, { Global, GlobalData } from './loadData/Globals.js';
|
|
11
6
|
import Events from './plugins/Events.js';
|
|
12
7
|
import Plugins, { Plugin } from './plugins/Plugins.js';
|
|
13
|
-
import
|
|
14
|
-
import
|
|
15
|
-
import Site, { SiteFromGraphQl } from './routing/Site.js';
|
|
8
|
+
import Router, { RouterOptions } from './routing/Router.js';
|
|
9
|
+
import { SiteFromGraphQl } from './routing/Site.js';
|
|
16
10
|
import SsrCache from './ssr/SsrCache.js';
|
|
17
11
|
|
|
18
12
|
export class CrelteBuilder {
|
|
@@ -39,7 +33,7 @@ export class CrelteBuilder {
|
|
|
39
33
|
this.graphQl = new GraphQl(endpoint, this.ssrCache, opts);
|
|
40
34
|
}
|
|
41
35
|
|
|
42
|
-
setupRouter(sites: SiteFromGraphQl[], opts:
|
|
36
|
+
setupRouter(sites: SiteFromGraphQl[], opts: RouterOptions = {}) {
|
|
43
37
|
this.router = new Router(sites, opts);
|
|
44
38
|
}
|
|
45
39
|
|
|
@@ -51,80 +45,158 @@ export class CrelteBuilder {
|
|
|
51
45
|
return new Crelte(this);
|
|
52
46
|
}
|
|
53
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Options to configure the request
|
|
50
|
+
*/
|
|
51
|
+
export type QueryOptions = {
|
|
52
|
+
/**
|
|
53
|
+
* Ignore the response status code
|
|
54
|
+
* @default false
|
|
55
|
+
*/
|
|
56
|
+
ignoreStatusCode?: boolean;
|
|
54
57
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
events: Events;
|
|
61
|
-
globals: Globals;
|
|
62
|
-
cookies: Cookies;
|
|
58
|
+
/**
|
|
59
|
+
* Configure caching
|
|
60
|
+
* @default true
|
|
61
|
+
*/
|
|
62
|
+
caching?: boolean;
|
|
63
63
|
|
|
64
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Status code of the response
|
|
66
|
+
* This will be set after the request if
|
|
67
|
+
* `ignoreStatusCode` is set to `true`
|
|
68
|
+
*/
|
|
69
|
+
status?: number;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* This is the main class of Crelte and can be accessed
|
|
74
|
+
* in component initialisation via `getCrelte()` and is the
|
|
75
|
+
* first parameter in `loadData`
|
|
76
|
+
*/
|
|
77
|
+
export default class Crelte {
|
|
78
|
+
protected _ssrCache: SsrCache;
|
|
79
|
+
protected _graphQl: GraphQl;
|
|
80
|
+
protected _router: Router;
|
|
81
|
+
protected _plugins: Plugins;
|
|
82
|
+
protected _events: Events;
|
|
83
|
+
protected _globals: Globals;
|
|
84
|
+
protected _cookies: Cookies;
|
|
85
|
+
|
|
86
|
+
constructor(builder: CrelteBuilder | Crelte) {
|
|
65
87
|
if (!builder.graphQl || !builder.router)
|
|
66
88
|
throw new Error('builder not ready');
|
|
67
89
|
|
|
68
|
-
this.
|
|
69
|
-
this.
|
|
70
|
-
this.
|
|
71
|
-
this.
|
|
72
|
-
this.
|
|
73
|
-
this.
|
|
74
|
-
this.
|
|
90
|
+
this._ssrCache = builder.ssrCache;
|
|
91
|
+
this._graphQl = builder.graphQl;
|
|
92
|
+
this._router = builder.router;
|
|
93
|
+
this._plugins = builder.plugins;
|
|
94
|
+
this._events = builder.events;
|
|
95
|
+
this._globals = builder.globals;
|
|
96
|
+
this._cookies = builder.cookies;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Get the SSR cache
|
|
101
|
+
*/
|
|
102
|
+
get ssrCache(): SsrCache {
|
|
103
|
+
return this._ssrCache;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Get the GraphQl instance
|
|
108
|
+
*/
|
|
109
|
+
get graphQl(): GraphQl {
|
|
110
|
+
return this._graphQl;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Get the Router instance
|
|
115
|
+
*/
|
|
116
|
+
get router(): Router {
|
|
117
|
+
return this._router;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Get the Plugins instance
|
|
122
|
+
*/
|
|
123
|
+
get plugins(): Plugins {
|
|
124
|
+
return this._plugins;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Get the Events instance
|
|
129
|
+
*/
|
|
130
|
+
get events(): Events {
|
|
131
|
+
return this._events;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Get the Globals instance
|
|
136
|
+
*/
|
|
137
|
+
get globals(): Globals {
|
|
138
|
+
return this._globals;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Get the Cookies instance
|
|
143
|
+
*/
|
|
144
|
+
get cookies(): Cookies {
|
|
145
|
+
return this._cookies;
|
|
75
146
|
}
|
|
76
147
|
|
|
148
|
+
/**
|
|
149
|
+
* Get a Plugin by name
|
|
150
|
+
*/
|
|
77
151
|
getPlugin(name: string): Plugin | null {
|
|
78
152
|
return this.plugins.get(name);
|
|
79
153
|
}
|
|
80
154
|
|
|
81
155
|
/**
|
|
82
|
-
* returns an env
|
|
83
|
-
*
|
|
156
|
+
* returns an env variable from the craft/.env file.
|
|
157
|
+
* All env variables need to start with VITE_
|
|
158
|
+
* except ENDPOINT_URL and CRAFT_WEB_URL
|
|
84
159
|
*/
|
|
160
|
+
getEnv(name: 'ENDPOINT_URL'): string;
|
|
161
|
+
getEnv(name: 'CRAFT_WEB_URL'): string;
|
|
162
|
+
getEnv(name: string): string | null;
|
|
85
163
|
getEnv(name: string): string | null {
|
|
86
164
|
return this.ssrCache.get(name);
|
|
87
165
|
}
|
|
88
166
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
167
|
+
/**
|
|
168
|
+
* returns a store which contains a globalSet
|
|
169
|
+
*
|
|
170
|
+
* ## Note
|
|
171
|
+
* This only works in loadData, in loadGlobalData this will
|
|
172
|
+
* always return null. In that context you should use
|
|
173
|
+
* `CrelteRequest.getGlobalAsync`
|
|
174
|
+
*/
|
|
175
|
+
getGlobal<T extends GlobalData>(name: string): Global<T> | null {
|
|
92
176
|
return this.globals.get(name) ?? null;
|
|
93
177
|
}
|
|
94
178
|
|
|
95
|
-
/// requires a site if the route does not contain a site
|
|
96
|
-
toRouted(route?: Route, site?: Site): CrelteRouted {
|
|
97
|
-
if (!route) {
|
|
98
|
-
route = this.router.route.get();
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (!site) {
|
|
102
|
-
if (!route.site) throw new Error('site is required');
|
|
103
|
-
site = route.site;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return new CrelteRouted(this, route, site);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
179
|
/**
|
|
110
180
|
* Run a GraphQl Query
|
|
111
181
|
*
|
|
112
|
-
* @param query the default export from a graphql file
|
|
182
|
+
* @param query the default export from a graphql file or the gql`query {}`
|
|
183
|
+
* function
|
|
113
184
|
* @param variables variables that should be passed to the
|
|
114
185
|
* graphql query
|
|
115
|
-
* @param options opts `{ caching: true, previewToken: string,
|
|
116
|
-
* siteToken: string, ignoreStatusCode: false, headers: {} }`
|
|
117
186
|
*/
|
|
118
187
|
async query(
|
|
119
188
|
query: GraphQlQuery,
|
|
120
189
|
variables: Record<string, unknown> = {},
|
|
121
|
-
opts:
|
|
190
|
+
opts: QueryOptions = {},
|
|
122
191
|
): Promise<unknown> {
|
|
123
192
|
// this function is added as convenience
|
|
124
|
-
return this.
|
|
193
|
+
return this.graphQl.query(query, variables, {
|
|
194
|
+
route: this.router.route.get(),
|
|
195
|
+
...opts,
|
|
196
|
+
});
|
|
125
197
|
}
|
|
126
198
|
|
|
127
|
-
|
|
199
|
+
/** @hidden */
|
|
128
200
|
_getContext(): Map<string, any> {
|
|
129
201
|
const map = new Map();
|
|
130
202
|
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import Crelte, { QueryOptions } from './Crelte.js';
|
|
2
|
+
import { GraphQlQuery } from './graphql/GraphQl.js';
|
|
3
|
+
import Site from './routing/Site.js';
|
|
4
|
+
import Request from './routing/Request.js';
|
|
5
|
+
import Route from './routing/Route.js';
|
|
6
|
+
import { GlobalData } from './loadData/Globals.js';
|
|
7
|
+
|
|
8
|
+
export default class CrelteRequest extends Crelte {
|
|
9
|
+
/**
|
|
10
|
+
* The current request
|
|
11
|
+
*/
|
|
12
|
+
req: Request;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The current site
|
|
16
|
+
*/
|
|
17
|
+
site: Site;
|
|
18
|
+
|
|
19
|
+
private innerGlobals: Map<string, any>;
|
|
20
|
+
|
|
21
|
+
/// requires a site if the route does not contain a site
|
|
22
|
+
constructor(inner: Crelte, req: Request, site: Site) {
|
|
23
|
+
super(inner);
|
|
24
|
+
|
|
25
|
+
this.req = req;
|
|
26
|
+
this.site = site;
|
|
27
|
+
this.innerGlobals = new Map();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Create a CrelteRequest from a Crelte instance
|
|
32
|
+
*
|
|
33
|
+
* If you don't provide a route or request the current route
|
|
34
|
+
* will be used.
|
|
35
|
+
*
|
|
36
|
+
* ## Note
|
|
37
|
+
* If you provide a route it must contain a site or you must
|
|
38
|
+
* provide one,
|
|
39
|
+
*/
|
|
40
|
+
static fromCrelte(
|
|
41
|
+
inner: Crelte,
|
|
42
|
+
req?: Route | Request,
|
|
43
|
+
site?: Site,
|
|
44
|
+
): CrelteRequest {
|
|
45
|
+
if (!req) {
|
|
46
|
+
req = inner.router.route.get();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (!site) {
|
|
50
|
+
if (!req.site) throw new Error('site is required');
|
|
51
|
+
site = req.site;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return new CrelteRequest(
|
|
55
|
+
inner,
|
|
56
|
+
req instanceof Request ? req : Request.fromRoute(req),
|
|
57
|
+
site,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Get the current request
|
|
63
|
+
* @deprecated
|
|
64
|
+
*/
|
|
65
|
+
get route(): Request {
|
|
66
|
+
return this.req;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* returns a globalSet
|
|
71
|
+
*
|
|
72
|
+
* ## Note
|
|
73
|
+
* This only works in loadData, in loadGlobalData this will
|
|
74
|
+
* always return null. In that context you should use
|
|
75
|
+
* `.getGlobalAsync`
|
|
76
|
+
*/
|
|
77
|
+
getGlobal<T extends GlobalData>(name: string): T | null {
|
|
78
|
+
return this.innerGlobals.get(name) ?? null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Get a globalSet and wait until it is loaded
|
|
83
|
+
*
|
|
84
|
+
* ## Note
|
|
85
|
+
* This is only useful in loadGlobalData in all other cases
|
|
86
|
+
* you can use `.getGlobal` which does return a Promise
|
|
87
|
+
*/
|
|
88
|
+
async getGlobalAsync<T extends GlobalData>(
|
|
89
|
+
name: string,
|
|
90
|
+
): Promise<T | null> {
|
|
91
|
+
const global = this.innerGlobals.get(name);
|
|
92
|
+
if (global) return global;
|
|
93
|
+
|
|
94
|
+
const r = await this.globals.getAsync<T>(name);
|
|
95
|
+
if (!r) return null;
|
|
96
|
+
|
|
97
|
+
return r.bySiteId(this.site.id);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Run a GraphQl Query
|
|
102
|
+
*
|
|
103
|
+
* @param query the default export from a graphql file or the gql`query {}`
|
|
104
|
+
* function
|
|
105
|
+
* @param variables variables that should be passed to the
|
|
106
|
+
* graphql query
|
|
107
|
+
*/
|
|
108
|
+
async query(
|
|
109
|
+
query: GraphQlQuery,
|
|
110
|
+
variables: Record<string, unknown> = {},
|
|
111
|
+
opts: QueryOptions = {},
|
|
112
|
+
): Promise<unknown> {
|
|
113
|
+
// this function is added as convenience
|
|
114
|
+
return this.graphQl.query(query, variables, {
|
|
115
|
+
route: this.req,
|
|
116
|
+
...opts,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** @hidden */
|
|
121
|
+
_globalDataLoaded() {
|
|
122
|
+
this.innerGlobals = this.globals._globalsBySite(this.site.id);
|
|
123
|
+
}
|
|
124
|
+
}
|
package/src/blocks/Blocks.svelte
CHANGED
|
@@ -1,22 +1,10 @@
|
|
|
1
1
|
<script context="module" lang="ts">
|
|
2
|
-
import
|
|
2
|
+
import CrelteRequest from '../CrelteRequest.js';
|
|
3
3
|
|
|
4
4
|
/*
|
|
5
5
|
usage:
|
|
6
6
|
|
|
7
|
-
const mods = import.meta.glob('./contentDetail/*.svelte'
|
|
8
|
-
// can either be eager loaded or not
|
|
9
|
-
eager: true,
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
const mods = blockModules(
|
|
13
|
-
import.meta.glob('./contentDetail/*.svelte', { eager: true }),
|
|
14
|
-
{
|
|
15
|
-
alias: {
|
|
16
|
-
'contentDetail': 'contentDetail',
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
);
|
|
7
|
+
const mods = blockModules(import.meta.glob('./contentDetail/*.svelte'));
|
|
20
8
|
|
|
21
9
|
export const loadData = {
|
|
22
10
|
blocks: (cr, entry) => loadBlocksData(cr, entry.blocks, mods)
|
|
@@ -31,14 +19,32 @@
|
|
|
31
19
|
|
|
32
20
|
import Blocks, {
|
|
33
21
|
type AsyncModule,
|
|
34
|
-
type Module,
|
|
35
22
|
type BlockModulesOptions,
|
|
36
23
|
BlockModules,
|
|
37
24
|
newBlocks,
|
|
38
25
|
} from './Blocks.js';
|
|
39
26
|
|
|
40
|
-
|
|
41
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Create a BlockModules instance from modules
|
|
29
|
+
*
|
|
30
|
+
* ## Example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const mods = blockModules(
|
|
33
|
+
* import.meta.glob('./contentDetail/*.svelte', { eager: true })
|
|
34
|
+
);
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* ## Example with alias
|
|
38
|
+
* ```ts
|
|
39
|
+
* const mods = blockModules(
|
|
40
|
+
* import.meta.glob('./contentDetail/*.svelte'),
|
|
41
|
+
* {
|
|
42
|
+
* alias: {
|
|
43
|
+
* fakename: 'filename',
|
|
44
|
+
* }
|
|
45
|
+
* }
|
|
46
|
+
* );
|
|
47
|
+
*/
|
|
42
48
|
export function blockModules(
|
|
43
49
|
modules: Record<string, AsyncModule>,
|
|
44
50
|
opts: BlockModulesOptions = {},
|
|
@@ -46,8 +52,19 @@
|
|
|
46
52
|
return new BlockModules(modules, opts);
|
|
47
53
|
}
|
|
48
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Load blocks data
|
|
57
|
+
*
|
|
58
|
+
* ## Example
|
|
59
|
+
* ```ts
|
|
60
|
+
* const mods = blockModules(import.meta.glob('./contentDetail/*.svelte'));
|
|
61
|
+
*
|
|
62
|
+
* export const loadData = {
|
|
63
|
+
* blocks: (cr, entry) => loadBlocksData(cr, entry.blocks, mods)
|
|
64
|
+
* };
|
|
65
|
+
*/
|
|
49
66
|
export async function loadBlocksData(
|
|
50
|
-
cr:
|
|
67
|
+
cr: CrelteRequest,
|
|
51
68
|
blocks: any[],
|
|
52
69
|
modules: BlockModules,
|
|
53
70
|
): Promise<Blocks> {
|
package/src/blocks/Blocks.ts
CHANGED
|
@@ -1,19 +1,40 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CrelteRequest } from '../index.js';
|
|
2
2
|
import { callLoadData } from '../loadData/index.js';
|
|
3
3
|
|
|
4
4
|
export interface Module {
|
|
5
|
-
|
|
5
|
+
/** Svelte component */
|
|
6
6
|
default: any;
|
|
7
|
+
|
|
8
|
+
/** Handle of the block, this only works with { eager: true } */
|
|
7
9
|
handle?: string | string[];
|
|
10
|
+
|
|
11
|
+
/** If true the typeHandle will be avaiable for this component */
|
|
8
12
|
keepTypeHandle?: boolean;
|
|
9
13
|
|
|
10
|
-
loadData?: (
|
|
14
|
+
loadData?: (
|
|
15
|
+
cr: CrelteRequest,
|
|
16
|
+
block: any,
|
|
17
|
+
opts: BlockOptions,
|
|
18
|
+
) => Promise<any>;
|
|
11
19
|
}
|
|
12
20
|
|
|
21
|
+
export type BlockOptions = {
|
|
22
|
+
/**
|
|
23
|
+
* The returns the sibling block or null if it does not exist
|
|
24
|
+
*
|
|
25
|
+
* -1 = previous block
|
|
26
|
+
* 0 = current block
|
|
27
|
+
* 1 = next block
|
|
28
|
+
*/
|
|
29
|
+
getSibling: (offset: number) => Record<string, any> | null;
|
|
30
|
+
};
|
|
31
|
+
|
|
13
32
|
export type AsyncModule = (() => Promise<Module>) | Module;
|
|
14
33
|
|
|
15
34
|
export type BlockModulesOptions = {
|
|
16
|
-
|
|
35
|
+
/**
|
|
36
|
+
* If a block should handle multiple typehandles
|
|
37
|
+
*/
|
|
17
38
|
alias?: Record<string, string>;
|
|
18
39
|
};
|
|
19
40
|
|
|
@@ -81,7 +102,18 @@ export class BlockModules {
|
|
|
81
102
|
}
|
|
82
103
|
}
|
|
83
104
|
|
|
84
|
-
|
|
105
|
+
/**
|
|
106
|
+
* Creates a new Blocks instance
|
|
107
|
+
*
|
|
108
|
+
* Consider using the Blocks component instead
|
|
109
|
+
*
|
|
110
|
+
* @param blocks the blocks data each block must have a typeHandle
|
|
111
|
+
* @param modules the modules created with `blockModules`
|
|
112
|
+
*/
|
|
113
|
+
export async function newBlocks(
|
|
114
|
+
blocks: any[],
|
|
115
|
+
modules: BlockModules,
|
|
116
|
+
): Promise<Blocks> {
|
|
85
117
|
// define all required modules
|
|
86
118
|
const requiredModules: Set<string> = new Set();
|
|
87
119
|
|
|
@@ -107,7 +139,7 @@ export default class Blocks {
|
|
|
107
139
|
this.data = blocks.map(() => null);
|
|
108
140
|
}
|
|
109
141
|
|
|
110
|
-
async loadData(cr:
|
|
142
|
+
async loadData(cr: CrelteRequest) {
|
|
111
143
|
this.data = await Promise.all(
|
|
112
144
|
this.blocks.map((block, i) => {
|
|
113
145
|
const mod = this.modules.get(block.typeHandle)!;
|
package/src/blocks/index.ts
CHANGED
|
@@ -1,14 +1,23 @@
|
|
|
1
|
-
import Blocks, {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
} from './Blocks.
|
|
1
|
+
import Blocks, { blockModules, loadBlocksData } from './Blocks.svelte';
|
|
2
|
+
import type {
|
|
3
|
+
BlockModules,
|
|
4
|
+
BlockModulesOptions,
|
|
5
|
+
AsyncModule,
|
|
6
|
+
Module,
|
|
7
|
+
BlockOptions,
|
|
8
|
+
} from './Blocks.js';
|
|
9
|
+
import type BlocksInstance from './Blocks.js';
|
|
10
|
+
import { newBlocks } from './Blocks.js';
|
|
9
11
|
|
|
10
|
-
export type {
|
|
12
|
+
export type {
|
|
13
|
+
BlocksInstance,
|
|
14
|
+
BlockModules,
|
|
15
|
+
BlockModulesOptions,
|
|
16
|
+
AsyncModule,
|
|
17
|
+
Module,
|
|
18
|
+
BlockOptions,
|
|
19
|
+
};
|
|
11
20
|
|
|
12
|
-
export { Blocks, blockModules, loadBlocksData };
|
|
21
|
+
export { newBlocks, Blocks, blockModules, loadBlocksData };
|
|
13
22
|
|
|
14
23
|
export default Blocks;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Cookies, SetOptions } from './index.js';
|
|
2
|
-
import { parseCookies, SetCookie, setCookieToString } from './utils.js';
|
|
2
|
+
import { parseCookies, type SetCookie, setCookieToString } from './utils.js';
|
|
3
3
|
|
|
4
4
|
export default class ServerCookies implements Cookies {
|
|
5
5
|
requestCookies: Map<string, string>;
|
package/src/cookies/index.ts
CHANGED
|
@@ -14,11 +14,17 @@ export interface Cookies {
|
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* sets the value of the cookie
|
|
17
|
+
*
|
|
18
|
+
* ## Note
|
|
19
|
+
* in most cases you probably wan't to set the path to '/'
|
|
17
20
|
*/
|
|
18
21
|
set(name: string, value: string, opts?: SetOptions): void;
|
|
19
22
|
|
|
23
|
+
/**
|
|
24
|
+
* removes the cookie
|
|
25
|
+
*/
|
|
20
26
|
remove(name: string): void;
|
|
21
27
|
|
|
22
|
-
|
|
28
|
+
/** @hidden */
|
|
23
29
|
_init(cookies: string): void;
|
|
24
30
|
}
|