Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): change QueryBuilderParams keys to partial #1203

Merged
merged 2 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -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
16 changes: 8 additions & 8 deletions src/runtime/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,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