Skip to content

Commit b2fc8c1

Browse files
authored
Merge pull request #99 from nicgirault/populate-context
fix: compute additionalAttributes in getOne
2 parents de62308 + a6ba6de commit b2fc8c1

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

src/getList/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const getFilter = async (
8080
}
8181

8282

83-
const computeAdditionalAttributes =
83+
export const computeAdditionalAttributes =
8484
<R>(additionalAttributes: GetListOptions<R>["additionalAttributes"], concurrency: number, req: Request) => {
8585
const limit = pLimit(concurrency)
8686

src/getOne.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { RequestHandler, Request, Response } from 'express'
2-
import { Get } from './getList'
2+
import { Get, GetListOptions, computeAdditionalAttributes } from './getList'
33

4-
export const getOne = <R>(doGetList: Get<R>): RequestHandler => async (
4+
export const getOne = <R>(doGetList: Get<R>, options?: Partial<GetListOptions<R>>): RequestHandler => async (
55
req,
66
res,
77
next
@@ -18,7 +18,11 @@ export const getOne = <R>(doGetList: Get<R>): RequestHandler => async (
1818
if (rows.length === 0) {
1919
return res.status(404).json({ error: 'Record not found' })
2020
}
21-
res.json(rows[0])
21+
const populatedRows =
22+
options?.additionalAttributes
23+
? await computeAdditionalAttributes(options.additionalAttributes, options.additionalAttributesConcurrency ?? 1, req)(rows)
24+
: rows
25+
res.json(populatedRows[0])
2226
} catch (error) {
2327
next(error)
2428
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const crud = <I extends string | number, R>(
3333
options
3434
)
3535
)
36-
router.get(`${path}/:id`, getOne(actions.get))
36+
router.get(`${path}/:id`, getOne(actions.get, options))
3737
}
3838

3939
if (actions.create) {

0 commit comments

Comments
 (0)