Skip to content

Typed Query Results

For better TypeScript support, define custom response types for useKql to catch errors in your templates.

The KirbyQueryResponse<T = any, Pagination extends boolean = false> accepts a generic type parameter T for the query result.

ts
// Extend the default response type with the result we expect from the query response
await useKql<KirbyQueryResponse<{ title: string }>>({
  query: 'site',
  select: ['title'],
})

Example

Pass a custom KirbySite type to KirbyQueryResponse as its first parameter, and data carries that shape:

vue
<script setup lang="ts">
import type { KirbyQueryResponse } from 'kirby-types'

// Create an interface for the query result, respectively the data returned by the API
export interface KirbySite {
  title: string
  children: {
    id: string
    title: string
    isListed: boolean
  }[]
}

// `data` will be of type `KirbyQueryResponse<KirbySite>`
const { data } = await useKql<KirbyQueryResponse<KirbySite>>({
  query: 'site',
  select: {
    title: true,
    children: {
      query: 'site.children',
      select: {
        id: true,
        title: true,
        isListed: true
      }
    }
  }
})
</script>

<template>
  <div>
    <!-- The IDE will provide auto completion and error checking for nested keys -->
    <h1>{{ data?.result?.title }}</h1>
  </div>
</template>

Released under the MIT License.