nuxt-kirby 3.0.3 โ 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 +7 -11
- package/dist/module.d.mts +32 -32
- package/dist/module.json +3 -3
- package/dist/module.mjs +6 -31
- package/dist/runtime/cache.d.ts +11 -0
- package/dist/runtime/cache.js +25 -0
- package/dist/runtime/composables/$kirby.d.ts +19 -5
- package/dist/runtime/composables/$kirby.js +27 -35
- package/dist/runtime/composables/$kql.d.ts +15 -5
- package/dist/runtime/composables/$kql.js +20 -25
- package/dist/runtime/composables/useKirbyData.d.ts +11 -6
- package/dist/runtime/composables/useKirbyData.js +23 -56
- package/dist/runtime/composables/useKql.d.ts +7 -5
- package/dist/runtime/composables/useKql.js +21 -42
- package/dist/runtime/server/handler.d.ts +1 -1
- package/dist/runtime/server/handler.js +81 -53
- package/dist/runtime/server/imports.d.ts +12 -4
- package/dist/runtime/server/imports.js +4 -6
- package/dist/runtime/types.d.ts +6 -1
- package/dist/runtime/utils.d.ts +12 -1
- package/dist/runtime/utils.js +5 -2
- package/package.json +23 -21
package/README.md
CHANGED
|
@@ -2,36 +2,30 @@
|
|
|
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
|
|
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
|
|
21
21
|
|
|
22
|
-
> [!TIP]
|
|
23
|
-
> [๐ Read the documentation](https://nuxt-kirby.byjohann.dev)
|
|
24
|
-
|
|
25
22
|
```bash
|
|
26
23
|
npx nuxt module add kirby
|
|
27
24
|
```
|
|
28
25
|
|
|
29
26
|
## Basic Usage
|
|
30
27
|
|
|
31
|
-
|
|
32
|
-
> [๐ Read the documentation](https://nuxt-kirby.byjohann.dev)
|
|
33
|
-
|
|
34
|
-
Add the Nuxt Kirby module to your Nuxt config:
|
|
28
|
+
Add Nuxt Kirby to your Nuxt configuration:
|
|
35
29
|
|
|
36
30
|
```ts
|
|
37
31
|
// `nuxt.config.ts`
|
|
@@ -57,6 +51,8 @@ const { data, error, status } = await useKql({
|
|
|
57
51
|
</template>
|
|
58
52
|
```
|
|
59
53
|
|
|
54
|
+
The [documentation](https://nuxt-kirby.byjohann.dev) covers authentication, multi-language sites and every module option.
|
|
55
|
+
|
|
60
56
|
## ๐ป Development
|
|
61
57
|
|
|
62
58
|
1. Clone this repository
|
package/dist/module.d.mts
CHANGED
|
@@ -3,43 +3,31 @@ import { KirbyQueryRequest } from 'kirby-types';
|
|
|
3
3
|
|
|
4
4
|
interface ModuleOptions {
|
|
5
5
|
/**
|
|
6
|
-
* Kirby base URL, like `https://kirby.example.com
|
|
6
|
+
* Kirby base URL, like `https://kirby.example.com`.
|
|
7
7
|
*
|
|
8
8
|
* @default process.env.KIRBY_BASE_URL
|
|
9
9
|
*/
|
|
10
10
|
url?: string;
|
|
11
11
|
/**
|
|
12
|
-
* Kirby KQL API endpoint path
|
|
12
|
+
* Kirby KQL API endpoint path.
|
|
13
13
|
*
|
|
14
|
-
* @default 'api/
|
|
15
|
-
* @default 'api/kql' // for `bearer` authentication
|
|
14
|
+
* @default 'api/kql' for `bearer` authentication, 'api/query' otherwise
|
|
16
15
|
*/
|
|
17
16
|
kqlPath?: string;
|
|
18
17
|
/**
|
|
19
|
-
* Kirby
|
|
20
|
-
*
|
|
21
|
-
* @deprecated Use `kqlPath` instead
|
|
22
|
-
* @default 'api/query' // for `basic` authentication
|
|
23
|
-
* @default 'api/kql' // for `bearer` authentication
|
|
24
|
-
*/
|
|
25
|
-
prefix?: string;
|
|
26
|
-
/**
|
|
27
|
-
* Kirby API authentication method
|
|
28
|
-
*
|
|
29
|
-
* @remarks
|
|
30
|
-
* Set to `none` to disable authentication
|
|
18
|
+
* Kirby API authentication method.
|
|
31
19
|
*
|
|
32
20
|
* @default 'basic'
|
|
33
21
|
*/
|
|
34
22
|
auth?: 'basic' | 'bearer' | 'none';
|
|
35
23
|
/**
|
|
36
|
-
* Token for bearer authentication
|
|
24
|
+
* Token for bearer authentication.
|
|
37
25
|
*
|
|
38
26
|
* @default process.env.KIRBY_API_TOKEN
|
|
39
27
|
*/
|
|
40
28
|
token?: string;
|
|
41
29
|
/**
|
|
42
|
-
* Username/password pair for basic authentication
|
|
30
|
+
* Username/password pair for basic authentication.
|
|
43
31
|
*
|
|
44
32
|
* @default { username: process.env.KIRBY_API_USERNAME, password: process.env.KIRBY_API_PASSWORD }
|
|
45
33
|
*/
|
|
@@ -48,19 +36,34 @@ interface ModuleOptions {
|
|
|
48
36
|
password: string;
|
|
49
37
|
};
|
|
50
38
|
/**
|
|
51
|
-
* Send
|
|
39
|
+
* Send queries straight from the browser instead of through the server-side proxy.
|
|
52
40
|
*
|
|
53
41
|
* @remarks
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* 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.
|
|
58
47
|
*
|
|
59
48
|
* @default false
|
|
60
49
|
*/
|
|
61
50
|
client?: boolean;
|
|
62
51
|
/**
|
|
63
|
-
*
|
|
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;
|
|
65
|
+
/**
|
|
66
|
+
* Prefetch custom KQL queries at build-time.
|
|
64
67
|
*
|
|
65
68
|
* @remarks
|
|
66
69
|
* The queries will be fully typed and importable from `#nuxt-kirby`.
|
|
@@ -71,39 +74,36 @@ interface ModuleOptions {
|
|
|
71
74
|
query: KirbyQueryRequest;
|
|
72
75
|
language: string;
|
|
73
76
|
}>;
|
|
74
|
-
/**
|
|
75
|
-
* Server-side features
|
|
76
|
-
*/
|
|
77
77
|
server?: {
|
|
78
78
|
/**
|
|
79
|
-
* Enable server-side caching of queries using the Nitro cache API
|
|
79
|
+
* Enable server-side caching of queries using the Nitro cache API.
|
|
80
80
|
*
|
|
81
81
|
* @see https://nitro.unjs.io/guide/cache
|
|
82
82
|
*/
|
|
83
83
|
cache?: boolean;
|
|
84
84
|
/**
|
|
85
|
-
* Name of the storage mountpoint to use for caching
|
|
85
|
+
* Name of the storage mountpoint to use for caching.
|
|
86
86
|
*
|
|
87
87
|
* @see https://nitro.unjs.io/guide/cache
|
|
88
88
|
* @default 'cache'
|
|
89
89
|
*/
|
|
90
90
|
storage?: string;
|
|
91
91
|
/**
|
|
92
|
-
* Enable stale-while-revalidate behavior (cache is returned while it is being updated)
|
|
92
|
+
* Enable stale-while-revalidate behavior (cache is returned while it is being updated).
|
|
93
93
|
*
|
|
94
94
|
* @see https://nitro.unjs.io/guide/cache#options
|
|
95
95
|
* @default false
|
|
96
96
|
*/
|
|
97
97
|
swr?: boolean;
|
|
98
98
|
/**
|
|
99
|
-
* Number of seconds to cache the query response
|
|
99
|
+
* Number of seconds to cache the query response.
|
|
100
100
|
*
|
|
101
101
|
* @see https://nitro.unjs.io/guide/cache#options
|
|
102
102
|
* @default 1
|
|
103
103
|
*/
|
|
104
104
|
maxAge?: number;
|
|
105
105
|
/**
|
|
106
|
-
* Log verbose errors to the console if a query fails
|
|
106
|
+
* Log verbose errors to the console if a query fails.
|
|
107
107
|
*
|
|
108
108
|
* @remarks
|
|
109
109
|
* This will log the full query to the console. Depending on the content of the query, this could be a security risk.
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -5,7 +5,7 @@ import { join } from 'pathe';
|
|
|
5
5
|
import { pascalCase } from 'scule';
|
|
6
6
|
import { withLeadingSlash } from 'ufo';
|
|
7
7
|
import { ofetch } from 'ofetch';
|
|
8
|
-
import { createAuthHeader } from '../dist/runtime/utils.js';
|
|
8
|
+
import { createAuthHeader, createLanguageHeader } from '../dist/runtime/utils.js';
|
|
9
9
|
|
|
10
10
|
const name = "nuxt-kirby";
|
|
11
11
|
|
|
@@ -39,8 +39,8 @@ async function prefetchQueries(options) {
|
|
|
39
39
|
method: "POST",
|
|
40
40
|
body: language ? query.query : query,
|
|
41
41
|
headers: {
|
|
42
|
-
...
|
|
43
|
-
...
|
|
42
|
+
...createLanguageHeader(language),
|
|
43
|
+
...createAuthHeader(options)
|
|
44
44
|
}
|
|
45
45
|
})
|
|
46
46
|
);
|
|
@@ -61,20 +61,6 @@ ${JSON.stringify(_error.data, void 0, 2)}` : `: ${_error}`}`
|
|
|
61
61
|
return results;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
const KIRBY_TYPES_PKG_EXPORT_NAMES = [
|
|
65
|
-
"KirbyApiResponse",
|
|
66
|
-
"KirbyBlock",
|
|
67
|
-
"KirbyDefaultBlocks",
|
|
68
|
-
"KirbyDefaultBlockType",
|
|
69
|
-
"KirbyLayout",
|
|
70
|
-
"KirbyLayoutColumn",
|
|
71
|
-
"KirbyQuery",
|
|
72
|
-
"KirbyQueryChain",
|
|
73
|
-
"KirbyQueryModel",
|
|
74
|
-
"KirbyQueryRequest",
|
|
75
|
-
"KirbyQueryResponse",
|
|
76
|
-
"KirbyQuerySchema"
|
|
77
|
-
];
|
|
78
64
|
const module$1 = defineNuxtModule({
|
|
79
65
|
meta: {
|
|
80
66
|
name,
|
|
@@ -86,7 +72,6 @@ const module$1 = defineNuxtModule({
|
|
|
86
72
|
defaults: {
|
|
87
73
|
url: process.env.KIRBY_BASE_URL || "",
|
|
88
74
|
kqlPath: "",
|
|
89
|
-
prefix: "",
|
|
90
75
|
auth: "basic",
|
|
91
76
|
token: process.env.KIRBY_API_TOKEN || "",
|
|
92
77
|
credentials: {
|
|
@@ -94,6 +79,7 @@ const module$1 = defineNuxtModule({
|
|
|
94
79
|
password: process.env.KIRBY_API_PASSWORD || ""
|
|
95
80
|
},
|
|
96
81
|
client: false,
|
|
82
|
+
forwardCookies: false,
|
|
97
83
|
prefetch: {},
|
|
98
84
|
server: {
|
|
99
85
|
cache: false,
|
|
@@ -111,16 +97,7 @@ const module$1 = defineNuxtModule({
|
|
|
111
97
|
logger.error("Missing `KIRBY_API_USERNAME` and `KIRBY_API_PASSWORD` environment variable for basic authentication");
|
|
112
98
|
if (options.auth === "bearer" && !options.token)
|
|
113
99
|
logger.error("Missing `KIRBY_API_TOKEN` environment variable for bearer authentication");
|
|
114
|
-
|
|
115
|
-
logger.warn("The `prefix` option is deprecated. Please use `kqlPath` instead.");
|
|
116
|
-
options.kqlPath = options.prefix;
|
|
117
|
-
}
|
|
118
|
-
if (!options.kqlPath) {
|
|
119
|
-
if (options.auth === "basic")
|
|
120
|
-
options.kqlPath = "api/query";
|
|
121
|
-
else if (options.auth === "bearer")
|
|
122
|
-
options.kqlPath = "api/kql";
|
|
123
|
-
}
|
|
100
|
+
options.kqlPath ||= options.auth === "bearer" ? "api/kql" : "api/query";
|
|
124
101
|
if (!nuxt.options.ssr) {
|
|
125
102
|
logger.info("Kirby requests are client-only because SSR is disabled");
|
|
126
103
|
options.client = true;
|
|
@@ -147,7 +124,6 @@ const module$1 = defineNuxtModule({
|
|
|
147
124
|
addImports(
|
|
148
125
|
["$kirby", "$kql", "useKirbyData", "useKql"].map((name2) => ({
|
|
149
126
|
name: name2,
|
|
150
|
-
as: name2,
|
|
151
127
|
from: resolve(`runtime/composables/${name2}`)
|
|
152
128
|
}))
|
|
153
129
|
);
|
|
@@ -163,7 +139,6 @@ const module$1 = defineNuxtModule({
|
|
|
163
139
|
});
|
|
164
140
|
});
|
|
165
141
|
nuxt.options.alias[`#${moduleName}`] = join(nuxt.options.buildDir, `module/${moduleName}`);
|
|
166
|
-
nuxt.options.alias["#nuxt-kql"] = join(nuxt.options.buildDir, `module/${moduleName}`);
|
|
167
142
|
const prefetchedQueries = await prefetchQueries(options);
|
|
168
143
|
addTemplate({
|
|
169
144
|
filename: `module/${moduleName}.mjs`,
|
|
@@ -183,7 +158,7 @@ export const ${key} = ${JSON.stringify(response?.result || null, void 0, 2)}
|
|
|
183
158
|
getContents() {
|
|
184
159
|
return `
|
|
185
160
|
// Generated by ${moduleName}
|
|
186
|
-
export type
|
|
161
|
+
export type * from 'kirby-types'
|
|
187
162
|
|
|
188
163
|
${[...prefetchedQueries.entries()].map(([key, response]) => `
|
|
189
164
|
export declare const ${key}: ${JSON.stringify(response?.result || null, void 0, 2)}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sends the request unless an identical one is already in flight, or has already put its response
|
|
3
|
+
* into the Nuxt payload.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* `payloadCache` decides what happens within one environment. Across the two, the payload is the
|
|
7
|
+
* only way an SSR response reaches the client, so the server always writes and the hydrating client
|
|
8
|
+
* always reads โ otherwise turning the option off would cost every request a second round trip in
|
|
9
|
+
* the browser.
|
|
10
|
+
*/
|
|
11
|
+
export declare function sendCachedRequest<T>(requestKey: string, payloadCache: boolean, send: () => Promise<T>): Promise<T>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useNuxtApp } from "#imports";
|
|
2
|
+
export function sendCachedRequest(requestKey, payloadCache, send) {
|
|
3
|
+
const nuxt = useNuxtApp();
|
|
4
|
+
const pendingRequests = nuxt._pendingRequests ||= /* @__PURE__ */ new Map();
|
|
5
|
+
const cachesPayload = import.meta.server || payloadCache;
|
|
6
|
+
const key = `raw:${requestKey}`;
|
|
7
|
+
if ((nuxt.isHydrating || payloadCache) && nuxt.payload.data[key])
|
|
8
|
+
return Promise.resolve(nuxt.payload.data[key]);
|
|
9
|
+
const pendingRequest = pendingRequests.get(key);
|
|
10
|
+
if (pendingRequest)
|
|
11
|
+
return pendingRequest;
|
|
12
|
+
const request = send().then((response) => {
|
|
13
|
+
if (cachesPayload)
|
|
14
|
+
nuxt.payload.data[key] = response;
|
|
15
|
+
return response;
|
|
16
|
+
}).catch((error) => {
|
|
17
|
+
if (cachesPayload)
|
|
18
|
+
nuxt.payload.data[key] = void 0;
|
|
19
|
+
throw error;
|
|
20
|
+
}).finally(() => {
|
|
21
|
+
pendingRequests.delete(key);
|
|
22
|
+
});
|
|
23
|
+
pendingRequests.set(key, request);
|
|
24
|
+
return request;
|
|
25
|
+
}
|
|
@@ -2,18 +2,32 @@ import type { NitroFetchOptions } from 'nitropack';
|
|
|
2
2
|
export type KirbyFetchOptions = Pick<NitroFetchOptions<string>, 'onRequest' | 'onRequestError' | 'onResponse' | 'onResponseError' | 'method' | 'headers' | 'query' | 'body' | 'retry' | 'retryDelay' | 'retryStatusCodes' | 'timeout' | 'signal'> & {
|
|
3
3
|
/**
|
|
4
4
|
* Language code to fetch data for in multi-language Kirby setups.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* Travels as the `X-Language` header, which Kirby reads on API routes. Kirby Headless 8.1 also
|
|
8
|
+
* reads it on the global-routes catch-all, but only for a path that names no language itself.
|
|
5
9
|
*/
|
|
6
10
|
language?: string;
|
|
7
11
|
/**
|
|
8
|
-
*
|
|
12
|
+
* Serve a repeated request from the Nuxt payload instead of sending it again.
|
|
9
13
|
* @default true
|
|
10
14
|
*/
|
|
11
|
-
|
|
15
|
+
payloadCache?: boolean;
|
|
12
16
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* @
|
|
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;
|
|
24
|
+
/**
|
|
25
|
+
* Cache key, generated from the request options by default.
|
|
16
26
|
*/
|
|
17
27
|
key?: string;
|
|
18
28
|
};
|
|
19
29
|
export declare function $kirby<T = any>(path: string, opts?: KirbyFetchOptions): Promise<T>;
|
|
30
|
+
/** Sends the request with nothing cached in front of it. */
|
|
31
|
+
export declare function sendKirbyRequest<T = any>(path: string, opts: Omit<KirbyFetchOptions, 'payloadCache'> & {
|
|
32
|
+
key: string;
|
|
33
|
+
}): Promise<T>;
|
|
@@ -1,35 +1,36 @@
|
|
|
1
|
-
import { useNuxtApp, useRequestFetch, useRuntimeConfig } from "#imports";
|
|
2
1
|
import { hash } from "ohash";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { useRequestFetch, useRuntimeConfig } from "#imports";
|
|
3
|
+
import { sendCachedRequest } from "../cache.js";
|
|
4
|
+
import { buildApiProxyPath, createAuthHeader, createLanguageHeader, headersToObject } from "../utils.js";
|
|
5
5
|
export function $kirby(path, opts = {}) {
|
|
6
|
-
const
|
|
7
|
-
const
|
|
6
|
+
const { payloadCache = true, ...requestOptions } = opts;
|
|
7
|
+
const key = opts.key || `$kirby${hash([
|
|
8
|
+
path,
|
|
9
|
+
opts.method,
|
|
10
|
+
opts.query,
|
|
11
|
+
opts.body,
|
|
12
|
+
opts.language,
|
|
13
|
+
opts.forwardCookies,
|
|
14
|
+
headersToObject(opts.headers)
|
|
15
|
+
])}`;
|
|
16
|
+
return sendCachedRequest(key, payloadCache, () => sendKirbyRequest(path, { ...requestOptions, key }));
|
|
17
|
+
}
|
|
18
|
+
export function sendKirbyRequest(path, opts) {
|
|
19
|
+
const kirby = useRuntimeConfig().public.kirby;
|
|
8
20
|
const {
|
|
9
21
|
method,
|
|
10
22
|
headers,
|
|
11
23
|
query,
|
|
12
24
|
body,
|
|
13
25
|
language,
|
|
14
|
-
cache = true,
|
|
15
26
|
key,
|
|
27
|
+
forwardCookies,
|
|
16
28
|
...fetchOptions
|
|
17
29
|
} = opts;
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
path,
|
|
23
|
-
method,
|
|
24
|
-
query,
|
|
25
|
-
body,
|
|
26
|
-
language
|
|
27
|
-
])}`;
|
|
28
|
-
if ((nuxt.isHydrating || cache) && nuxt.payload.data[_key])
|
|
29
|
-
return Promise.resolve(nuxt.payload.data[_key]);
|
|
30
|
-
if (promiseMap.has(_key))
|
|
31
|
-
return promiseMap.get(_key);
|
|
32
|
-
const sharedHeaders = headersToObject(headers);
|
|
30
|
+
const sharedHeaders = {
|
|
31
|
+
...headersToObject(headers),
|
|
32
|
+
...createLanguageHeader(language)
|
|
33
|
+
};
|
|
33
34
|
const _serverFetchOptions = {
|
|
34
35
|
method: "POST",
|
|
35
36
|
body: {
|
|
@@ -38,7 +39,7 @@ export function $kirby(path, opts = {}) {
|
|
|
38
39
|
query,
|
|
39
40
|
body,
|
|
40
41
|
headers: sharedHeaders,
|
|
41
|
-
|
|
42
|
+
forwardCookies
|
|
42
43
|
}
|
|
43
44
|
};
|
|
44
45
|
const _clientFetchOptions = {
|
|
@@ -49,21 +50,12 @@ export function $kirby(path, opts = {}) {
|
|
|
49
50
|
...sharedHeaders,
|
|
50
51
|
...createAuthHeader(kirby)
|
|
51
52
|
},
|
|
52
|
-
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
|
|
53
56
|
};
|
|
54
|
-
|
|
57
|
+
return useRequestFetch()(kirby.client ? path : buildApiProxyPath(key), {
|
|
55
58
|
...fetchOptions,
|
|
56
59
|
...kirby.client ? _clientFetchOptions : _serverFetchOptions
|
|
57
|
-
}).then((response) => {
|
|
58
|
-
if (import.meta.server || cache)
|
|
59
|
-
nuxt.payload.data[_key] = response;
|
|
60
|
-
return response;
|
|
61
|
-
}).catch((error) => {
|
|
62
|
-
nuxt.payload.data[_key] = void 0;
|
|
63
|
-
throw error;
|
|
64
|
-
}).finally(() => {
|
|
65
|
-
promiseMap.delete(_key);
|
|
66
60
|
});
|
|
67
|
-
promiseMap.set(_key, request);
|
|
68
|
-
return request;
|
|
69
61
|
}
|
|
@@ -6,15 +6,25 @@ export type KqlOptions = Pick<NitroFetchOptions<string>, 'onRequest' | 'onReques
|
|
|
6
6
|
*/
|
|
7
7
|
language?: string;
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* Serve a repeated request from the Nuxt payload instead of sending it again.
|
|
10
10
|
* @default true
|
|
11
11
|
*/
|
|
12
|
-
|
|
12
|
+
payloadCache?: boolean;
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* @
|
|
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;
|
|
21
|
+
/**
|
|
22
|
+
* Cache key, generated from the request options by default.
|
|
17
23
|
*/
|
|
18
24
|
key?: string;
|
|
19
25
|
};
|
|
20
26
|
export declare function $kql<T extends KirbyQueryResponse<any, boolean> = KirbyQueryResponse>(query: KirbyQueryRequest, opts?: KqlOptions): Promise<T>;
|
|
27
|
+
/** Sends the query with nothing cached in front of it. */
|
|
28
|
+
export declare function sendKqlRequest<T extends KirbyQueryResponse<any, boolean> = KirbyQueryResponse>(query: KirbyQueryRequest, opts: Omit<KqlOptions, 'payloadCache'> & {
|
|
29
|
+
key: string;
|
|
30
|
+
}): Promise<T>;
|
|
@@ -1,32 +1,36 @@
|
|
|
1
|
-
import { useNuxtApp, useRequestFetch, useRuntimeConfig } from "#imports";
|
|
2
1
|
import { hash } from "ohash";
|
|
3
|
-
import {
|
|
2
|
+
import { useRequestFetch, useRuntimeConfig } from "#imports";
|
|
3
|
+
import { sendCachedRequest } from "../cache.js";
|
|
4
|
+
import { buildApiProxyPath, createAuthHeader, createLanguageHeader, headersToObject } from "../utils.js";
|
|
4
5
|
export function $kql(query, opts = {}) {
|
|
5
|
-
const
|
|
6
|
-
const
|
|
6
|
+
const { payloadCache = true, ...requestOptions } = opts;
|
|
7
|
+
const key = opts.key || `$kql${hash([
|
|
8
|
+
query,
|
|
9
|
+
opts.language,
|
|
10
|
+
opts.forwardCookies,
|
|
11
|
+
headersToObject(opts.headers)
|
|
12
|
+
])}`;
|
|
13
|
+
return sendCachedRequest(key, payloadCache, () => sendKqlRequest(query, { ...requestOptions, key }));
|
|
14
|
+
}
|
|
15
|
+
export function sendKqlRequest(query, opts) {
|
|
7
16
|
const kirby = useRuntimeConfig().public.kirby;
|
|
8
17
|
const {
|
|
9
18
|
headers,
|
|
10
19
|
language,
|
|
11
|
-
cache = true,
|
|
12
20
|
key,
|
|
21
|
+
forwardCookies,
|
|
13
22
|
...fetchOptions
|
|
14
23
|
} = opts;
|
|
15
|
-
const _key = key || `$kql${hash([query, language])}`;
|
|
16
|
-
if ((nuxt.isHydrating || cache) && nuxt.payload.data[_key])
|
|
17
|
-
return Promise.resolve(nuxt.payload.data[_key]);
|
|
18
|
-
if (promiseMap.has(_key))
|
|
19
|
-
return promiseMap.get(_key);
|
|
20
24
|
const sharedHeaders = {
|
|
21
25
|
...headersToObject(headers),
|
|
22
|
-
...language
|
|
26
|
+
...createLanguageHeader(language)
|
|
23
27
|
};
|
|
24
28
|
const _serverFetchOptions = {
|
|
25
29
|
method: "POST",
|
|
26
30
|
body: {
|
|
27
31
|
query,
|
|
28
32
|
headers: sharedHeaders,
|
|
29
|
-
|
|
33
|
+
forwardCookies
|
|
30
34
|
}
|
|
31
35
|
};
|
|
32
36
|
const _clientFetchOptions = {
|
|
@@ -36,21 +40,12 @@ export function $kql(query, opts = {}) {
|
|
|
36
40
|
...sharedHeaders,
|
|
37
41
|
...createAuthHeader(kirby)
|
|
38
42
|
},
|
|
39
|
-
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
|
|
40
46
|
};
|
|
41
|
-
|
|
47
|
+
return useRequestFetch()(kirby.client ? kirby.kqlPath : buildApiProxyPath(key), {
|
|
42
48
|
...fetchOptions,
|
|
43
49
|
...kirby.client ? _clientFetchOptions : _serverFetchOptions
|
|
44
|
-
}).then((response) => {
|
|
45
|
-
if (import.meta.server || cache)
|
|
46
|
-
nuxt.payload.data[_key] = response;
|
|
47
|
-
return response;
|
|
48
|
-
}).catch((error) => {
|
|
49
|
-
nuxt.payload.data[_key] = void 0;
|
|
50
|
-
throw error;
|
|
51
|
-
}).finally(() => {
|
|
52
|
-
promiseMap.delete(_key);
|
|
53
50
|
});
|
|
54
|
-
promiseMap.set(_key, request);
|
|
55
|
-
return request;
|
|
56
51
|
}
|
|
@@ -1,22 +1,27 @@
|
|
|
1
1
|
import type { NitroFetchOptions } from 'nitropack';
|
|
2
2
|
import type { AsyncData, AsyncDataOptions, NuxtError } from 'nuxt/app';
|
|
3
3
|
import type { MaybeRefOrGetter, MultiWatchSources } from 'vue';
|
|
4
|
-
type UseKirbyDataOptions<T> = Omit<AsyncDataOptions<T>, 'watch'> & Pick<NitroFetchOptions<string>, 'onRequest' | 'onRequestError' | 'onResponse' | 'onResponseError' | 'method' | 'headers' | 'query' | 'body' | 'retry' | 'retryDelay' | 'retryStatusCodes'
|
|
4
|
+
export type UseKirbyDataOptions<T> = Omit<AsyncDataOptions<T>, 'watch'> & Pick<NitroFetchOptions<string>, 'onRequest' | 'onRequestError' | 'onResponse' | 'onResponseError' | 'method' | 'headers' | 'query' | 'body' | 'retry' | 'retryDelay' | 'retryStatusCodes'> & {
|
|
5
5
|
/**
|
|
6
6
|
* Language code to fetch data for in multi-language Kirby setups.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* Travels as the `X-Language` header, which Kirby reads on API routes. Kirby Headless 8.1 also
|
|
10
|
+
* reads it on the global-routes catch-all, but only for a path that names no language itself.
|
|
7
11
|
*/
|
|
8
12
|
language?: MaybeRefOrGetter<string>;
|
|
9
13
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
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.
|
|
12
19
|
*/
|
|
13
|
-
|
|
20
|
+
forwardCookies?: boolean;
|
|
14
21
|
/**
|
|
15
22
|
* Watch an array of reactive sources and auto-refresh the fetch result when they change.
|
|
16
23
|
* Path and language are watched by default. You can completely ignore reactive sources by using `watch: false`.
|
|
17
|
-
* @default undefined
|
|
18
24
|
*/
|
|
19
25
|
watch?: MultiWatchSources | false;
|
|
20
26
|
};
|
|
21
27
|
export declare function useKirbyData<T = any>(path: MaybeRefOrGetter<string>, opts?: UseKirbyDataOptions<T>): AsyncData<T | undefined, NuxtError>;
|
|
22
|
-
export {};
|
|
@@ -1,64 +1,31 @@
|
|
|
1
|
-
import { useAsyncData } from "#imports";
|
|
2
1
|
import { hash } from "ohash";
|
|
3
|
-
import { joinURL } from "ufo";
|
|
4
2
|
import { computed, toValue } from "vue";
|
|
5
|
-
import {
|
|
3
|
+
import { useFetch } from "#imports";
|
|
4
|
+
import { headersToObject } from "../utils.js";
|
|
5
|
+
import { sendKirbyRequest } from "./$kirby.js";
|
|
6
6
|
export function useKirbyData(path, opts = {}) {
|
|
7
|
-
const {
|
|
8
|
-
server,
|
|
9
|
-
lazy,
|
|
10
|
-
default: defaultFn,
|
|
11
|
-
transform,
|
|
12
|
-
pick,
|
|
13
|
-
watch: watchSources,
|
|
14
|
-
immediate,
|
|
15
|
-
method,
|
|
16
|
-
headers,
|
|
17
|
-
query,
|
|
18
|
-
body,
|
|
19
|
-
language,
|
|
20
|
-
cache = true,
|
|
21
|
-
...fetchOptions
|
|
22
|
-
} = opts;
|
|
7
|
+
const { language, forwardCookies, ...fetchOptions } = opts;
|
|
23
8
|
const _language = computed(() => toValue(language));
|
|
24
|
-
const _path = computed(() =>
|
|
25
|
-
const value = toValue(path).replace(/^\//, "");
|
|
26
|
-
return _language.value ? joinURL(_language.value, value) : value;
|
|
27
|
-
});
|
|
9
|
+
const _path = computed(() => toValue(path).replace(/^\//, ""));
|
|
28
10
|
const key = computed(() => `$kirby${hash([
|
|
29
11
|
_path.value,
|
|
30
|
-
|
|
31
|
-
|
|
12
|
+
_language.value,
|
|
13
|
+
toValue(fetchOptions.query),
|
|
14
|
+
toValue(fetchOptions.method),
|
|
15
|
+
toValue(fetchOptions.body),
|
|
16
|
+
forwardCookies,
|
|
17
|
+
headersToObject(toValue(fetchOptions.headers))
|
|
32
18
|
])}`);
|
|
33
|
-
if (!_path.value
|
|
34
|
-
console.warn("[
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return useAsyncData(
|
|
46
|
-
watchSources === false ? key.value : key,
|
|
47
|
-
() => {
|
|
48
|
-
controller?.abort?.();
|
|
49
|
-
controller = new AbortController();
|
|
50
|
-
return $kirby(_path.value, {
|
|
51
|
-
...fetchOptions,
|
|
52
|
-
method,
|
|
53
|
-
headers,
|
|
54
|
-
query,
|
|
55
|
-
body,
|
|
56
|
-
signal: controller.signal,
|
|
57
|
-
language: _language.value,
|
|
58
|
-
cache,
|
|
59
|
-
key: key.value
|
|
60
|
-
});
|
|
61
|
-
},
|
|
62
|
-
asyncDataOptions
|
|
63
|
-
);
|
|
19
|
+
if (!_path.value)
|
|
20
|
+
console.warn("[nuxt-kirby] Empty Kirby path");
|
|
21
|
+
return useFetch(_path, {
|
|
22
|
+
...fetchOptions,
|
|
23
|
+
key,
|
|
24
|
+
$fetch: ((_request, options) => sendKirbyRequest(_path.value, {
|
|
25
|
+
...options,
|
|
26
|
+
language: _language.value,
|
|
27
|
+
forwardCookies,
|
|
28
|
+
key: key.value
|
|
29
|
+
}))
|
|
30
|
+
});
|
|
64
31
|
}
|
|
@@ -2,20 +2,22 @@ import type { KirbyQueryRequest, KirbyQueryResponse } from 'kirby-types';
|
|
|
2
2
|
import type { NitroFetchOptions } from 'nitropack';
|
|
3
3
|
import type { AsyncData, AsyncDataOptions, NuxtError } from 'nuxt/app';
|
|
4
4
|
import type { MaybeRefOrGetter, MultiWatchSources } from 'vue';
|
|
5
|
-
export type UseKqlOptions<T> = Omit<AsyncDataOptions<T>, 'watch'> & Pick<NitroFetchOptions<string>, 'onRequest' | 'onRequestError' | 'onResponse' | 'onResponseError' | 'headers' | 'retry' | 'retryDelay' | 'retryStatusCodes'
|
|
5
|
+
export type UseKqlOptions<T> = Omit<AsyncDataOptions<T>, 'watch'> & Pick<NitroFetchOptions<string>, 'onRequest' | 'onRequestError' | 'onResponse' | 'onResponseError' | 'headers' | 'retry' | 'retryDelay' | 'retryStatusCodes'> & {
|
|
6
6
|
/**
|
|
7
7
|
* Language code to fetch data for in multi-language Kirby setups.
|
|
8
8
|
*/
|
|
9
9
|
language?: MaybeRefOrGetter<string>;
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
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.
|
|
13
16
|
*/
|
|
14
|
-
|
|
17
|
+
forwardCookies?: boolean;
|
|
15
18
|
/**
|
|
16
19
|
* Watch an array of reactive sources and auto-refresh the fetch result when they change.
|
|
17
20
|
* Query and language are watched by default. You can completely ignore reactive sources by using `watch: false`.
|
|
18
|
-
* @default undefined
|
|
19
21
|
*/
|
|
20
22
|
watch?: MultiWatchSources | false;
|
|
21
23
|
};
|
|
@@ -1,49 +1,28 @@
|
|
|
1
|
-
import { useAsyncData } from "#imports";
|
|
2
1
|
import { hash } from "ohash";
|
|
3
2
|
import { computed, toValue } from "vue";
|
|
4
|
-
import {
|
|
3
|
+
import { useFetch } from "#imports";
|
|
4
|
+
import { headersToObject } from "../utils.js";
|
|
5
|
+
import { sendKqlRequest } from "./$kql.js";
|
|
5
6
|
export function useKql(query, opts = {}) {
|
|
6
|
-
const {
|
|
7
|
-
server,
|
|
8
|
-
lazy,
|
|
9
|
-
default: defaultFn,
|
|
10
|
-
transform,
|
|
11
|
-
pick,
|
|
12
|
-
watch: watchSources,
|
|
13
|
-
immediate,
|
|
14
|
-
headers,
|
|
15
|
-
language,
|
|
16
|
-
cache = true,
|
|
17
|
-
...fetchOptions
|
|
18
|
-
} = opts;
|
|
7
|
+
const { language, forwardCookies, ...fetchOptions } = opts;
|
|
19
8
|
const _query = computed(() => toValue(query));
|
|
20
9
|
const _language = computed(() => toValue(language));
|
|
21
|
-
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
|
+
])}`);
|
|
22
16
|
if (Object.keys(_query.value).length === 0 || !_query.value.query)
|
|
23
|
-
console.error("[
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return useAsyncData(
|
|
35
|
-
watchSources === false ? key.value : key,
|
|
36
|
-
() => {
|
|
37
|
-
controller?.abort?.();
|
|
38
|
-
controller = new AbortController();
|
|
39
|
-
return $kql(_query.value, {
|
|
40
|
-
...fetchOptions,
|
|
41
|
-
signal: controller.signal,
|
|
42
|
-
language: _language.value,
|
|
43
|
-
cache,
|
|
44
|
-
key: key.value
|
|
45
|
-
});
|
|
46
|
-
},
|
|
47
|
-
asyncDataOptions
|
|
48
|
-
);
|
|
17
|
+
console.error("[nuxt-kirby] Empty KQL query");
|
|
18
|
+
return useFetch(key, {
|
|
19
|
+
...fetchOptions,
|
|
20
|
+
key,
|
|
21
|
+
$fetch: ((_request, options) => sendKqlRequest(_query.value, {
|
|
22
|
+
...options,
|
|
23
|
+
language: _language.value,
|
|
24
|
+
forwardCookies,
|
|
25
|
+
key: key.value
|
|
26
|
+
}))
|
|
27
|
+
});
|
|
49
28
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<Uint8Array<
|
|
1
|
+
declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<Uint8Array<ArrayBufferLike>>>;
|
|
2
2
|
export default _default;
|
|
@@ -1,65 +1,84 @@
|
|
|
1
|
-
import { useRuntimeConfig } from "#imports";
|
|
2
|
-
import { consola } from "consola";
|
|
3
1
|
import { destr } from "destr";
|
|
4
|
-
import { createError, defineEventHandler, getRouterParam, readBody, setResponseHeader, setResponseStatus, splitCookiesString } from "h3";
|
|
5
|
-
import { defineCachedFunction } from "nitropack/runtime
|
|
2
|
+
import { createError, defineEventHandler, getRequestHeader, getRouterParam, readBody, setResponseHeader, setResponseStatus, splitCookiesString } from "h3";
|
|
3
|
+
import { defineCachedFunction } from "nitropack/runtime";
|
|
4
|
+
import { hash } from "ohash";
|
|
6
5
|
import { base64ToUint8Array, uint8ArrayToBase64, uint8ArrayToString } from "uint8array-extras";
|
|
6
|
+
import { useRuntimeConfig } from "#imports";
|
|
7
7
|
import { createAuthHeader } from "../utils.js";
|
|
8
8
|
const EXCLUDED_HEADERS = /* @__PURE__ */ new Set([
|
|
9
9
|
// https://github.com/h3js/h3/blob/fe9800bbbe9bda2972cc5d11db7353f4ab70f0ba/src/utils/proxy.ts#L97
|
|
10
10
|
"content-encoding",
|
|
11
11
|
"content-length",
|
|
12
|
-
// Reduce information leakage
|
|
12
|
+
// Reduce information leakage.
|
|
13
13
|
"server",
|
|
14
14
|
"x-powered-by"
|
|
15
15
|
]);
|
|
16
|
+
async function fetchFromKirby(event, {
|
|
17
|
+
isQueryRequest,
|
|
18
|
+
query,
|
|
19
|
+
path,
|
|
20
|
+
headers,
|
|
21
|
+
method,
|
|
22
|
+
body,
|
|
23
|
+
forwardCookies
|
|
24
|
+
}) {
|
|
25
|
+
const kirby = useRuntimeConfig(event).kirby;
|
|
26
|
+
const cookie = forwardCookies ? getRequestHeader(event, "cookie") : void 0;
|
|
27
|
+
const response = await globalThis.$fetch.raw(isQueryRequest ? kirby.kqlPath : path, {
|
|
28
|
+
responseType: "arrayBuffer",
|
|
29
|
+
ignoreResponseError: true,
|
|
30
|
+
baseURL: kirby.url,
|
|
31
|
+
...isQueryRequest ? {
|
|
32
|
+
method: "POST",
|
|
33
|
+
body: query
|
|
34
|
+
} : {
|
|
35
|
+
method,
|
|
36
|
+
query,
|
|
37
|
+
body
|
|
38
|
+
},
|
|
39
|
+
headers: {
|
|
40
|
+
...headers,
|
|
41
|
+
...cookie ? { cookie } : {},
|
|
42
|
+
...createAuthHeader(kirby)
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
return {
|
|
46
|
+
status: response.status,
|
|
47
|
+
statusText: response.statusText,
|
|
48
|
+
headers: [...response.headers.entries()],
|
|
49
|
+
data: new Uint8Array(response._data ?? [])
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function createCachedFetcher(kirby) {
|
|
53
|
+
return defineCachedFunction(
|
|
54
|
+
// Nitro reads the event from the first argument only, and hands the cache write to
|
|
55
|
+
// `event.waitUntil()` โ without it an edge worker is torn down before the entry reaches storage.
|
|
56
|
+
async (event, options) => {
|
|
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
|
+
};
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
name: "nuxt-kirby",
|
|
68
|
+
base: kirby.server.storage,
|
|
69
|
+
swr: kirby.server.swr,
|
|
70
|
+
maxAge: kirby.server.maxAge,
|
|
71
|
+
// Reading the caller's `key` instead would let one visitor plant a response under the key another reads.
|
|
72
|
+
getKey: (event, options) => hash(options)
|
|
73
|
+
}
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
let cachedFetcher;
|
|
16
77
|
export default defineEventHandler(async (event) => {
|
|
17
78
|
const kirby = useRuntimeConfig(event).kirby;
|
|
18
79
|
const body = await readBody(event);
|
|
19
80
|
const key = decodeURIComponent(getRouterParam(event, "key"));
|
|
20
81
|
const isQueryRequest = key.startsWith("$kql");
|
|
21
|
-
const fetcher = async (event2, {
|
|
22
|
-
key: key2,
|
|
23
|
-
query,
|
|
24
|
-
path,
|
|
25
|
-
headers,
|
|
26
|
-
method,
|
|
27
|
-
body: body2
|
|
28
|
-
}) => {
|
|
29
|
-
const isQueryRequest2 = key2.startsWith("$kql");
|
|
30
|
-
const response = await globalThis.$fetch.raw(isQueryRequest2 ? kirby.kqlPath : path, {
|
|
31
|
-
responseType: "arrayBuffer",
|
|
32
|
-
ignoreResponseError: true,
|
|
33
|
-
baseURL: kirby.url,
|
|
34
|
-
...isQueryRequest2 ? {
|
|
35
|
-
method: "POST",
|
|
36
|
-
body: query
|
|
37
|
-
} : {
|
|
38
|
-
method,
|
|
39
|
-
query,
|
|
40
|
-
body: body2
|
|
41
|
-
},
|
|
42
|
-
headers: {
|
|
43
|
-
...headers,
|
|
44
|
-
...createAuthHeader(kirby)
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
const dataArray = new Uint8Array(response._data ?? []);
|
|
48
|
-
const data = uint8ArrayToBase64(dataArray);
|
|
49
|
-
return {
|
|
50
|
-
status: response.status,
|
|
51
|
-
statusText: response.statusText,
|
|
52
|
-
headers: [...response.headers.entries()],
|
|
53
|
-
data
|
|
54
|
-
};
|
|
55
|
-
};
|
|
56
|
-
const cachedFetcher = defineCachedFunction(fetcher, {
|
|
57
|
-
name: "nuxt-kirby",
|
|
58
|
-
base: kirby.server.storage,
|
|
59
|
-
swr: kirby.server.swr,
|
|
60
|
-
maxAge: kirby.server.maxAge,
|
|
61
|
-
getKey: (event2, { key: key2 }) => key2
|
|
62
|
-
});
|
|
63
82
|
if (isQueryRequest) {
|
|
64
83
|
if (!body.query?.query) {
|
|
65
84
|
throw createError({
|
|
@@ -75,19 +94,28 @@ export default defineEventHandler(async (event) => {
|
|
|
75
94
|
});
|
|
76
95
|
}
|
|
77
96
|
}
|
|
97
|
+
const forwardCookies = body.forwardCookies ?? kirby.forwardCookies;
|
|
78
98
|
try {
|
|
79
|
-
|
|
80
|
-
const
|
|
99
|
+
let response;
|
|
100
|
+
const request = { isQueryRequest, ...body, forwardCookies };
|
|
101
|
+
if (kirby.server.cache && !forwardCookies) {
|
|
102
|
+
cachedFetcher ??= createCachedFetcher(kirby);
|
|
103
|
+
const cached = await cachedFetcher(event, request);
|
|
104
|
+
response = { ...cached, data: base64ToUint8Array(cached.data) };
|
|
105
|
+
} else {
|
|
106
|
+
response = await fetchFromKirby(event, request);
|
|
107
|
+
}
|
|
108
|
+
const dataArray = response.data;
|
|
81
109
|
if (response.status >= 400 && response.status < 600) {
|
|
82
110
|
if (isQueryRequest) {
|
|
83
|
-
|
|
111
|
+
console.error(`[nuxt-kirby] Failed KQL query "${body.query?.query}" (...) with status code ${response.status}:
|
|
84
112
|
`, destr(
|
|
85
113
|
uint8ArrayToString(dataArray)
|
|
86
114
|
));
|
|
87
115
|
if (kirby.server.verboseErrors)
|
|
88
|
-
|
|
116
|
+
console.error("[nuxt-kirby] Full KQL query request:", body.query);
|
|
89
117
|
} else {
|
|
90
|
-
|
|
118
|
+
console.error(`[nuxt-kirby] Failed ${(body.method || "get").toUpperCase()} request to "${body.path}"`);
|
|
91
119
|
}
|
|
92
120
|
}
|
|
93
121
|
const cookies = [];
|
|
@@ -105,7 +133,7 @@ export default defineEventHandler(async (event) => {
|
|
|
105
133
|
setResponseStatus(event, response.status, response.statusText);
|
|
106
134
|
return dataArray;
|
|
107
135
|
} catch (error) {
|
|
108
|
-
|
|
136
|
+
console.error("[nuxt-kirby]", error);
|
|
109
137
|
throw createError({
|
|
110
138
|
statusCode: 503,
|
|
111
139
|
statusMessage: "Service Unavailable"
|
|
@@ -1,16 +1,24 @@
|
|
|
1
1
|
import type { KirbyQueryRequest, KirbyQueryResponse } from 'kirby-types';
|
|
2
2
|
import type { NitroFetchOptions } from 'nitropack';
|
|
3
|
-
export type
|
|
3
|
+
export type NitroKirbyFetchOptions = Omit<NitroFetchOptions<string>, 'baseURL'> & {
|
|
4
4
|
/**
|
|
5
5
|
* Language code to fetch data for in multi-language Kirby setups.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* Travels as the `X-Language` header, which Kirby reads on API routes. Kirby Headless 8.1 also
|
|
9
|
+
* reads it on the global-routes catch-all, but only for a path that names no language itself.
|
|
6
10
|
*/
|
|
7
11
|
language?: string;
|
|
8
12
|
};
|
|
9
|
-
export type
|
|
13
|
+
export type NitroKqlFetchOptions = Pick<NitroFetchOptions<string>, 'onRequest' | 'onRequestError' | 'onResponse' | 'onResponseError' | 'headers' | 'retry' | 'retryDelay' | 'retryStatusCodes' | 'timeout' | 'signal'> & {
|
|
10
14
|
/**
|
|
11
15
|
* Language code to fetch data for in multi-language Kirby setups.
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* Travels as the `X-Language` header, which Kirby reads on API routes. Kirby Headless 8.1 also
|
|
19
|
+
* reads it on the global-routes catch-all, but only for a path that names no language itself.
|
|
12
20
|
*/
|
|
13
21
|
language?: string;
|
|
14
22
|
};
|
|
15
|
-
export declare function $kirby<T = any>(path: string, opts?:
|
|
16
|
-
export declare function $kql<T extends KirbyQueryResponse<any, boolean> = KirbyQueryResponse>(query: KirbyQueryRequest, opts?:
|
|
23
|
+
export declare function $kirby<T = any>(path: string, opts?: NitroKirbyFetchOptions): Promise<T>;
|
|
24
|
+
export declare function $kql<T extends KirbyQueryResponse<any, boolean> = KirbyQueryResponse>(query: KirbyQueryRequest, opts?: NitroKqlFetchOptions): Promise<T>;
|
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import { useRuntimeConfig } from "#imports";
|
|
2
|
-
import {
|
|
3
|
-
import { createAuthHeader, headersToObject } from "../utils.js";
|
|
2
|
+
import { createAuthHeader, createLanguageHeader, headersToObject } from "../utils.js";
|
|
4
3
|
export function $kirby(path, opts = {}) {
|
|
5
4
|
const { headers, language, ...fetchOptions } = opts;
|
|
6
5
|
const kirby = useRuntimeConfig().kirby;
|
|
7
|
-
if (language)
|
|
8
|
-
path = joinURL(language, path);
|
|
9
6
|
return globalThis.$fetch(path, {
|
|
10
7
|
...fetchOptions,
|
|
11
8
|
baseURL: kirby.url,
|
|
12
9
|
headers: {
|
|
13
10
|
...headersToObject(headers),
|
|
11
|
+
...createLanguageHeader(language),
|
|
14
12
|
...createAuthHeader(kirby)
|
|
15
13
|
}
|
|
16
14
|
});
|
|
@@ -25,8 +23,8 @@ export function $kql(query, opts = {}) {
|
|
|
25
23
|
body: query,
|
|
26
24
|
headers: {
|
|
27
25
|
...headersToObject(headers),
|
|
28
|
-
...
|
|
29
|
-
...
|
|
26
|
+
...createLanguageHeader(language),
|
|
27
|
+
...createAuthHeader(kirby)
|
|
30
28
|
}
|
|
31
29
|
});
|
|
32
30
|
}
|
package/dist/runtime/types.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import type { KirbyQueryRequest } from 'kirby-types';
|
|
2
2
|
import type { NitroFetchOptions } from 'nitropack';
|
|
3
3
|
export interface ServerFetchOptions extends Pick<NitroFetchOptions<string>, 'method' | 'headers' | 'query' | 'body'> {
|
|
4
|
+
/** A request carries one or the other: `query` for KQL, `path` for the REST API. */
|
|
4
5
|
query?: Partial<KirbyQueryRequest>;
|
|
5
6
|
path?: string;
|
|
6
|
-
|
|
7
|
+
/** Overrides the module's `forwardCookies` for this request. Absent leaves the decision to it. */
|
|
8
|
+
forwardCookies?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface ServerFetchRequest extends ServerFetchOptions {
|
|
11
|
+
isQueryRequest: boolean;
|
|
7
12
|
}
|
package/dist/runtime/utils.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
export declare function buildApiProxyPath(key: string): string;
|
|
2
2
|
export declare function headersToObject(headers?: HeadersInit): Record<string, string>;
|
|
3
|
+
/**
|
|
4
|
+
* Builds the credentials header, lowercased so that it overwrites a caller's own
|
|
5
|
+
* `Authorization` instead of being joined with it into one comma-separated value.
|
|
6
|
+
*/
|
|
3
7
|
export declare function createAuthHeader({ auth, token, credentials, }: {
|
|
4
8
|
auth?: string;
|
|
5
9
|
token?: string;
|
|
@@ -8,5 +12,12 @@ export declare function createAuthHeader({ auth, token, credentials, }: {
|
|
|
8
12
|
password: string;
|
|
9
13
|
};
|
|
10
14
|
}): {
|
|
11
|
-
|
|
15
|
+
authorization: string;
|
|
16
|
+
} | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Builds the header Kirby reads the language from, lowercased so that it overwrites
|
|
19
|
+
* a caller's own `X-Language` instead of traveling alongside it as a second value.
|
|
20
|
+
*/
|
|
21
|
+
export declare function createLanguageHeader(language: string | undefined): {
|
|
22
|
+
'x-language': string;
|
|
12
23
|
} | undefined;
|
package/dist/runtime/utils.js
CHANGED
|
@@ -12,8 +12,11 @@ export function createAuthHeader({
|
|
|
12
12
|
if (auth === "basic" && credentials) {
|
|
13
13
|
const { username, password } = credentials;
|
|
14
14
|
const encoded = globalThis.btoa(`${username}:${password}`);
|
|
15
|
-
return {
|
|
15
|
+
return { authorization: `Basic ${encoded}` };
|
|
16
16
|
}
|
|
17
17
|
if (auth === "bearer")
|
|
18
|
-
return {
|
|
18
|
+
return { authorization: `Bearer ${token}` };
|
|
19
|
+
}
|
|
20
|
+
export function createLanguageHeader(language) {
|
|
21
|
+
return language ? { "x-language": language } : void 0;
|
|
19
22
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-kirby",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
5
|
-
"packageManager": "pnpm@
|
|
6
|
-
"description": "Kirby CMS
|
|
4
|
+
"version": "4.1.0",
|
|
5
|
+
"packageManager": "pnpm@11.20.0",
|
|
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",
|
|
@@ -38,9 +38,9 @@
|
|
|
38
38
|
"dev": "nuxt dev playground",
|
|
39
39
|
"dev:build": "nuxt build playground",
|
|
40
40
|
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxt prepare playground && nuxt prepare test/fixture",
|
|
41
|
-
"docs:dev": "
|
|
42
|
-
"docs:build": "
|
|
43
|
-
"docs:preview": "
|
|
41
|
+
"docs:dev": "pnpm --filter docs run dev",
|
|
42
|
+
"docs:build": "pnpm --filter docs run build",
|
|
43
|
+
"docs:preview": "pnpm --filter docs run preview",
|
|
44
44
|
"lint": "eslint .",
|
|
45
45
|
"lint:fix": "eslint . --fix",
|
|
46
46
|
"test": "vitest",
|
|
@@ -48,27 +48,29 @@
|
|
|
48
48
|
"release": "bumpp"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@nuxt/kit": "^4.
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"kirby-types": "^1.
|
|
51
|
+
"@nuxt/kit": "^4.5.2",
|
|
52
|
+
"defu": "^6.1.7",
|
|
53
|
+
"destr": "^2.0.5",
|
|
54
|
+
"kirby-types": "^1.6.0",
|
|
55
55
|
"ofetch": "^1.5.1",
|
|
56
56
|
"ohash": "^2.0.11",
|
|
57
57
|
"pathe": "^2.0.3",
|
|
58
58
|
"scule": "^1.3.0",
|
|
59
|
-
"ufo": "^1.6.
|
|
59
|
+
"ufo": "^1.6.4",
|
|
60
60
|
"uint8array-extras": "^1.5.0"
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
|
-
"@antfu/eslint-config": "^
|
|
64
|
-
"@nuxt/module-builder": "^1.0.
|
|
65
|
-
"@nuxt/
|
|
66
|
-
"@
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"eslint": "^
|
|
70
|
-
"nuxt": "^4.
|
|
71
|
-
"
|
|
72
|
-
"
|
|
63
|
+
"@antfu/eslint-config": "^9.3.0",
|
|
64
|
+
"@nuxt/module-builder": "^1.0.3",
|
|
65
|
+
"@nuxt/schema": "^4.5.2",
|
|
66
|
+
"@nuxt/test-utils": "^4.1.0",
|
|
67
|
+
"@types/node": "^26.1.2",
|
|
68
|
+
"bumpp": "^12.2.0",
|
|
69
|
+
"eslint": "^10.8.0",
|
|
70
|
+
"nuxt": "^4.5.2",
|
|
71
|
+
"playwright-core": "^1.62.1",
|
|
72
|
+
"srvx": "^0.12.5",
|
|
73
|
+
"typescript": "^6.0.3",
|
|
74
|
+
"vitest": "^4.1.10"
|
|
73
75
|
}
|
|
74
76
|
}
|