-
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
- Loading branch information
Showing
13 changed files
with
440 additions
and
503 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
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,45 @@ | ||
import { HookContext, HookContextData, HookDefaultsInitializer } from './base' | ||
import { NextFunction } from './compose' | ||
|
||
export function contextParams (...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 defineParams (context: HookContext, next: NextFunction) { | ||
Object.defineProperties(context, descriptors); | ||
await next(); | ||
}; | ||
} | ||
|
||
export function contextProperties (properties: HookContextData) { | ||
return async function setProps (context: HookContext, next: NextFunction) { | ||
Object.assign(context, properties); | ||
await next(); | ||
} | ||
} | ||
|
||
export function contextDefaults (initializer: HookDefaultsInitializer) { | ||
return async function setDefaults (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 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.