@arrirpc/server 0.45.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +9 -0
- package/README.md +592 -0
- package/dist/index.cjs +1223 -0
- package/dist/index.d.cts +286 -0
- package/dist/index.d.mts +286 -0
- package/dist/index.d.ts +286 -0
- package/dist/index.mjs +1191 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2024 Joshua Sosso
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,592 @@
|
|
|
1
|
+
# Arri RPC
|
|
2
|
+
|
|
3
|
+
Typescript implementation of [Arri RPC](/README.md). It's built on top of [H3](https://github.com/unjs/h3) and uses [esbuild](https://esbuild.github.io/) for bundling.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
- [Quickstart](#quickstart)
|
|
8
|
+
- [Manual Setup](#manual-setup)
|
|
9
|
+
- [Install Dependencies](#install-dependencies)
|
|
10
|
+
- [Scaffold Your Project](#scaffold-your-project)
|
|
11
|
+
- [Usage](#usage)
|
|
12
|
+
- [Creating Procedures](#creating-procedures)
|
|
13
|
+
- [File-Based Routing](#file-based-routing)
|
|
14
|
+
- [Manual Routing](#manual-routing)
|
|
15
|
+
- [Creating Event Stream Procedures](#creating-event-stream-procedures)
|
|
16
|
+
- [Creating Websocket Procedures](#creating-websocket-procedures)
|
|
17
|
+
- [Adding Non-RPC Routes](#adding-non-rpc-routes)
|
|
18
|
+
- [Adding Middleware](#adding-middleware)
|
|
19
|
+
- [Key Concepts](#key-concepts)
|
|
20
|
+
- [Arri Definition File](#arri-definition-file)
|
|
21
|
+
- [How Procedures Map To Endpoints](#how-procedures-map-to-endpoints)
|
|
22
|
+
- [H3 Support](#h3-support)
|
|
23
|
+
- [Arri CLI](#arri-cli)
|
|
24
|
+
|
|
25
|
+
## Quickstart
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
# npm
|
|
29
|
+
npx arri init [project-name]
|
|
30
|
+
cd [project-name]
|
|
31
|
+
npm install
|
|
32
|
+
npm run dev
|
|
33
|
+
|
|
34
|
+
# pnpm
|
|
35
|
+
pnpm dlx arri init [project-name]
|
|
36
|
+
cd [project-name]
|
|
37
|
+
pnpm install
|
|
38
|
+
pnpm run dev
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Manual Setup
|
|
42
|
+
|
|
43
|
+
### Install Dependencies
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
# npm
|
|
47
|
+
npm install arri @arri/server @arri/schema
|
|
48
|
+
|
|
49
|
+
# pnpm
|
|
50
|
+
pnpm install arri @arri/server @arri/schema
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Scaffold Your Project
|
|
54
|
+
|
|
55
|
+
A basic Arri app directory looks something like this:
|
|
56
|
+
|
|
57
|
+
```fs
|
|
58
|
+
|-- project-dir
|
|
59
|
+
|-- .arri // temp files go here
|
|
60
|
+
|-- .output // final bundle goes here
|
|
61
|
+
|-- src
|
|
62
|
+
|-- procedures // .rpc.ts files go here
|
|
63
|
+
|-- app.ts
|
|
64
|
+
|-- arri.config.ts
|
|
65
|
+
|-- package.json
|
|
66
|
+
|-- tsconfig.json
|
|
67
|
+
|
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Both `.arri` and `.output` should be added to your `.gitignore` file
|
|
71
|
+
|
|
72
|
+
```txt
|
|
73
|
+
.arri
|
|
74
|
+
.output
|
|
75
|
+
node_modules
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
#### Configuration File
|
|
79
|
+
|
|
80
|
+
Create an `arri.config.ts` in the project directory
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
// arri.config.ts
|
|
84
|
+
import {
|
|
85
|
+
defineConfig,
|
|
86
|
+
typescriptClientGenerator,
|
|
87
|
+
dartClientGenerator,
|
|
88
|
+
} from "arri";
|
|
89
|
+
|
|
90
|
+
export default defineConfig({
|
|
91
|
+
entry: "app.ts",
|
|
92
|
+
port: 3000,
|
|
93
|
+
srcDir: "src",
|
|
94
|
+
generators: [
|
|
95
|
+
typescriptClientGenerator({
|
|
96
|
+
// options
|
|
97
|
+
}),
|
|
98
|
+
dartClientGenerator({
|
|
99
|
+
// options
|
|
100
|
+
}),
|
|
101
|
+
],
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
##### App Entry
|
|
106
|
+
|
|
107
|
+
Create an app entry file in your src directory. The name of the file must match whatever you put as the `entry` in your `arri.config.ts`.
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
// ./src/app.ts
|
|
111
|
+
import { ArriApp } from "arri";
|
|
112
|
+
|
|
113
|
+
const app = new ArriApp();
|
|
114
|
+
|
|
115
|
+
export default app;
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
##### Package.json
|
|
119
|
+
|
|
120
|
+
Setup your npm scripts:
|
|
121
|
+
|
|
122
|
+
```json
|
|
123
|
+
{
|
|
124
|
+
"name": "my-arri-app",
|
|
125
|
+
"type": "module",
|
|
126
|
+
"scripts": {
|
|
127
|
+
"dev": "arri dev",
|
|
128
|
+
"build": "arri build"
|
|
129
|
+
},
|
|
130
|
+
"dependencies": {
|
|
131
|
+
...
|
|
132
|
+
},
|
|
133
|
+
"devDependencies": {
|
|
134
|
+
...
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Usage
|
|
140
|
+
|
|
141
|
+
### Creating Procedures
|
|
142
|
+
|
|
143
|
+
#### File Based Router
|
|
144
|
+
|
|
145
|
+
Arri RPC comes with an optional file based router that will automatically register functions in the `./procedures` directory that end with the `.rpc.ts` file extension.
|
|
146
|
+
|
|
147
|
+
```fs
|
|
148
|
+
|-- src
|
|
149
|
+
|-- procedures
|
|
150
|
+
|-- sayHello.rpc.ts // becomes sayHello()
|
|
151
|
+
|-- users
|
|
152
|
+
|-- getUser.rpc.ts // becomes users.getUser()
|
|
153
|
+
|-- updateUser.rpc.ts // becomes users.updateUser()
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Example `.rpc.ts` file
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
// ./src/users/getUser.rpc.ts
|
|
160
|
+
import { defineRpc } from "@arrirpc/server";
|
|
161
|
+
import { a } from "@arrirpc/schema";
|
|
162
|
+
|
|
163
|
+
export default defineRpc({
|
|
164
|
+
params: a.object({
|
|
165
|
+
userId: a.string(),
|
|
166
|
+
}),
|
|
167
|
+
response: a.object({
|
|
168
|
+
id: a.string(),
|
|
169
|
+
name: a.string(),
|
|
170
|
+
createdAt: a.timestamp(),
|
|
171
|
+
}),
|
|
172
|
+
handler({ params }) {
|
|
173
|
+
// function body
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
##### Customizing the File Based Router
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
export default defineConfig({
|
|
182
|
+
// rest of config
|
|
183
|
+
procedureDir: "procedures", // change which directory to look for procedures (This is relative to the srcDir)
|
|
184
|
+
procedureGlobPatterns: ["**/*.rpc.ts"], // change the file name glob pattern for finding rpcs
|
|
185
|
+
});
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
#### Manual Routing
|
|
189
|
+
|
|
190
|
+
For those that want to opt out of the file-based routing system you can manually register procedures like so.
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
// using the app instance
|
|
194
|
+
const app = new ArriApp()
|
|
195
|
+
app.rpc('sayHello', {...})
|
|
196
|
+
|
|
197
|
+
// using a sub-router
|
|
198
|
+
const app = new ArriApp();
|
|
199
|
+
const router = new ArriRoute();
|
|
200
|
+
router.rpc('sayHello', {...})
|
|
201
|
+
app.use(router)
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
#### Creating Event Stream Procedures
|
|
205
|
+
|
|
206
|
+
Event stream procedures make use of [Server Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) to stream events to clients.
|
|
207
|
+
|
|
208
|
+
Arri Event streams sent the following event types:
|
|
209
|
+
|
|
210
|
+
- `message` - A standard message with the response data serialized as JSON
|
|
211
|
+
- `error` - An error message with an `ArriRequestError` sent as JSON
|
|
212
|
+
- `done` - A message to tell clients that there will be no more events
|
|
213
|
+
- `ping` - A message periodically sent by the server to keep the connection alive.
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
/// message event ///
|
|
217
|
+
id: string | undefined;
|
|
218
|
+
event: "message";
|
|
219
|
+
data: Response; // whatever you have specified as the response serialized to json
|
|
220
|
+
|
|
221
|
+
/// error event ///
|
|
222
|
+
id: string | undefined;
|
|
223
|
+
event: "error";
|
|
224
|
+
data: ArriRequestError; // serialized to json
|
|
225
|
+
|
|
226
|
+
/// done event ///
|
|
227
|
+
event: "done";
|
|
228
|
+
data: "this stream has ended";
|
|
229
|
+
|
|
230
|
+
/// ping event ///
|
|
231
|
+
event: "ping";
|
|
232
|
+
data: "";
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
##### Example Usage:
|
|
236
|
+
|
|
237
|
+
```ts
|
|
238
|
+
// procedures/users/watchUser.rpc.ts
|
|
239
|
+
export default defineEventStreamRpc({
|
|
240
|
+
params: a.object({
|
|
241
|
+
userId: a.string(),
|
|
242
|
+
}),
|
|
243
|
+
response: a.object({
|
|
244
|
+
id: a.string(),
|
|
245
|
+
name: a.string(),
|
|
246
|
+
createdAt: a.timestamp(),
|
|
247
|
+
updatedAt: a.timestamp(),
|
|
248
|
+
}),
|
|
249
|
+
handler({ params, stream }) {
|
|
250
|
+
// initialize the stream and send it to the client
|
|
251
|
+
stream.send();
|
|
252
|
+
|
|
253
|
+
// send a message every second
|
|
254
|
+
const interval = setInterval(async () => {
|
|
255
|
+
await stream.push({
|
|
256
|
+
id: "1",
|
|
257
|
+
name: "John Doe",
|
|
258
|
+
createdAt: new Date(),
|
|
259
|
+
updatedAt: new Date(),
|
|
260
|
+
});
|
|
261
|
+
}, 1000);
|
|
262
|
+
|
|
263
|
+
// cleanup when the client disconnects
|
|
264
|
+
stream.on("close", () => {
|
|
265
|
+
clearInterval(interval);
|
|
266
|
+
});
|
|
267
|
+
},
|
|
268
|
+
});
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
#### EventStreamConnection methods
|
|
272
|
+
|
|
273
|
+
```ts
|
|
274
|
+
stream.push(data: Data, eventId?: string)
|
|
275
|
+
stream.pushError(error: ArriRequestError, eventId?: string)
|
|
276
|
+
stream.send()
|
|
277
|
+
stream.end()
|
|
278
|
+
stream.on(e: 'request:close' | 'close', callback: () => any)
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### Creating Websocket Procedures
|
|
282
|
+
|
|
283
|
+
```ts
|
|
284
|
+
// Websocket procedures work really well with discriminated unions
|
|
285
|
+
const IncomingMsg = a.discriminator('type', {
|
|
286
|
+
FOO: a.object({
|
|
287
|
+
message: a.string(),
|
|
288
|
+
}),
|
|
289
|
+
PING: a.object({
|
|
290
|
+
message: a.string(),
|
|
291
|
+
})
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
const OutgoingMsg = a.discriminator('type', {
|
|
295
|
+
BAR: a.object({
|
|
296
|
+
message: a.string(),
|
|
297
|
+
}),
|
|
298
|
+
PONG: a.object({
|
|
299
|
+
message: a.string()
|
|
300
|
+
})
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
export default defineWebsocketRpc(
|
|
304
|
+
params: IncomingMsg,
|
|
305
|
+
response: OutgoingMsg,
|
|
306
|
+
handler: {
|
|
307
|
+
onOpen: (peer) => {},
|
|
308
|
+
onMessage: (peer, message) => {
|
|
309
|
+
switch(message.type) {
|
|
310
|
+
case "FOO":
|
|
311
|
+
peer.send({
|
|
312
|
+
type: "BAR",
|
|
313
|
+
message: "You sent a FOO message"
|
|
314
|
+
});
|
|
315
|
+
break;
|
|
316
|
+
case "PING":
|
|
317
|
+
peer.send({
|
|
318
|
+
type: "PONG",
|
|
319
|
+
message: "You sent a PING message"
|
|
320
|
+
});
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
},
|
|
324
|
+
onError: (peer, error) => {}
|
|
325
|
+
}
|
|
326
|
+
)
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
Under the hood Websocket RPCs use [crossws](https://crossws.unjs.io/).
|
|
330
|
+
|
|
331
|
+
The possible payloads sent by the server will look like the following:
|
|
332
|
+
|
|
333
|
+
```
|
|
334
|
+
event: message
|
|
335
|
+
data: <response serialized to json>
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
```
|
|
339
|
+
event: error
|
|
340
|
+
data: {"code": <some-err-code>, "message": <some-error-msg>}
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### Adding Non-RPC Routes
|
|
344
|
+
|
|
345
|
+
You can also add generic endpoints for instances when a message-based RPC endpoint doesn't fit.
|
|
346
|
+
|
|
347
|
+
```ts
|
|
348
|
+
// using the app instance
|
|
349
|
+
const app = new ArriApp();
|
|
350
|
+
app.route({
|
|
351
|
+
method: "get",
|
|
352
|
+
path: "/hello-world",
|
|
353
|
+
handler(event) {
|
|
354
|
+
return "hello world";
|
|
355
|
+
},
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
// using a sub-router
|
|
359
|
+
const app = new ArriApp();
|
|
360
|
+
const router = new ArriRouter();
|
|
361
|
+
router.route({
|
|
362
|
+
method: "get",
|
|
363
|
+
path: "/hello-world",
|
|
364
|
+
handler(event) {
|
|
365
|
+
return "hello world",
|
|
366
|
+
}
|
|
367
|
+
})
|
|
368
|
+
app.use(router)
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
### Adding Middleware
|
|
372
|
+
|
|
373
|
+
```ts
|
|
374
|
+
const app = new ArriApp();
|
|
375
|
+
|
|
376
|
+
const requestLoggerMiddleware = defineMiddleware((event) => {
|
|
377
|
+
console.log(`new request at ${event.path}`);
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
app.use(requestLoggerMiddleware);
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
#### Adding to the RPC Context
|
|
384
|
+
|
|
385
|
+
Any values added to `event.context` will become available in the rpc instance
|
|
386
|
+
|
|
387
|
+
```ts
|
|
388
|
+
const authMiddleware = defineMiddleware(async (event) => {
|
|
389
|
+
// assume you did something to get the user from the request
|
|
390
|
+
event.context.user = {
|
|
391
|
+
id: 1,
|
|
392
|
+
name: "John Doe",
|
|
393
|
+
email: "johndoe@gmail.com",
|
|
394
|
+
};
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
app.rpc("sayHello", {
|
|
398
|
+
params: undefined,
|
|
399
|
+
response: a.object({
|
|
400
|
+
message: a.string(),
|
|
401
|
+
}),
|
|
402
|
+
// user is available here
|
|
403
|
+
handler({ user }) {
|
|
404
|
+
return {
|
|
405
|
+
message: `Hello ${user.name}`,
|
|
406
|
+
};
|
|
407
|
+
},
|
|
408
|
+
});
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
To get type safety for these new properties create a `.d.ts` file and augment the `ArriEventContext` provided by `@arri/server`
|
|
412
|
+
|
|
413
|
+
```ts
|
|
414
|
+
import "@arri/server";
|
|
415
|
+
|
|
416
|
+
declare module "@arri/server" {
|
|
417
|
+
interface ArriEventContext {
|
|
418
|
+
user?: {
|
|
419
|
+
id: number;
|
|
420
|
+
name: string;
|
|
421
|
+
email: string;
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
### Adding Client Generators
|
|
428
|
+
|
|
429
|
+
Right now Arri RPC has client generators for the following languages:
|
|
430
|
+
|
|
431
|
+
- typescript
|
|
432
|
+
- dart
|
|
433
|
+
- kotlin
|
|
434
|
+
|
|
435
|
+
```ts
|
|
436
|
+
// arri.config.ts
|
|
437
|
+
import { defineConfig, typescriptClientGenerator, dartClientGenerator, kotlinClientGenerator } from "arri";
|
|
438
|
+
|
|
439
|
+
export default defineConfig({
|
|
440
|
+
// rest of config
|
|
441
|
+
clientGenerators: [
|
|
442
|
+
typescriptClientGenerator({...}),
|
|
443
|
+
dartClientGenerator({...}),
|
|
444
|
+
kotlinClientGenerator({...})
|
|
445
|
+
]
|
|
446
|
+
});
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
## Key Concepts
|
|
450
|
+
|
|
451
|
+
### Arri Definition File
|
|
452
|
+
|
|
453
|
+
The server generates a `__definition.json` file that acts as a schema for all of the procedures and models in the API. By default this schema is viewable from `/__definition` when the server is running, but it can be modified. The endpoint is also relative to the `rpcRoutePrefix` option.
|
|
454
|
+
|
|
455
|
+
It looks something like this:
|
|
456
|
+
|
|
457
|
+
```json
|
|
458
|
+
{
|
|
459
|
+
"procedures": {
|
|
460
|
+
"sayHello": {
|
|
461
|
+
"transport": "http",
|
|
462
|
+
"path": "/say-hello",
|
|
463
|
+
"method": "post",
|
|
464
|
+
"params": "SayHelloParams",
|
|
465
|
+
"response": "SayHelloResponse"
|
|
466
|
+
}
|
|
467
|
+
// rest of procedures
|
|
468
|
+
},
|
|
469
|
+
"definitions": {
|
|
470
|
+
"SayHelloParams": {
|
|
471
|
+
"properties": {
|
|
472
|
+
"name": {
|
|
473
|
+
"type": "string"
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
},
|
|
477
|
+
"SayHelloResponse": {
|
|
478
|
+
"properties": {
|
|
479
|
+
"message": {
|
|
480
|
+
"type": "string"
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
// rest of models
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
```
|
|
488
|
+
|
|
489
|
+
Arri is able to use this schema file to automatically generate clients in multiple languages. In this way it works similarly to an Open API schema, but with much better code-generation support. I've made a lot of deliberate choices in designing this schema to make code-generation easier and more consistent across languages. For example, Arri schemas use a superset of [JSON Type Definition](https://jsontypedef.com/) for their models instead of JSON Schema.
|
|
490
|
+
|
|
491
|
+
### How Procedures Map To Endpoints
|
|
492
|
+
|
|
493
|
+
Every procedure maps to a different url based on it's name. For example given the following file structure:
|
|
494
|
+
|
|
495
|
+
```fs
|
|
496
|
+
|--src
|
|
497
|
+
|--procedures
|
|
498
|
+
|--getStatus.rpc.ts
|
|
499
|
+
|--users
|
|
500
|
+
|--getUser.rpc.ts
|
|
501
|
+
|--updateUser.rpc.ts
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
We will get the following endpoints:
|
|
505
|
+
|
|
506
|
+
```txt
|
|
507
|
+
POST /get-status
|
|
508
|
+
POST /users/get-user
|
|
509
|
+
POST /users/update-user
|
|
510
|
+
|
|
511
|
+
(Note: these will always be relative to the `rpcRoutePrefix` option)
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
By default all procedures will become post requests, but you can change this when creating a procedure:
|
|
515
|
+
|
|
516
|
+
```ts
|
|
517
|
+
// procedures/users/getUser.rpc.ts
|
|
518
|
+
export default defineRpc({
|
|
519
|
+
method: "get",
|
|
520
|
+
// rest of config
|
|
521
|
+
});
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
The supported HTTP methods are as follows:
|
|
525
|
+
|
|
526
|
+
- post
|
|
527
|
+
- get
|
|
528
|
+
- delete
|
|
529
|
+
- patch
|
|
530
|
+
- put
|
|
531
|
+
|
|
532
|
+
When using a get method the RPC params will be mapped as query parameters which will be coerced into their type using the `a.coerce` method from `arri-validate`. Get methods support all basic scalar types however arrays and nested objects are not supported.
|
|
533
|
+
|
|
534
|
+
### H3 Support
|
|
535
|
+
|
|
536
|
+
Arri is built on top of [H3](https://h3.unjs.io/utils/request#getrequestipevent) so many of the concepts that apply to H3 also apply to Arri.
|
|
537
|
+
|
|
538
|
+
#### Accessing Utilities
|
|
539
|
+
|
|
540
|
+
Arri re-eports all of the H3 utilities.
|
|
541
|
+
|
|
542
|
+
```ts
|
|
543
|
+
import { getRequestIP, setResponseHeader } from "@arrirpc/server";
|
|
544
|
+
```
|
|
545
|
+
|
|
546
|
+
#### Accessing H3 Events
|
|
547
|
+
|
|
548
|
+
You can access H3 events from inside procedures handlers.
|
|
549
|
+
|
|
550
|
+
```ts
|
|
551
|
+
defineRpc({
|
|
552
|
+
params: undefined,
|
|
553
|
+
response: undefined,
|
|
554
|
+
handler(_, event) {
|
|
555
|
+
getRequestIP(event);
|
|
556
|
+
}
|
|
557
|
+
)
|
|
558
|
+
|
|
559
|
+
defineEventStreamRpc({
|
|
560
|
+
params: undefined,
|
|
561
|
+
response: undefined,
|
|
562
|
+
handler(_, event) {
|
|
563
|
+
getRequestIP(event);
|
|
564
|
+
}
|
|
565
|
+
)
|
|
566
|
+
```
|
|
567
|
+
|
|
568
|
+
## Arri CLI
|
|
569
|
+
|
|
570
|
+
```bash
|
|
571
|
+
# start the dev server
|
|
572
|
+
arri dev [flags]
|
|
573
|
+
|
|
574
|
+
# create a production build
|
|
575
|
+
arri build [flags]
|
|
576
|
+
|
|
577
|
+
# create a new project
|
|
578
|
+
arri init [dir]
|
|
579
|
+
|
|
580
|
+
# run codegen
|
|
581
|
+
arri codegen [path-to-definition-file]
|
|
582
|
+
```
|
|
583
|
+
|
|
584
|
+
## Development
|
|
585
|
+
|
|
586
|
+
### Building
|
|
587
|
+
|
|
588
|
+
Run `nx build arri-rpc` to build the library.
|
|
589
|
+
|
|
590
|
+
### Running unit tests
|
|
591
|
+
|
|
592
|
+
Run `nx test arri-rpc` to execute the unit tests via Vitest.
|