|
1 | | -import { |
2 | | - type FetchHandler, |
3 | | - type RPCOptions, |
4 | | - XRPC, |
5 | | - type XRPCRequestOptions, |
6 | | - type XRPCResponse, |
7 | | -} from '@atcute/client'; |
8 | | -import type { Procedures, Queries } from '@tsky/lexicons'; |
9 | | - |
10 | | -// From @atcute/client |
11 | | -type OutputOf<T> = T extends { |
12 | | - // biome-ignore lint/suspicious/noExplicitAny: <explanation> |
13 | | - output: any; |
14 | | -} |
15 | | - ? T['output'] |
16 | | - : never; |
17 | | - |
18 | | -export class Client<Q = Queries, P = Procedures> { |
19 | | - xrpc: XRPC; |
20 | | - |
21 | | - constructor(handler: FetchHandler) { |
22 | | - this.xrpc = new XRPC({ handler }); |
23 | | - } |
24 | | - |
25 | | - /** |
26 | | - * Makes a query (GET) request |
27 | | - * @param nsid Namespace ID of a query endpoint |
28 | | - * @param options Options to include like parameters |
29 | | - * @returns The response of the request |
30 | | - */ |
31 | | - async get<K extends keyof Q>( |
32 | | - nsid: K, |
33 | | - options: RPCOptions<Q[K]>, |
34 | | - ): Promise<XRPCResponse<OutputOf<Q[K]>>> { |
35 | | - // biome-ignore lint/suspicious/noExplicitAny: <explanation> |
36 | | - return this.xrpc.get(nsid as any, options); |
37 | | - } |
38 | | - |
39 | | - /** |
40 | | - * Makes a procedure (POST) request |
41 | | - * @param nsid Namespace ID of a procedure endpoint |
42 | | - * @param options Options to include like input body or parameters |
43 | | - * @returns The response of the request |
44 | | - */ |
45 | | - async call<K extends keyof P>( |
46 | | - nsid: K, |
47 | | - options: RPCOptions<P[K]>, |
48 | | - ): Promise<XRPCResponse<OutputOf<P[K]>>> { |
49 | | - // biome-ignore lint/suspicious/noExplicitAny: <explanation> |
50 | | - return this.xrpc.call(nsid as any, options); |
51 | | - } |
52 | | - |
53 | | - /** Makes a request to the XRPC service */ |
54 | | - async request(options: XRPCRequestOptions): Promise<XRPCResponse> { |
55 | | - return this.xrpc.request(options); |
56 | | - } |
57 | | -} |
| 1 | +import type { |
| 2 | + RPCOptions, |
| 3 | + XRPC, |
| 4 | + XRPCRequestOptions, |
| 5 | + XRPCResponse, |
| 6 | +} from '@atcute/client'; |
| 7 | +import type { Procedures, Queries } from '@tsky/lexicons'; |
| 8 | + |
| 9 | +// From @atcute/client |
| 10 | +type OutputOf<T> = T extends { |
| 11 | + // biome-ignore lint/suspicious/noExplicitAny: <explanation> |
| 12 | + output: any; |
| 13 | +} |
| 14 | + ? T['output'] |
| 15 | + : never; |
| 16 | + |
| 17 | +export class Client<Q = Queries, P = Procedures> { |
| 18 | + xrpc: XRPC; |
| 19 | + |
| 20 | + constructor(xrpc: XRPC) { |
| 21 | + this.xrpc = xrpc; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Makes a query (GET) request |
| 26 | + * @param nsid Namespace ID of a query endpoint |
| 27 | + * @param options Options to include like parameters |
| 28 | + * @returns The response of the request |
| 29 | + */ |
| 30 | + async get<K extends keyof Q>( |
| 31 | + nsid: K, |
| 32 | + options: RPCOptions<Q[K]>, |
| 33 | + ): Promise<XRPCResponse<OutputOf<Q[K]>>> { |
| 34 | + // biome-ignore lint/suspicious/noExplicitAny: <explanation> |
| 35 | + return this.xrpc.get(nsid as any, options); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Makes a procedure (POST) request |
| 40 | + * @param nsid Namespace ID of a procedure endpoint |
| 41 | + * @param options Options to include like input body or parameters |
| 42 | + * @returns The response of the request |
| 43 | + */ |
| 44 | + async call<K extends keyof P>( |
| 45 | + nsid: K, |
| 46 | + options: RPCOptions<P[K]>, |
| 47 | + ): Promise<XRPCResponse<OutputOf<P[K]>>> { |
| 48 | + // biome-ignore lint/suspicious/noExplicitAny: <explanation> |
| 49 | + return this.xrpc.call(nsid as any, options); |
| 50 | + } |
| 51 | + |
| 52 | + /** Makes a request to the XRPC service */ |
| 53 | + async request(options: XRPCRequestOptions): Promise<XRPCResponse> { |
| 54 | + return this.xrpc.request(options); |
| 55 | + } |
| 56 | +} |
0 commit comments