Skip to content

Commit

Permalink
perf: drop #build/nuxt-kql/options from build
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Sep 23, 2022
1 parent f16e6ce commit 2b0c490
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 42 deletions.
35 changes: 6 additions & 29 deletions src/module.ts
@@ -1,9 +1,9 @@
import { defu } from 'defu'
import { $fetch } from 'ohmyfetch'
import { pascalCase } from 'scule'
import { addServerHandler, addTemplate, createResolver, defineNuxtModule, useLogger } from '@nuxt/kit'
import { addImportsDir, addServerHandler, addTemplate, createResolver, defineNuxtModule, useLogger } from '@nuxt/kit'
import type { KirbyQueryRequest, KirbyQueryResponse } from 'kirby-fest'
import { buildAuthHeader } from './runtime/utils'
import { buildAuthHeader, kirbyApiRoute, kqlApiRoute } from './runtime/utils'

export interface ModuleOptions {
/**
Expand Down Expand Up @@ -80,7 +80,6 @@ export default defineNuxtModule<ModuleOptions>({
},
async setup(options, nuxt) {
const logger = useLogger()
const apiRoute = '/api/__kql__' as const
const prefetchResults: Record<string, KirbyQueryResponse> = {}

function kql(query: KirbyQueryRequest) {
Expand Down Expand Up @@ -139,39 +138,17 @@ export default defineNuxtModule<ModuleOptions>({
config.externals.inline.push(resolve('runtime'))
})

// Add KQL proxy endpoint to send queries on server-side
// Add KQL proxy endpoint to send queries server-side
addServerHandler({
route: apiRoute,
route: kqlApiRoute,
method: 'post',
handler: resolve('runtime/server/api/kql'),
})

// Add KQL composables
nuxt.hook('imports:dirs', (dirs) => {
dirs.push(resolve('runtime/composables'))
})

// Add module options
addTemplate({
filename: 'nuxt-kql/options.mjs',
getContents() {
return `
export const apiRoute = '${apiRoute}'
`.trimStart()
},
})

// Add module options types
addTemplate({
filename: 'nuxt-kql/options.d.ts',
getContents() {
return `
export declare const apiRoute = '${apiRoute}'
`.trimStart()
},
})
addImportsDir(resolve('runtime/composables'))

// Copy global KQL type helpers to Nuxt types dir
// Provide global KQL type helpers
addTemplate({
filename: 'types/nuxt-kql.d.ts',
getContents: async () => `
Expand Down
7 changes: 3 additions & 4 deletions src/runtime/composables/$kql.ts
@@ -1,8 +1,7 @@
import { hash } from 'ohash'
import type { KirbyQueryRequest, KirbyQueryResponse } from 'kirby-fest'
import { headersToObject } from '../utils'
import { headersToObject, kqlApiRoute } from '../utils'
import { useNuxtApp } from '#imports'
import { apiRoute } from '#build/nuxt-kql/options'

export interface KqlOptions {
/**
Expand Down Expand Up @@ -34,7 +33,7 @@ export function $kql<T extends KirbyQueryResponse = KirbyQueryResponse>(
}

if (opts.cache === false) {
return $fetch<T>(apiRoute, {
return $fetch<T>(kqlApiRoute, {
method: 'POST',
body,
})
Expand All @@ -50,7 +49,7 @@ export function $kql<T extends KirbyQueryResponse = KirbyQueryResponse>(
if (key in nuxt._kqlPromises)
return nuxt._kqlPromises[key]

const request = $fetch(apiRoute, { method: 'POST', body })
const request = $fetch(kqlApiRoute, { method: 'POST', body })
.then((response) => {
nuxt.payload.data![key] = response
delete nuxt._kqlPromises[key]
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/composables/useKql.ts
Expand Up @@ -5,9 +5,8 @@ import type { NitroFetchRequest } from 'nitropack'
import type { AsyncData, UseFetchOptions } from 'nuxt/app'
import type { KirbyQueryRequest, KirbyQueryResponse } from 'kirby-fest'
import type { MaybeComputedRef } from '../utils'
import { headersToObject, resolveUnref } from '../utils'
import { headersToObject, kqlApiRoute, resolveUnref } from '../utils'
import { useFetch } from '#imports'
import { apiRoute } from '#build/nuxt-kql/options'

export type UseKqlOptions<T> = Omit<
UseFetchOptions<T>,
Expand Down Expand Up @@ -35,7 +34,7 @@ export function useKql<
if (Object.keys(_query.value).length === 0 || !_query.value.query)
console.error('[useKql] Empty KQL query')

return useFetch<ResT, FetchError, NitroFetchRequest, ResT>(apiRoute, {
return useFetch<ResT, FetchError, NitroFetchRequest, ResT>(kqlApiRoute, {
...opts,
key: hash(_query.value),
method: 'POST',
Expand Down
1 change: 0 additions & 1 deletion src/runtime/server/api/kql.ts
Expand Up @@ -7,7 +7,6 @@ import { useRuntimeConfig } from '#imports'

export default defineEventHandler(async (event): Promise<KirbyQueryResponse> => {
const body = await readBody(event)

const query: Partial<KirbyQueryRequest> = body.query || {}
const headers: Record<string, string> = body.headers || {}

Expand Down
9 changes: 4 additions & 5 deletions src/runtime/utils.ts
Expand Up @@ -2,6 +2,9 @@ import { unref } from 'vue'
import type { ComputedRef, Ref } from 'vue'
import type { ModuleOptions } from '../module'

export const kqlApiRoute = '/api/__kql__' as const
export const kirbyApiRoute = '/api/__kirby__' as const

/**
* Maybe it's a ref, or a plain value, or a getter function
*/
Expand Down Expand Up @@ -37,11 +40,7 @@ export function buildAuthHeader({
auth,
token,
credentials,
}: {
auth: ModuleOptions['auth']
token: ModuleOptions['token']
credentials: ModuleOptions['credentials']
}) {
}: Pick<ModuleOptions, 'auth' | 'token' | 'credentials'>) {
const headers: Record<string, string> = {}

if (auth === 'basic' && credentials) {
Expand Down

0 comments on commit 2b0c490

Please sign in to comment.