Skip to content

Commit 058bb12

Browse files
feat!: client: true disables server proxy for all composables
1 parent 5c30da2 commit 058bb12

File tree

17 files changed

+35
-141
lines changed

17 files changed

+35
-141
lines changed

docs/api/kql.md

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ type KqlOptions = Pick<
2727
* Language code to fetch data for in multi-language Kirby setups.
2828
*/
2929
language?: string
30-
/**
31-
* Skip the Nuxt server proxy and fetch directly from the API.
32-
* Requires `client` to be enabled in the module options as well.
33-
* @default false
34-
*/
35-
client?: boolean
3630
/**
3731
* Cache the response between function calls for the same query.
3832
* @default true
@@ -80,16 +74,7 @@ const data = await $kql(
8074
Authorization credentials will be publicly visible. Also, possible CORS issues ahead if the backend is not configured properly.
8175
:::
8276

83-
To fetch data directly from your Kirby instance, without the Nuxt proxy, set the option `client` to `true`:
84-
85-
```ts{3}
86-
const data = await $kql(
87-
query,
88-
{ client: true }
89-
)
90-
```
91-
92-
This requires the `client` option to be `true` in your `nuxt-kql` module configuration:
77+
To fetch data directly from your Kirby instance without the Nuxt proxy, set the module option `client` to `true`:
9378

9479
```ts{6}
9580
// `nuxt.config.ts`
@@ -101,3 +86,9 @@ export default defineNuxtConfig({
10186
}
10287
})
10388
```
89+
90+
Now, every `$kql` call will be directly use the Kirby instance by sending requests from the client:
91+
92+
```ts{3}
93+
const data = await $kql(query)
94+
```

docs/api/use-kql.md

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ type UseKqlOptions<T> = AsyncDataOptions<T> & Pick<
3030
* Language code to fetch data for in multi-language Kirby setups.
3131
*/
3232
language?: MaybeRefOrGetter<string>
33-
/**
34-
* Skip the Nuxt server proxy and fetch directly from the API.
35-
* Requires `client` to be enabled in the module options as well.
36-
* @default false
37-
*/
38-
client?: boolean
3933
/**
4034
* Cache the response between function calls for the same query.
4135
* @default true
@@ -87,16 +81,7 @@ const { data, pending, error, refresh } = await useKql({
8781
Authorization credentials will be publicly visible. Also, possible CORS issues ahead if the backend is not configured properly.
8882
:::
8983

90-
To fetch data directly from your Kirby instance, without the Nuxt proxy, set the option `client` to `true`:
91-
92-
```ts{3}
93-
const { data } = await useKql(
94-
query,
95-
{ client: true }
96-
)
97-
```
98-
99-
This requires the `client` option to be `true` in your `nuxt-kql` module configuration:
84+
To fetch data directly from your Kirby instance without the Nuxt proxy, set the module option `client` to `true`:
10085

10186
```ts{6}
10287
// `nuxt.config.ts`
@@ -108,3 +93,9 @@ export default defineNuxtConfig({
10893
}
10994
})
11095
```
96+
97+
Now, every `useKql` call will be directly use the Kirby instance by sending requests from the client:
98+
99+
```ts{3}
100+
const { data } = await useKql(query)
101+
```

docs/config/caching.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ If you want to disable caching, you can do so by setting the `cache` option to `
1212
const { data } = await useKql(
1313
query,
1414
{
15-
// Disable caching
15+
// Disable in-memory caching
1616
cache: false
1717
}
1818
)

docs/config/index.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,13 @@ interface ModuleOptions {
6464
}
6565

6666
/**
67-
* Enable client-side requests besides server-side ones
67+
* Send client-side requests instead of using the server-side proxy
6868
*
6969
* @remarks
7070
* By default, KQL data is fetched safely with a server-side proxy.
71-
* If enabled, query requests are allowed to be sent directly from the client.
71+
* If enabled, query requests will be be sent directly from the client.
7272
* Note: This means your token or user credentials will be publicly visible.
73+
* If Nuxt SSR is disabled, this option is enabled by default.
7374
*
7475
* @default false
7576
*/

docs/guide/what-is-nuxt-kql.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# What is nuxt-kql?
22

3-
`nuxt-kql` is a lightweight module for [Nuxt](https://nuxt.com) to reliably fetch data from your Kirby instance using the **Kirby Query Language API**. It works on the server and client-side (the latter has to be enabled explicitly).
3+
`nuxt-kql` is a lightweight module for [Nuxt](https://nuxt.com) to reliably fetch data from your Kirby instance using the **Kirby Query Language API**, while keeping your authentication credentials safe. It works on the server and the client.
44

55
## Motivation
66

playground/nuxt.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ export default defineNuxtConfig({
55
// Enable token-based authentication
66
auth: 'bearer',
77

8-
// Enable client-side query requests with `useKql(query, { client: true })`
9-
client: true,
8+
// Send client-side query requests to Kirby instead of the KQL proxy
9+
// client: true,
1010

1111
// Prefetch queries at build-time
1212
prefetch: {

playground/pages/client-query.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const query = ref<KirbyQueryRequest>({
1010
},
1111
})
1212
13-
const { data } = await useKql(query, { client: true })
13+
const { data } = await useKql(query)
1414
1515
function updateQuery() {
1616
query.value = {

src/runtime/composables/$kirby.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { joinURL } from 'ufo'
22
import { hash } from 'ohash'
33
import type { NitroFetchOptions } from 'nitropack'
44
import type { ServerFetchOptions } from '../types'
5-
import { DEFAULT_CLIENT_ERROR, getAuthHeader, getProxyPath, headersToObject } from '../utils'
5+
import { getAuthHeader, getProxyPath, headersToObject } from '../utils'
66
import { useNuxtApp, useRuntimeConfig } from '#imports'
77

88
export type KirbyFetchOptions = Pick<
@@ -23,12 +23,6 @@ export type KirbyFetchOptions = Pick<
2323
* Language code to fetch data for in multi-language Kirby setups.
2424
*/
2525
language?: string
26-
/**
27-
* Skip the Nuxt server proxy and fetch directly from the API.
28-
* Requires `client` to be enabled in the module options as well.
29-
* @default false
30-
*/
31-
client?: boolean
3226
/**
3327
* Cache the response between function calls for the same path.
3428
* @default true
@@ -48,7 +42,6 @@ export function $kirby<T = any>(
4842
method,
4943
body,
5044
language,
51-
client = false,
5245
cache = true,
5346
...fetchOptions
5447
} = opts
@@ -65,9 +58,6 @@ export function $kirby<T = any>(
6558
language,
6659
])}`
6760

68-
if (client && !kql.client)
69-
throw new Error(DEFAULT_CLIENT_ERROR)
70-
7161
if ((nuxt.isHydrating || cache) && key in nuxt.payload.data)
7262
return Promise.resolve(nuxt.payload.data[key])
7363

@@ -99,9 +89,9 @@ export function $kirby<T = any>(
9989
body,
10090
}
10191

102-
const request = globalThis.$fetch(client ? path : getProxyPath(key), {
92+
const request = globalThis.$fetch(kql.client ? path : getProxyPath(key), {
10393
...fetchOptions,
104-
...(client ? _clientFetchOptions : _serverFetchOptions),
94+
...(kql.client ? _clientFetchOptions : _serverFetchOptions),
10595
})
10696
.then((response) => {
10797
if (process.server || cache)

src/runtime/composables/$kql.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { hash } from 'ohash'
22
import type { NitroFetchOptions } from 'nitropack'
33
import type { KirbyQueryRequest, KirbyQueryResponse } from 'kirby-types'
44
import type { ServerFetchOptions } from '../types'
5-
import { DEFAULT_CLIENT_ERROR, getAuthHeader, getProxyPath, headersToObject } from '../utils'
5+
import { getAuthHeader, getProxyPath, headersToObject } from '../utils'
66
import { useNuxtApp, useRuntimeConfig } from '#imports'
77

88
export type KqlOptions = Pick<
@@ -20,12 +20,6 @@ export type KqlOptions = Pick<
2020
* Language code to fetch data for in multi-language Kirby setups.
2121
*/
2222
language?: string
23-
/**
24-
* Skip the Nuxt server proxy and fetch directly from the API.
25-
* Requires `client` to be enabled in the module options as well.
26-
* @default false
27-
*/
28-
client?: boolean
2923
/**
3024
* Cache the response between function calls for the same query.
3125
* @default true
@@ -39,13 +33,10 @@ export function $kql<T extends KirbyQueryResponse<any, boolean> = KirbyQueryResp
3933
): Promise<T> {
4034
const nuxt = useNuxtApp()
4135
const promiseMap = (nuxt._promiseMap = nuxt._promiseMap || new Map()) as Map<string, Promise<T>>
42-
const { headers, language, client = false, cache = true, ...fetchOptions } = opts
36+
const { headers, language, cache = true, ...fetchOptions } = opts
4337
const { kql } = useRuntimeConfig().public
4438
const key = `$kql${hash([query, language])}`
4539

46-
if (client && !kql.client)
47-
throw new Error(DEFAULT_CLIENT_ERROR)
48-
4940
if ((nuxt.isHydrating || cache) && key in nuxt.payload.data)
5041
return Promise.resolve(nuxt.payload.data[key])
5142

@@ -76,9 +67,9 @@ export function $kql<T extends KirbyQueryResponse<any, boolean> = KirbyQueryResp
7667
},
7768
}
7869

79-
const request = globalThis.$fetch(client ? kql.prefix : getProxyPath(key), {
70+
const request = globalThis.$fetch(kql.client ? kql.prefix : getProxyPath(key), {
8071
...fetchOptions,
81-
...(client ? _clientFetchOptions : _serverFetchOptions),
72+
...(kql.client ? _clientFetchOptions : _serverFetchOptions),
8273
})
8374
.then((response) => {
8475
if (process.server || cache)

src/runtime/composables/useKirbyData.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { WatchSource } from 'vue'
77
import type { AsyncData, AsyncDataOptions } from 'nuxt/app'
88
import { toValue } from '@vueuse/core'
99
import type { MaybeRefOrGetter } from '@vueuse/core'
10-
import { DEFAULT_CLIENT_ERROR, getAuthHeader, getProxyPath, headersToObject } from '../utils'
10+
import { getAuthHeader, getProxyPath, headersToObject } from '../utils'
1111
import { useAsyncData, useRuntimeConfig } from '#imports'
1212

1313
type UseKirbyDataOptions<T> = Omit<AsyncDataOptions<T>, 'watch'> & Pick<
@@ -28,12 +28,6 @@ type UseKirbyDataOptions<T> = Omit<AsyncDataOptions<T>, 'watch'> & Pick<
2828
* Language code to fetch data for in multi-language Kirby setups.
2929
*/
3030
language?: MaybeRefOrGetter<string>
31-
/**
32-
* Skip the Nuxt server proxy and fetch directly from the API.
33-
* Requires `client` to be enabled in the module options as well.
34-
* @default false
35-
*/
36-
client?: boolean
3731
/**
3832
* Cache the response between function calls for the same path.
3933
* @default true
@@ -64,7 +58,6 @@ export function useKirbyData<T = any>(
6458
method,
6559
body,
6660
language,
67-
client = false,
6861
cache = true,
6962
...fetchOptions
7063
} = opts
@@ -84,9 +77,6 @@ export function useKirbyData<T = any>(
8477
if (!_path.value || (_language.value && !_path.value.replace(new RegExp(`^${_language.value}/`), '')))
8578
console.warn('[useKirbyData] Empty Kirby path')
8679

87-
if (client && !kql.client)
88-
throw new Error(DEFAULT_CLIENT_ERROR)
89-
9080
const asyncDataOptions: AsyncDataOptions<T> = {
9181
server,
9282
lazy,
@@ -119,7 +109,7 @@ export function useKirbyData<T = any>(
119109
try {
120110
let result: T | undefined
121111

122-
if (client) {
112+
if (kql.client) {
123113
result = (await globalThis.$fetch<T>(_path.value, {
124114
...fetchOptions,
125115
signal: controller.signal,

0 commit comments

Comments
 (0)