Skip to content

Commit 5549466

Browse files
committed
fix: represent QueryBuilderParams accurately with optional keys
1 parent ac9fa5b commit 5549466

File tree

8 files changed

+15
-13
lines changed

8 files changed

+15
-13
lines changed

src/runtime/composables/query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { withContentBase } from './utils'
99
/**
1010
* Query fetcher
1111
*/
12-
export const queryFetch = <T = ParsedContent>(params: Partial<QueryBuilderParams>) => {
12+
export const queryFetch = <T = ParsedContent>(params: QueryBuilderParams) => {
1313
const apiPath = withContentBase(process.dev ? '/query' : `/query/${hash(params)}`)
1414

1515
// Prefetch the query

src/runtime/query/match/pipeline.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { QueryBuilderParams, QueryPipe } from '../../types'
1+
import type { QueryBuilderParams, QueryBuilderSchema, QueryPipe } from '../../types'
22
import { apply, ensureArray, sortList, withoutKeys, withKeys } from './utils'
33
import { createMatch } from '.'
44

@@ -9,7 +9,7 @@ export function createPipelineFetcher<T> (getContentsList: () => Promise<T[]>) {
99
/**
1010
* Exctract surrounded items of specific condition
1111
*/
12-
const surround = (data: any[], { query, before, after }: QueryBuilderParams['surround']) => {
12+
const surround = (data: any[], { query, before, after }: QueryBuilderSchema['surround']) => {
1313
const matchQuery = typeof query === 'string' ? { _path: query } : query
1414
// Find matched item index
1515
const index = data.findIndex(item => match(item, matchQuery))

src/runtime/query/query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const arrayParams = ['sort', 'where', 'only', 'without']
55

66
export const createQuery = <T>(
77
fetcher: DatabaseFetcher<T>,
8-
queryParams?: Partial<QueryBuilderParams>
8+
queryParams?: QueryBuilderParams
99
): QueryBuilder<T> => {
1010
const params = {
1111
...queryParams

src/runtime/server/api/navigation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { defineEventHandler } from 'h3'
22
import { serverQueryContent } from '../storage'
33
import { createNav } from '../navigation'
4-
import { ParsedContentMeta, QueryBuilderParams } from '../../types'
4+
import { ParsedContentMeta } from '../../types'
55
import { getContentQuery } from '../../utils/query'
66

77
export default defineEventHandler(async (event) => {
8-
const query: Partial<QueryBuilderParams> = getContentQuery(event)
8+
const query = getContentQuery(event)
99

1010
const contents = await serverQueryContent(event, query)
1111
.where({

src/runtime/server/api/query.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { createError, defineEventHandler } from 'h3'
2-
import type { QueryBuilderParams } from '../../types'
32
import { serverQueryContent } from '../storage'
43
import { getContentQuery } from '../../utils/query'
54

65
export default defineEventHandler(async (event) => {
7-
const query: Partial<QueryBuilderParams> = getContentQuery(event)
6+
const query = getContentQuery(event)
87
const contents = await serverQueryContent(event, query).find()
98

109
// If no documents matchs and using findOne()

src/runtime/server/storage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ export const getContent = async (event: CompatibilityEvent, id: string): Promise
122122
* Query contents
123123
*/
124124
export function serverQueryContent<T = ParsedContent>(event: CompatibilityEvent): QueryBuilder<T>;
125-
export function serverQueryContent<T = ParsedContent>(event: CompatibilityEvent, params?: Partial<QueryBuilderParams>): QueryBuilder<T>;
125+
export function serverQueryContent<T = ParsedContent>(event: CompatibilityEvent, params?: QueryBuilderParams): QueryBuilder<T>;
126126
export function serverQueryContent<T = ParsedContent>(event: CompatibilityEvent, path?: string, ...pathParts: string[]): QueryBuilder<T>;
127-
export function serverQueryContent<T = ParsedContent> (event: CompatibilityEvent, path?: string | Partial<QueryBuilderParams>, ...pathParts: string[]) {
128-
let params = (path || {}) as Partial<QueryBuilderParams>
127+
export function serverQueryContent<T = ParsedContent> (event: CompatibilityEvent, path?: string | QueryBuilderParams, ...pathParts: string[]) {
128+
let params = (path || {}) as QueryBuilderParams
129129
if (typeof path === 'string') {
130130
path = withLeadingSlash(joinURL(path, ...pathParts))
131131
// escape regex special chars

src/runtime/types.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export interface SortFields {
153153

154154
export type SortOptions = SortParams | SortFields
155155

156-
export interface QueryBuilderParams {
156+
export interface QueryBuilderSchema {
157157
first: boolean
158158
skip: number
159159
limit: number
@@ -170,6 +170,8 @@ export interface QueryBuilderParams {
170170
[key: string]: any
171171
}
172172

173+
export type QueryBuilderParams = Partial<QueryBuilderSchema>
174+
173175
export interface QueryBuilder<T = ParsedContentMeta> {
174176
/**
175177
* Select a subset of fields

src/runtime/utils/query.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useQuery, CompatibilityEvent, createError } from 'h3'
2+
import { QueryBuilderParams } from '../types'
23
import { jsonParse } from './json'
34

45
const parseQueryParams = (body: string) => {
@@ -10,7 +11,7 @@ const parseQueryParams = (body: string) => {
1011
}
1112

1213
const memory = {}
13-
export const getContentQuery = (event: CompatibilityEvent) => {
14+
export const getContentQuery = (event: CompatibilityEvent): QueryBuilderParams => {
1415
const { qid } = event.context.params
1516
const query: any = useQuery(event) || {}
1617

0 commit comments

Comments
 (0)