Skip to content

Commit

Permalink
fix(types): change QueryBuilderParams keys to partial (#1203)
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Jun 3, 2022
1 parent afb791b commit 32d5236
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 18 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
2 changes: 1 addition & 1 deletion src/runtime/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const arrayParams = ['sort', 'where', 'only', 'without']

export const createQuery = <T = ParsedContent>(
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
16 changes: 8 additions & 8 deletions src/runtime/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ export interface SortFields {
export type SortOptions = SortParams | SortFields

export interface QueryBuilderParams {
first: boolean
skip: number
limit: number
only: string[]
without: string[]
sort: SortOptions[]
where: object[]
surround: {
first?: boolean
skip?: number
limit?: number
only?: string[]
without?: string[]
sort?: SortOptions[]
where?: object[]
surround?: {
query: string | object
before?: number
after?: number
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 32d5236

Please sign in to comment.