Skip to content

Commit b3738dc

Browse files
authored
refactor: improve typings and simplify logic (#1669)
1 parent 3b5a80e commit b3738dc

File tree

8 files changed

+182
-83
lines changed

8 files changed

+182
-83
lines changed

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,23 @@
4242
"test:unit": "nuxi prepare test/fixtures/basic && nuxi prepare test/fixtures/document-driven && vitest run"
4343
},
4444
"dependencies": {
45-
"@nuxt/kit": "^3.0.0-rc.12",
45+
"@nuxt/kit": "^3.0.0-rc.13",
4646
"consola": "^2.15.3",
4747
"defu": "^6.1.0",
4848
"destr": "^1.2.0",
4949
"detab": "^3.0.1",
5050
"html-tags": "^3.2.0",
5151
"json5": "^2.2.1",
5252
"knitwork": "^0.1.2",
53-
"listhen": "^0.3.4",
53+
"listhen": "^0.3.5",
5454
"mdast-util-to-hast": "^12.2.4",
5555
"mdurl": "^1.0.1",
5656
"ohash": "^0.1.5",
5757
"pathe": "^0.3.9",
5858
"property-information": "^6.1.1",
5959
"rehype-external-links": "^2.0.1",
6060
"rehype-raw": "^6.1.1",
61-
"rehype-slug": "^5.0.1",
61+
"rehype-slug": "^5.1.0",
6262
"rehype-sort-attribute-values": "^4.0.0",
6363
"rehype-sort-attributes": "^4.0.0",
6464
"remark-emoji": "^3.0.2",
@@ -77,26 +77,26 @@
7777
"unist-util-position": "^4.0.3",
7878
"unist-util-visit": "^4.1.1",
7979
"unstorage": "^0.6.0",
80-
"ws": "^8.10.0"
80+
"ws": "^8.11.0"
8181
},
8282
"devDependencies": {
8383
"@nuxt/module-builder": "^0.2.0",
84-
"@nuxt/schema": "^3.0.0-rc.12",
85-
"@nuxt/test-utils": "^3.0.0-rc.12",
84+
"@nuxt/schema": "^3.0.0-rc.13",
85+
"@nuxt/test-utils": "^3.0.0-rc.13",
8686
"@nuxthq/admin": "npm:@nuxthq/admin-edge@latest",
8787
"@nuxtjs/eslint-config-typescript": "latest",
8888
"@types/ws": "^8.5.3",
8989
"c8": "^7.12.0",
9090
"csvtojson": "^2.0.10",
91-
"eslint": "^8.26.0",
91+
"eslint": "^8.27.0",
9292
"globby": "^13.1.2",
93-
"husky": "^8.0.1",
93+
"husky": "^8.0.2",
9494
"jiti": "^1.16.0",
9595
"lint-staged": "^13.0.3",
9696
"nuxt": "npm:nuxt3@latest",
9797
"rehype-figure": "^1.0.1",
9898
"remark-oembed": "^1.2.2",
99-
"vitest": "^0.24.3",
99+
"vitest": "^0.25.1",
100100
"vue-docgen-web-types": "^0.1.8"
101101
}
102102
}

src/runtime/query/match/pipeline.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,20 @@ export function createPipelineFetcher<T> (getContentsList: () => Promise<T[]>) {
3535
// Remove unwanted fields
3636
(data, params) => apply(withoutKeys(params.without))(data),
3737
// Select only wanted fields
38-
(data, params) => apply(withKeys(params.only))(data),
39-
// Evaluate result
40-
(data, params) => params.first ? data[0] : data
38+
(data, params) => apply(withKeys(params.only))(data)
4139
]
4240

4341
return async (query: QueryBuilder<T>): Promise<T | T[]> => {
4442
const data = await getContentsList()
43+
const params = query.params()
4544

46-
return pipelines.reduce(($data: Array<T>, pipe: any) => pipe($data, query.params()) || $data, data)
45+
const filteredData = pipelines.reduce(($data: Array<T>, pipe: QueryPipe) => pipe($data, params) || $data, data)
46+
47+
// return first item if query is for single item
48+
if (params.first) {
49+
return filteredData[0]
50+
}
51+
52+
return filteredData
4753
}
4854
}

src/runtime/server/api/query.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,39 @@ import { getContentQuery } from '../../utils/query'
55

66
export default defineEventHandler(async (event) => {
77
const query = getContentQuery(event)
8-
const contents = await serverQueryContent(event, query).find()
98

109
if (query.first) {
11-
const path = contents?._path || query.where.find(w => w._path)?._path
10+
const content = await serverQueryContent(event, query).findOne()
11+
12+
// Try to find `_dir` file before throwing 404
13+
const path = content?._path || query.where?.find(w => w._path)?._path as string
1214
if (path) {
1315
const _dir = await serverQueryContent(event).where({ _path: join(path, '_dir') }).without('_').findOne()
1416
if (!Array.isArray(_dir)) {
1517
return {
1618
_path: path,
17-
...contents,
19+
...(content || {}),
1820
_dir
1921
}
2022
}
2123
}
22-
}
2324

24-
// If no documents matchs and using findOne()
25-
if (query.first && Array.isArray(contents) && contents.length === 0) {
26-
throw createError({
27-
statusMessage: 'Document not found!',
28-
statusCode: 404,
29-
data: {
30-
description: 'Could not find document for the given query.',
31-
query
32-
}
33-
})
25+
// If no documents matchs and using findOne()
26+
if (!content) {
27+
throw createError({
28+
statusMessage: 'Document not found!',
29+
statusCode: 404,
30+
data: {
31+
description: 'Could not find document for the given query.',
32+
query
33+
}
34+
})
35+
}
36+
37+
return content
3438
}
3539

40+
const contents = await serverQueryContent(event, query).find()
41+
3642
return contents
3743
})

src/runtime/server/content-index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type { CompatibilityEvent } from 'h3'
1+
import type { H3Event } from 'h3'
22
import type { ParsedContent, QueryBuilder } from '../types'
33
import { isPreview } from './preview'
44
import { cacheStorage, getContent, getContentsList, serverQueryContent } from './storage'
55

6-
export async function getContentIndex (event: CompatibilityEvent) {
6+
export async function getContentIndex (event: H3Event) {
77
let contentIndex = await cacheStorage.getItem('content-index.json') as Record<string, string>
88
if (!contentIndex) {
99
// Fetch all content
@@ -24,7 +24,7 @@ export async function getContentIndex (event: CompatibilityEvent) {
2424
return contentIndex
2525
}
2626

27-
export async function getIndexedContentsList<T = ParsedContent> (event: CompatibilityEvent, query: QueryBuilder<T>): Promise<T[]> {
27+
export async function getIndexedContentsList<T = ParsedContent> (event: H3Event, query: QueryBuilder<T>): Promise<T[]> {
2828
const params = query.params()
2929
const path = params?.where?.find(wh => wh._path)?._path
3030

src/runtime/server/preview.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { CompatibilityEvent } from 'h3'
1+
import type { H3Event } from 'h3'
22
import { getQuery, getCookie } from 'h3'
33

4-
export const isPreview = (event: CompatibilityEvent) => {
4+
export const isPreview = (event: H3Event) => {
55
const previewToken = getQuery(event).previewToken || getCookie(event, 'previewToken')
66
return !!previewToken
77
}
88

9-
export const getPreview = (event: CompatibilityEvent) => {
9+
export const getPreview = (event: H3Event) => {
1010
const key = getQuery(event).previewToken as string || getCookie(event, 'previewToken')
1111

1212
return { key }

src/runtime/server/storage.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { prefixStorage } from 'unstorage'
22
import { joinURL, withLeadingSlash, withoutTrailingSlash } from 'ufo'
33
import { hash as ohash } from 'ohash'
4-
import type { CompatibilityEvent } from 'h3'
4+
import type { H3Event } from 'h3'
55
// eslint-disable-next-line import/no-named-as-default
66
import defu from 'defu'
77
import type { QueryBuilderParams, ParsedContent, QueryBuilder, ContentTransformer } from '../types'
@@ -67,7 +67,7 @@ const contentIgnorePredicate = (key: string) => {
6767
return true
6868
}
6969

70-
export const getContentsIds = async (event: CompatibilityEvent, prefix?: string) => {
70+
export const getContentsIds = async (event: H3Event, prefix?: string) => {
7171
let keys = []
7272

7373
if (isProduction) {
@@ -103,14 +103,14 @@ export const getContentsIds = async (event: CompatibilityEvent, prefix?: string)
103103
return keys.filter(contentIgnorePredicate)
104104
}
105105

106-
export const getContentsList = async (event: CompatibilityEvent, prefix?: string) => {
106+
export const getContentsList = async (event: H3Event, prefix?: string) => {
107107
const keys = await getContentsIds(event, prefix)
108108
const contents = await Promise.all(keys.map(key => getContent(event, key)))
109109

110110
return contents
111111
}
112112

113-
export const getContent = async (event: CompatibilityEvent, id: string): Promise<ParsedContent> => {
113+
export const getContent = async (event: H3Event, id: string): Promise<ParsedContent> => {
114114
const contentId = id
115115
// Handle ignored id
116116
if (!contentIgnorePredicate(id)) {
@@ -187,7 +187,7 @@ export async function parseContent (id: string, content: string, opts: ParseCont
187187
return result
188188
}
189189

190-
export const createServerQueryFetch = <T = ParsedContent>(event: CompatibilityEvent, path?: string) => (query: QueryBuilder<T>) => {
190+
export const createServerQueryFetch = <T = ParsedContent>(event: H3Event, path?: string) => (query: QueryBuilder<T>) => {
191191
if (path) {
192192
if (query.params().first) {
193193
query.where({ _path: withoutTrailingSlash(path) })
@@ -207,10 +207,10 @@ export const createServerQueryFetch = <T = ParsedContent>(event: CompatibilityEv
207207
/**
208208
* Query contents
209209
*/
210-
export function serverQueryContent<T = ParsedContent>(event: CompatibilityEvent): QueryBuilder<T>;
211-
export function serverQueryContent<T = ParsedContent>(event: CompatibilityEvent, params?: QueryBuilderParams): QueryBuilder<T>;
212-
export function serverQueryContent<T = ParsedContent>(event: CompatibilityEvent, path?: string, ...pathParts: string[]): QueryBuilder<T>;
213-
export function serverQueryContent<T = ParsedContent> (event: CompatibilityEvent, path?: string | QueryBuilderParams, ...pathParts: string[]) {
210+
export function serverQueryContent<T = ParsedContent>(event: H3Event): QueryBuilder<T>;
211+
export function serverQueryContent<T = ParsedContent>(event: H3Event, params?: QueryBuilderParams): QueryBuilder<T>;
212+
export function serverQueryContent<T = ParsedContent>(event: H3Event, path?: string, ...pathParts: string[]): QueryBuilder<T>;
213+
export function serverQueryContent<T = ParsedContent> (event: H3Event, path?: string | QueryBuilderParams, ...pathParts: string[]) {
214214
if (typeof path === 'string') {
215215
path = withLeadingSlash(joinURL(path, ...pathParts))
216216
return createQuery<T>(createServerQueryFetch(event, path))

src/runtime/utils/query.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getQuery, CompatibilityEvent, createError } from 'h3'
1+
import { getQuery, H3Event, createError } from 'h3'
22
import { QueryBuilderParams } from '../types'
33
import { jsonParse } from './json'
44

@@ -11,7 +11,7 @@ const parseQueryParams = (body: string) => {
1111
}
1212

1313
const memory = {}
14-
export const getContentQuery = (event: CompatibilityEvent): QueryBuilderParams => {
14+
export const getContentQuery = (event: H3Event): QueryBuilderParams => {
1515
const qid = event.context.params.qid?.replace(/.json$/, '')
1616
const query: any = getQuery(event) || {}
1717

@@ -49,11 +49,6 @@ export const getContentQuery = (event: CompatibilityEvent): QueryBuilderParams =
4949
delete query[key]
5050
}
5151
}
52-
if (Object.keys(where).length > 0) {
53-
query.where = [where]
54-
} else {
55-
delete query.where
56-
}
5752

5853
// ?sortyBy=size:1
5954
if (query.sort) {
@@ -70,5 +65,11 @@ export const getContentQuery = (event: CompatibilityEvent): QueryBuilderParams =
7065
query.where[key] = query[key]
7166
}
7267

68+
if (Object.keys(where).length > 0) {
69+
query.where = [where]
70+
} else {
71+
delete query.where
72+
}
73+
7374
return query
7475
}

0 commit comments

Comments
 (0)