nuxt-kirby 1.1.0 โ 3.0.1
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/LICENSE +21 -0
- package/README.md +70 -0
- package/dist/module.d.mts +124 -0
- package/dist/module.json +12 -0
- package/dist/module.mjs +198 -0
- package/dist/runtime/composables/$kirby.d.ts +19 -0
- package/dist/runtime/composables/$kirby.js +69 -0
- package/dist/runtime/composables/$kql.d.ts +20 -0
- package/dist/runtime/composables/$kql.js +56 -0
- package/dist/runtime/composables/useKirbyData.d.ts +22 -0
- package/dist/runtime/composables/useKirbyData.js +60 -0
- package/dist/runtime/composables/useKql.d.ts +22 -0
- package/dist/runtime/composables/useKql.js +49 -0
- package/dist/runtime/server/handler.d.ts +2 -0
- package/dist/runtime/server/handler.js +114 -0
- package/dist/runtime/server/imports.d.ts +16 -0
- package/dist/runtime/server/imports.js +32 -0
- package/dist/runtime/types.d.ts +7 -0
- package/dist/runtime/types.js +0 -0
- package/dist/runtime/utils.d.ts +12 -0
- package/dist/runtime/utils.js +19 -0
- package/dist/types.d.mts +3 -0
- package/package.json +69 -7
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-PRESENT Johann Schopplich <https://github.com/johannschopplich>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
[](https://nuxt-kirby.byjohann.dev)
|
|
2
|
+
|
|
3
|
+
# Nuxt Kirby
|
|
4
|
+
|
|
5
|
+
[Nuxt](https://nuxt.com) module to interact with [Kirby CMS](https://getkirby.com) with support for [Kirby's Query Language](https://github.com/getkirby/kql) API.
|
|
6
|
+
|
|
7
|
+
- [โจ Release Notes](https://github.com/johannschopplich/nuxt-kirby/releases)
|
|
8
|
+
- [๐ Read the documentation](https://nuxt-kirby.byjohann.dev)
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- ๐ Protected Kirby credentials when sending queries
|
|
13
|
+
- ๐ชข Supports token-based authentication with the [Kirby Headless plugin](https://kirby.tools/docs/headless/getting-started/) (recommended)
|
|
14
|
+
- ๐ฑ Handle request just like with the [`useFetch`](https://nuxt.com/docs/getting-started/data-fetching) composable
|
|
15
|
+
- ๐ฆฆ [Multiple starter kits](https://nuxt-kirby.byjohann.dev/essentials/starter-kits) available
|
|
16
|
+
- ๐ Cached query responses
|
|
17
|
+
- ๐คน No CORS issues!
|
|
18
|
+
- ๐ฆพ Strongly typed
|
|
19
|
+
|
|
20
|
+
## Setup
|
|
21
|
+
|
|
22
|
+
> [!TIP]
|
|
23
|
+
> [๐ Read the documentation](https://nuxt-kirby.byjohann.dev)
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npx nuxi@latest module add kirby
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Basic Usage
|
|
30
|
+
|
|
31
|
+
> [!TIP]
|
|
32
|
+
> [๐ Read the documentation](https://nuxt-kirby.byjohann.dev)
|
|
33
|
+
|
|
34
|
+
Add the Nuxt Kirby module to your Nuxt config:
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
// `nuxt.config.ts`
|
|
38
|
+
export default defineNuxtConfig({
|
|
39
|
+
modules: ['nuxt-kirby']
|
|
40
|
+
})
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
And send queries in your template:
|
|
44
|
+
|
|
45
|
+
```vue
|
|
46
|
+
<script setup lang="ts">
|
|
47
|
+
const { data, error, status } = await useKql({
|
|
48
|
+
query: 'site'
|
|
49
|
+
})
|
|
50
|
+
</script>
|
|
51
|
+
|
|
52
|
+
<template>
|
|
53
|
+
<div>
|
|
54
|
+
<h1>{{ data?.result?.title }}</h1>
|
|
55
|
+
<pre>{{ JSON.stringify(data?.result, undefined, 2) }}</pre>
|
|
56
|
+
</div>
|
|
57
|
+
</template>
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## ๐ป Development
|
|
61
|
+
|
|
62
|
+
1. Clone this repository
|
|
63
|
+
2. Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`
|
|
64
|
+
3. Install dependencies using `pnpm install`
|
|
65
|
+
4. Run `pnpm run dev:prepare`
|
|
66
|
+
5. Start development server using `pnpm run dev`
|
|
67
|
+
|
|
68
|
+
## License
|
|
69
|
+
|
|
70
|
+
[MIT](./LICENSE) License ยฉ 2022-PRESENT [Johann Schopplich](https://github.com/johannschopplich)
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
+
import { KirbyQueryRequest } from 'kirby-types';
|
|
3
|
+
|
|
4
|
+
interface ModuleOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Kirby base URL, like `https://kirby.example.com`
|
|
7
|
+
*
|
|
8
|
+
* @default process.env.KIRBY_BASE_URL
|
|
9
|
+
*/
|
|
10
|
+
url?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Kirby KQL API endpoint path
|
|
13
|
+
*
|
|
14
|
+
* @default 'api/query' // for `basic` authentication
|
|
15
|
+
* @default 'api/kql' // for `bearer` authentication
|
|
16
|
+
*/
|
|
17
|
+
kqlPath?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Kirby KQL API route path
|
|
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
|
|
31
|
+
*
|
|
32
|
+
* @default 'basic'
|
|
33
|
+
*/
|
|
34
|
+
auth?: 'basic' | 'bearer' | 'none';
|
|
35
|
+
/**
|
|
36
|
+
* Token for bearer authentication
|
|
37
|
+
*
|
|
38
|
+
* @default process.env.KIRBY_API_TOKEN
|
|
39
|
+
*/
|
|
40
|
+
token?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Username/password pair for basic authentication
|
|
43
|
+
*
|
|
44
|
+
* @default { username: process.env.KIRBY_API_USERNAME, password: process.env.KIRBY_API_PASSWORD }
|
|
45
|
+
*/
|
|
46
|
+
credentials?: {
|
|
47
|
+
username: string;
|
|
48
|
+
password: string;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Send client-side requests instead of using the server-side proxy
|
|
52
|
+
*
|
|
53
|
+
* @remarks
|
|
54
|
+
* By default, data from Kirby is fetched safely with a server-side proxy.
|
|
55
|
+
* If enabled, query requests will be be sent directly from the client.
|
|
56
|
+
* Note: This means your token or user credentials will be publicly visible.
|
|
57
|
+
* If Nuxt SSR is disabled, this option is enabled by default.
|
|
58
|
+
*
|
|
59
|
+
* @default false
|
|
60
|
+
*/
|
|
61
|
+
client?: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Prefetch custom KQL queries at build-time
|
|
64
|
+
*
|
|
65
|
+
* @remarks
|
|
66
|
+
* The queries will be fully typed and importable from `#nuxt-kirby`.
|
|
67
|
+
*
|
|
68
|
+
* @default {}
|
|
69
|
+
*/
|
|
70
|
+
prefetch?: Record<string, KirbyQueryRequest | {
|
|
71
|
+
query: KirbyQueryRequest;
|
|
72
|
+
language: string;
|
|
73
|
+
}>;
|
|
74
|
+
/**
|
|
75
|
+
* Server-side features
|
|
76
|
+
*/
|
|
77
|
+
server?: {
|
|
78
|
+
/**
|
|
79
|
+
* Enable server-side caching of queries using the Nitro cache API
|
|
80
|
+
*
|
|
81
|
+
* @see https://nitro.unjs.io/guide/cache
|
|
82
|
+
*/
|
|
83
|
+
cache?: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Name of the storage mountpoint to use for caching
|
|
86
|
+
*
|
|
87
|
+
* @see https://nitro.unjs.io/guide/cache
|
|
88
|
+
* @default 'cache'
|
|
89
|
+
*/
|
|
90
|
+
storage?: string;
|
|
91
|
+
/**
|
|
92
|
+
* Enable stale-while-revalidate behavior (cache is returned while it is being updated)
|
|
93
|
+
*
|
|
94
|
+
* @see https://nitro.unjs.io/guide/cache#options
|
|
95
|
+
* @default false
|
|
96
|
+
*/
|
|
97
|
+
swr?: boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Number of seconds to cache the query response
|
|
100
|
+
*
|
|
101
|
+
* @see https://nitro.unjs.io/guide/cache#options
|
|
102
|
+
* @default 1
|
|
103
|
+
*/
|
|
104
|
+
maxAge?: number;
|
|
105
|
+
/**
|
|
106
|
+
* Log verbose errors to the console if a query fails
|
|
107
|
+
*
|
|
108
|
+
* @remarks
|
|
109
|
+
* This will log the full query to the console. Depending on the content of the query, this could be a security risk.
|
|
110
|
+
*
|
|
111
|
+
* @default false
|
|
112
|
+
*/
|
|
113
|
+
verboseErrors?: boolean;
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
declare module '@nuxt/schema' {
|
|
117
|
+
interface RuntimeConfig {
|
|
118
|
+
kirby: ModuleOptions;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
|
122
|
+
|
|
123
|
+
export { _default as default };
|
|
124
|
+
export type { ModuleOptions };
|
package/dist/module.json
ADDED
package/dist/module.mjs
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import process from 'node:process';
|
|
2
|
+
import { useLogger, defineNuxtModule, createResolver, addServerHandler, addImports, addTemplate } from '@nuxt/kit';
|
|
3
|
+
import { defu } from 'defu';
|
|
4
|
+
import { join } from 'pathe';
|
|
5
|
+
import { pascalCase } from 'scule';
|
|
6
|
+
import { withLeadingSlash } from 'ufo';
|
|
7
|
+
import { ofetch } from 'ofetch';
|
|
8
|
+
import { createAuthHeader } from '../dist/runtime/utils.js';
|
|
9
|
+
|
|
10
|
+
const name = "nuxt-kirby";
|
|
11
|
+
|
|
12
|
+
const logger = useLogger("nuxt-kirby");
|
|
13
|
+
|
|
14
|
+
async function prefetchQueries(options) {
|
|
15
|
+
const results = /* @__PURE__ */ new Map();
|
|
16
|
+
if (!options.prefetch || Object.keys(options.prefetch).length === 0)
|
|
17
|
+
return results;
|
|
18
|
+
if (!options.url) {
|
|
19
|
+
logger.error("Skipping KQL prefetch, since no Kirby base URL is provided");
|
|
20
|
+
return results;
|
|
21
|
+
}
|
|
22
|
+
for (const key of Object.keys(options.prefetch)) {
|
|
23
|
+
if (key === pascalCase(key)) {
|
|
24
|
+
logger.error(`Prefetch query key "${key}" conflicts with its PascalCase type name. Please use a different key (e.g., "${key.charAt(0).toLowerCase() + key.slice(1)}")`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const start = Date.now();
|
|
28
|
+
for (const [key, query] of Object.entries(options.prefetch)) {
|
|
29
|
+
const language = "language" in query ? query.language : void 0;
|
|
30
|
+
if (language && !query.query) {
|
|
31
|
+
logger.error(`Prefetch KQL query "${key}" requires the "query" property in multi-language mode`);
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
results.set(
|
|
36
|
+
key,
|
|
37
|
+
await ofetch(options.kqlPath, {
|
|
38
|
+
baseURL: options.url,
|
|
39
|
+
method: "POST",
|
|
40
|
+
body: language ? query.query : query,
|
|
41
|
+
headers: {
|
|
42
|
+
...createAuthHeader(options),
|
|
43
|
+
...language && { "X-Language": language }
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
const _error = error;
|
|
49
|
+
logger.error(
|
|
50
|
+
`Prefetch KQL query "${key}" failed${_error.status ? ` with status code ${_error.status}:
|
|
51
|
+
${JSON.stringify(_error.data, void 0, 2)}` : `: ${_error}`}`
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (results.size > 0) {
|
|
56
|
+
const firstKey = results.keys().next().value;
|
|
57
|
+
logger.info(
|
|
58
|
+
`Prefetched ${results.size === 1 ? "" : `${results.size} `}KQL ${results.size === 1 ? `query "${firstKey}"` : "queries"} in ${Date.now() - start}ms`
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
return results;
|
|
62
|
+
}
|
|
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
|
+
const module = defineNuxtModule({
|
|
79
|
+
meta: {
|
|
80
|
+
name,
|
|
81
|
+
configKey: "kirby",
|
|
82
|
+
compatibility: {
|
|
83
|
+
nuxt: ">=4.0.0"
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
defaults: {
|
|
87
|
+
url: process.env.KIRBY_BASE_URL || "",
|
|
88
|
+
kqlPath: "",
|
|
89
|
+
prefix: "",
|
|
90
|
+
auth: "basic",
|
|
91
|
+
token: process.env.KIRBY_API_TOKEN || "",
|
|
92
|
+
credentials: {
|
|
93
|
+
username: process.env.KIRBY_API_USERNAME || "",
|
|
94
|
+
password: process.env.KIRBY_API_PASSWORD || ""
|
|
95
|
+
},
|
|
96
|
+
client: false,
|
|
97
|
+
prefetch: {},
|
|
98
|
+
server: {
|
|
99
|
+
cache: false,
|
|
100
|
+
storage: "cache",
|
|
101
|
+
swr: false,
|
|
102
|
+
maxAge: 1,
|
|
103
|
+
verboseErrors: false
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
async setup(options, nuxt) {
|
|
107
|
+
const moduleName = name;
|
|
108
|
+
if (!options.url)
|
|
109
|
+
logger.error("Missing `KIRBY_BASE_URL` environment variable");
|
|
110
|
+
if (options.auth === "basic" && (!options.credentials || !options.credentials.username || !options.credentials.password))
|
|
111
|
+
logger.error("Missing `KIRBY_API_USERNAME` and `KIRBY_API_PASSWORD` environment variable for basic authentication");
|
|
112
|
+
if (options.auth === "bearer" && !options.token)
|
|
113
|
+
logger.error("Missing `KIRBY_API_TOKEN` environment variable for bearer authentication");
|
|
114
|
+
if (options.prefix && !options.kqlPath) {
|
|
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
|
+
}
|
|
124
|
+
if (!nuxt.options.ssr) {
|
|
125
|
+
logger.info("Kirby requests are client-only because SSR is disabled");
|
|
126
|
+
options.client = true;
|
|
127
|
+
}
|
|
128
|
+
if (options.server) {
|
|
129
|
+
options.server.storage ||= "cache";
|
|
130
|
+
options.server.storage = withLeadingSlash(options.server.storage);
|
|
131
|
+
}
|
|
132
|
+
nuxt.options.runtimeConfig.kirby = defu(
|
|
133
|
+
nuxt.options.runtimeConfig.kirby,
|
|
134
|
+
options
|
|
135
|
+
);
|
|
136
|
+
nuxt.options.runtimeConfig.public.kirby = defu(
|
|
137
|
+
nuxt.options.runtimeConfig.public.kirby,
|
|
138
|
+
options.client ? options : { client: false }
|
|
139
|
+
);
|
|
140
|
+
const { resolve } = createResolver(import.meta.url);
|
|
141
|
+
nuxt.options.build.transpile.push(resolve("runtime"));
|
|
142
|
+
addServerHandler({
|
|
143
|
+
route: "/api/__kirby__/:key",
|
|
144
|
+
handler: resolve("runtime/server/handler"),
|
|
145
|
+
method: "post"
|
|
146
|
+
});
|
|
147
|
+
addImports(
|
|
148
|
+
["$kirby", "$kql", "useKirbyData", "useKql"].map((name2) => ({
|
|
149
|
+
name: name2,
|
|
150
|
+
as: name2,
|
|
151
|
+
from: resolve(`runtime/composables/${name2}`)
|
|
152
|
+
}))
|
|
153
|
+
);
|
|
154
|
+
nuxt.hooks.hook("nitro:config", (config) => {
|
|
155
|
+
config.externals ||= {};
|
|
156
|
+
config.externals.inline ||= [];
|
|
157
|
+
config.externals.inline.push(resolve("runtime/utils"));
|
|
158
|
+
config.imports = defu(config.imports, {
|
|
159
|
+
presets: [{
|
|
160
|
+
from: resolve("runtime/server/imports"),
|
|
161
|
+
imports: ["$kirby", "$kql"]
|
|
162
|
+
}]
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
nuxt.options.alias[`#${moduleName}`] = join(nuxt.options.buildDir, `module/${moduleName}`);
|
|
166
|
+
nuxt.options.alias["#nuxt-kql"] = join(nuxt.options.buildDir, `module/${moduleName}`);
|
|
167
|
+
const prefetchedQueries = await prefetchQueries(options);
|
|
168
|
+
addTemplate({
|
|
169
|
+
filename: `module/${moduleName}.mjs`,
|
|
170
|
+
write: true,
|
|
171
|
+
getContents() {
|
|
172
|
+
return `
|
|
173
|
+
// Generated by ${moduleName}
|
|
174
|
+
${[...prefetchedQueries.entries()].map(([key, response]) => `
|
|
175
|
+
export const ${key} = ${JSON.stringify(response?.result || null, void 0, 2)}
|
|
176
|
+
`.trimStart()).join("") || `export {}
|
|
177
|
+
`}`.trimStart();
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
addTemplate({
|
|
181
|
+
filename: `module/${moduleName}.d.ts`,
|
|
182
|
+
write: true,
|
|
183
|
+
getContents() {
|
|
184
|
+
return `
|
|
185
|
+
// Generated by ${moduleName}
|
|
186
|
+
export type { ${KIRBY_TYPES_PKG_EXPORT_NAMES.join(", ")} } from 'kirby-types'
|
|
187
|
+
|
|
188
|
+
${[...prefetchedQueries.entries()].map(([key, response]) => `
|
|
189
|
+
export declare const ${key}: ${JSON.stringify(response?.result || null, void 0, 2)}
|
|
190
|
+
export type ${pascalCase(key)} = typeof ${key}
|
|
191
|
+
`.trimStart()).join("") || `export {}
|
|
192
|
+
`}`.trimStart();
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
export { module as default };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { NitroFetchOptions } from 'nitropack';
|
|
2
|
+
export type KirbyFetchOptions = Pick<NitroFetchOptions<string>, 'onRequest' | 'onRequestError' | 'onResponse' | 'onResponseError' | 'method' | 'headers' | 'query' | 'body' | 'retry' | 'retryDelay' | 'retryStatusCodes' | 'timeout' | 'signal'> & {
|
|
3
|
+
/**
|
|
4
|
+
* Language code to fetch data for in multi-language Kirby setups.
|
|
5
|
+
*/
|
|
6
|
+
language?: string;
|
|
7
|
+
/**
|
|
8
|
+
* Cache the response between function calls for the same path.
|
|
9
|
+
* @default true
|
|
10
|
+
*/
|
|
11
|
+
cache?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* By default, a cache key will be generated from the request options.
|
|
14
|
+
* With this option, you can provide a custom cache key.
|
|
15
|
+
* @default undefined
|
|
16
|
+
*/
|
|
17
|
+
key?: string;
|
|
18
|
+
};
|
|
19
|
+
export declare function $kirby<T = any>(path: string, opts?: KirbyFetchOptions): Promise<T>;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { useNuxtApp, useRequestFetch, useRuntimeConfig } from "#imports";
|
|
2
|
+
import { hash } from "ohash";
|
|
3
|
+
import { joinURL } from "ufo";
|
|
4
|
+
import { buildApiProxyPath, createAuthHeader, headersToObject } from "../utils.js";
|
|
5
|
+
export function $kirby(path, opts = {}) {
|
|
6
|
+
const nuxt = useNuxtApp();
|
|
7
|
+
const promiseMap = nuxt._pendingRequests ||= /* @__PURE__ */ new Map();
|
|
8
|
+
const {
|
|
9
|
+
method,
|
|
10
|
+
headers,
|
|
11
|
+
query,
|
|
12
|
+
body,
|
|
13
|
+
language,
|
|
14
|
+
cache = true,
|
|
15
|
+
key,
|
|
16
|
+
...fetchOptions
|
|
17
|
+
} = opts;
|
|
18
|
+
const kirby = useRuntimeConfig().public.kirby;
|
|
19
|
+
if (language)
|
|
20
|
+
path = joinURL(language, path);
|
|
21
|
+
const _key = key || `$kirby${hash([
|
|
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);
|
|
33
|
+
const _serverFetchOptions = {
|
|
34
|
+
method: "POST",
|
|
35
|
+
body: {
|
|
36
|
+
path,
|
|
37
|
+
method,
|
|
38
|
+
query,
|
|
39
|
+
body,
|
|
40
|
+
headers: sharedHeaders,
|
|
41
|
+
cache
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const _clientFetchOptions = {
|
|
45
|
+
baseURL: kirby.url,
|
|
46
|
+
query,
|
|
47
|
+
method,
|
|
48
|
+
headers: {
|
|
49
|
+
...sharedHeaders,
|
|
50
|
+
...createAuthHeader(kirby)
|
|
51
|
+
},
|
|
52
|
+
body
|
|
53
|
+
};
|
|
54
|
+
const request = useRequestFetch()(kirby.client ? path : buildApiProxyPath(_key), {
|
|
55
|
+
...fetchOptions,
|
|
56
|
+
...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
|
+
});
|
|
67
|
+
promiseMap.set(_key, request);
|
|
68
|
+
return request;
|
|
69
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { KirbyQueryRequest, KirbyQueryResponse } from 'kirby-types';
|
|
2
|
+
import type { NitroFetchOptions } from 'nitropack';
|
|
3
|
+
export type KqlOptions = Pick<NitroFetchOptions<string>, 'onRequest' | 'onRequestError' | 'onResponse' | 'onResponseError' | 'headers' | 'retry' | 'retryDelay' | 'retryStatusCodes' | 'timeout' | 'signal'> & {
|
|
4
|
+
/**
|
|
5
|
+
* Language code to fetch data for in multi-language Kirby setups.
|
|
6
|
+
*/
|
|
7
|
+
language?: string;
|
|
8
|
+
/**
|
|
9
|
+
* Cache the response between function calls for the same query.
|
|
10
|
+
* @default true
|
|
11
|
+
*/
|
|
12
|
+
cache?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* By default, a cache key will be generated from the request options.
|
|
15
|
+
* With this option, you can provide a custom cache key.
|
|
16
|
+
* @default undefined
|
|
17
|
+
*/
|
|
18
|
+
key?: string;
|
|
19
|
+
};
|
|
20
|
+
export declare function $kql<T extends KirbyQueryResponse<any, boolean> = KirbyQueryResponse>(query: KirbyQueryRequest, opts?: KqlOptions): Promise<T>;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { useNuxtApp, useRequestFetch, useRuntimeConfig } from "#imports";
|
|
2
|
+
import { hash } from "ohash";
|
|
3
|
+
import { buildApiProxyPath, createAuthHeader, headersToObject } from "../utils.js";
|
|
4
|
+
export function $kql(query, opts = {}) {
|
|
5
|
+
const nuxt = useNuxtApp();
|
|
6
|
+
const promiseMap = nuxt._pendingRequests ||= /* @__PURE__ */ new Map();
|
|
7
|
+
const kirby = useRuntimeConfig().public.kirby;
|
|
8
|
+
const {
|
|
9
|
+
headers,
|
|
10
|
+
language,
|
|
11
|
+
cache = true,
|
|
12
|
+
key,
|
|
13
|
+
...fetchOptions
|
|
14
|
+
} = 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
|
+
const sharedHeaders = {
|
|
21
|
+
...headersToObject(headers),
|
|
22
|
+
...language && { "X-Language": language }
|
|
23
|
+
};
|
|
24
|
+
const _serverFetchOptions = {
|
|
25
|
+
method: "POST",
|
|
26
|
+
body: {
|
|
27
|
+
query,
|
|
28
|
+
headers: sharedHeaders,
|
|
29
|
+
cache
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
const _clientFetchOptions = {
|
|
33
|
+
baseURL: kirby.url,
|
|
34
|
+
method: "POST",
|
|
35
|
+
headers: {
|
|
36
|
+
...sharedHeaders,
|
|
37
|
+
...createAuthHeader(kirby)
|
|
38
|
+
},
|
|
39
|
+
body: query
|
|
40
|
+
};
|
|
41
|
+
const request = useRequestFetch()(kirby.client ? kirby.kqlPath : buildApiProxyPath(_key), {
|
|
42
|
+
...fetchOptions,
|
|
43
|
+
...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
|
+
});
|
|
54
|
+
promiseMap.set(_key, request);
|
|
55
|
+
return request;
|
|
56
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { NitroFetchOptions } from 'nitropack';
|
|
2
|
+
import type { AsyncData, AsyncDataOptions, NuxtError } from 'nuxt/app';
|
|
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' | 'timeout'> & {
|
|
5
|
+
/**
|
|
6
|
+
* Language code to fetch data for in multi-language Kirby setups.
|
|
7
|
+
*/
|
|
8
|
+
language?: MaybeRefOrGetter<string>;
|
|
9
|
+
/**
|
|
10
|
+
* Cache the response between function calls for the same path.
|
|
11
|
+
* @default true
|
|
12
|
+
*/
|
|
13
|
+
cache?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Watch an array of reactive sources and auto-refresh the fetch result when they change.
|
|
16
|
+
* Path and language are watched by default. You can completely ignore reactive sources by using `watch: false`.
|
|
17
|
+
* @default undefined
|
|
18
|
+
*/
|
|
19
|
+
watch?: MultiWatchSources | false;
|
|
20
|
+
};
|
|
21
|
+
export declare function useKirbyData<T = any>(path: MaybeRefOrGetter<string>, opts?: UseKirbyDataOptions<T>): AsyncData<T | undefined, NuxtError>;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { useAsyncData } from "#imports";
|
|
2
|
+
import { hash } from "ohash";
|
|
3
|
+
import { joinURL } from "ufo";
|
|
4
|
+
import { computed, toValue } from "vue";
|
|
5
|
+
import { $kirby } from "./$kirby.js";
|
|
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;
|
|
23
|
+
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
|
+
});
|
|
28
|
+
const key = computed(() => `$kirby${hash([
|
|
29
|
+
_path.value,
|
|
30
|
+
query,
|
|
31
|
+
method
|
|
32
|
+
])}`);
|
|
33
|
+
if (!_path.value || _language.value && !_path.value.replace(new RegExp(`^${_language.value}/`), ""))
|
|
34
|
+
console.warn("[useKirbyData] Empty Kirby path");
|
|
35
|
+
const asyncDataOptions = {
|
|
36
|
+
server,
|
|
37
|
+
lazy,
|
|
38
|
+
default: defaultFn,
|
|
39
|
+
transform,
|
|
40
|
+
pick,
|
|
41
|
+
watch: watchSources === false ? [] : [...watchSources || [], key],
|
|
42
|
+
immediate
|
|
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
|
+
);
|
|
60
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { KirbyQueryRequest, KirbyQueryResponse } from 'kirby-types';
|
|
2
|
+
import type { NitroFetchOptions } from 'nitropack';
|
|
3
|
+
import type { AsyncData, AsyncDataOptions, NuxtError } from 'nuxt/app';
|
|
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' | 'timeout'> & {
|
|
6
|
+
/**
|
|
7
|
+
* Language code to fetch data for in multi-language Kirby setups.
|
|
8
|
+
*/
|
|
9
|
+
language?: MaybeRefOrGetter<string>;
|
|
10
|
+
/**
|
|
11
|
+
* Cache the response between function calls for the same query.
|
|
12
|
+
* @default true
|
|
13
|
+
*/
|
|
14
|
+
cache?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Watch an array of reactive sources and auto-refresh the fetch result when they change.
|
|
17
|
+
* Query and language are watched by default. You can completely ignore reactive sources by using `watch: false`.
|
|
18
|
+
* @default undefined
|
|
19
|
+
*/
|
|
20
|
+
watch?: MultiWatchSources | false;
|
|
21
|
+
};
|
|
22
|
+
export declare function useKql<ResT extends KirbyQueryResponse<any, boolean> = KirbyQueryResponse, ReqT extends KirbyQueryRequest = KirbyQueryRequest>(query: MaybeRefOrGetter<ReqT>, opts?: UseKqlOptions<ResT>): AsyncData<ResT | undefined, NuxtError>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { useAsyncData } from "#imports";
|
|
2
|
+
import { hash } from "ohash";
|
|
3
|
+
import { computed, toValue } from "vue";
|
|
4
|
+
import { $kql } from "./$kql.js";
|
|
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;
|
|
19
|
+
const _query = computed(() => toValue(query));
|
|
20
|
+
const _language = computed(() => toValue(language));
|
|
21
|
+
const key = computed(() => `$kql${hash([_query.value, _language.value])}`);
|
|
22
|
+
if (Object.keys(_query.value).length === 0 || !_query.value.query)
|
|
23
|
+
console.error("[useKql] Empty KQL query");
|
|
24
|
+
const asyncDataOptions = {
|
|
25
|
+
server,
|
|
26
|
+
lazy,
|
|
27
|
+
default: defaultFn,
|
|
28
|
+
transform,
|
|
29
|
+
pick,
|
|
30
|
+
watch: watchSources === false ? [] : [...watchSources || [], key],
|
|
31
|
+
immediate
|
|
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
|
+
);
|
|
49
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { useRuntimeConfig } from "#imports";
|
|
2
|
+
import { consola } from "consola";
|
|
3
|
+
import { destr } from "destr";
|
|
4
|
+
import { createError, defineEventHandler, getRouterParam, readBody, setResponseHeader, setResponseStatus, splitCookiesString } from "h3";
|
|
5
|
+
import { defineCachedFunction } from "nitropack/runtime/internal/cache";
|
|
6
|
+
import { base64ToUint8Array, uint8ArrayToBase64, uint8ArrayToString } from "uint8array-extras";
|
|
7
|
+
import { createAuthHeader } from "../utils.js";
|
|
8
|
+
const EXCLUDED_HEADERS = /* @__PURE__ */ new Set([
|
|
9
|
+
// https://github.com/h3js/h3/blob/fe9800bbbe9bda2972cc5d11db7353f4ab70f0ba/src/utils/proxy.ts#L97
|
|
10
|
+
"content-encoding",
|
|
11
|
+
"content-length",
|
|
12
|
+
// Reduce information leakage
|
|
13
|
+
"server",
|
|
14
|
+
"x-powered-by"
|
|
15
|
+
]);
|
|
16
|
+
export default defineEventHandler(async (event) => {
|
|
17
|
+
const kirby = useRuntimeConfig(event).kirby;
|
|
18
|
+
const body = await readBody(event);
|
|
19
|
+
const key = decodeURIComponent(getRouterParam(event, "key"));
|
|
20
|
+
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
|
+
if (isQueryRequest) {
|
|
64
|
+
if (!body.query?.query) {
|
|
65
|
+
throw createError({
|
|
66
|
+
statusCode: 400,
|
|
67
|
+
statusMessage: "KQL query is empty"
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
} else {
|
|
71
|
+
if (body.path && new URL(body.path, "http://localhost").origin !== "http://localhost") {
|
|
72
|
+
throw createError({
|
|
73
|
+
statusCode: 400,
|
|
74
|
+
statusMessage: "Absolute URLs are not allowed"
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
const response = kirby.server.cache && body.cache ? await cachedFetcher(event, { key, ...body }) : await fetcher(event, { key, ...body });
|
|
80
|
+
const dataArray = base64ToUint8Array(response.data);
|
|
81
|
+
if (response.status >= 400 && response.status < 600) {
|
|
82
|
+
if (isQueryRequest) {
|
|
83
|
+
consola.error(`Failed KQL query "${body.query?.query}" (...) with status code ${response.status}:
|
|
84
|
+
`, destr(
|
|
85
|
+
uint8ArrayToString(dataArray)
|
|
86
|
+
));
|
|
87
|
+
if (kirby.server.verboseErrors)
|
|
88
|
+
consola.log("Full KQL query request:", body.query);
|
|
89
|
+
} else {
|
|
90
|
+
consola.error(`Failed ${(body.method || "get").toUpperCase()} request to "${body.path}"`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const cookies = [];
|
|
94
|
+
for (const [key2, value] of response.headers) {
|
|
95
|
+
if (EXCLUDED_HEADERS.has(key2))
|
|
96
|
+
continue;
|
|
97
|
+
if (key2 === "set-cookie") {
|
|
98
|
+
cookies.push(...splitCookiesString(value));
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
setResponseHeader(event, key2, value);
|
|
102
|
+
}
|
|
103
|
+
if (cookies.length > 0)
|
|
104
|
+
setResponseHeader(event, "set-cookie", cookies);
|
|
105
|
+
setResponseStatus(event, response.status, response.statusText);
|
|
106
|
+
return dataArray;
|
|
107
|
+
} catch (error) {
|
|
108
|
+
consola.error(error);
|
|
109
|
+
throw createError({
|
|
110
|
+
statusCode: 503,
|
|
111
|
+
statusMessage: "Service Unavailable"
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { KirbyQueryRequest, KirbyQueryResponse } from 'kirby-types';
|
|
2
|
+
import type { NitroFetchOptions } from 'nitropack';
|
|
3
|
+
export type KirbyFetchOptions = Omit<NitroFetchOptions<string>, 'baseURL'> & {
|
|
4
|
+
/**
|
|
5
|
+
* Language code to fetch data for in multi-language Kirby setups.
|
|
6
|
+
*/
|
|
7
|
+
language?: string;
|
|
8
|
+
};
|
|
9
|
+
export type KqlFetchOptions = Pick<NitroFetchOptions<string>, 'onRequest' | 'onRequestError' | 'onResponse' | 'onResponseError' | 'headers' | 'retry' | 'retryDelay' | 'retryStatusCodes' | 'timeout' | 'signal'> & {
|
|
10
|
+
/**
|
|
11
|
+
* Language code to fetch data for in multi-language Kirby setups.
|
|
12
|
+
*/
|
|
13
|
+
language?: string;
|
|
14
|
+
};
|
|
15
|
+
export declare function $kirby<T = any>(path: string, opts?: KirbyFetchOptions): Promise<T>;
|
|
16
|
+
export declare function $kql<T extends KirbyQueryResponse<any, boolean> = KirbyQueryResponse>(query: KirbyQueryRequest, opts?: KqlFetchOptions): Promise<T>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { useRuntimeConfig } from "#imports";
|
|
2
|
+
import { joinURL } from "ufo";
|
|
3
|
+
import { createAuthHeader, headersToObject } from "../utils.js";
|
|
4
|
+
export function $kirby(path, opts = {}) {
|
|
5
|
+
const { headers, language, ...fetchOptions } = opts;
|
|
6
|
+
const kirby = useRuntimeConfig().kirby;
|
|
7
|
+
if (language)
|
|
8
|
+
path = joinURL(language, path);
|
|
9
|
+
return globalThis.$fetch(path, {
|
|
10
|
+
...fetchOptions,
|
|
11
|
+
baseURL: kirby.url,
|
|
12
|
+
headers: {
|
|
13
|
+
...headersToObject(headers),
|
|
14
|
+
...createAuthHeader(kirby)
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
export function $kql(query, opts = {}) {
|
|
19
|
+
const { headers, language, ...fetchOptions } = opts;
|
|
20
|
+
const kirby = useRuntimeConfig().kirby;
|
|
21
|
+
return globalThis.$fetch(kirby.kqlPath, {
|
|
22
|
+
...fetchOptions,
|
|
23
|
+
baseURL: kirby.url,
|
|
24
|
+
method: "POST",
|
|
25
|
+
body: query,
|
|
26
|
+
headers: {
|
|
27
|
+
...headersToObject(headers),
|
|
28
|
+
...createAuthHeader(kirby),
|
|
29
|
+
...language && { "X-Language": language }
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { KirbyQueryRequest } from 'kirby-types';
|
|
2
|
+
import type { NitroFetchOptions } from 'nitropack';
|
|
3
|
+
export interface ServerFetchOptions extends Pick<NitroFetchOptions<string>, 'method' | 'headers' | 'query' | 'body'> {
|
|
4
|
+
query?: Partial<KirbyQueryRequest>;
|
|
5
|
+
path?: string;
|
|
6
|
+
cache?: boolean;
|
|
7
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare function buildApiProxyPath(key: string): string;
|
|
2
|
+
export declare function headersToObject(headers?: HeadersInit): Record<string, string>;
|
|
3
|
+
export declare function createAuthHeader({ auth, token, credentials, }: {
|
|
4
|
+
auth?: string;
|
|
5
|
+
token?: string;
|
|
6
|
+
credentials?: {
|
|
7
|
+
username: string;
|
|
8
|
+
password: string;
|
|
9
|
+
};
|
|
10
|
+
}): {
|
|
11
|
+
Authorization: string;
|
|
12
|
+
} | undefined;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function buildApiProxyPath(key) {
|
|
2
|
+
return `/api/__kirby__/${encodeURIComponent(key)}`;
|
|
3
|
+
}
|
|
4
|
+
export function headersToObject(headers = {}) {
|
|
5
|
+
return Object.fromEntries(new Headers(headers));
|
|
6
|
+
}
|
|
7
|
+
export function createAuthHeader({
|
|
8
|
+
auth,
|
|
9
|
+
token,
|
|
10
|
+
credentials
|
|
11
|
+
}) {
|
|
12
|
+
if (auth === "basic" && credentials) {
|
|
13
|
+
const { username, password } = credentials;
|
|
14
|
+
const encoded = globalThis.btoa(`${username}:${password}`);
|
|
15
|
+
return { Authorization: `Basic ${encoded}` };
|
|
16
|
+
}
|
|
17
|
+
if (auth === "bearer")
|
|
18
|
+
return { Authorization: `Bearer ${token}` };
|
|
19
|
+
}
|
package/dist/types.d.mts
ADDED
package/package.json
CHANGED
|
@@ -1,12 +1,74 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-kirby",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "3.0.1",
|
|
5
|
+
"packageManager": "pnpm@10.17.0",
|
|
6
|
+
"description": "Kirby CMS Integration for Nuxt with KQL support",
|
|
7
|
+
"author": "Johann Schopplich <hello@johannschopplich.com>",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"homepage": "https://nuxt-kirby.byjohann.dev",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/johannschopplich/nuxt-kirby.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/johannschopplich/nuxt-kirby/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"getkirby",
|
|
19
|
+
"kirby",
|
|
20
|
+
"kql",
|
|
21
|
+
"nuxt",
|
|
22
|
+
"query"
|
|
23
|
+
],
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/types.d.mts",
|
|
28
|
+
"default": "./dist/module.mjs"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"main": "./dist/module.mjs",
|
|
32
|
+
"types": "./dist/types.d.mts",
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
9
36
|
"scripts": {
|
|
10
|
-
"
|
|
37
|
+
"prepack": "nuxt-module-build build",
|
|
38
|
+
"dev": "nuxt dev playground",
|
|
39
|
+
"dev:build": "nuxt build playground",
|
|
40
|
+
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxt prepare playground && nuxt prepare test/fixture",
|
|
41
|
+
"docs:dev": "vitepress dev docs",
|
|
42
|
+
"docs:build": "vitepress build docs",
|
|
43
|
+
"docs:preview": "vitepress preview docs",
|
|
44
|
+
"lint": "eslint .",
|
|
45
|
+
"lint:fix": "eslint . --fix",
|
|
46
|
+
"test": "vitest",
|
|
47
|
+
"test:types": "tsc --noEmit",
|
|
48
|
+
"release": "bumpp"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@nuxt/kit": "^4.1.2",
|
|
52
|
+
"@vueuse/core": "^13.9.0",
|
|
53
|
+
"defu": "^6.1.4",
|
|
54
|
+
"kirby-types": "^1.0.0",
|
|
55
|
+
"ofetch": "^1.4.1",
|
|
56
|
+
"ohash": "^2.0.11",
|
|
57
|
+
"pathe": "^2.0.3",
|
|
58
|
+
"scule": "^1.3.0",
|
|
59
|
+
"ufo": "^1.6.1",
|
|
60
|
+
"uint8array-extras": "^1.5.0"
|
|
61
|
+
},
|
|
62
|
+
"devDependencies": {
|
|
63
|
+
"@antfu/eslint-config": "^5.3.0",
|
|
64
|
+
"@nuxt/module-builder": "^1.0.2",
|
|
65
|
+
"@nuxt/test-utils": "^3.19.2",
|
|
66
|
+
"@types/node": "^24.5.2",
|
|
67
|
+
"bumpp": "^10.2.3",
|
|
68
|
+
"destr": "^2.0.5",
|
|
69
|
+
"eslint": "^9.35.0",
|
|
70
|
+
"nuxt": "^4.1.2",
|
|
71
|
+
"typescript": "^5.9.2",
|
|
72
|
+
"vitest": "^3.2.4"
|
|
11
73
|
}
|
|
12
74
|
}
|