Skip to content

Commit 80845bb

Browse files
authored
fix: MaybeArray with readonly array (#24)
1 parent 0bca80d commit 80845bb

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/hooks/params-for-server/params-for-server.hook.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@ describe('paramsForServer', () => {
2323
})
2424
})
2525

26+
it('should accept a readonly array', () => {
27+
const whitelist = ['a', 'b'] as const
28+
expect(
29+
paramsForServer(whitelist)({
30+
params: {
31+
a: 1,
32+
b: 2,
33+
query: {},
34+
},
35+
} as HookContext),
36+
).toEqual({
37+
params: {
38+
query: {
39+
_$client: {
40+
a: 1,
41+
b: 2,
42+
},
43+
},
44+
},
45+
})
46+
})
47+
2648
it('should move params to query._$client and leave remaining', () => {
2749
expect(
2850
paramsForServer('a')({

src/internal.utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ export const hasOwnProperty = (
77
return keys.some((x) => Object.prototype.hasOwnProperty.call(obj, x))
88
}
99

10-
export type MaybeArray<T> = T | T[]
11-
export const toArray = <T>(value: T | T[]): T[] =>
12-
Array.isArray(value) ? value : [value]
10+
export type MaybeArray<T> = T | readonly T[]
11+
export const toArray = <T>(value: T | readonly T[]): T[] =>
12+
Array.isArray(value) ? [...value] : [value as T]
1313

1414
export type Promisable<T> = T | Promise<T>
1515
export type KeyOf<T> = Extract<keyof T, string>

0 commit comments

Comments
 (0)