Skip to content

Commit 7108df5

Browse files
authored
Merge pull request feathersjs-ecosystem#509 from NickBolles/njb/fix-typings
Update iff predicates typings to be either sync or async
2 parents 6e91fd4 + 56c2fec commit 7108df5

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

tests/services/iffelse.test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ describe('services iffElse', () => {
6565
hookFcnAsyncCalls = 0;
6666
});
6767

68+
describe('runs single hook', () => {
69+
it('when true', () => {
70+
return hooks.iffElse(true, hookFcnSync, hookFcnAsync)(hook)
71+
.then(hook => {
72+
assert.deepEqual(hook, hookAfter);
73+
assert.equal(hookFcnSyncCalls, 1);
74+
assert.equal(hookFcnAsyncCalls, 0);
75+
assert.deepEqual(hook, hookAfter);
76+
});
77+
});
78+
79+
it('when false', () => {
80+
return hooks.iffElse(false, hookFcnSync, hookFcnAsync)(hook)
81+
.then(hook => {
82+
assert.deepEqual(hook, hookAfter);
83+
assert.equal(hookFcnSyncCalls, 0);
84+
assert.equal(hookFcnAsyncCalls, 1);
85+
assert.deepEqual(hook, hookAfter);
86+
});
87+
});
88+
});
89+
6890
describe('runs multiple hooks', () => {
6991
it('when true', () => {
7092
return hooks.iffElse(true, [hookFcnSync, hookFcnAsync, hookFcnCb], null)(hook)
@@ -154,6 +176,6 @@ describe('services iffElse', () => {
154176

155177
// Helpers
156178

157-
function clone (obj) {
179+
function clone(obj) {
158180
return JSON.parse(JSON.stringify(obj));
159181
}

types/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ export function validateSchema(schema: object, ajv: AjvOrNewable, options?: Vali
547547
* Execute one array of hooks or another based on a sync or async predicate.
548548
* {@link https://feathers-plus.github.io/v1/feathers-hooks-common/index.html#IffElse}
549549
*/
550-
export function iffElse(predicate: SyncPredicateFn, hooksTrue: Hook | Hook[], hooksFalse: Hook | Hook[]): Hook;
550+
export function iffElse(predicate: PredicateFn, hooksTrue: Hook | Hook[], hooksFalse: Hook | Hook[]): Hook;
551551

552552
export interface IffHook extends Hook {
553553
else(...hooks: Hook[]): Hook;
@@ -557,7 +557,7 @@ export interface IffHook extends Hook {
557557
* Execute one or another series of hooks depending on a sync or async predicate.
558558
* {@link https://feathers-plus.github.io/v1/feathers-hooks-common/index.html#Iff}
559559
*/
560-
export function iff(predicate: SyncPredicateFn, ...hooks: Hook[]): IffHook;
560+
export function iff(predicate: PredicateFn, ...hooks: Hook[]): IffHook;
561561

562562
/**
563563
* Alias for iff

types/tests.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ const fgraphqlOptions2: FGraphQLHookOptions = {
260260
Query: {}
261261
},
262262
options: {
263-
extraAuthProps: ['asdf' ],
263+
extraAuthProps: ['asdf'],
264264
inclAllFields: false,
265265
inclJoinedNames: false,
266266
inclAllFieldsClient: true,
@@ -306,7 +306,7 @@ makeCallingParams(
306306
// tslint:disable-next-line
307307
class ObjId {
308308
// tslint:disable-next-line
309-
constructor(id?: string | number) {}
309+
constructor(id?: string | number) { }
310310
}
311311

312312
// $ExpectType Hook
@@ -388,17 +388,17 @@ setNow('createdAt', 'updatedAt');
388388
setSlug('storeId');
389389

390390
// $ExpectType Hook
391-
sequelizeConvert ({
391+
sequelizeConvert({
392392
aBool: 'boolean',
393393
aDate: 'date',
394394
anObject: 'json',
395395
aNumber: 'strToInt'
396396
}, null, {
397-
strToInt: {
398-
js: (sqlValue: string) => +sqlValue,
399-
sql: (jsValue: number) => `${jsValue}`,
400-
}
401-
});
397+
strToInt: {
398+
js: (sqlValue: string) => +sqlValue,
399+
sql: (jsValue: number) => `${jsValue}`,
400+
}
401+
});
402402

403403
// $ExpectType Hook
404404
sifter(ctx => item => true);
@@ -449,11 +449,17 @@ validateSchema({}, ajv);
449449

450450
// $ExpectType Hook
451451
iffElse(syncTrue, [hook1, hook2], [hook3, hook4]);
452+
// $ExpectType Hook
453+
iffElse(asyncTrue, [hook1, hook2], [hook3, hook4]);
452454

453455
// $ExpectType IffHook
454456
iff(syncTrue, hook1, hook2);
457+
// $ExpectType IffHook
458+
iff(asyncTrue, hook1, hook2);
455459
// $ExpectType Hook
456460
iff(syncTrue, hook1, hook2).else(hook3, hook4);
461+
// $ExpectType Hook
462+
iff(asyncTrue, hook1, hook2).else(hook3, hook4);
457463

458464
// $ExpectType IffHook
459465
when(syncTrue, hook1, hook2);

0 commit comments

Comments
 (0)