Skip to content

Commit b3e8195

Browse files
committed
Fix typescript check for extra server check
1 parent 2e84fc2 commit b3e8195

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

packages/client/src/__tests__/api.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ describe('API functions', () => {
147147
};
148148

149149
// Use type assertion to bypass TypeScript check for testing runtime validation
150-
expect(() => registerAbility(ability as unknown as ClientAbility)).toThrow(
150+
expect(() =>
151+
registerAbility(ability as unknown as ClientAbility)
152+
).toThrow(
151153
'Server abilities cannot be registered via registerAbility'
152154
);
153155
});
@@ -170,9 +172,9 @@ describe('API functions', () => {
170172
};
171173

172174
// Use type assertion to bypass TypeScript check
173-
expect(() => registerAbility(ability as unknown as ClientAbility)).toThrow(
174-
'Client abilities must include a callback function'
175-
);
175+
expect(() =>
176+
registerAbility(ability as unknown as ClientAbility)
177+
).toThrow('Client abilities must include a callback function');
176178
});
177179

178180
it('should throw error for ability without name', () => {

packages/client/src/__tests__/validation.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,18 @@ describe('validateValueFromSchema', () => {
382382
it('should handle empty schema as valid', () => {
383383
expect(validateValueFromSchema('anything', undefined)).toBe(true);
384384
// Testing edge cases where schema is null or falsy
385-
expect(validateValueFromSchema(123, null as unknown as Record<string, any>)).toBe(true);
386-
expect(validateValueFromSchema(true, false as unknown as Record<string, any>)).toBe(true);
385+
expect(
386+
validateValueFromSchema(
387+
123,
388+
null as unknown as Record<string, any>
389+
)
390+
).toBe(true);
391+
expect(
392+
validateValueFromSchema(
393+
true,
394+
false as unknown as Record<string, any>
395+
)
396+
).toBe(true);
387397
});
388398

389399
it('should handle schema compilation errors', () => {

packages/client/src/api.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ export function registerAbility(ability: Ability): void {
7171
if (!ability.description) {
7272
throw new Error(__('Ability description is required'));
7373
}
74+
75+
// Runtime check for JavaScript consumers who might pass server abilities
76+
// TypeScript users are protected by the ClientAbility type
77+
const anyAbility = ability as any;
78+
if (anyAbility.location === 'server') {
79+
throw new Error(
80+
'Server abilities cannot be registered via registerAbility'
81+
);
82+
}
83+
7484
if (!ability.callback || typeof ability.callback !== 'function') {
7585
throw new Error(__('Abilities registered on the client require a callback function'));
7686
}

0 commit comments

Comments
 (0)