-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hooks): Refactor .params, .props and .defaults into hooks (#37)
- Loading branch information
Showing
13 changed files
with
458 additions
and
502 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { HookContext, HookContextData, HookDefaultsInitializer } from './base' | ||
import { NextFunction } from './compose' | ||
|
||
/** | ||
* Returns a hook that initializes named function parameters on the | ||
* hook context. | ||
* | ||
* @param names A list of parameter names | ||
*/ | ||
export function params (...names: string[]) { | ||
const descriptors = names.reduce((result, name, index) => { | ||
result[name] = { | ||
enumerable: true, | ||
get (this: any) { | ||
return this.arguments[index]; | ||
}, | ||
|
||
set (this: any, value) { | ||
this.arguments[index] = value; | ||
} | ||
} | ||
|
||
return result; | ||
}, {} as PropertyDescriptorMap); | ||
|
||
return async function contextParams (context: HookContext, next: NextFunction) { | ||
Object.defineProperties(context, descriptors); | ||
await next(); | ||
}; | ||
} | ||
|
||
/** | ||
* Returns a hook that sets the given properties on the hook context. | ||
* | ||
* @param properties The properties to set. | ||
*/ | ||
export function properties (properties: HookContextData) { | ||
return async function contextProperties (context: HookContext, next: NextFunction) { | ||
Object.assign(context, properties); | ||
await next(); | ||
} | ||
} | ||
|
||
/** | ||
* Returns a hook that calls a `callback(context)` function that | ||
* returns default values which will be set on the context if they | ||
* are currently `undefined`. | ||
* | ||
* @param initializer The initialization callback. | ||
*/ | ||
export function defaults (initializer: HookDefaultsInitializer) { | ||
return async function contextDefaults (context: HookContext, next: NextFunction) { | ||
const defaults = await initializer(context); | ||
|
||
for (const name of Object.keys(defaults)) { | ||
if (context[name] === undefined) { | ||
context[name] = defaults[name]; | ||
} | ||
} | ||
|
||
await next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.