1- import _get from 'lodash/get.js'
21import _isEmpty from 'lodash/isEmpty.js'
3- import _merge from 'lodash/merge.js'
42
53import { 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'
1013import type { GraphPopulateApplication } from '../app/graph-populate.class'
1114
1215const 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 } )
0 commit comments