Skip to content

Commit acf79c2

Browse files
committed
refactor: simplify Feathers context prop helpers
- Extract shared statusCodeDescriptor to avoid duplication - Split into createFeathersProps() for HookContextData (standard path) - Split into createFeathersDescriptors() for PropertyDescriptorMap (decorated path) - Remove propsFromDescriptors() conversion function - no longer needed
1 parent 6f3e99d commit acf79c2

1 file changed

Lines changed: 40 additions & 32 deletions

File tree

packages/feathers/src/hooks.ts

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,45 @@ export function createContext(service: Service, method: string, data: HookContex
148148
return createContext(data) as HookContext
149149
}
150150

151+
/** Shared statusCode property descriptor */
152+
const statusCodeDescriptor: PropertyDescriptor = {
153+
enumerable: true,
154+
get(this: HookContext) {
155+
return this.http?.status
156+
},
157+
set(this: HookContext, value: number) {
158+
this.http = this.http || {}
159+
this.http.status = value
160+
}
161+
}
162+
151163
/**
152-
* Creates property descriptors for Feathers-specific context properties.
153-
* Used to add app, path, service, method, etc. to hook contexts.
164+
* Creates Feathers-specific context properties as HookContextData.
165+
* Used with HookManager.props() for standard service methods.
154166
*/
155-
function createFeathersContextProps<A>(
167+
function createFeathersProps<A>(
168+
app: A,
169+
path: string,
170+
method: string,
171+
service: FeathersService<A>
172+
): HookContextData {
173+
const props: HookContextData = {
174+
app,
175+
path,
176+
method,
177+
service,
178+
event: null,
179+
type: 'around'
180+
}
181+
Object.defineProperty(props, 'statusCode', statusCodeDescriptor)
182+
return props
183+
}
184+
185+
/**
186+
* Creates Feathers-specific context properties as PropertyDescriptorMap.
187+
* Used with Object.defineProperties() for decorated methods.
188+
*/
189+
function createFeathersDescriptors<A>(
156190
app: A,
157191
path: string,
158192
method: string,
@@ -165,34 +199,10 @@ function createFeathersContextProps<A>(
165199
service: { value: service, enumerable: true, writable: true },
166200
event: { value: null, enumerable: true, writable: true },
167201
type: { value: 'around', enumerable: true, writable: true },
168-
statusCode: {
169-
enumerable: true,
170-
get(this: HookContext) {
171-
return this.http?.status
172-
},
173-
set(this: HookContext, value: number) {
174-
this.http = this.http || {}
175-
this.http.status = value
176-
}
177-
}
202+
statusCode: statusCodeDescriptor
178203
}
179204
}
180205

181-
/**
182-
* Converts PropertyDescriptorMap to HookContextData for use with HookManager.props()
183-
*/
184-
function propsFromDescriptors(descriptors: PropertyDescriptorMap): HookContextData {
185-
const props: HookContextData = {}
186-
for (const [key, descriptor] of Object.entries(descriptors)) {
187-
if ('value' in descriptor) {
188-
props[key] = descriptor.value
189-
} else {
190-
Object.defineProperty(props, key, descriptor)
191-
}
192-
}
193-
return props
194-
}
195-
196206
export class FeathersHookManager<A> extends HookManager {
197207
constructor(
198208
public app: A,
@@ -232,8 +242,6 @@ export function hookMixin<A>(this: A, service: FeathersService<A>, path: string,
232242
const hookMethods = getHookMethods(service, options)
233243

234244
const serviceMethodHooks = hookMethods.reduce((res, method) => {
235-
const feathersContextProps = createFeathersContextProps(this, path, method, service)
236-
237245
// Check if the method already has params configured via @hooks().params()
238246
const existingManager = getManager((service as any)[method])
239247
const existingParams = existingManager?.getParams()
@@ -249,7 +257,7 @@ export function hookMixin<A>(this: A, service: FeathersService<A>, path: string,
249257

250258
const contextProto = wrapper.Context?.prototype
251259
if (contextProto) {
252-
Object.defineProperties(contextProto, feathersContextProps)
260+
Object.defineProperties(contextProto, createFeathersDescriptors(this, path, method, service))
253261
}
254262
return res
255263
}
@@ -259,7 +267,7 @@ export function hookMixin<A>(this: A, service: FeathersService<A>, path: string,
259267

260268
res[method] = new FeathersHookManager<A>(this, method)
261269
.params(...params)
262-
.props(propsFromDescriptors(feathersContextProps))
270+
.props(createFeathersProps(this, path, method, service))
263271

264272
return res
265273
}, {} as BaseHookMap)

0 commit comments

Comments
 (0)