|
1 | | -import type { NitroFetchRequest } from 'nitropack' |
2 | | -import type { Ref } from 'vue' |
3 | | -import { computed, unref } from 'vue' |
4 | | -import type { FetchOptions } from 'ohmyfetch' |
5 | | -import type { KqlQueryRequest, KqlQueryResponse } from '../types' |
6 | | -import { getAuthHeaders, normalizeHeaders } from '../utils/headers' |
7 | | -import type { AsyncData, UseFetchOptions } from '#app' |
8 | | -import { useFetch, useRuntimeConfig } from '#app' |
9 | | - |
10 | | -export function useKql<ResT = KqlQueryResponse, ReqT = KqlQueryRequest>( |
11 | | - query: Ref<ReqT> | ReqT | (() => ReqT), |
12 | | - opts: Omit<UseFetchOptions<ResT>, 'baseURL' | 'method' | 'body' > = {}, |
13 | | -) { |
14 | | - const { public: { kql: { url, endpoint } } } = useRuntimeConfig() |
15 | | - |
16 | | - if (!endpoint) |
17 | | - throw new Error('KQL endpoint is not configured') |
18 | | - |
19 | | - const _query = computed(() => { |
20 | | - let q = query |
21 | | - if (typeof q === 'function') |
22 | | - q = (q as (() => ReqT))() |
23 | | - |
24 | | - return unref(q) |
25 | | - }) |
| 1 | +import { hash as ohash } from 'ohash' |
| 2 | +import type { KqlPrivateFetchOptions, KqlPublicFetchOptions, KqlQueryRequest, KqlQueryResponse } from '../types' |
| 3 | +import { assertKqlPublicConfig, getAuthHeaders, normalizeHeaders } from '../utils' |
| 4 | +import type { ModuleOptions } from '../../module' |
| 5 | +import { useRuntimeConfig } from '#app' |
26 | 6 |
|
27 | | - return useFetch<ResT, Error, NitroFetchRequest, ResT>(endpoint, { |
28 | | - ...opts, |
29 | | - baseURL: url, |
30 | | - method: 'POST', |
31 | | - body: _query.value, |
32 | | - headers: { ...normalizeHeaders(opts.headers), ...getAuthHeaders() }, |
33 | | - }) as AsyncData<ResT, true | Error> |
| 7 | +interface InternalState<T> { |
| 8 | + promiseMap: Map<string, Promise<T>> |
34 | 9 | } |
35 | 10 |
|
36 | 11 | export function $kql<T = KqlQueryResponse>( |
37 | | - query: T, |
38 | | - opts: Omit<FetchOptions, 'baseURL' | 'method' | 'body' > = {}, |
39 | | -) { |
40 | | - const { public: { kql: { url, endpoint } } } = useRuntimeConfig() |
| 12 | + query: KqlQueryRequest, |
| 13 | + options: KqlPrivateFetchOptions = {}, |
| 14 | +): Promise<T> { |
| 15 | + const { cache = true } = options |
| 16 | + const { public: { kql } } = useRuntimeConfig() |
| 17 | + |
| 18 | + const nuxt = useNuxtApp() |
| 19 | + const queries: Record<string, T> = nuxt.payload.kqlQueries = (nuxt.payload.kqlQueries || {}) |
| 20 | + |
| 21 | + const state = (nuxt.__kql__ || {}) as InternalState<T> |
| 22 | + state.promiseMap = state.promiseMap || new Map() |
| 23 | + |
| 24 | + const body = { data: query } |
| 25 | + |
| 26 | + if (!cache) { |
| 27 | + return $fetch<T>(kql.apiRoute, { |
| 28 | + method: 'POST', |
| 29 | + body, |
| 30 | + }) |
| 31 | + } |
| 32 | + |
| 33 | + const key = ohash(query) |
| 34 | + |
| 35 | + if (key in queries) |
| 36 | + return Promise.resolve(queries[key]) |
| 37 | + |
| 38 | + if (state.promiseMap.has(key)) |
| 39 | + return state.promiseMap.get(key) |
| 40 | + |
| 41 | + const request = $fetch<T>(kql.apiRoute, { method: 'POST', body }) |
| 42 | + .then((r) => { |
| 43 | + queries[key] = r |
| 44 | + state.promiseMap.delete(key) |
| 45 | + return r |
| 46 | + }) |
| 47 | + |
| 48 | + state.promiseMap.set(key, request) |
| 49 | + |
| 50 | + return request |
| 51 | +} |
41 | 52 |
|
42 | | - if (!endpoint) |
43 | | - throw new Error('KQL endpoint is not configured') |
| 53 | +export function $publicKql<T = KqlQueryResponse>( |
| 54 | + query: KqlQueryRequest, |
| 55 | + opts: KqlPublicFetchOptions = {}, |
| 56 | +): Promise<T> { |
| 57 | + const { public: { kql } } = useRuntimeConfig() |
| 58 | + assertKqlPublicConfig(kql as ModuleOptions) |
44 | 59 |
|
45 | | - return $fetch<T>(endpoint, { |
| 60 | + return $fetch<T>(kql.kirbyEndpoint, { |
46 | 61 | ...opts, |
47 | | - baseURL: url, |
| 62 | + baseURL: kql.kirbyUrl, |
48 | 63 | method: 'POST', |
49 | 64 | body: query, |
50 | | - headers: { ...normalizeHeaders(opts.headers), ...getAuthHeaders() }, |
| 65 | + headers: { ...normalizeHeaders(opts.headers), ...getAuthHeaders(kql as ModuleOptions) }, |
51 | 66 | }) |
52 | 67 | } |
0 commit comments