Skip to content

Commit 9cdf642

Browse files
authored
fix(checkContext): type as array (#636)
1 parent c1831bd commit 9cdf642

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

docs/utilities.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,16 @@ Restrict a hook to run for certain methods and method types.
118118

119119
- **Arguments**
120120
- `{Object} context`
121-
- `{String} [ type ]`
122-
- `{Array< String >} [ methods ]`
121+
- `{String | Array< String >} [ type ]`
122+
- `{String | Array< String >} [ methods ]`
123123
- `{String} [ label ]`
124124

125-
| Argument | Type | Default | Description |
126-
| --------- | :---------------: | ------------- | --------------------------------------------------------------- |
127-
| `context` | `Object` | | The hook context. |
128-
| `type` | `String` | all types | The service type allowed - before, after. |
129-
| `methods` | `Array< String >` | all methods | The service methods allowed - find, get, update, patch, remove. |
130-
| `label` | `String` | `'anonymous'` | Name of hook to use with `throw`. |
125+
| Argument | Type | Default | Description |
126+
| --------- | :------------------------: | ------------- | --------------------------------------------------------------- |
127+
| `context` | `Object` | | The hook context. |
128+
| `type` | `String | Array< String >` | all types | The service type allowed - before, after, error. |
129+
| `methods` | `String | Array< String >` | all methods | The service methods allowed - find, get, update, patch, remove. |
130+
| `label` | `String` | `'anonymous'` | Name of hook to use with `throw`. |
131131

132132
- **Example**
133133

lib/services/check-context.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
const stndMethods = ['find', 'get', 'create', 'update', 'patch', 'remove'];
33

44
module.exports = function (context, type = null, methods = [], label = 'anonymous') {
5-
if (type && context.type !== type) {
6-
throw new Error(`The '${label}' hook can only be used as a '${type}' hook.`);
5+
if (type) {
6+
const types = Array.isArray(type) ? type : [type]; // safe enough for allowed values
7+
if (!types.includes(context.type)) {
8+
throw new Error(`The '${label}' hook can only be used as a '${type}' hook.`);
9+
}
710
}
811

912
if (!methods) { return; }

tests/services/check-context.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ describe('services checkContext', () => {
3838
assert.equal(checkContext(hook, null), undefined);
3939
});
4040

41+
it('handles expected type as array', () => {
42+
hook.type = 'before';
43+
assert.equal(checkContext(hook, ['before', 'after']), undefined);
44+
});
45+
46+
it('handles unexpected type as array', () => {
47+
hook.type = 'error';
48+
assert.throws(() => { checkContext(hook, ['before', 'after']); });
49+
});
50+
4151
it('handles expected method as string', () => {
4252
hook.method = 'create';
4353
assert.equal(checkContext(hook, null, 'create'), undefined);

0 commit comments

Comments
 (0)