Skip to content

Commit

Permalink
feat: import types from kirby-fest package
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Sep 16, 2022
1 parent 7958c34 commit d30ee1c
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 101 deletions.
25 changes: 18 additions & 7 deletions docs/api/types-query-request.md
Expand Up @@ -8,16 +8,27 @@ import type { KirbyQueryRequest } from '#nuxt-kql'

## Types

::: info
Types are re-exported by the [`kirby-fest`](https://github.com/johannschopplich/kirby-fest) package.
:::

```ts
type KirbyQueryModel = 'collection' | 'file' | 'kirby' | 'page' | 'site' | 'user'
type KirbyQuery = KirbyQueryModel | `${KirbyQueryModel}.${string}` | `${KirbyQueryModel}(${string})`
type KirbyQueryModel<T extends string = never> =
| 'collection'
| 'file'
| 'kirby'
| 'page'
| 'site'
| 'user'
| T

type KirbyQuery<T extends string = never> =
| KirbyQueryModel<T>
| `${KirbyQueryModel<T>}.${string}`
| `${KirbyQueryModel<T>}(${string})`

interface KirbyQueryRequest {
/**
* @example
* kirby.page("about")
*/
query: `${'kirby' | 'site' | 'page'}${string}`
query: KirbyQuery
select?: Record<string, any> | string[]
pagination?: {
/** @default 100 */
Expand Down
9 changes: 8 additions & 1 deletion docs/api/types-query-response.md
Expand Up @@ -8,8 +8,15 @@ import type { KirbyQueryResponse } from '#nuxt-kql'

## Types

::: info
Types are re-exported by the [`kirby-fest`](https://github.com/johannschopplich/kirby-fest) package.
:::

```ts
interface KirbyQueryResponse<T = any, Pagination extends boolean = false> {
interface KirbyQueryResponse<
T = any,
Pagination extends boolean = false,
> {
code: number
status: string
result?: Pagination extends true
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -51,6 +51,7 @@
"dependencies": {
"@nuxt/kit": "^3.0.0-rc.10",
"defu": "^6.1.0",
"kirby-fest": "^0.2.0",
"ohash": "^0.1.5",
"ohmyfetch": "^0.4.18",
"scule": "^0.3.2"
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions src/module.ts
@@ -1,10 +1,9 @@
import { readFile } from 'fs/promises'
import { defu } from 'defu'
import { $fetch } from 'ohmyfetch'
import { pascalCase } from 'scule'
import { addServerHandler, addTemplate, createResolver, defineNuxtModule, useLogger } from '@nuxt/kit'
import type { KirbyQueryRequest, KirbyQueryResponse } from 'kirby-fest'
import { buildAuthHeader } from './runtime/utils'
import type { KirbyQueryRequest, KirbyQueryResponse } from './types'

export interface ModuleOptions {
/**
Expand Down Expand Up @@ -162,16 +161,25 @@ export declare const apiRoute = '${apiRoute}'
},
})

const kqlTypes = [
'KirbyQueryModel',
'KirbyQuery',
'KirbyQueryRequest',
'KirbyQueryResponse',
'KirbyBlockType',
'KirbyBlock',
'KirbyLayoutColumn',
'KirbyLayout',
].join(', ')

// Copy global KQL type helpers to Nuxt types dir
addTemplate({
filename: 'types/nuxt-kql.d.ts',
getContents: async () => `
declare module '#nuxt-kql' {
${(await readFile(resolve('types.ts'), 'utf-8'))
.replace(/^export\s+/gm, '')
.split('\n')
.map(i => ` ${i}`)
.join('\n')}
// TODO: Update docs to import types from \`kirby-fest\` instead
import { ${kqlTypes} } from 'kirby-fest'
export { ${kqlTypes} }
}
`.trimStart(),
})
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/composables/$kql.ts
@@ -1,8 +1,8 @@
import { hash } from 'ohash'
import type { KirbyQueryRequest, KirbyQueryResponse } from 'kirby-fest'
import { headersToObject } from '../utils'
import { useNuxtApp } from '#imports'
import { apiRoute } from '#build/nuxt-kql/options'
import type { KirbyQueryRequest, KirbyQueryResponse } from '#nuxt-kql'

export interface KqlOptions {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/composables/$publicKql.ts
@@ -1,8 +1,8 @@
import type { FetchOptions } from 'ohmyfetch'
import type { KirbyQueryRequest, KirbyQueryResponse } from 'kirby-fest'
import type { ModuleOptions } from '../../module'
import { buildAuthHeader, headersToObject } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { KirbyQueryRequest, KirbyQueryResponse } from '#nuxt-kql'

export type PublicKqlOptions = Omit<
FetchOptions,
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/composables/useKql.ts
Expand Up @@ -3,11 +3,11 @@ import { hash } from 'ohash'
import type { FetchError } from 'ohmyfetch'
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 { useFetch } from '#imports'
import { apiRoute } from '#build/nuxt-kql/options'
import type { KirbyQueryRequest, KirbyQueryResponse } from '#nuxt-kql'

export type UseKqlOptions<T> = Omit<
UseFetchOptions<T>,
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/composables/usePublicKql.ts
Expand Up @@ -3,11 +3,11 @@ import { hash } from 'ohash'
import type { FetchError } from 'ohmyfetch'
import type { NitroFetchRequest } from 'nitropack'
import type { AsyncData, UseFetchOptions } from 'nuxt/app'
import type { KirbyQueryRequest, KirbyQueryResponse } from 'kirby-fest'
import type { ModuleOptions } from '../../module'
import type { MaybeComputedRef } from '../utils'
import { buildAuthHeader, headersToObject, resolveUnref } from '../utils'
import { useFetch, useRuntimeConfig } from '#imports'
import type { KirbyQueryRequest, KirbyQueryResponse } from '#nuxt-kql'

export type UseKqlOptions<T> = Omit<
UseFetchOptions<T>,
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/server/api/kql.ts
@@ -1,9 +1,9 @@
import { createError, defineEventHandler, readBody } from 'h3'
import type { FetchError } from 'ohmyfetch'
import type { KirbyQueryRequest, KirbyQueryResponse } from 'kirby-fest'
import type { ModuleOptions } from '../../../module'
import { buildAuthHeader } from '../../utils'
import { useRuntimeConfig } from '#imports'
import type { KirbyQueryRequest, KirbyQueryResponse } from '#nuxt-kql'

export default defineEventHandler(async (event): Promise<KirbyQueryResponse> => {
const body = await readBody(event)
Expand Down
81 changes: 0 additions & 81 deletions src/types.ts

This file was deleted.

0 comments on commit d30ee1c

Please sign in to comment.