Skip to content

Commit

Permalink
fix: represent QueryBuilderParams accurately with optional keys
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Jun 3, 2022
1 parent ac9fa5b commit 5549466
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/runtime/composables/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { withContentBase } from './utils'
/**
* Query fetcher
*/
export const queryFetch = <T = ParsedContent>(params: Partial<QueryBuilderParams>) => {
export const queryFetch = <T = ParsedContent>(params: QueryBuilderParams) => {
const apiPath = withContentBase(process.dev ? '/query' : `/query/${hash(params)}`)

// Prefetch the query
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/query/match/pipeline.ts
Original file line number Diff line number Diff line change
@@ -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 '.'

Expand All @@ -9,7 +9,7 @@ export function createPipelineFetcher<T> (getContentsList: () => Promise<T[]>) {
/**
* 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))
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const arrayParams = ['sort', 'where', 'only', 'without']

export const createQuery = <T>(
fetcher: DatabaseFetcher<T>,
queryParams?: Partial<QueryBuilderParams>
queryParams?: QueryBuilderParams
): QueryBuilder<T> => {
const params = {
...queryParams
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/server/api/navigation.ts
Original file line number Diff line number Diff line change
@@ -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<QueryBuilderParams> = getContentQuery(event)
const query = getContentQuery(event)

const contents = await serverQueryContent(event, query)
.where({
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/server/api/query.ts
Original file line number Diff line number Diff line change
@@ -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<QueryBuilderParams> = getContentQuery(event)
const query = getContentQuery(event)
const contents = await serverQueryContent(event, query).find()

// If no documents matchs and using findOne()
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/server/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ export const getContent = async (event: CompatibilityEvent, id: string): Promise
* Query contents
*/
export function serverQueryContent<T = ParsedContent>(event: CompatibilityEvent): QueryBuilder<T>;
export function serverQueryContent<T = ParsedContent>(event: CompatibilityEvent, params?: Partial<QueryBuilderParams>): QueryBuilder<T>;
export function serverQueryContent<T = ParsedContent>(event: CompatibilityEvent, params?: QueryBuilderParams): QueryBuilder<T>;
export function serverQueryContent<T = ParsedContent>(event: CompatibilityEvent, path?: string, ...pathParts: string[]): QueryBuilder<T>;
export function serverQueryContent<T = ParsedContent> (event: CompatibilityEvent, path?: string | Partial<QueryBuilderParams>, ...pathParts: string[]) {
let params = (path || {}) as Partial<QueryBuilderParams>
export function serverQueryContent<T = ParsedContent> (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
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export interface SortFields {

export type SortOptions = SortParams | SortFields

export interface QueryBuilderParams {
export interface QueryBuilderSchema {
first: boolean
skip: number
limit: number
Expand All @@ -170,6 +170,8 @@ export interface QueryBuilderParams {
[key: string]: any
}

export type QueryBuilderParams = Partial<QueryBuilderSchema>

export interface QueryBuilder<T = ParsedContentMeta> {
/**
* Select a subset of fields
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/utils/query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useQuery, CompatibilityEvent, createError } from 'h3'
import { QueryBuilderParams } from '../types'
import { jsonParse } from './json'

const parseQueryParams = (body: string) => {
Expand All @@ -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) || {}

Expand Down

0 comments on commit 5549466

Please sign in to comment.