From 5549466464e3c89b7a9a7a3a7c1512197d1491ec Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Fri, 3 Jun 2022 12:15:40 +1000 Subject: [PATCH 1/2] fix: represent `QueryBuilderParams` accurately with optional keys --- src/runtime/composables/query.ts | 2 +- src/runtime/query/match/pipeline.ts | 4 ++-- src/runtime/query/query.ts | 2 +- src/runtime/server/api/navigation.ts | 4 ++-- src/runtime/server/api/query.ts | 3 +-- src/runtime/server/storage.ts | 6 +++--- src/runtime/types.d.ts | 4 +++- src/runtime/utils/query.ts | 3 ++- 8 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/runtime/composables/query.ts b/src/runtime/composables/query.ts index 75a2a2009..dd05ac9b4 100644 --- a/src/runtime/composables/query.ts +++ b/src/runtime/composables/query.ts @@ -9,7 +9,7 @@ import { withContentBase } from './utils' /** * Query fetcher */ -export const queryFetch = (params: Partial) => { +export const queryFetch = (params: QueryBuilderParams) => { const apiPath = withContentBase(process.dev ? '/query' : `/query/${hash(params)}`) // Prefetch the query diff --git a/src/runtime/query/match/pipeline.ts b/src/runtime/query/match/pipeline.ts index acab570ba..454e8629e 100644 --- a/src/runtime/query/match/pipeline.ts +++ b/src/runtime/query/match/pipeline.ts @@ -1,4 +1,4 @@ -import type { QueryBuilderParams, QueryPipe } from '../../types' +import type { QueryBuilderParams, QueryBuilderSchema, QueryPipe } from '../../types' import { apply, ensureArray, sortList, withoutKeys, withKeys } from './utils' import { createMatch } from '.' @@ -9,7 +9,7 @@ export function createPipelineFetcher (getContentsList: () => Promise) { /** * Exctract surrounded items of specific condition */ - const surround = (data: any[], { query, before, after }: QueryBuilderParams['surround']) => { + const surround = (data: any[], { query, before, after }: QueryBuilderSchema['surround']) => { const matchQuery = typeof query === 'string' ? { _path: query } : query // Find matched item index const index = data.findIndex(item => match(item, matchQuery)) diff --git a/src/runtime/query/query.ts b/src/runtime/query/query.ts index 87e4d82fb..0288c25bc 100644 --- a/src/runtime/query/query.ts +++ b/src/runtime/query/query.ts @@ -5,7 +5,7 @@ const arrayParams = ['sort', 'where', 'only', 'without'] export const createQuery = ( fetcher: DatabaseFetcher, - queryParams?: Partial + queryParams?: QueryBuilderParams ): QueryBuilder => { const params = { ...queryParams diff --git a/src/runtime/server/api/navigation.ts b/src/runtime/server/api/navigation.ts index 4f11e3063..b3da1ad79 100644 --- a/src/runtime/server/api/navigation.ts +++ b/src/runtime/server/api/navigation.ts @@ -1,11 +1,11 @@ import { defineEventHandler } from 'h3' import { serverQueryContent } from '../storage' import { createNav } from '../navigation' -import { ParsedContentMeta, QueryBuilderParams } from '../../types' +import { ParsedContentMeta } from '../../types' import { getContentQuery } from '../../utils/query' export default defineEventHandler(async (event) => { - const query: Partial = getContentQuery(event) + const query = getContentQuery(event) const contents = await serverQueryContent(event, query) .where({ diff --git a/src/runtime/server/api/query.ts b/src/runtime/server/api/query.ts index 381eb3827..a8b9e33e5 100644 --- a/src/runtime/server/api/query.ts +++ b/src/runtime/server/api/query.ts @@ -1,10 +1,9 @@ import { createError, defineEventHandler } from 'h3' -import type { QueryBuilderParams } from '../../types' import { serverQueryContent } from '../storage' import { getContentQuery } from '../../utils/query' export default defineEventHandler(async (event) => { - const query: Partial = getContentQuery(event) + const query = getContentQuery(event) const contents = await serverQueryContent(event, query).find() // If no documents matchs and using findOne() diff --git a/src/runtime/server/storage.ts b/src/runtime/server/storage.ts index 22b138d58..e0880436c 100644 --- a/src/runtime/server/storage.ts +++ b/src/runtime/server/storage.ts @@ -122,10 +122,10 @@ export const getContent = async (event: CompatibilityEvent, id: string): Promise * Query contents */ export function serverQueryContent(event: CompatibilityEvent): QueryBuilder; -export function serverQueryContent(event: CompatibilityEvent, params?: Partial): QueryBuilder; +export function serverQueryContent(event: CompatibilityEvent, params?: QueryBuilderParams): QueryBuilder; export function serverQueryContent(event: CompatibilityEvent, path?: string, ...pathParts: string[]): QueryBuilder; -export function serverQueryContent (event: CompatibilityEvent, path?: string | Partial, ...pathParts: string[]) { - let params = (path || {}) as Partial +export function serverQueryContent (event: CompatibilityEvent, path?: string | QueryBuilderParams, ...pathParts: string[]) { + let params = (path || {}) as QueryBuilderParams if (typeof path === 'string') { path = withLeadingSlash(joinURL(path, ...pathParts)) // escape regex special chars diff --git a/src/runtime/types.d.ts b/src/runtime/types.d.ts index f0df92b64..0b01b510e 100644 --- a/src/runtime/types.d.ts +++ b/src/runtime/types.d.ts @@ -153,7 +153,7 @@ export interface SortFields { export type SortOptions = SortParams | SortFields -export interface QueryBuilderParams { +export interface QueryBuilderSchema { first: boolean skip: number limit: number @@ -170,6 +170,8 @@ export interface QueryBuilderParams { [key: string]: any } +export type QueryBuilderParams = Partial + export interface QueryBuilder { /** * Select a subset of fields diff --git a/src/runtime/utils/query.ts b/src/runtime/utils/query.ts index c51db59ff..7d0b5fd39 100644 --- a/src/runtime/utils/query.ts +++ b/src/runtime/utils/query.ts @@ -1,4 +1,5 @@ import { useQuery, CompatibilityEvent, createError } from 'h3' +import { QueryBuilderParams } from '../types' import { jsonParse } from './json' const parseQueryParams = (body: string) => { @@ -10,7 +11,7 @@ const parseQueryParams = (body: string) => { } const memory = {} -export const getContentQuery = (event: CompatibilityEvent) => { +export const getContentQuery = (event: CompatibilityEvent): QueryBuilderParams => { const { qid } = event.context.params const query: any = useQuery(event) || {} From 5c9f6ca2851ffa3ac0eb7874ced49df643b6ac90 Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Fri, 3 Jun 2022 13:06:58 +1000 Subject: [PATCH 2/2] fix: use optional keys instead of new type --- src/runtime/query/match/pipeline.ts | 4 ++-- src/runtime/types.d.ts | 20 +++++++++----------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/runtime/query/match/pipeline.ts b/src/runtime/query/match/pipeline.ts index 454e8629e..acab570ba 100644 --- a/src/runtime/query/match/pipeline.ts +++ b/src/runtime/query/match/pipeline.ts @@ -1,4 +1,4 @@ -import type { QueryBuilderParams, QueryBuilderSchema, QueryPipe } from '../../types' +import type { QueryBuilderParams, QueryPipe } from '../../types' import { apply, ensureArray, sortList, withoutKeys, withKeys } from './utils' import { createMatch } from '.' @@ -9,7 +9,7 @@ export function createPipelineFetcher (getContentsList: () => Promise) { /** * Exctract surrounded items of specific condition */ - const surround = (data: any[], { query, before, after }: QueryBuilderSchema['surround']) => { + const surround = (data: any[], { query, before, after }: QueryBuilderParams['surround']) => { const matchQuery = typeof query === 'string' ? { _path: query } : query // Find matched item index const index = data.findIndex(item => match(item, matchQuery)) diff --git a/src/runtime/types.d.ts b/src/runtime/types.d.ts index 0b01b510e..ffd39cfd3 100644 --- a/src/runtime/types.d.ts +++ b/src/runtime/types.d.ts @@ -153,15 +153,15 @@ export interface SortFields { export type SortOptions = SortParams | SortFields -export interface QueryBuilderSchema { - first: boolean - skip: number - limit: number - only: string[] - without: string[] - sort: SortOptions[] - where: object[] - surround: { +export interface QueryBuilderParams { + first?: boolean + skip?: number + limit?: number + only?: string[] + without?: string[] + sort?: SortOptions[] + where?: object[] + surround?: { query: string | object before?: number after?: number @@ -170,8 +170,6 @@ export interface QueryBuilderSchema { [key: string]: any } -export type QueryBuilderParams = Partial - export interface QueryBuilder { /** * Select a subset of fields