nuxt-kirby 4.0.0 โ 4.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/README.md +4 -4
- package/dist/module.d.mts +20 -5
- package/dist/module.json +1 -1
- package/dist/module.mjs +1 -0
- package/dist/runtime/composables/$kirby.d.ts +8 -0
- package/dist/runtime/composables/$kirby.js +9 -3
- package/dist/runtime/composables/$kql.d.ts +8 -0
- package/dist/runtime/composables/$kql.js +12 -3
- package/dist/runtime/composables/useKirbyData.d.ts +8 -0
- package/dist/runtime/composables/useKirbyData.js +6 -2
- package/dist/runtime/composables/useKql.d.ts +8 -0
- package/dist/runtime/composables/useKql.js +9 -2
- package/dist/runtime/server/handler.js +18 -7
- package/dist/runtime/types.d.ts +2 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -2,19 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
# Nuxt Kirby
|
|
4
4
|
|
|
5
|
-
[
|
|
5
|
+
[Kirby CMS](https://getkirby.com) data in [Nuxt](https://nuxt.com), via [KQL](https://github.com/getkirby/kql) or the REST API.
|
|
6
6
|
|
|
7
7
|
- [โจ Release Notes](https://github.com/johannschopplich/nuxt-kirby/releases)
|
|
8
8
|
- [๐ Read the documentation](https://nuxt-kirby.byjohann.dev)
|
|
9
9
|
|
|
10
10
|
## Features
|
|
11
11
|
|
|
12
|
-
- ๐ Protected Kirby credentials
|
|
12
|
+
- ๐ Protected Kirby credentials on every request
|
|
13
13
|
- ๐ชข Supports token-based authentication with the [Kirby Headless plugin](https://kirby.tools/docs/headless/getting-started/) (recommended)
|
|
14
|
-
- ๐ฑ Handle requests just like with the [`useFetch`](https://nuxt.com/docs/
|
|
14
|
+
- ๐ฑ Handle requests just like with the [`useFetch`](https://nuxt.com/docs/api/composables/use-fetch) composable
|
|
15
15
|
- ๐ฆฆ [Multiple starter kits](https://nuxt-kirby.byjohann.dev/essentials/starter-kits) available
|
|
16
16
|
- ๐ Cached query responses
|
|
17
|
-
- ๐คน No CORS issues
|
|
17
|
+
- ๐คน No CORS issues
|
|
18
18
|
- ๐ฆพ Strongly typed
|
|
19
19
|
|
|
20
20
|
## Setup
|
package/dist/module.d.mts
CHANGED
|
@@ -36,17 +36,32 @@ interface ModuleOptions {
|
|
|
36
36
|
password: string;
|
|
37
37
|
};
|
|
38
38
|
/**
|
|
39
|
-
* Send
|
|
39
|
+
* Send queries straight from the browser instead of through the server-side proxy.
|
|
40
40
|
*
|
|
41
41
|
* @remarks
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* If Nuxt SSR is disabled, this
|
|
42
|
+
* Leaving this off is what keeps your token out of the client bundle. Turn it on only for an
|
|
43
|
+
* instance whose credentials may be public, or one that needs none.
|
|
44
|
+
*
|
|
45
|
+
* If Nuxt SSR is disabled, this is enabled by default, because there is no server to proxy
|
|
46
|
+
* through.
|
|
46
47
|
*
|
|
47
48
|
* @default false
|
|
48
49
|
*/
|
|
49
50
|
client?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Forward the visitor's cookies to Kirby.
|
|
53
|
+
*
|
|
54
|
+
* @remarks
|
|
55
|
+
* A Kirby session lives in a cookie, so without this a logged-in visitor still receives the
|
|
56
|
+
* logged-out response. Turning it on skips the server-side cache, whether or not the visitor
|
|
57
|
+
* actually sent a cookie โ one stored response is shared between all visitors.
|
|
58
|
+
*
|
|
59
|
+
* Individual calls override this through the `forwardCookies` option of `useKql`, `useKirbyData`,
|
|
60
|
+
* `$kql` and `$kirby`.
|
|
61
|
+
*
|
|
62
|
+
* @default false
|
|
63
|
+
*/
|
|
64
|
+
forwardCookies?: boolean;
|
|
50
65
|
/**
|
|
51
66
|
* Prefetch custom KQL queries at build-time.
|
|
52
67
|
*
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -13,6 +13,14 @@ export type KirbyFetchOptions = Pick<NitroFetchOptions<string>, 'onRequest' | 'o
|
|
|
13
13
|
* @default true
|
|
14
14
|
*/
|
|
15
15
|
payloadCache?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Forward the visitor's cookies to Kirby, overriding the module's `forwardCookies`.
|
|
18
|
+
*
|
|
19
|
+
* @remarks
|
|
20
|
+
* Such a request never reads from or writes to the server-side cache, since one stored response
|
|
21
|
+
* is shared between all visitors.
|
|
22
|
+
*/
|
|
23
|
+
forwardCookies?: boolean;
|
|
16
24
|
/**
|
|
17
25
|
* Cache key, generated from the request options by default.
|
|
18
26
|
*/
|
|
@@ -9,7 +9,9 @@ export function $kirby(path, opts = {}) {
|
|
|
9
9
|
opts.method,
|
|
10
10
|
opts.query,
|
|
11
11
|
opts.body,
|
|
12
|
-
opts.language
|
|
12
|
+
opts.language,
|
|
13
|
+
opts.forwardCookies,
|
|
14
|
+
headersToObject(opts.headers)
|
|
13
15
|
])}`;
|
|
14
16
|
return sendCachedRequest(key, payloadCache, () => sendKirbyRequest(path, { ...requestOptions, key }));
|
|
15
17
|
}
|
|
@@ -22,6 +24,7 @@ export function sendKirbyRequest(path, opts) {
|
|
|
22
24
|
body,
|
|
23
25
|
language,
|
|
24
26
|
key,
|
|
27
|
+
forwardCookies,
|
|
25
28
|
...fetchOptions
|
|
26
29
|
} = opts;
|
|
27
30
|
const sharedHeaders = {
|
|
@@ -35,7 +38,8 @@ export function sendKirbyRequest(path, opts) {
|
|
|
35
38
|
method,
|
|
36
39
|
query,
|
|
37
40
|
body,
|
|
38
|
-
headers: sharedHeaders
|
|
41
|
+
headers: sharedHeaders,
|
|
42
|
+
forwardCookies
|
|
39
43
|
}
|
|
40
44
|
};
|
|
41
45
|
const _clientFetchOptions = {
|
|
@@ -46,7 +50,9 @@ export function sendKirbyRequest(path, opts) {
|
|
|
46
50
|
...sharedHeaders,
|
|
47
51
|
...createAuthHeader(kirby)
|
|
48
52
|
},
|
|
49
|
-
body
|
|
53
|
+
body,
|
|
54
|
+
// There is no proxy to forward anything here, so the browser has to attach the cookie itself.
|
|
55
|
+
credentials: forwardCookies ?? kirby.forwardCookies ? "include" : void 0
|
|
50
56
|
};
|
|
51
57
|
return useRequestFetch()(kirby.client ? path : buildApiProxyPath(key), {
|
|
52
58
|
...fetchOptions,
|
|
@@ -10,6 +10,14 @@ export type KqlOptions = Pick<NitroFetchOptions<string>, 'onRequest' | 'onReques
|
|
|
10
10
|
* @default true
|
|
11
11
|
*/
|
|
12
12
|
payloadCache?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Forward the visitor's cookies to Kirby, overriding the module's `forwardCookies`.
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* Such a request never reads from or writes to the server-side cache, since one stored response
|
|
18
|
+
* is shared between all visitors.
|
|
19
|
+
*/
|
|
20
|
+
forwardCookies?: boolean;
|
|
13
21
|
/**
|
|
14
22
|
* Cache key, generated from the request options by default.
|
|
15
23
|
*/
|
|
@@ -4,7 +4,12 @@ import { sendCachedRequest } from "../cache.js";
|
|
|
4
4
|
import { buildApiProxyPath, createAuthHeader, createLanguageHeader, headersToObject } from "../utils.js";
|
|
5
5
|
export function $kql(query, opts = {}) {
|
|
6
6
|
const { payloadCache = true, ...requestOptions } = opts;
|
|
7
|
-
const key = opts.key || `$kql${hash([
|
|
7
|
+
const key = opts.key || `$kql${hash([
|
|
8
|
+
query,
|
|
9
|
+
opts.language,
|
|
10
|
+
opts.forwardCookies,
|
|
11
|
+
headersToObject(opts.headers)
|
|
12
|
+
])}`;
|
|
8
13
|
return sendCachedRequest(key, payloadCache, () => sendKqlRequest(query, { ...requestOptions, key }));
|
|
9
14
|
}
|
|
10
15
|
export function sendKqlRequest(query, opts) {
|
|
@@ -13,6 +18,7 @@ export function sendKqlRequest(query, opts) {
|
|
|
13
18
|
headers,
|
|
14
19
|
language,
|
|
15
20
|
key,
|
|
21
|
+
forwardCookies,
|
|
16
22
|
...fetchOptions
|
|
17
23
|
} = opts;
|
|
18
24
|
const sharedHeaders = {
|
|
@@ -23,7 +29,8 @@ export function sendKqlRequest(query, opts) {
|
|
|
23
29
|
method: "POST",
|
|
24
30
|
body: {
|
|
25
31
|
query,
|
|
26
|
-
headers: sharedHeaders
|
|
32
|
+
headers: sharedHeaders,
|
|
33
|
+
forwardCookies
|
|
27
34
|
}
|
|
28
35
|
};
|
|
29
36
|
const _clientFetchOptions = {
|
|
@@ -33,7 +40,9 @@ export function sendKqlRequest(query, opts) {
|
|
|
33
40
|
...sharedHeaders,
|
|
34
41
|
...createAuthHeader(kirby)
|
|
35
42
|
},
|
|
36
|
-
body: query
|
|
43
|
+
body: query,
|
|
44
|
+
// There is no proxy to forward anything here, so the browser has to attach the cookie itself.
|
|
45
|
+
credentials: forwardCookies ?? kirby.forwardCookies ? "include" : void 0
|
|
37
46
|
};
|
|
38
47
|
return useRequestFetch()(kirby.client ? kirby.kqlPath : buildApiProxyPath(key), {
|
|
39
48
|
...fetchOptions,
|
|
@@ -10,6 +10,14 @@ export type UseKirbyDataOptions<T> = Omit<AsyncDataOptions<T>, 'watch'> & Pick<N
|
|
|
10
10
|
* reads it on the global-routes catch-all, but only for a path that names no language itself.
|
|
11
11
|
*/
|
|
12
12
|
language?: MaybeRefOrGetter<string>;
|
|
13
|
+
/**
|
|
14
|
+
* Forward the visitor's cookies to Kirby, overriding the module's `forwardCookies`.
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* Such a request never reads from or writes to the server-side cache, since one stored response
|
|
18
|
+
* is shared between all visitors.
|
|
19
|
+
*/
|
|
20
|
+
forwardCookies?: boolean;
|
|
13
21
|
/**
|
|
14
22
|
* Watch an array of reactive sources and auto-refresh the fetch result when they change.
|
|
15
23
|
* Path and language are watched by default. You can completely ignore reactive sources by using `watch: false`.
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { hash } from "ohash";
|
|
2
2
|
import { computed, toValue } from "vue";
|
|
3
3
|
import { useFetch } from "#imports";
|
|
4
|
+
import { headersToObject } from "../utils.js";
|
|
4
5
|
import { sendKirbyRequest } from "./$kirby.js";
|
|
5
6
|
export function useKirbyData(path, opts = {}) {
|
|
6
|
-
const { language, ...fetchOptions } = opts;
|
|
7
|
+
const { language, forwardCookies, ...fetchOptions } = opts;
|
|
7
8
|
const _language = computed(() => toValue(language));
|
|
8
9
|
const _path = computed(() => toValue(path).replace(/^\//, ""));
|
|
9
10
|
const key = computed(() => `$kirby${hash([
|
|
@@ -11,7 +12,9 @@ export function useKirbyData(path, opts = {}) {
|
|
|
11
12
|
_language.value,
|
|
12
13
|
toValue(fetchOptions.query),
|
|
13
14
|
toValue(fetchOptions.method),
|
|
14
|
-
toValue(fetchOptions.body)
|
|
15
|
+
toValue(fetchOptions.body),
|
|
16
|
+
forwardCookies,
|
|
17
|
+
headersToObject(toValue(fetchOptions.headers))
|
|
15
18
|
])}`);
|
|
16
19
|
if (!_path.value)
|
|
17
20
|
console.warn("[nuxt-kirby] Empty Kirby path");
|
|
@@ -21,6 +24,7 @@ export function useKirbyData(path, opts = {}) {
|
|
|
21
24
|
$fetch: ((_request, options) => sendKirbyRequest(_path.value, {
|
|
22
25
|
...options,
|
|
23
26
|
language: _language.value,
|
|
27
|
+
forwardCookies,
|
|
24
28
|
key: key.value
|
|
25
29
|
}))
|
|
26
30
|
});
|
|
@@ -7,6 +7,14 @@ export type UseKqlOptions<T> = Omit<AsyncDataOptions<T>, 'watch'> & Pick<NitroFe
|
|
|
7
7
|
* Language code to fetch data for in multi-language Kirby setups.
|
|
8
8
|
*/
|
|
9
9
|
language?: MaybeRefOrGetter<string>;
|
|
10
|
+
/**
|
|
11
|
+
* Forward the visitor's cookies to Kirby, overriding the module's `forwardCookies`.
|
|
12
|
+
*
|
|
13
|
+
* @remarks
|
|
14
|
+
* Such a request never reads from or writes to the server-side cache, since one stored response
|
|
15
|
+
* is shared between all visitors.
|
|
16
|
+
*/
|
|
17
|
+
forwardCookies?: boolean;
|
|
10
18
|
/**
|
|
11
19
|
* Watch an array of reactive sources and auto-refresh the fetch result when they change.
|
|
12
20
|
* Query and language are watched by default. You can completely ignore reactive sources by using `watch: false`.
|
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
import { hash } from "ohash";
|
|
2
2
|
import { computed, toValue } from "vue";
|
|
3
3
|
import { useFetch } from "#imports";
|
|
4
|
+
import { headersToObject } from "../utils.js";
|
|
4
5
|
import { sendKqlRequest } from "./$kql.js";
|
|
5
6
|
export function useKql(query, opts = {}) {
|
|
6
|
-
const { language, ...fetchOptions } = opts;
|
|
7
|
+
const { language, forwardCookies, ...fetchOptions } = opts;
|
|
7
8
|
const _query = computed(() => toValue(query));
|
|
8
9
|
const _language = computed(() => toValue(language));
|
|
9
|
-
const key = computed(() => `$kql${hash([
|
|
10
|
+
const key = computed(() => `$kql${hash([
|
|
11
|
+
_query.value,
|
|
12
|
+
_language.value,
|
|
13
|
+
forwardCookies,
|
|
14
|
+
headersToObject(toValue(fetchOptions.headers))
|
|
15
|
+
])}`);
|
|
10
16
|
if (Object.keys(_query.value).length === 0 || !_query.value.query)
|
|
11
17
|
console.error("[nuxt-kirby] Empty KQL query");
|
|
12
18
|
return useFetch(key, {
|
|
@@ -15,6 +21,7 @@ export function useKql(query, opts = {}) {
|
|
|
15
21
|
$fetch: ((_request, options) => sendKqlRequest(_query.value, {
|
|
16
22
|
...options,
|
|
17
23
|
language: _language.value,
|
|
24
|
+
forwardCookies,
|
|
18
25
|
key: key.value
|
|
19
26
|
}))
|
|
20
27
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { destr } from "destr";
|
|
2
|
-
import { createError, defineEventHandler, getRouterParam, readBody, setResponseHeader, setResponseStatus, splitCookiesString } from "h3";
|
|
2
|
+
import { createError, defineEventHandler, getRequestHeader, getRouterParam, readBody, setResponseHeader, setResponseStatus, splitCookiesString } from "h3";
|
|
3
3
|
import { defineCachedFunction } from "nitropack/runtime";
|
|
4
4
|
import { hash } from "ohash";
|
|
5
5
|
import { base64ToUint8Array, uint8ArrayToBase64, uint8ArrayToString } from "uint8array-extras";
|
|
@@ -19,9 +19,11 @@ async function fetchFromKirby(event, {
|
|
|
19
19
|
path,
|
|
20
20
|
headers,
|
|
21
21
|
method,
|
|
22
|
-
body
|
|
22
|
+
body,
|
|
23
|
+
forwardCookies
|
|
23
24
|
}) {
|
|
24
25
|
const kirby = useRuntimeConfig(event).kirby;
|
|
26
|
+
const cookie = forwardCookies ? getRequestHeader(event, "cookie") : void 0;
|
|
25
27
|
const response = await globalThis.$fetch.raw(isQueryRequest ? kirby.kqlPath : path, {
|
|
26
28
|
responseType: "arrayBuffer",
|
|
27
29
|
ignoreResponseError: true,
|
|
@@ -36,6 +38,7 @@ async function fetchFromKirby(event, {
|
|
|
36
38
|
},
|
|
37
39
|
headers: {
|
|
38
40
|
...headers,
|
|
41
|
+
...cookie ? { cookie } : {},
|
|
39
42
|
...createAuthHeader(kirby)
|
|
40
43
|
}
|
|
41
44
|
});
|
|
@@ -51,8 +54,14 @@ function createCachedFetcher(kirby) {
|
|
|
51
54
|
// Nitro reads the event from the first argument only, and hands the cache write to
|
|
52
55
|
// `event.waitUntil()` โ without it an edge worker is torn down before the entry reaches storage.
|
|
53
56
|
async (event, options) => {
|
|
54
|
-
const { data, ...rest } = await fetchFromKirby(event, options);
|
|
55
|
-
return {
|
|
57
|
+
const { data, headers, ...rest } = await fetchFromKirby(event, options);
|
|
58
|
+
return {
|
|
59
|
+
...rest,
|
|
60
|
+
// A stored `set-cookie` would hand every later visitor on this key the session Kirby
|
|
61
|
+
// issued for the first one.
|
|
62
|
+
headers: headers.filter(([key]) => key !== "set-cookie"),
|
|
63
|
+
data: uint8ArrayToBase64(data)
|
|
64
|
+
};
|
|
56
65
|
},
|
|
57
66
|
{
|
|
58
67
|
name: "nuxt-kirby",
|
|
@@ -85,14 +94,16 @@ export default defineEventHandler(async (event) => {
|
|
|
85
94
|
});
|
|
86
95
|
}
|
|
87
96
|
}
|
|
97
|
+
const forwardCookies = body.forwardCookies ?? kirby.forwardCookies;
|
|
88
98
|
try {
|
|
89
99
|
let response;
|
|
90
|
-
|
|
100
|
+
const request = { isQueryRequest, ...body, forwardCookies };
|
|
101
|
+
if (kirby.server.cache && !forwardCookies) {
|
|
91
102
|
cachedFetcher ??= createCachedFetcher(kirby);
|
|
92
|
-
const cached = await cachedFetcher(event,
|
|
103
|
+
const cached = await cachedFetcher(event, request);
|
|
93
104
|
response = { ...cached, data: base64ToUint8Array(cached.data) };
|
|
94
105
|
} else {
|
|
95
|
-
response = await fetchFromKirby(event,
|
|
106
|
+
response = await fetchFromKirby(event, request);
|
|
96
107
|
}
|
|
97
108
|
const dataArray = response.data;
|
|
98
109
|
if (response.status >= 400 && response.status < 600) {
|
package/dist/runtime/types.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ export interface ServerFetchOptions extends Pick<NitroFetchOptions<string>, 'met
|
|
|
4
4
|
/** A request carries one or the other: `query` for KQL, `path` for the REST API. */
|
|
5
5
|
query?: Partial<KirbyQueryRequest>;
|
|
6
6
|
path?: string;
|
|
7
|
+
/** Overrides the module's `forwardCookies` for this request. Absent leaves the decision to it. */
|
|
8
|
+
forwardCookies?: boolean;
|
|
7
9
|
}
|
|
8
10
|
export interface ServerFetchRequest extends ServerFetchOptions {
|
|
9
11
|
isQueryRequest: boolean;
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-kirby",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "4.
|
|
4
|
+
"version": "4.1.0",
|
|
5
5
|
"packageManager": "pnpm@11.20.0",
|
|
6
|
-
"description": "Kirby CMS
|
|
6
|
+
"description": "Kirby CMS data in Nuxt, via KQL or the REST API",
|
|
7
7
|
"author": "Johann Schopplich <hello@johannschopplich.com>",
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"homepage": "https://nuxt-kirby.byjohann.dev",
|