Skip to content

Commit

Permalink
Merge pull request #99 from nicgirault/populate-context
Browse files Browse the repository at this point in the history
fix: compute additionalAttributes in getOne
  • Loading branch information
nicgirault authored Sep 21, 2023
2 parents de62308 + a6ba6de commit b2fc8c1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/getList/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const getFilter = async (
}


const computeAdditionalAttributes =
export const computeAdditionalAttributes =
<R>(additionalAttributes: GetListOptions<R>["additionalAttributes"], concurrency: number, req: Request) => {
const limit = pLimit(concurrency)

Expand Down
10 changes: 7 additions & 3 deletions src/getOne.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RequestHandler, Request, Response } from 'express'
import { Get } from './getList'
import { Get, GetListOptions, computeAdditionalAttributes } from './getList'

export const getOne = <R>(doGetList: Get<R>): RequestHandler => async (
export const getOne = <R>(doGetList: Get<R>, options?: Partial<GetListOptions<R>>): RequestHandler => async (
req,
res,
next
Expand All @@ -18,7 +18,11 @@ export const getOne = <R>(doGetList: Get<R>): RequestHandler => async (
if (rows.length === 0) {
return res.status(404).json({ error: 'Record not found' })
}
res.json(rows[0])
const populatedRows =
options?.additionalAttributes
? await computeAdditionalAttributes(options.additionalAttributes, options.additionalAttributesConcurrency ?? 1, req)(rows)
: rows
res.json(populatedRows[0])
} catch (error) {
next(error)
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const crud = <I extends string | number, R>(
options
)
)
router.get(`${path}/:id`, getOne(actions.get))
router.get(`${path}/:id`, getOne(actions.get, options))
}

if (actions.create) {
Expand Down

0 comments on commit b2fc8c1

Please sign in to comment.