Skip to content

Commit 386f021

Browse files
committed
feat: add array type
1 parent 7606319 commit 386f021

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

src/common/msg/rpc.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ describe('rpc async', () => {
7373
await new Promise(resolve => setTimeout(resolve, 100))
7474
expect(Bob.getCount()).toBe(1)
7575

76-
c1.close()
77-
c2.close()
76+
c1.dispose()
77+
c2.dispose()
7878

7979
expect(log).toMatchInlineSnapshot(`
8080
Array [
@@ -146,8 +146,8 @@ describe('rpc async', () => {
146146
await new Promise(resolve => setTimeout(resolve, 100))
147147
expect(Bob.getCount()).toBe(1)
148148

149-
c1.close()
150-
c2.close()
149+
c1.dispose()
150+
c2.dispose()
151151
})
152152

153153
it('timeout async', async (done) => {

src/common/schema/rpc.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Infer } from './schema'
2+
import { useRPC } from '../msg/rpc'
23
import { boolean, func, object, string } from './schema'
34

45
// see https://github.com/colinhacks/zod?tab=readme-ov-file#functions
@@ -63,4 +64,36 @@ describe('rpc.spec', () => {
6364
}
6465
`)
6566
})
67+
68+
it('should use a schema for useRPC', async () => {
69+
const rpcSchema = object({
70+
echo: func(
71+
[string(), boolean()],
72+
string().optional(),
73+
).props({
74+
rpcDesc: 'Just echo the string',
75+
}),
76+
noFunc: string(),
77+
})
78+
79+
type RpcRaw = Infer<typeof rpcSchema>
80+
81+
expectTypeOf<RpcRaw>().toMatchObjectType<{
82+
echo: (args_0: string, args_1: boolean) => string | undefined
83+
noFunc: string
84+
}>()
85+
86+
const bob = useRPC<RpcRaw>({
87+
echo: (a: string, b: boolean) => {
88+
return a + b
89+
},
90+
noFunc: 'noFunc',
91+
}, {
92+
post: () => { },
93+
on: () => { },
94+
})
95+
96+
const r = await bob.echo('hello', true)
97+
expect(r).toEqual('hellotrue')
98+
})
6699
})

src/common/schema/schema.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Infer } from './schema'
22
import type { Expect, IsEqual } from './test'
33
import { cloneJsonObject } from '../data'
4-
import { boolean, float, int, literal, number, object, string, stringLiterals, tuple, union } from './schema'
4+
import { array, boolean, float, int, literal, number, object, string, stringLiterals, tuple, union } from './schema'
55

66
describe('schema', () => {
77
it('create schema', async () => {
@@ -28,6 +28,7 @@ describe('schema', () => {
2828
name: string(),
2929
age: int().optional(),
3030
active: boolean(),
31+
tags: array(string()).optional(),
3132
// status: stringLiterals(['active', 'trialing', 'past_due', 'paused', 'deleted']),
3233
// status: string<Status>(),
3334
obj: object({
@@ -45,6 +46,7 @@ describe('schema', () => {
4546
} | undefined
4647
name: string
4748
active: boolean
49+
tags?: string[]
4850
lit: 'active' | 'trialing' | 'past_due' | 'paused' | 'deleted'
4951
}>> // Should pass
5052

@@ -86,6 +88,10 @@ describe('schema', () => {
8688
"_optional": true,
8789
"type": "object",
8890
},
91+
"tags": Object {
92+
"_optional": true,
93+
"type": "array",
94+
},
8995
},
9096
"type": "object",
9197
}

src/common/schema/schema.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ export function tuple<T extends [] | [Type, ...Type[]]>(items: T): ArrayType<T,
241241
})
242242
}
243243

244+
export function array<T>(itemType: Type<T>): Type<T[]> {
245+
return generic<T[]>('array', {
246+
_check: v => Array.isArray(v) && v.every(item => itemType._check(item)),
247+
})
248+
}
249+
244250
// const tt = tuple([number(), string(), boolean()])
245251
// type ttt = Infer<typeof tt> // expected [number, string, boolean]
246252

0 commit comments

Comments
 (0)