crelte 0.5.7 → 0.5.9

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.
Files changed (182) hide show
  1. package/dist/blocks/Blocks.d.ts +36 -0
  2. package/dist/blocks/Blocks.d.ts.map +1 -1
  3. package/dist/blocks/Blocks.js +42 -0
  4. package/dist/blocks/Blocks.svelte +2 -16
  5. package/dist/blocks/Blocks.svelte.d.ts +20 -37
  6. package/dist/blocks/Blocks.svelte.d.ts.map +1 -1
  7. package/dist/blocks/index.d.ts +2 -2
  8. package/dist/blocks/index.d.ts.map +1 -1
  9. package/dist/blocks/index.js +2 -2
  10. package/dist/cookies/ServerCookies.d.ts +1 -1
  11. package/dist/cookies/ServerCookies.js +1 -1
  12. package/dist/cookies/index.d.ts +1 -1
  13. package/dist/crelte.d.ts +21 -10
  14. package/dist/crelte.d.ts.map +1 -1
  15. package/dist/index.d.ts +19 -14
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +17 -13
  18. package/dist/init/client.d.ts +2 -2
  19. package/dist/init/client.d.ts.map +1 -1
  20. package/dist/init/client.js +3 -2
  21. package/dist/init/server.d.ts +7 -6
  22. package/dist/init/server.d.ts.map +1 -1
  23. package/dist/init/server.js +6 -6
  24. package/dist/loadData/Globals.d.ts +3 -3
  25. package/dist/loadData/Globals.js +3 -3
  26. package/dist/loadData/loadData.d.ts +8 -18
  27. package/dist/loadData/loadData.d.ts.map +1 -1
  28. package/dist/loadData/loadData.js +2 -2
  29. package/dist/node/index.d.ts +5 -0
  30. package/dist/node/index.d.ts.map +1 -1
  31. package/dist/node/index.js +84 -35
  32. package/dist/plugins/Events.d.ts +4 -4
  33. package/dist/plugins/Plugins.d.ts +2 -2
  34. package/dist/plugins/index.d.ts +2 -3
  35. package/dist/plugins/index.d.ts.map +1 -1
  36. package/dist/plugins/index.js +2 -1
  37. package/dist/queries/Queries.d.ts +9 -0
  38. package/dist/queries/Queries.d.ts.map +1 -1
  39. package/dist/queries/Queries.js +29 -17
  40. package/dist/queries/QueryError.d.ts +2 -0
  41. package/dist/queries/QueryError.d.ts.map +1 -1
  42. package/dist/queries/QueryError.js +6 -1
  43. package/dist/queries/gql.d.ts +2 -2
  44. package/dist/queries/gql.js +2 -2
  45. package/dist/queries/index.d.ts +7 -5
  46. package/dist/queries/index.d.ts.map +1 -1
  47. package/dist/queries/vars.d.ts +7 -7
  48. package/dist/queries/vars.d.ts.map +1 -1
  49. package/dist/queries/vars.js +10 -8
  50. package/dist/routing/LoadRunner.d.ts +1 -1
  51. package/dist/routing/LoadRunner.js +1 -1
  52. package/dist/routing/index.d.ts +4 -4
  53. package/dist/routing/index.d.ts.map +1 -1
  54. package/dist/routing/route/BaseRoute.d.ts +25 -25
  55. package/dist/routing/route/BaseRoute.js +24 -24
  56. package/dist/routing/route/Request.d.ts +4 -4
  57. package/dist/routing/route/Request.js +4 -4
  58. package/dist/routing/route/Route.d.ts +2 -2
  59. package/dist/routing/route/Route.js +2 -2
  60. package/dist/routing/router/BaseRouter.d.ts +4 -4
  61. package/dist/routing/router/BaseRouter.js +3 -3
  62. package/dist/routing/router/ClientRouter.d.ts +1 -1
  63. package/dist/routing/router/ClientRouter.js +1 -1
  64. package/dist/routing/router/Router.d.ts +27 -25
  65. package/dist/routing/router/Router.d.ts.map +1 -1
  66. package/dist/routing/router/Router.js +24 -21
  67. package/dist/routing/router/ServerRouter.d.ts +1 -1
  68. package/dist/routing/router/ServerRouter.js +1 -1
  69. package/dist/server/CrelteServer.d.ts +5 -5
  70. package/dist/server/CrelteServer.d.ts.map +1 -1
  71. package/dist/server/CrelteServer.js +4 -4
  72. package/dist/server/Request.d.ts +7 -1
  73. package/dist/server/Request.d.ts.map +1 -1
  74. package/dist/server/Request.js +7 -1
  75. package/dist/server/ServerRouter.d.ts +1 -1
  76. package/dist/server/ServerRouter.d.ts.map +1 -1
  77. package/dist/server/index.d.ts +4 -4
  78. package/dist/server/index.d.ts.map +1 -1
  79. package/dist/server/index.js +1 -1
  80. package/dist/server/queries/queries.d.ts.map +1 -1
  81. package/dist/server/queries/queries.js +14 -1
  82. package/dist/server/queries/routes.d.ts +10 -1
  83. package/dist/server/queries/routes.d.ts.map +1 -1
  84. package/dist/server/queries/routes.js +22 -5
  85. package/dist/server/shared.d.ts +7 -13
  86. package/dist/server/shared.d.ts.map +1 -1
  87. package/dist/server/shared.js +14 -16
  88. package/dist/ssr/SsrCache.d.ts +56 -4
  89. package/dist/ssr/SsrCache.d.ts.map +1 -1
  90. package/dist/ssr/SsrCache.js +94 -20
  91. package/dist/std/index.d.ts +13 -0
  92. package/dist/std/index.d.ts.map +1 -1
  93. package/dist/std/index.js +13 -0
  94. package/dist/std/rand/index.d.ts +34 -0
  95. package/dist/std/rand/index.d.ts.map +1 -0
  96. package/dist/std/rand/index.js +45 -0
  97. package/dist/std/rand/internal.d.ts +8 -0
  98. package/dist/std/rand/internal.d.ts.map +1 -0
  99. package/dist/std/rand/internal.js +15 -0
  100. package/dist/std/stores/Writable.d.ts +1 -1
  101. package/dist/std/stores/Writable.js +1 -1
  102. package/dist/translations/index.d.ts +28 -0
  103. package/dist/translations/index.d.ts.map +1 -0
  104. package/dist/translations/index.js +31 -0
  105. package/dist/translations/loadTranslations.d.ts +23 -0
  106. package/dist/translations/loadTranslations.d.ts.map +1 -0
  107. package/dist/translations/loadTranslations.js +26 -0
  108. package/dist/translations/loader/GlobalLoader.d.ts +34 -0
  109. package/dist/translations/loader/GlobalLoader.d.ts.map +1 -0
  110. package/dist/translations/loader/GlobalLoader.js +45 -0
  111. package/dist/translations/loader/fileLoader/ClientFileLoader.d.ts +10 -0
  112. package/dist/translations/loader/fileLoader/ClientFileLoader.d.ts.map +1 -0
  113. package/dist/translations/loader/fileLoader/ClientFileLoader.js +24 -0
  114. package/dist/translations/loader/fileLoader/NodeFileLoader.d.ts +9 -0
  115. package/dist/translations/loader/fileLoader/NodeFileLoader.d.ts.map +1 -0
  116. package/dist/translations/loader/fileLoader/NodeFileLoader.js +21 -0
  117. package/dist/translations/loader/fileLoader/fileLoader.d.ts +5 -0
  118. package/dist/translations/loader/fileLoader/fileLoader.d.ts.map +1 -0
  119. package/dist/translations/loader/fileLoader/fileLoader.js +16 -0
  120. package/dist/translations/loader/index.d.ts +7 -0
  121. package/dist/translations/loader/index.d.ts.map +1 -0
  122. package/dist/translations/loader/index.js +1 -0
  123. package/dist/translations/translationsPlugin.d.ts +64 -0
  124. package/dist/translations/translationsPlugin.d.ts.map +1 -0
  125. package/dist/translations/translationsPlugin.js +110 -0
  126. package/dist/translations/utils.d.ts +8 -0
  127. package/dist/translations/utils.d.ts.map +1 -0
  128. package/dist/translations/utils.js +15 -0
  129. package/dist/vite/index.d.ts.map +1 -1
  130. package/dist/vite/index.js +30 -38
  131. package/package.json +9 -1
  132. package/src/blocks/Blocks.svelte +4 -63
  133. package/src/blocks/Blocks.ts +53 -0
  134. package/src/blocks/index.ts +2 -2
  135. package/src/cookies/ServerCookies.ts +1 -1
  136. package/src/cookies/index.ts +1 -1
  137. package/src/crelte.ts +21 -10
  138. package/src/index.ts +19 -14
  139. package/src/init/client.ts +3 -2
  140. package/src/init/server.ts +14 -8
  141. package/src/loadData/Globals.ts +3 -3
  142. package/src/loadData/loadData.ts +8 -18
  143. package/src/node/index.ts +100 -39
  144. package/src/plugins/Events.ts +4 -4
  145. package/src/plugins/Plugins.ts +2 -2
  146. package/src/plugins/index.ts +2 -3
  147. package/src/queries/Queries.ts +35 -18
  148. package/src/queries/QueryError.ts +10 -1
  149. package/src/queries/gql.ts +2 -2
  150. package/src/queries/index.ts +7 -4
  151. package/src/queries/vars.ts +12 -8
  152. package/src/routing/LoadRunner.ts +1 -1
  153. package/src/routing/index.ts +13 -3
  154. package/src/routing/route/BaseRoute.ts +25 -25
  155. package/src/routing/route/Request.ts +4 -4
  156. package/src/routing/route/Route.ts +2 -2
  157. package/src/routing/router/BaseRouter.ts +4 -4
  158. package/src/routing/router/ClientRouter.ts +1 -1
  159. package/src/routing/router/Router.ts +28 -25
  160. package/src/routing/router/ServerRouter.ts +1 -1
  161. package/src/server/CrelteServer.ts +5 -5
  162. package/src/server/Request.ts +7 -1
  163. package/src/server/ServerRouter.ts +1 -2
  164. package/src/server/index.ts +17 -3
  165. package/src/server/queries/queries.ts +21 -3
  166. package/src/server/queries/routes.ts +41 -10
  167. package/src/server/shared.ts +28 -33
  168. package/src/ssr/SsrCache.ts +104 -22
  169. package/src/std/index.ts +14 -0
  170. package/src/std/rand/index.ts +55 -0
  171. package/src/std/rand/internal.ts +17 -0
  172. package/src/std/stores/Writable.ts +1 -1
  173. package/src/translations/index.ts +56 -0
  174. package/src/translations/loadTranslations.ts +33 -0
  175. package/src/translations/loader/GlobalLoader.ts +62 -0
  176. package/src/translations/loader/fileLoader/ClientFileLoader.ts +40 -0
  177. package/src/translations/loader/fileLoader/NodeFileLoader.ts +32 -0
  178. package/src/translations/loader/fileLoader/fileLoader.ts +19 -0
  179. package/src/translations/loader/index.ts +8 -0
  180. package/src/translations/translationsPlugin.ts +145 -0
  181. package/src/translations/utils.ts +17 -0
  182. package/src/vite/index.ts +43 -39
@@ -22,7 +22,7 @@ export type BaseRouteOptions = {
22
22
  * - `'replace'`: is set when a route is replaced via `Router.replaceState`
23
23
  * - `'push'`: is set when a route is pushed via `Router.pushState`
24
24
  *
25
- * ## Note
25
+ * #### Note
26
26
  * `replace` and `push` will not call loadData
27
27
  */
28
28
  export type RouteOrigin =
@@ -45,9 +45,9 @@ export default class BaseRoute {
45
45
  url: URL;
46
46
 
47
47
  /**
48
- * The site of the route
48
+ * The {@link Site} of the route
49
49
  *
50
- * ## Note
50
+ * #### Note
51
51
  * In the `beforeRequest` event the site might not match
52
52
  * the url and could be the default site or one that matches
53
53
  * the users language.
@@ -59,7 +59,7 @@ export default class BaseRoute {
59
59
  /**
60
60
  * The scroll position of the current route
61
61
  *
62
- * ## Note
62
+ * #### Note
63
63
  * This does not have to represent the current scroll position
64
64
  * should more be used internally.
65
65
  *
@@ -75,7 +75,7 @@ export default class BaseRoute {
75
75
  index: number;
76
76
 
77
77
  /**
78
- * The origin of this route, See [[RouteOrigin]]
78
+ * The origin of this route, See {@link RouteOrigin}
79
79
  */
80
80
  origin: RouteOrigin;
81
81
 
@@ -92,7 +92,7 @@ export default class BaseRoute {
92
92
  * This context is not persistant and should be considered "valid"
93
93
  * only for the current request / route
94
94
  *
95
- * ## Note
95
+ * #### Note
96
96
  * Consider using state instead. This will not be cloned in the clone
97
97
  * call so will always be the same object
98
98
  */
@@ -117,8 +117,8 @@ export default class BaseRoute {
117
117
  *
118
118
  * Never ends with a slash
119
119
  *
120
- * ## Example
121
- * ```
120
+ * #### Example
121
+ * ```js
122
122
  * const site = _; // site with url https://example.com/fo
123
123
  * const route = new Route('https://example.com/foo/bar/', site);
124
124
  * console.log(route.uri); // '/bar'
@@ -143,8 +143,8 @@ export default class BaseRoute {
143
143
  *
144
144
  * Never ends with a slash
145
145
  *
146
- * ## Example
147
- * ```
146
+ * #### Example
147
+ * ```js
148
148
  * const site = _; // site with url https://example.com/foo
149
149
  * const route = new Route('https://example.com/foo/bar/', null);
150
150
  * console.log(route.baseUrl); // 'https://example.com/foo'
@@ -163,11 +163,11 @@ export default class BaseRoute {
163
163
  /**
164
164
  * Returns the search params
165
165
  *
166
- * ## Note
166
+ * #### Note
167
167
  * You might also have a look at `getSearchParam` and `setSearchParam`
168
168
  *
169
- * ## Example
170
- * ```
169
+ * #### Example
170
+ * ```js
171
171
  * const route = new Route('https://example.com/foo/bar/?a=1&b=2', null);
172
172
  * console.log(route.search.get('a')); // '1'
173
173
  * ```
@@ -179,8 +179,8 @@ export default class BaseRoute {
179
179
  /**
180
180
  * Returns the hash of the route
181
181
  *
182
- * ## Example
183
- * ```
182
+ * #### Example
183
+ * ```js
184
184
  * const route = new Route('https://example.com/foo/bar/#hash', null);
185
185
  * console.log(route.hash); // '#hash'
186
186
  * ```
@@ -192,8 +192,8 @@ export default class BaseRoute {
192
192
  /**
193
193
  * Set the hash of the route
194
194
  *
195
- * ## Example
196
- * ```
195
+ * #### Example
196
+ * ```js
197
197
  * const route = new Route('https://example.com/foo/bar/', null);
198
198
  * route.hash = '#hash';
199
199
  * console.log(route.url.href); // 'https://example.com/foo/bar/#hash'
@@ -213,8 +213,8 @@ export default class BaseRoute {
213
213
  /**
214
214
  * Gets the search param
215
215
  *
216
- * ## Example
217
- * ```
216
+ * #### Example
217
+ * ```js
218
218
  * const route = new Route('https://example.com/foo/bar/?a=1&b=2', null);
219
219
  * console.log(route.getSearchParam('a')); // '1'
220
220
  * ```
@@ -227,8 +227,8 @@ export default class BaseRoute {
227
227
  * Sets the search param or removes it if the value is null, undefined or an
228
228
  * empty string
229
229
  *
230
- * ## Example
231
- * ```
230
+ * #### Example
231
+ * ```js
232
232
  * const route = new Route('https://example.com/foo/bar/?a=1&b=2', null);
233
233
  * route.setSearchParam('a', '3');
234
234
  * console.log(route.url.href); // 'https://example.com/foo/bar/?a=3&b=2'
@@ -261,7 +261,7 @@ export default class BaseRoute {
261
261
  * Sets a state value.
262
262
  * If the value is null or undefined, the key will be removed.
263
263
  *
264
- * ## When to use state
264
+ * #### When to use state
265
265
  * State is used to store additional information that persists across route changes.
266
266
  * The State is only available in the client code since it is stored using window.history.
267
267
  *
@@ -286,7 +286,7 @@ export default class BaseRoute {
286
286
  * Sets a context value.
287
287
  * If the value is null or undefined, the key will be removed.
288
288
  *
289
- * ## When to use context
289
+ * #### When to use context
290
290
  * Context is used to pass data to onRoute and onRequest handlers or exchange data between loadData calls.
291
291
  * This context is not persistent and should be considered valid only for the current request/route.
292
292
  * The context is not cloned in the clone call and will be the same object.
@@ -324,7 +324,7 @@ export default class BaseRoute {
324
324
  * This checks the url but search params do not have to be in the
325
325
  * same order
326
326
  *
327
- * ## Note
327
+ * #### Note
328
328
  * This only check the url, not site or anything else.
329
329
  */
330
330
  eq(route: BaseRoute | null) {
@@ -380,7 +380,7 @@ export default class BaseRoute {
380
380
  /**
381
381
  * Create a copy of the Route
382
382
  *
383
- * ## Note
383
+ * #### Note
384
384
  * This does not make a copy of the template or context
385
385
  */
386
386
  clone() {
@@ -107,12 +107,12 @@ export default class Request extends BaseRoute {
107
107
  * With delayRender you can make sure that the render waits
108
108
  * until you are ready. This is useful for building page transitions.
109
109
  *
110
- * ## Important
110
+ * #### Important
111
111
  * If you call delayRender you need to call `ready/remove` or the render
112
112
  * will never happen
113
113
  *
114
- * ## Example
115
- * ```
114
+ * #### Example
115
+ * ```js
116
116
  * import { onRequest } from 'crelte';
117
117
  * import { animate } from 'motion';
118
118
  *
@@ -175,7 +175,7 @@ export default class Request extends BaseRoute {
175
175
  /**
176
176
  * Create a Route from the Request
177
177
  *
178
- * ## Throws
178
+ * #### Throws
179
179
  * if the entry, template or loadedData is missing
180
180
  * or the request was cancelled
181
181
  */
@@ -48,7 +48,7 @@ export default class Route extends BaseRoute {
48
48
  /**
49
49
  * Create a new Route
50
50
  *
51
- * ## Note
51
+ * #### Note
52
52
  * This should only be created by crelte
53
53
  */
54
54
  constructor(
@@ -70,7 +70,7 @@ export default class Route extends BaseRoute {
70
70
  /**
71
71
  * Create a copy of the EntryRoute
72
72
  *
73
- * ## Note
73
+ * #### Note
74
74
  * This does not make a copy of the entry, template or loadedData.
75
75
  */
76
76
  clone() {
@@ -25,7 +25,7 @@ export default class BaseRouter {
25
25
  /**
26
26
  * The current route
27
27
  *
28
- * ## Note
28
+ * #### Note
29
29
  * Will always contain a route except in the first loadData call
30
30
  */
31
31
  route: Writable<Route | null>;
@@ -34,7 +34,7 @@ export default class BaseRouter {
34
34
  /**
35
35
  * The current site
36
36
  *
37
- * ## Note
37
+ * #### Note
38
38
  * Will always contain a site except in the first loadData call
39
39
  */
40
40
  site: Writable<Site | null>;
@@ -79,7 +79,7 @@ export default class BaseRouter {
79
79
  onRender: (
80
80
  cr: CrelteRequest,
81
81
  /**
82
- * ## Throws
82
+ * #### Throws
83
83
  * if the route is missing entry, template or loadedData
84
84
  */
85
85
  readyForRoute: () => Route,
@@ -252,7 +252,7 @@ export default class BaseRouter {
252
252
  }
253
253
 
254
254
  /**
255
- * ## Throws
255
+ * #### Throws
256
256
  * If the request completed but had an error
257
257
  */
258
258
  async handleRequest(
@@ -24,7 +24,7 @@ export default class ClientRouter extends BaseRouter {
24
24
  }
25
25
 
26
26
  /**
27
- * ## Throws
27
+ * #### Throws
28
28
  */
29
29
  async init() {
30
30
  this.listen();
@@ -9,26 +9,29 @@ import { Entry } from '../../loadData/index.js';
9
9
  *
10
10
  * If you return `false` the request will be aborted
11
11
  *
12
- * ## Example
13
- * ```
12
+ * #### Example
13
+ * ```js
14
14
  * router.replace(req => (req.hash = ''));
15
15
  * ```
16
16
  */
17
17
  export type UpdateRequest = (req: Request) => boolean | null | undefined | void;
18
18
 
19
+ /**
20
+ * The Crelte Router
21
+ */
19
22
  export default class Router {
20
23
  private inner: BaseRouter;
21
24
  private _request: Request | null;
22
25
 
23
- constructor(inner: BaseRouter) {
24
- this.inner = inner;
26
+ constructor(inner: any) {
27
+ this.inner = inner as BaseRouter;
25
28
  this._request = null;
26
29
  }
27
30
 
28
31
  /**
29
32
  * returns a store with the current route
30
33
  *
31
- * ## Note
34
+ * #### Note
32
35
  * Will always contain a route except in the first loadData call this
33
36
  * is intentional since you will get the wrong route in a loadData call.
34
37
  * In a loadData you should always use the `CrelteRequest` provided
@@ -41,10 +44,10 @@ export default class Router {
41
44
  /**
42
45
  * returns a store with the current site
43
46
  *
44
- * ## Note
47
+ * #### Note
45
48
  * Will always contain a site except in the first loadData call this
46
49
  * is intentional since you might get the wrong site if a site switch
47
- * is happening and you call this in loadData. If possible use the CrelteRequest
50
+ * is happening and you call this in loadData. If possible use the {@link CrelteRequest#site}
48
51
  * provided in each loadData call.
49
52
  *
50
53
  * Else use `router.site.get() ?? router.req.site`
@@ -56,7 +59,7 @@ export default class Router {
56
59
  /**
57
60
  * returns a store with the current entry
58
61
  *
59
- * ## Note
62
+ * #### Note
60
63
  * Will always contain an entry except in the first loadData call this
61
64
  * is intentional since you might get the wrong entry if a request is happening
62
65
  * and you call this in loadData. If possible use the CrelteRequest
@@ -69,8 +72,8 @@ export default class Router {
69
72
  /**
70
73
  * returns the latest request in progress otherwise returns null.
71
74
  *
72
- * ## Important !!
73
- * If at all possible prefer using the `CrelteRequest` provided in each
75
+ * #### Important !!
76
+ * If at all possible prefer using the {@link CrelteRequest} provided in each
74
77
  * loadData call. For example in a preload request this will return null.
75
78
  * Or a user has clicked multiple times on different links you might get
76
79
  * the url of the newer request.
@@ -121,7 +124,7 @@ export default class Router {
121
124
  }
122
125
 
123
126
  /**
124
- * Returns a site which is preffered based on the users language
127
+ * Returns a site which is preferred based on the users language
125
128
  *
126
129
  * Returns null if no site could be determined
127
130
  */
@@ -136,11 +139,11 @@ export default class Router {
136
139
  * the url needs to start with http or with a / which will be considered as
137
140
  * the site baseUrl
138
141
  *
139
- * ## Note
142
+ * #### Note
140
143
  * The origin will always be set to 'manual'
141
144
  *
142
- * ## Example
143
- * ```
145
+ * #### Example
146
+ * ```js
144
147
  * import { getRouter } from 'crelte';
145
148
  *
146
149
  * const router = getRouter();
@@ -181,13 +184,13 @@ export default class Router {
181
184
  * You can use this when using pagination for example change the route object
182
185
  * (search argument) and then call push
183
186
  *
184
- * ## Note
187
+ * #### Note
185
188
  * This will always set the origin to 'push'
186
189
  * And will clear the scrollY value if you dont provide a new one via the `opts`
187
190
  * This will disableLoadData by default if you dont provide an override via the `opts`
188
191
  *
189
- * ## Example using the update function
190
- * ```
192
+ * #### Example using the update function
193
+ * ```js
191
194
  * import { getRouter } from 'crelte';
192
195
  *
193
196
  * const router = getRouter();
@@ -196,8 +199,8 @@ export default class Router {
196
199
  * router.push(req => req.setSearchParam('page', page || null));
197
200
  * ```
198
201
  *
199
- * ## Example using the route object
200
- * ```
202
+ * #### Example using the route object
203
+ * ```js
201
204
  * import { getRouter } from 'crelte';
202
205
  *
203
206
  * const router = getRouter();
@@ -244,13 +247,13 @@ export default class Router {
244
247
  *
245
248
  * You can use this when using some filters for example a search filter
246
249
  *
247
- * ## Note
250
+ * #### Note
248
251
  * This will always set the origin to 'replace'
249
252
  * And will clear the scrollY value if you don't provide a new one via the `opts`
250
253
  * This will disableLoadData by default if you don't provide an override via the `opts`
251
254
  *
252
- * ## Example using the update function
253
- * ```
255
+ * #### Example using the update function
256
+ * ```js
254
257
  * import { getRouter } from 'crelte';
255
258
  *
256
259
  * const router = getRouter();
@@ -259,8 +262,8 @@ export default class Router {
259
262
  * router.replace(req => req.setSearchParam('search', search));
260
263
  * ```
261
264
  *
262
- * ## Example using the route object
263
- * ```
265
+ * #### Example using the route object
266
+ * ```js
264
267
  * import { getRouter } from 'crelte';
265
268
  *
266
269
  * const router = getRouter();
@@ -355,7 +358,7 @@ export default class Router {
355
358
  * Resolve a url or Route and convert it to a Request
356
359
  *
357
360
  * @param target
358
- * @param opts, any option present will override the value in target
361
+ * @param opts any option present will override the value in target
359
362
  * @return Returns null if the url does not match our host (the protocol get's ignored)
360
363
  */
361
364
  targetToRequest(
@@ -67,7 +67,7 @@ export default class ServerRouter extends BaseRouter {
67
67
  *
68
68
  * And then if no redirect happen the final route
69
69
  *
70
- * ## Throws
70
+ * #### Throws
71
71
  * If the request fails
72
72
  */
73
73
  async init(url: string): Promise<[Request, Route | null]> {
@@ -15,7 +15,7 @@ export type CrelteServerRequestOptions = {
15
15
 
16
16
  export default class CrelteServerRequest {
17
17
  /**
18
- * The current request
18
+ * The current request {@link ServerRequest}
19
19
  */
20
20
  req: ServerRequest;
21
21
 
@@ -24,7 +24,7 @@ export default class CrelteServerRequest {
24
24
  private _sites: Site[];
25
25
  private _langs: string[];
26
26
  private _queries: Queries;
27
- protected _cookies: Cookies;
27
+ private _cookies: Cookies;
28
28
 
29
29
  constructor(req: ServerRequest, opts: CrelteServerRequestOptions) {
30
30
  this.req = req;
@@ -43,7 +43,7 @@ export default class CrelteServerRequest {
43
43
  /**
44
44
  * Easy access to this.req.site
45
45
  *
46
- * ## Note
46
+ * #### Note
47
47
  * The site might not always match with the current route
48
48
  * but be the site default site or one that matches the
49
49
  * users language.
@@ -94,7 +94,7 @@ export default class CrelteServerRequest {
94
94
  /**
95
95
  * returns the frontend url with an optional path
96
96
  *
97
- * ## Note
97
+ * #### Note
98
98
  * For the origin the `FRONTEND_URL` env variable is used
99
99
  */
100
100
  frontendUrl(path?: string): URL {
@@ -104,7 +104,7 @@ export default class CrelteServerRequest {
104
104
  /**
105
105
  * returns the backend url with an optional path
106
106
  *
107
- * ## Note
107
+ * #### Note
108
108
  * For the origin the `ENDPOINT_URL` env variable is used
109
109
  */
110
110
  backendUrl(path?: string): URL {
@@ -1,12 +1,18 @@
1
1
  import { Site } from '../routing/index.js';
2
2
 
3
+ /**
4
+ * Extended Request class for server requests
5
+ *
6
+ * This extends the WebApi [Request](https://developer.mozilla.org/docs/Web/API/Request)
7
+ * and adds three properties {@link site}, {@link getParam} and {@link siteMatches}.
8
+ */
3
9
  export default class ServerRequest extends Request {
4
10
  private params: Map<string, string>;
5
11
 
6
12
  /**
7
13
  * The site of the route
8
14
  *
9
- * ## Note
15
+ * #### Note
10
16
  * The site might not always match with the current route
11
17
  * but be the site default site or one that matches the
12
18
  * users language.
@@ -1,12 +1,11 @@
1
1
  import { Methods, Pattern, Trouter } from 'trouter';
2
2
  import CrelteServerRequest from './CrelteServer.js';
3
3
  import ServerRequest from './Request.js';
4
- import { SiteFromGraphQl } from './shared.js';
5
4
  import { Queries, Query, QueryOptions } from '../queries/index.js';
6
5
  import { Site } from '../routing/index.js';
7
6
  import { SsrCache } from '../ssr/index.js';
8
7
  import { parseAcceptLanguage } from '../std/intl/index.js';
9
- import { siteFromUrl } from '../routing/Site.js';
8
+ import { SiteFromGraphQl, siteFromUrl } from '../routing/Site.js';
10
9
  import { preferredSite } from '../routing/utils.js';
11
10
 
12
11
  export type Handler = (
@@ -1,9 +1,16 @@
1
1
  import ServerRequest from './Request.js';
2
- import ServerRouter, { Handler } from './ServerRouter.js';
3
- import CrelteServerRequest from './CrelteServer.js';
2
+ import ServerRouter, {
3
+ type RouterOptions,
4
+ type Handler,
5
+ } from './ServerRouter.js';
6
+ import CrelteServerRequest, {
7
+ type CrelteServerRequestOptions,
8
+ } from './CrelteServer.js';
4
9
 
5
10
  export {
6
11
  type ServerData,
12
+ type RenderRequest,
13
+ type RenderResponse,
7
14
  type MainData,
8
15
  main,
9
16
  type Error,
@@ -11,4 +18,11 @@ export {
11
18
  mainError,
12
19
  } from '../init/server.js';
13
20
 
14
- export { CrelteServerRequest, ServerRouter, type Handler, ServerRequest };
21
+ export {
22
+ CrelteServerRequest,
23
+ CrelteServerRequestOptions,
24
+ ServerRouter,
25
+ RouterOptions,
26
+ Handler,
27
+ ServerRequest,
28
+ };
@@ -1,6 +1,6 @@
1
1
  import ServerRouter from '../ServerRouter.js';
2
2
  import QueriesCaching from './QueriesCaching.js';
3
- import { CacheIfFn, QueryRoute } from './routes.js';
3
+ import { CacheIfFn, QueryRoute, TransformFn } from './routes.js';
4
4
  import { isQueryVar, QueryVar } from '../../queries/vars.js';
5
5
  import { Platform } from '../platform.js';
6
6
 
@@ -10,8 +10,9 @@ type ModQuery = {
10
10
  };
11
11
 
12
12
  type ModTs = {
13
- variables: any;
13
+ variables?: any;
14
14
  caching?: any;
15
+ transform?: any;
15
16
  };
16
17
 
17
18
  type ModQueries = Record<string, ModQuery | ModTs>;
@@ -21,6 +22,8 @@ type PreRoute = {
21
22
  jsFile: string | null;
22
23
  vars: Record<string, QueryVar> | null;
23
24
  cacheIfFn: CacheIfFn | null;
25
+ preventCaching: boolean;
26
+ transformFn: TransformFn | null;
24
27
  };
25
28
 
26
29
  export async function initQueryRoutes(
@@ -51,6 +54,8 @@ export async function initQueryRoutes(
51
54
  jsFile: null,
52
55
  vars: null,
53
56
  cacheIfFn: null,
57
+ preventCaching: false,
58
+ transformFn: null,
54
59
  };
55
60
  preRoutes.set(name, preRoute);
56
61
  }
@@ -75,6 +80,12 @@ export async function initQueryRoutes(
75
80
 
76
81
  if (mts.caching) {
77
82
  preRoute.cacheIfFn = parseCaching(mts.caching);
83
+ } else if (typeof mts.caching === 'boolean') {
84
+ preRoute.preventCaching = true;
85
+ }
86
+
87
+ if (mts.transform) {
88
+ preRoute.transformFn = parseTransform(mts.transform);
78
89
  }
79
90
  }
80
91
 
@@ -85,7 +96,7 @@ export async function initQueryRoutes(
85
96
  for (const [name, pr] of preRoutes.entries()) {
86
97
  if (!pr.query) throw new Error(`no .graphql file for query ${name}`);
87
98
 
88
- const route = new QueryRoute(name, pr.query, pr.vars, pr.cacheIfFn);
99
+ const route = new QueryRoute(name, pr.query, pr);
89
100
 
90
101
  router.post('/queries/' + route.name, async csr =>
91
102
  route.handle(caching, csr),
@@ -119,3 +130,10 @@ function parseCaching(caching: any): CacheIfFn {
119
130
 
120
131
  return caching;
121
132
  }
133
+
134
+ function parseTransform(transform: any): TransformFn {
135
+ if (typeof transform !== 'function')
136
+ throw new Error('transform should be a function');
137
+
138
+ return transform;
139
+ }
@@ -7,30 +7,50 @@ import { calcKey } from '../../ssr/index.js';
7
7
 
8
8
  export type CacheIfFn = (response: any, vars: Record<string, any>) => boolean;
9
9
 
10
+ /// Anything other than returning undefined will replace the response
11
+ //
12
+ // Note that even if you return undefined since the response is by reference
13
+ // you're modifications will be reflected
14
+ export type TransformFn = (
15
+ response: any,
16
+ vars: Record<string, any>,
17
+ ) => void | any | Promise<void | any>;
18
+
19
+ export type QueryRouteArgs = {
20
+ vars: Record<string, QueryVar> | null;
21
+ cacheIfFn: CacheIfFn | null;
22
+ preventCaching: boolean;
23
+ transformFn: TransformFn | null;
24
+ };
25
+
10
26
  // only internal
11
27
  export class QueryRoute {
12
28
  name: string;
13
29
  query: string;
14
30
  vars: Record<string, QueryVar> | null;
15
31
  cacheIfFn: CacheIfFn | null;
32
+ transformFn: TransformFn | null;
16
33
 
17
- constructor(
18
- name: string,
19
- query: string,
20
- vars: Record<string, QueryVar> | null,
21
- cacheIfFn: CacheIfFn | null,
22
- ) {
23
- if (cacheIfFn && !vars)
34
+ constructor(name: string, query: string, args: QueryRouteArgs) {
35
+ if (args.cacheIfFn && !vars)
24
36
  throw new Error(
25
37
  'queryRoute: ' +
26
38
  name +
27
- ' cannot have cacheIfFn if there are no vars defined',
39
+ ' cannot have caching function if there are no ' +
40
+ 'variables defined',
28
41
  );
29
42
 
30
43
  this.name = name;
31
44
  this.query = query;
32
- this.vars = vars;
33
- this.cacheIfFn = cacheIfFn;
45
+ this.vars = args.vars;
46
+ this.cacheIfFn = args.cacheIfFn;
47
+ this.transformFn = args.transformFn;
48
+
49
+ if (args.preventCaching) {
50
+ if (this.cacheIfFn) throw new Error('unreachable');
51
+ // prevent filling defaults
52
+ return;
53
+ }
34
54
 
35
55
  // add default vars and cacheIfFn if we know the route
36
56
  if (this.name === 'entry') this.fillEntryDefaults();
@@ -96,6 +116,16 @@ export class QueryRoute {
96
116
  return nVars;
97
117
  }
98
118
 
119
+ private async transform(
120
+ jsonResp: Record<string, any>,
121
+ vars: Record<string, any>,
122
+ ): Promise<void> {
123
+ if (!this.transformFn || !jsonResp.data) return;
124
+
125
+ const transformed = await this.transformFn(jsonResp.data, vars);
126
+ if (typeof transformed !== 'undefined') jsonResp.data = transformed;
127
+ }
128
+
99
129
  async handle(
100
130
  caching: QueriesCaching,
101
131
  csr: CrelteServerRequest,
@@ -187,6 +217,7 @@ export class QueryRoute {
187
217
  jsonResp = await resp.json();
188
218
  if (!jsonResp || typeof jsonResp !== 'object')
189
219
  throw new Error('invalid json response');
220
+ await this.transform(jsonResp, vars);
190
221
  } catch (e) {
191
222
  return newError(e, 500);
192
223
  }