nuxt-kirby 3.0.2 โ 4.0.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 +5 -9
- package/dist/module.d.mts +14 -29
- package/dist/module.json +3 -3
- package/dist/module.mjs +7 -33
- package/dist/runtime/cache.d.ts +11 -0
- package/dist/runtime/cache.js +25 -0
- package/dist/runtime/composables/$kirby.d.ts +11 -5
- package/dist/runtime/composables/$kirby.js +21 -35
- package/dist/runtime/composables/$kql.d.ts +7 -5
- package/dist/runtime/composables/$kql.js +11 -25
- package/dist/runtime/composables/useKirbyData.d.ts +5 -8
- package/dist/runtime/composables/useKirbyData.js +19 -52
- package/dist/runtime/composables/useKql.d.ts +1 -7
- package/dist/runtime/composables/useKql.js +13 -41
- package/dist/runtime/server/handler.d.ts +1 -1
- package/dist/runtime/server/handler.js +69 -52
- package/dist/runtime/server/imports.d.ts +12 -4
- package/dist/runtime/server/imports.js +4 -6
- package/dist/runtime/types.d.ts +4 -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
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
- ๐ Protected Kirby credentials when sending queries
|
|
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/getting-started/data-fetching) composable
|
|
15
15
|
- ๐ฆฆ [Multiple starter kits](https://nuxt-kirby.byjohann.dev/essentials/starter-kits) available
|
|
16
16
|
- ๐ Cached query responses
|
|
17
17
|
- ๐คน No CORS issues!
|
|
@@ -19,19 +19,13 @@
|
|
|
19
19
|
|
|
20
20
|
## Setup
|
|
21
21
|
|
|
22
|
-
> [!TIP]
|
|
23
|
-
> [๐ Read the documentation](https://nuxt-kirby.byjohann.dev)
|
|
24
|
-
|
|
25
22
|
```bash
|
|
26
|
-
npx
|
|
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,11 +36,11 @@ interface ModuleOptions {
|
|
|
48
36
|
password: string;
|
|
49
37
|
};
|
|
50
38
|
/**
|
|
51
|
-
* Send client-side requests instead of using the server-side proxy
|
|
39
|
+
* Send client-side requests instead of using the server-side proxy.
|
|
52
40
|
*
|
|
53
41
|
* @remarks
|
|
54
42
|
* By default, data from Kirby is fetched safely with a server-side proxy.
|
|
55
|
-
* If enabled, query requests will be
|
|
43
|
+
* If enabled, query requests will be sent directly from the client.
|
|
56
44
|
* Note: This means your token or user credentials will be publicly visible.
|
|
57
45
|
* If Nuxt SSR is disabled, this option is enabled by default.
|
|
58
46
|
*
|
|
@@ -60,7 +48,7 @@ interface ModuleOptions {
|
|
|
60
48
|
*/
|
|
61
49
|
client?: boolean;
|
|
62
50
|
/**
|
|
63
|
-
* Prefetch custom KQL queries at build-time
|
|
51
|
+
* Prefetch custom KQL queries at build-time.
|
|
64
52
|
*
|
|
65
53
|
* @remarks
|
|
66
54
|
* The queries will be fully typed and importable from `#nuxt-kirby`.
|
|
@@ -71,39 +59,36 @@ interface ModuleOptions {
|
|
|
71
59
|
query: KirbyQueryRequest;
|
|
72
60
|
language: string;
|
|
73
61
|
}>;
|
|
74
|
-
/**
|
|
75
|
-
* Server-side features
|
|
76
|
-
*/
|
|
77
62
|
server?: {
|
|
78
63
|
/**
|
|
79
|
-
* Enable server-side caching of queries using the Nitro cache API
|
|
64
|
+
* Enable server-side caching of queries using the Nitro cache API.
|
|
80
65
|
*
|
|
81
66
|
* @see https://nitro.unjs.io/guide/cache
|
|
82
67
|
*/
|
|
83
68
|
cache?: boolean;
|
|
84
69
|
/**
|
|
85
|
-
* Name of the storage mountpoint to use for caching
|
|
70
|
+
* Name of the storage mountpoint to use for caching.
|
|
86
71
|
*
|
|
87
72
|
* @see https://nitro.unjs.io/guide/cache
|
|
88
73
|
* @default 'cache'
|
|
89
74
|
*/
|
|
90
75
|
storage?: string;
|
|
91
76
|
/**
|
|
92
|
-
* Enable stale-while-revalidate behavior (cache is returned while it is being updated)
|
|
77
|
+
* Enable stale-while-revalidate behavior (cache is returned while it is being updated).
|
|
93
78
|
*
|
|
94
79
|
* @see https://nitro.unjs.io/guide/cache#options
|
|
95
80
|
* @default false
|
|
96
81
|
*/
|
|
97
82
|
swr?: boolean;
|
|
98
83
|
/**
|
|
99
|
-
* Number of seconds to cache the query response
|
|
84
|
+
* Number of seconds to cache the query response.
|
|
100
85
|
*
|
|
101
86
|
* @see https://nitro.unjs.io/guide/cache#options
|
|
102
87
|
* @default 1
|
|
103
88
|
*/
|
|
104
89
|
maxAge?: number;
|
|
105
90
|
/**
|
|
106
|
-
* Log verbose errors to the console if a query fails
|
|
91
|
+
* Log verbose errors to the console if a query fails.
|
|
107
92
|
*
|
|
108
93
|
* @remarks
|
|
109
94
|
* 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,21 +61,7 @@ ${JSON.stringify(_error.data, void 0, 2)}` : `: ${_error}`}`
|
|
|
61
61
|
return results;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
const
|
|
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
|
-
const module = defineNuxtModule({
|
|
64
|
+
const module$1 = defineNuxtModule({
|
|
79
65
|
meta: {
|
|
80
66
|
name,
|
|
81
67
|
configKey: "kirby",
|
|
@@ -86,7 +72,6 @@ const module = 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: {
|
|
@@ -111,16 +96,7 @@ const module = defineNuxtModule({
|
|
|
111
96
|
logger.error("Missing `KIRBY_API_USERNAME` and `KIRBY_API_PASSWORD` environment variable for basic authentication");
|
|
112
97
|
if (options.auth === "bearer" && !options.token)
|
|
113
98
|
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
|
-
}
|
|
99
|
+
options.kqlPath ||= options.auth === "bearer" ? "api/kql" : "api/query";
|
|
124
100
|
if (!nuxt.options.ssr) {
|
|
125
101
|
logger.info("Kirby requests are client-only because SSR is disabled");
|
|
126
102
|
options.client = true;
|
|
@@ -147,7 +123,6 @@ const module = defineNuxtModule({
|
|
|
147
123
|
addImports(
|
|
148
124
|
["$kirby", "$kql", "useKirbyData", "useKql"].map((name2) => ({
|
|
149
125
|
name: name2,
|
|
150
|
-
as: name2,
|
|
151
126
|
from: resolve(`runtime/composables/${name2}`)
|
|
152
127
|
}))
|
|
153
128
|
);
|
|
@@ -163,7 +138,6 @@ const module = defineNuxtModule({
|
|
|
163
138
|
});
|
|
164
139
|
});
|
|
165
140
|
nuxt.options.alias[`#${moduleName}`] = join(nuxt.options.buildDir, `module/${moduleName}`);
|
|
166
|
-
nuxt.options.alias["#nuxt-kql"] = join(nuxt.options.buildDir, `module/${moduleName}`);
|
|
167
141
|
const prefetchedQueries = await prefetchQueries(options);
|
|
168
142
|
addTemplate({
|
|
169
143
|
filename: `module/${moduleName}.mjs`,
|
|
@@ -183,7 +157,7 @@ export const ${key} = ${JSON.stringify(response?.result || null, void 0, 2)}
|
|
|
183
157
|
getContents() {
|
|
184
158
|
return `
|
|
185
159
|
// Generated by ${moduleName}
|
|
186
|
-
export type
|
|
160
|
+
export type * from 'kirby-types'
|
|
187
161
|
|
|
188
162
|
${[...prefetchedQueries.entries()].map(([key, response]) => `
|
|
189
163
|
export declare const ${key}: ${JSON.stringify(response?.result || null, void 0, 2)}
|
|
@@ -195,4 +169,4 @@ export type ${pascalCase(key)} = typeof ${key}
|
|
|
195
169
|
}
|
|
196
170
|
});
|
|
197
171
|
|
|
198
|
-
export { module as default };
|
|
172
|
+
export { module$1 as default };
|
|
@@ -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,24 @@ 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
|
-
* With this option, you can provide a custom cache key.
|
|
15
|
-
* @default undefined
|
|
17
|
+
* Cache key, generated from the request options by default.
|
|
16
18
|
*/
|
|
17
19
|
key?: string;
|
|
18
20
|
};
|
|
19
21
|
export declare function $kirby<T = any>(path: string, opts?: KirbyFetchOptions): Promise<T>;
|
|
22
|
+
/** Sends the request with nothing cached in front of it. */
|
|
23
|
+
export declare function sendKirbyRequest<T = any>(path: string, opts: Omit<KirbyFetchOptions, 'payloadCache'> & {
|
|
24
|
+
key: string;
|
|
25
|
+
}): Promise<T>;
|
|
@@ -1,35 +1,33 @@
|
|
|
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
|
+
])}`;
|
|
14
|
+
return sendCachedRequest(key, payloadCache, () => sendKirbyRequest(path, { ...requestOptions, key }));
|
|
15
|
+
}
|
|
16
|
+
export function sendKirbyRequest(path, opts) {
|
|
17
|
+
const kirby = useRuntimeConfig().public.kirby;
|
|
8
18
|
const {
|
|
9
19
|
method,
|
|
10
20
|
headers,
|
|
11
21
|
query,
|
|
12
22
|
body,
|
|
13
23
|
language,
|
|
14
|
-
cache = true,
|
|
15
24
|
key,
|
|
16
25
|
...fetchOptions
|
|
17
26
|
} = 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);
|
|
27
|
+
const sharedHeaders = {
|
|
28
|
+
...headersToObject(headers),
|
|
29
|
+
...createLanguageHeader(language)
|
|
30
|
+
};
|
|
33
31
|
const _serverFetchOptions = {
|
|
34
32
|
method: "POST",
|
|
35
33
|
body: {
|
|
@@ -37,8 +35,7 @@ export function $kirby(path, opts = {}) {
|
|
|
37
35
|
method,
|
|
38
36
|
query,
|
|
39
37
|
body,
|
|
40
|
-
headers: sharedHeaders
|
|
41
|
-
cache
|
|
38
|
+
headers: sharedHeaders
|
|
42
39
|
}
|
|
43
40
|
};
|
|
44
41
|
const _clientFetchOptions = {
|
|
@@ -51,19 +48,8 @@ export function $kirby(path, opts = {}) {
|
|
|
51
48
|
},
|
|
52
49
|
body
|
|
53
50
|
};
|
|
54
|
-
|
|
51
|
+
return useRequestFetch()(kirby.client ? path : buildApiProxyPath(key), {
|
|
55
52
|
...fetchOptions,
|
|
56
53
|
...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
54
|
});
|
|
67
|
-
promiseMap.set(_key, request);
|
|
68
|
-
return request;
|
|
69
55
|
}
|
|
@@ -6,15 +6,17 @@ 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
|
-
* With this option, you can provide a custom cache key.
|
|
16
|
-
* @default undefined
|
|
14
|
+
* Cache key, generated from the request options by default.
|
|
17
15
|
*/
|
|
18
16
|
key?: string;
|
|
19
17
|
};
|
|
20
18
|
export declare function $kql<T extends KirbyQueryResponse<any, boolean> = KirbyQueryResponse>(query: KirbyQueryRequest, opts?: KqlOptions): Promise<T>;
|
|
19
|
+
/** Sends the query with nothing cached in front of it. */
|
|
20
|
+
export declare function sendKqlRequest<T extends KirbyQueryResponse<any, boolean> = KirbyQueryResponse>(query: KirbyQueryRequest, opts: Omit<KqlOptions, 'payloadCache'> & {
|
|
21
|
+
key: string;
|
|
22
|
+
}): Promise<T>;
|
|
@@ -1,32 +1,29 @@
|
|
|
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([query, opts.language])}`;
|
|
8
|
+
return sendCachedRequest(key, payloadCache, () => sendKqlRequest(query, { ...requestOptions, key }));
|
|
9
|
+
}
|
|
10
|
+
export function sendKqlRequest(query, opts) {
|
|
7
11
|
const kirby = useRuntimeConfig().public.kirby;
|
|
8
12
|
const {
|
|
9
13
|
headers,
|
|
10
14
|
language,
|
|
11
|
-
cache = true,
|
|
12
15
|
key,
|
|
13
16
|
...fetchOptions
|
|
14
17
|
} = 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
18
|
const sharedHeaders = {
|
|
21
19
|
...headersToObject(headers),
|
|
22
|
-
...language
|
|
20
|
+
...createLanguageHeader(language)
|
|
23
21
|
};
|
|
24
22
|
const _serverFetchOptions = {
|
|
25
23
|
method: "POST",
|
|
26
24
|
body: {
|
|
27
25
|
query,
|
|
28
|
-
headers: sharedHeaders
|
|
29
|
-
cache
|
|
26
|
+
headers: sharedHeaders
|
|
30
27
|
}
|
|
31
28
|
};
|
|
32
29
|
const _clientFetchOptions = {
|
|
@@ -38,19 +35,8 @@ export function $kql(query, opts = {}) {
|
|
|
38
35
|
},
|
|
39
36
|
body: query
|
|
40
37
|
};
|
|
41
|
-
|
|
38
|
+
return useRequestFetch()(kirby.client ? kirby.kqlPath : buildApiProxyPath(key), {
|
|
42
39
|
...fetchOptions,
|
|
43
40
|
...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
41
|
});
|
|
54
|
-
promiseMap.set(_key, request);
|
|
55
|
-
return request;
|
|
56
42
|
}
|
|
@@ -1,22 +1,19 @@
|
|
|
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
|
-
/**
|
|
10
|
-
* Cache the response between function calls for the same path.
|
|
11
|
-
* @default true
|
|
12
|
-
*/
|
|
13
|
-
cache?: boolean;
|
|
14
13
|
/**
|
|
15
14
|
* Watch an array of reactive sources and auto-refresh the fetch result when they change.
|
|
16
15
|
* Path and language are watched by default. You can completely ignore reactive sources by using `watch: false`.
|
|
17
|
-
* @default undefined
|
|
18
16
|
*/
|
|
19
17
|
watch?: MultiWatchSources | false;
|
|
20
18
|
};
|
|
21
19
|
export declare function useKirbyData<T = any>(path: MaybeRefOrGetter<string>, opts?: UseKirbyDataOptions<T>): AsyncData<T | undefined, NuxtError>;
|
|
22
|
-
export {};
|
|
@@ -1,60 +1,27 @@
|
|
|
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 { sendKirbyRequest } from "./$kirby.js";
|
|
6
5
|
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;
|
|
6
|
+
const { language, ...fetchOptions } = opts;
|
|
23
7
|
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
|
-
});
|
|
8
|
+
const _path = computed(() => toValue(path).replace(/^\//, ""));
|
|
28
9
|
const key = computed(() => `$kirby${hash([
|
|
29
10
|
_path.value,
|
|
30
|
-
|
|
31
|
-
|
|
11
|
+
_language.value,
|
|
12
|
+
toValue(fetchOptions.query),
|
|
13
|
+
toValue(fetchOptions.method),
|
|
14
|
+
toValue(fetchOptions.body)
|
|
32
15
|
])}`);
|
|
33
|
-
if (!_path.value
|
|
34
|
-
console.warn("[
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
};
|
|
44
|
-
let controller;
|
|
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
|
-
signal: controller.signal,
|
|
53
|
-
language: _language.value,
|
|
54
|
-
cache,
|
|
55
|
-
key: key.value
|
|
56
|
-
});
|
|
57
|
-
},
|
|
58
|
-
asyncDataOptions
|
|
59
|
-
);
|
|
16
|
+
if (!_path.value)
|
|
17
|
+
console.warn("[nuxt-kirby] Empty Kirby path");
|
|
18
|
+
return useFetch(_path, {
|
|
19
|
+
...fetchOptions,
|
|
20
|
+
key,
|
|
21
|
+
$fetch: ((_request, options) => sendKirbyRequest(_path.value, {
|
|
22
|
+
...options,
|
|
23
|
+
language: _language.value,
|
|
24
|
+
key: key.value
|
|
25
|
+
}))
|
|
26
|
+
});
|
|
60
27
|
}
|
|
@@ -2,20 +2,14 @@ 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
|
-
/**
|
|
11
|
-
* Cache the response between function calls for the same query.
|
|
12
|
-
* @default true
|
|
13
|
-
*/
|
|
14
|
-
cache?: boolean;
|
|
15
10
|
/**
|
|
16
11
|
* Watch an array of reactive sources and auto-refresh the fetch result when they change.
|
|
17
12
|
* Query and language are watched by default. You can completely ignore reactive sources by using `watch: false`.
|
|
18
|
-
* @default undefined
|
|
19
13
|
*/
|
|
20
14
|
watch?: MultiWatchSources | false;
|
|
21
15
|
};
|
|
@@ -1,49 +1,21 @@
|
|
|
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 { sendKqlRequest } from "./$kql.js";
|
|
5
5
|
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;
|
|
6
|
+
const { language, ...fetchOptions } = opts;
|
|
19
7
|
const _query = computed(() => toValue(query));
|
|
20
8
|
const _language = computed(() => toValue(language));
|
|
21
9
|
const key = computed(() => `$kql${hash([_query.value, _language.value])}`);
|
|
22
10
|
if (Object.keys(_query.value).length === 0 || !_query.value.query)
|
|
23
|
-
console.error("[
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
};
|
|
33
|
-
let controller;
|
|
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
|
-
);
|
|
11
|
+
console.error("[nuxt-kirby] Empty KQL query");
|
|
12
|
+
return useFetch(key, {
|
|
13
|
+
...fetchOptions,
|
|
14
|
+
key,
|
|
15
|
+
$fetch: ((_request, options) => sendKqlRequest(_query.value, {
|
|
16
|
+
...options,
|
|
17
|
+
language: _language.value,
|
|
18
|
+
key: key.value
|
|
19
|
+
}))
|
|
20
|
+
});
|
|
49
21
|
}
|
|
@@ -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,75 @@
|
|
|
1
|
-
import { useRuntimeConfig } from "#imports";
|
|
2
|
-
import { consola } from "consola";
|
|
3
1
|
import { destr } from "destr";
|
|
4
2
|
import { createError, defineEventHandler, getRouterParam, readBody, setResponseHeader, setResponseStatus, splitCookiesString } from "h3";
|
|
5
|
-
import { defineCachedFunction } from "nitropack/runtime
|
|
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
|
+
}) {
|
|
24
|
+
const kirby = useRuntimeConfig(event).kirby;
|
|
25
|
+
const response = await globalThis.$fetch.raw(isQueryRequest ? kirby.kqlPath : path, {
|
|
26
|
+
responseType: "arrayBuffer",
|
|
27
|
+
ignoreResponseError: true,
|
|
28
|
+
baseURL: kirby.url,
|
|
29
|
+
...isQueryRequest ? {
|
|
30
|
+
method: "POST",
|
|
31
|
+
body: query
|
|
32
|
+
} : {
|
|
33
|
+
method,
|
|
34
|
+
query,
|
|
35
|
+
body
|
|
36
|
+
},
|
|
37
|
+
headers: {
|
|
38
|
+
...headers,
|
|
39
|
+
...createAuthHeader(kirby)
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
return {
|
|
43
|
+
status: response.status,
|
|
44
|
+
statusText: response.statusText,
|
|
45
|
+
headers: [...response.headers.entries()],
|
|
46
|
+
data: new Uint8Array(response._data ?? [])
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function createCachedFetcher(kirby) {
|
|
50
|
+
return defineCachedFunction(
|
|
51
|
+
// Nitro reads the event from the first argument only, and hands the cache write to
|
|
52
|
+
// `event.waitUntil()` โ without it an edge worker is torn down before the entry reaches storage.
|
|
53
|
+
async (event, options) => {
|
|
54
|
+
const { data, ...rest } = await fetchFromKirby(event, options);
|
|
55
|
+
return { ...rest, data: uint8ArrayToBase64(data) };
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: "nuxt-kirby",
|
|
59
|
+
base: kirby.server.storage,
|
|
60
|
+
swr: kirby.server.swr,
|
|
61
|
+
maxAge: kirby.server.maxAge,
|
|
62
|
+
// Reading the caller's `key` instead would let one visitor plant a response under the key another reads.
|
|
63
|
+
getKey: (event, options) => hash(options)
|
|
64
|
+
}
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
let cachedFetcher;
|
|
16
68
|
export default defineEventHandler(async (event) => {
|
|
17
69
|
const kirby = useRuntimeConfig(event).kirby;
|
|
18
70
|
const body = await readBody(event);
|
|
19
71
|
const key = decodeURIComponent(getRouterParam(event, "key"));
|
|
20
72
|
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
73
|
if (isQueryRequest) {
|
|
64
74
|
if (!body.query?.query) {
|
|
65
75
|
throw createError({
|
|
@@ -76,18 +86,25 @@ export default defineEventHandler(async (event) => {
|
|
|
76
86
|
}
|
|
77
87
|
}
|
|
78
88
|
try {
|
|
79
|
-
|
|
80
|
-
|
|
89
|
+
let response;
|
|
90
|
+
if (kirby.server.cache) {
|
|
91
|
+
cachedFetcher ??= createCachedFetcher(kirby);
|
|
92
|
+
const cached = await cachedFetcher(event, { isQueryRequest, ...body });
|
|
93
|
+
response = { ...cached, data: base64ToUint8Array(cached.data) };
|
|
94
|
+
} else {
|
|
95
|
+
response = await fetchFromKirby(event, { isQueryRequest, ...body });
|
|
96
|
+
}
|
|
97
|
+
const dataArray = response.data;
|
|
81
98
|
if (response.status >= 400 && response.status < 600) {
|
|
82
99
|
if (isQueryRequest) {
|
|
83
|
-
|
|
100
|
+
console.error(`[nuxt-kirby] Failed KQL query "${body.query?.query}" (...) with status code ${response.status}:
|
|
84
101
|
`, destr(
|
|
85
102
|
uint8ArrayToString(dataArray)
|
|
86
103
|
));
|
|
87
104
|
if (kirby.server.verboseErrors)
|
|
88
|
-
|
|
105
|
+
console.error("[nuxt-kirby] Full KQL query request:", body.query);
|
|
89
106
|
} else {
|
|
90
|
-
|
|
107
|
+
console.error(`[nuxt-kirby] Failed ${(body.method || "get").toUpperCase()} request to "${body.path}"`);
|
|
91
108
|
}
|
|
92
109
|
}
|
|
93
110
|
const cookies = [];
|
|
@@ -105,7 +122,7 @@ export default defineEventHandler(async (event) => {
|
|
|
105
122
|
setResponseStatus(event, response.status, response.statusText);
|
|
106
123
|
return dataArray;
|
|
107
124
|
} catch (error) {
|
|
108
|
-
|
|
125
|
+
console.error("[nuxt-kirby]", error);
|
|
109
126
|
throw createError({
|
|
110
127
|
statusCode: 503,
|
|
111
128
|
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,10 @@
|
|
|
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
|
+
}
|
|
8
|
+
export interface ServerFetchRequest extends ServerFetchOptions {
|
|
9
|
+
isQueryRequest: boolean;
|
|
7
10
|
}
|
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,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-kirby",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
5
|
-
"packageManager": "pnpm@
|
|
4
|
+
"version": "4.0.0",
|
|
5
|
+
"packageManager": "pnpm@11.20.0",
|
|
6
6
|
"description": "Kirby CMS integration for Nuxt with KQL support",
|
|
7
7
|
"author": "Johann Schopplich <hello@johannschopplich.com>",
|
|
8
8
|
"license": "MIT",
|
|
@@ -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.
|
|
55
|
-
"ofetch": "^1.
|
|
51
|
+
"@nuxt/kit": "^4.5.2",
|
|
52
|
+
"defu": "^6.1.7",
|
|
53
|
+
"destr": "^2.0.5",
|
|
54
|
+
"kirby-types": "^1.6.0",
|
|
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
|
}
|