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

feat: add result hooks #2446

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions docs/content/3.composables/1.query-content.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,40 @@ const count = await queryContent('articles')
// Returns
5 // number of articles
```

## `resultHook(hook)`

- `hook`
- Type: `(content: T) => HT`
- **Required**

Add a hook for to be executed on the results of the content query.

This hook is useful for simple transforms of returned content or validation of returned data with libraries such as `zod`.

```ts
// Some example front-matter on a content page
const articleSchema = z.object({
navigation: z.object({
nextBtn: z.string()
})
})

// Validate each returned article
const articles = await queryContent('articles')
.resultHook(c => articleSchema.parse(c))
.find()
```

You can also use `resultHook(hook)` to augment the returned content with new fields.

```ts
// Count of articles
const articles = await queryContent('articles')
.resultHook(c => {
...c,
newField: 'some-data'
})
.find()
```

27 changes: 24 additions & 3 deletions src/runtime/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@ export function createQuery <T = ParsedContent> (fetcher: ContentQueryFetcher<T>
}
}

const resultHooks: ((v: any) => any)[] = []

const resolveResultHooks = (result: any) => {
let res = result
for (const h of resultHooks) {
res = h(res)
}
return res
}

const resolveResult = (result: any) => {
let ret = result
if (opts.legacy) {
if (result?.surround) {
return result.surround
Expand All @@ -46,10 +57,18 @@ export function createQuery <T = ParsedContent> (fetcher: ContentQueryFetcher<T>
}
}

return result?._path || Array.isArray(result) || !Object.prototype.hasOwnProperty.call(result, 'result') ? result : result?.result
ret = result?._path || Array.isArray(result) || !Object.prototype.hasOwnProperty.call(result, 'result') ? result : result?.result
}

if (Array.isArray(ret)) {
return ret.map(r => resolveResultHooks(r))
}

if (ret?._path) {
return resolveResultHooks(ret)
}

return result
return ret
}

const query: any = {
Expand All @@ -71,7 +90,9 @@ export function createQuery <T = ParsedContent> (fetcher: ContentQueryFetcher<T>
// locale
locale: (_locale: string) => query.where({ _locale }),
withSurround: $set('surround', (surroundQuery, options) => ({ query: surroundQuery, ...options })),
withDirConfig: () => $set('dirConfig')(true)
withDirConfig: () => $set('dirConfig')(true),
// hooks
resultHook: <HT>(h: (v: any) => HT) => { resultHooks.push(h); return query as ContentQueryBuilder<HT> }
}

if (opts.legacy) {
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,11 @@ export interface QueryBuilder<T = ParsedContentMeta> {
*/
locale(locale: string): QueryBuilder<T>

/**
* Add result hook to returned content
*/
resultHook<HT>(hook: (content: T) => HT): QueryBuilder<HT>

/**
* Retrieve query builder params
* @internal
Expand Down
13 changes: 13 additions & 0 deletions test/features/query/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,17 @@ describe('Database Provider', () => {
expect(item._deleted).toBeUndefined()
})
})

test('Result Hook transform return', async () => {
const query = createQuery(pipelineFetcher, { legacy: true })
.where({ id: { $in: [1, 2] } })
.resultHook((c) => { return { transformedPath: c._path, exampleExtraField: 'hello' } })
const result = await query.find()

result.forEach((item: any) => {
expect(Object.keys(item)).toMatchObject(['transformedPath', 'exampleExtraField'])
assert(item.exampleExtraField === 'hello')
expect(item.id).toBeUndefined()
})
})
})
Loading