|
1 | 1 | import type { Infer } from './schema' |
2 | | -import { func, object, string } from './schema' |
| 2 | +import { expectTypeOf } from 'vitest' |
| 3 | +import { boolean, func, number, object, rpc, string } from './schema' |
3 | 4 |
|
4 | 5 | // see https://github.com/colinhacks/zod?tab=readme-ov-file#functions |
5 | 6 |
|
| 7 | +declare module './schema' { |
| 8 | + interface TypeProps { |
| 9 | + rpcDesc?: string |
| 10 | + } |
| 11 | +} |
| 12 | + |
6 | 13 | describe('rpc.spec', () => { |
7 | 14 | it('should do something', async () => { |
| 15 | + const rpcFn = object({ rpc: rpc() }) |
| 16 | + type RpcFn = Infer<typeof rpcFn> |
| 17 | + expectTypeOf<RpcFn>().toMatchObjectType<{ |
| 18 | + rpc: () => undefined |
| 19 | + }>() |
| 20 | + |
| 21 | + const rpcFn2 = object({ rpc: rpc(object({ |
| 22 | + a: number(), |
| 23 | + }), string()) }) |
| 24 | + type RpcFn2 = Infer<typeof rpcFn2> |
| 25 | + expectTypeOf<RpcFn2>().toMatchObjectType<{ |
| 26 | + rpc: (info: { a: number }) => string |
| 27 | + }>() |
| 28 | + |
| 29 | + const payloadSchema = object({ |
| 30 | + id: string(), |
| 31 | + method: string(), |
| 32 | + params: object({ |
| 33 | + a: string(), |
| 34 | + b: boolean(), |
| 35 | + }), |
| 36 | + }) |
| 37 | + |
| 38 | + type PayloadSchema = Infer<typeof payloadSchema> |
| 39 | + |
| 40 | + expectTypeOf<PayloadSchema>().toMatchObjectType<{ |
| 41 | + id: string |
| 42 | + method: string |
| 43 | + params: { |
| 44 | + a: string |
| 45 | + b: boolean |
| 46 | + } |
| 47 | + }>() |
| 48 | + |
8 | 49 | const rpcSchema = object({ |
9 | | - echo: func([string()], string()), |
| 50 | + echo: func( |
| 51 | + [string(), boolean()], |
| 52 | + string().optional(), |
| 53 | + ).props({ |
| 54 | + rpcDesc: 'Just echo the string', |
| 55 | + }), |
| 56 | + rpc: rpc(payloadSchema), |
| 57 | + noFunc: string(), |
10 | 58 | }) |
11 | 59 |
|
12 | 60 | type RpcRaw = Infer<typeof rpcSchema> |
| 61 | + |
| 62 | + expectTypeOf<RpcRaw>().toMatchObjectType<{ |
| 63 | + echo: (args_0: string, args_1: boolean) => string | undefined |
| 64 | + rpc: (info: { |
| 65 | + id: string |
| 66 | + method: string |
| 67 | + params: { |
| 68 | + a: string |
| 69 | + b: boolean |
| 70 | + } |
| 71 | + }) => undefined |
| 72 | + noFunc: string |
| 73 | + }>() |
| 74 | + |
| 75 | + const api: RpcRaw = { |
| 76 | + echo: (a: string, b: boolean) => { |
| 77 | + return a + b |
| 78 | + }, |
| 79 | + noFunc: 'noFunc', |
| 80 | + rpc(info) { |
| 81 | + const _ = info.id |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + expect(rpcSchema).toMatchInlineSnapshot(` |
| 86 | + TypeObjectClass { |
| 87 | + "_check": [Function], |
| 88 | + "_object": Object { |
| 89 | + "echo": TypeFuncClass { |
| 90 | + "_args": Array [ |
| 91 | + TypeStringClass { |
| 92 | + "_check": [Function], |
| 93 | + "type": "string", |
| 94 | + }, |
| 95 | + TypeGeneric { |
| 96 | + "_check": [Function], |
| 97 | + "type": "boolean", |
| 98 | + }, |
| 99 | + ], |
| 100 | + "_check": [Function], |
| 101 | + "_props": Object { |
| 102 | + "rpcDesc": "Just echo the string", |
| 103 | + }, |
| 104 | + "_ret": TypeStringClass { |
| 105 | + "_check": [Function], |
| 106 | + "_optional": true, |
| 107 | + "type": "string", |
| 108 | + }, |
| 109 | + "type": "function", |
| 110 | + }, |
| 111 | + "noFunc": TypeStringClass { |
| 112 | + "_check": [Function], |
| 113 | + "type": "string", |
| 114 | + }, |
| 115 | + "rpc": TypeRpcClass { |
| 116 | + "_check": [Function], |
| 117 | + "_info": TypeObjectClass { |
| 118 | + "_check": [Function], |
| 119 | + "_object": Object { |
| 120 | + "id": TypeStringClass { |
| 121 | + "_check": [Function], |
| 122 | + "type": "string", |
| 123 | + }, |
| 124 | + "method": TypeStringClass { |
| 125 | + "_check": [Function], |
| 126 | + "type": "string", |
| 127 | + }, |
| 128 | + "params": TypeObjectClass { |
| 129 | + "_check": [Function], |
| 130 | + "_object": Object { |
| 131 | + "a": TypeStringClass { |
| 132 | + "_check": [Function], |
| 133 | + "type": "string", |
| 134 | + }, |
| 135 | + "b": TypeGeneric { |
| 136 | + "_check": [Function], |
| 137 | + "type": "boolean", |
| 138 | + }, |
| 139 | + }, |
| 140 | + "type": "object", |
| 141 | + }, |
| 142 | + }, |
| 143 | + "type": "object", |
| 144 | + }, |
| 145 | + "_ret": TypeGeneric { |
| 146 | + "_check": [Function], |
| 147 | + "type": "none", |
| 148 | + }, |
| 149 | + "type": "rpc", |
| 150 | + }, |
| 151 | + }, |
| 152 | + "type": "object", |
| 153 | + } |
| 154 | + `) |
13 | 155 | }) |
| 156 | + |
| 157 | + // it('should use a schema for useRPC', async () => { |
| 158 | + // const rpcSchema = object({ |
| 159 | + // echo: func( |
| 160 | + // [string(), boolean()], |
| 161 | + // string().optional(), |
| 162 | + // ).props({ |
| 163 | + // rpcDesc: 'Just echo the string', |
| 164 | + // }), |
| 165 | + // noFunc: string(), |
| 166 | + // }) |
| 167 | + |
| 168 | + // type RpcRaw = Infer<typeof rpcSchema> |
| 169 | + |
| 170 | + // expectTypeOf<RpcRaw>().toMatchObjectType<{ |
| 171 | + // echo: (args_0: string, args_1: boolean) => string | undefined |
| 172 | + // noFunc: string |
| 173 | + // }>() |
| 174 | + |
| 175 | + // const bob = useRPC<RpcRaw>({ |
| 176 | + // echo: (a: string, b: boolean) => { |
| 177 | + // return a + b |
| 178 | + // }, |
| 179 | + // noFunc: 'noFunc', |
| 180 | + // }, { |
| 181 | + // post: () => { }, |
| 182 | + // on: () => { }, |
| 183 | + // }) |
| 184 | + |
| 185 | + // const r = await bob.echo('hello', true) |
| 186 | + // expect(r).toEqual('hellotrue') |
| 187 | + // }) |
14 | 188 | }) |
0 commit comments