Skip to content

Commit d7dc7f7

Browse files
committed
fix: type errors
1 parent 7e5cf92 commit d7dc7f7

File tree

11 files changed

+171
-112
lines changed

11 files changed

+171
-112
lines changed

package-lock.json

Lines changed: 79 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"@feathersjs/memory": "^5.0.33",
6666
"@tsconfig/node22": "^22.0.0",
6767
"@types/lodash": "^4.17.16",
68+
"@typescript-eslint/parser": "^8.25.0",
6869
"eslint": "^9.21.0",
6970
"np": "^10.2.0",
7071
"shx": "^0.3.4",

src/app/graph-populate.class.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class GraphPopulateApplication {
1414
private _app: Application
1515

1616
__hooks: any
17+
// @ts-expect-error setup in 'enableHooks'
1718
hooks: (hooks: GraphPopulateHook | AnyData | unknown[]) => void
1819
options?: InitOptions
1920

src/hooks/graph-populate.hook.ts

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
import _get from 'lodash/get.js'
21
import _isEmpty from 'lodash/isEmpty.js'
3-
import _merge from 'lodash/merge.js'
42

53
import { shallowPopulate as makeShallowPopulate } from './shallow-populate.hook'
64

7-
import type { HookContext, Query } from '@feathersjs/feathers'
5+
import type { HookContext, Params, Query } from '@feathersjs/feathers'
86

9-
import type { GraphPopulateHookOptions, Method } from '../types'
7+
import type {
8+
Method,
9+
PopulateObject,
10+
Populates,
11+
SingleGraphPopulateParams,
12+
} from '../types'
1013
import type { GraphPopulateApplication } from '../app/graph-populate.class'
1114

1215
const FILTERS = ['$limit', '$select', '$skip', '$sort']
1316

17+
export interface GraphPopulateHookOptions<S = string> {
18+
populates: Populates<S>
19+
/**
20+
* @default: false
21+
*/
22+
allowUnnamedQueryForExternal?: boolean
23+
}
24+
1425
/**
1526
* Sets up the deepPopulate hook using the provided options.
1627
* @param options
@@ -30,13 +41,9 @@ export function graphPopulate(
3041
}
3142
const { populates } = options
3243

33-
return async function deepPopulateHook(
34-
context: HookContext,
35-
): Promise<HookContext> {
36-
const populateQuery: Query | undefined = _get(
37-
context,
38-
'params.$populateParams.query',
39-
)
44+
return async (context: HookContext): Promise<HookContext> => {
45+
const populateQuery: Query | undefined =
46+
context.params?.$populateParams?.query
4047

4148
if (!populateQuery) return context
4249

@@ -50,12 +57,12 @@ export function graphPopulate(
5057
const currentPopulates = keys.reduce((currentPopulates, key) => {
5158
if (!populates[key]) return currentPopulates
5259

53-
const currentQuery = Object.assign({}, populateQuery[key])
60+
const currentQuery = { ...populateQuery[key] }
5461

5562
const populate = populates[key]
5663
const service = app.service(populate.service)
5764

58-
let params = []
65+
let params: SingleGraphPopulateParams[] = []
5966
if (populate.params) {
6067
if (Array.isArray(populate.params)) {
6168
params.push(...populate.params)
@@ -65,23 +72,24 @@ export function graphPopulate(
6572
}
6673

6774
if (!_isEmpty(currentQuery)) {
68-
const customKeysForQuery: string[] | undefined = _get(
69-
service,
70-
'options.graphPopulate.whitelist',
71-
)
75+
const customKeysForQuery = (service as any).options?.graphPopulate
76+
?.whitelist as string[] | undefined
7277
const extractKeys = [...FILTERS]
78+
7379
if (customKeysForQuery) {
7480
extractKeys.push(...customKeysForQuery)
7581
}
82+
7683
const paramsToAdd = Object.keys(currentQuery).reduce(
7784
(paramsToAdd, key) => {
7885
if (!extractKeys.includes(key)) return paramsToAdd
7986
const { query } = paramsToAdd
80-
_merge(query, { [key]: currentQuery[key] })
87+
query[key] = currentQuery[key]
8188
delete currentQuery[key]
89+
8290
return paramsToAdd
8391
},
84-
{ query: {} },
92+
{ query: {} } as { query: Query },
8593
)
8694
params.push(paramsToAdd)
8795
}
@@ -91,7 +99,7 @@ export function graphPopulate(
9199
$populateParams: {
92100
query: currentQuery,
93101
},
94-
})
102+
} as Params)
95103
}
96104

97105
if (graphPopulateApp) {
@@ -108,9 +116,9 @@ export function graphPopulate(
108116
})
109117

110118
return currentPopulates
111-
}, [])
119+
}, [] as PopulateObject[])
112120

113-
if (!currentPopulates || !currentPopulates.length) {
121+
if (!currentPopulates?.length) {
114122
return context
115123
}
116124
const shallowPopulate = makeShallowPopulate({ include: currentPopulates })

src/hooks/populate.hook.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
import _set from 'lodash/set.js'
2-
31
import { graphPopulate } from './graph-populate.hook'
4-
import { getQuery } from '../utils/get-query'
2+
import { GetPopulateQueryOptions, getQuery } from '../utils/get-query'
53

64
import type { HookContext } from '@feathersjs/feathers'
75

8-
import type { PopulateHookOptions, GraphPopulateHookOptions } from '../types'
6+
import type { Populates } from '../types'
7+
8+
export type PopulateHookOptions<S = string> = Pick<
9+
GetPopulateQueryOptions,
10+
'namedQueries' | 'defaultQueryName'
11+
> & {
12+
populates: Populates<S>
13+
/**
14+
* @default: false
15+
*/
16+
allowUnnamedQueryForExternal?: boolean
17+
}
18+
919
/**
1020
* $populateParams.name can be passed from the outside.
1121
* $populateParams.query can be directly used, internally.
@@ -23,25 +33,28 @@ export function populate(
2333
return async (context: HookContext): Promise<HookContext> => {
2434
// Skip this hook if there are no $populateParams or defaultQueryName
2535
if (!context.params.$populateParams && !defaultQueryName) {
26-
return Promise.resolve(context)
36+
return context
2737
}
2838
/**
2939
* The `getQuery` function sets up params.$populateParams.query.
3040
*/
31-
_set(
41+
const query = getQuery({
3242
context,
33-
'params.$populateParams.query',
34-
getQuery({
35-
context,
36-
defaultQueryName,
37-
namedQueries,
38-
allowUnnamedQueryForExternal,
39-
}),
40-
)
43+
namedQueries,
44+
defaultQueryName,
45+
allowUnnamedQueryForExternal,
46+
})
47+
48+
if (!query) return context
49+
50+
// Set the query at params.$populateParams.query
51+
if (!context.params.$populateParams) context.params.$populateParams = {}
52+
53+
context.params.$populateParams.query = query
4154

4255
/**
4356
* The graphPopulate hook expects to find a query at params.$populateParams.query
4457
*/
45-
return graphPopulate({ populates } as GraphPopulateHookOptions)(context)
58+
return graphPopulate({ populates })(context)
4659
}
4760
}

src/hooks/shallow-populate.hook.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ import type {
2424
import { toArray } from '../utils/to-array'
2525

2626
export function shallowPopulate(
27-
opts: Partial<ShallowPopulateOptions> &
28-
Pick<ShallowPopulateOptions, 'include'>,
27+
opts: ShallowPopulateOptions,
2928
): (context: HookContext) => Promise<HookContext> {
3029
const options = {
3130
catchOnError: false,

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export { graphPopulate } from './hooks/graph-populate.hook'
2-
export { populate } from './hooks/populate.hook'
3-
export { getQuery } from './utils/get-query'
1+
export * from './hooks/graph-populate.hook'
2+
export * from './hooks/populate.hook'
3+
export * from './utils/get-query'
44
export { populateUtil } from './utils/util.populate'
55
export { shallowPopulate } from './hooks/shallow-populate.hook'
66

src/types.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,6 @@ export type NestedQuery = Record<string, AnyData>
3636

3737
export type Populates<S = string> = Record<string, PopulateObject<S>>
3838

39-
export interface GraphPopulateHookOptions<S = string> {
40-
populates: Populates<S>
41-
/**
42-
* @default: false
43-
*/
44-
allowUnnamedQueryForExternal?: boolean
45-
}
46-
47-
export interface PopulateHookOptions<S = string> {
48-
populates: Populates<S>
49-
namedQueries?: AnyData
50-
defaultQueryName?: string
51-
/**
52-
* @default: false
53-
*/
54-
allowUnnamedQueryForExternal?: boolean
55-
}
56-
57-
export interface GetPopulateQueryOptions {
58-
context: HookContext
59-
namedQueries: AnyData
60-
defaultQueryName: string
61-
/**
62-
* @default: false
63-
*/
64-
allowUnnamedQueryForExternal?: boolean
65-
}
66-
6739
export interface PopulateUtilOptions<S = string> {
6840
app: Application
6941
params: Params

0 commit comments

Comments
 (0)