arri 0.0.15 → 0.0.17

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.
Files changed (64) hide show
  1. package/dist/chunks/index.cjs +8836 -65
  2. package/dist/chunks/index.mjs +8817 -66
  3. package/dist/cli.cjs +16563 -52524
  4. package/dist/cli.mjs +16529 -52475
  5. package/dist/codegen.cjs +308 -31
  6. package/dist/codegen.d.cts +23 -15
  7. package/dist/codegen.d.mts +23 -15
  8. package/dist/codegen.d.ts +23 -15
  9. package/dist/codegen.mjs +274 -17
  10. package/dist/index.cjs +47 -21
  11. package/dist/index.d.cts +19 -2
  12. package/dist/index.d.mts +19 -2
  13. package/dist/index.d.ts +19 -2
  14. package/dist/index.mjs +30 -1
  15. package/dist/shared/arri.234caf5e.cjs +655 -0
  16. package/dist/shared/arri.38792eb4.cjs +850 -0
  17. package/dist/shared/arri.4d2ea869.mjs +641 -0
  18. package/dist/shared/arri.54e67ffe.mjs +811 -0
  19. package/dist/shared/{arri.bb7e9a4e.d.ts → arri.b176bba1.d.cts} +61 -13
  20. package/dist/shared/{arri.bb7e9a4e.d.cts → arri.b176bba1.d.mts} +61 -13
  21. package/dist/shared/{arri.bb7e9a4e.d.mts → arri.b176bba1.d.ts} +61 -13
  22. package/package.json +1 -1
  23. package/dist/chunks/acorn.cjs +0 -18
  24. package/dist/chunks/acorn.mjs +0 -15
  25. package/dist/chunks/angular.cjs +0 -7
  26. package/dist/chunks/angular.mjs +0 -4
  27. package/dist/chunks/babel.cjs +0 -21
  28. package/dist/chunks/babel.mjs +0 -18
  29. package/dist/chunks/browser.cjs +0 -21
  30. package/dist/chunks/browser.mjs +0 -19
  31. package/dist/chunks/estree.cjs +0 -42
  32. package/dist/chunks/estree.mjs +0 -37
  33. package/dist/chunks/flow.cjs +0 -26
  34. package/dist/chunks/flow.mjs +0 -23
  35. package/dist/chunks/glimmer.cjs +0 -37
  36. package/dist/chunks/glimmer.mjs +0 -32
  37. package/dist/chunks/graphql.cjs +0 -37
  38. package/dist/chunks/graphql.mjs +0 -31
  39. package/dist/chunks/html.cjs +0 -27
  40. package/dist/chunks/html.mjs +0 -21
  41. package/dist/chunks/index2.cjs +0 -231
  42. package/dist/chunks/index2.mjs +0 -220
  43. package/dist/chunks/index3.cjs +0 -1822
  44. package/dist/chunks/index3.mjs +0 -1812
  45. package/dist/chunks/index4.cjs +0 -256
  46. package/dist/chunks/index4.mjs +0 -247
  47. package/dist/chunks/index5.cjs +0 -8854
  48. package/dist/chunks/index5.mjs +0 -8832
  49. package/dist/chunks/markdown.cjs +0 -67
  50. package/dist/chunks/markdown.mjs +0 -61
  51. package/dist/chunks/meriyah.cjs +0 -10
  52. package/dist/chunks/meriyah.mjs +0 -7
  53. package/dist/chunks/postcss.cjs +0 -60
  54. package/dist/chunks/postcss.mjs +0 -54
  55. package/dist/chunks/typescript.cjs +0 -30
  56. package/dist/chunks/typescript.mjs +0 -27
  57. package/dist/chunks/xdg-open.cjs +0 -1071
  58. package/dist/chunks/xdg-open.mjs +0 -1069
  59. package/dist/chunks/yaml.cjs +0 -169
  60. package/dist/chunks/yaml.mjs +0 -163
  61. package/dist/shared/arri.9717f131.cjs +0 -29694
  62. package/dist/shared/arri.bfebed73.cjs +0 -8711
  63. package/dist/shared/arri.dab1f9df.mjs +0 -8670
  64. package/dist/shared/arri.e335c10b.mjs +0 -29668
@@ -0,0 +1,850 @@
1
+ 'use strict';
2
+
3
+ const typebox = require('@sinclair/typebox');
4
+ const h3 = require('h3');
5
+ const errors = require('@sinclair/typebox/errors');
6
+ const value = require('@sinclair/typebox/value');
7
+
8
+ const ErrorResponse = typebox.Type.Object(
9
+ {
10
+ statusCode: typebox.Type.Integer(),
11
+ statusMessage: typebox.Type.String(),
12
+ stack: typebox.Type.Array(typebox.Type.Any()),
13
+ data: typebox.Type.Optional(typebox.Type.Object({}, { additionalProperties: true }))
14
+ },
15
+ { $id: "ErrorResponse" }
16
+ );
17
+ function defineError(statusCode, input = {}) {
18
+ const defaultVals = errorResponseDefaults[statusCode];
19
+ return h3.createError({
20
+ statusCode,
21
+ statusMessage: input.statusMessage ?? defaultVals.message ?? "An unknown error occurred",
22
+ stack: input.stack ?? "",
23
+ data: input.data
24
+ });
25
+ }
26
+ function errorResponseFromValidationErrors(errors, prefixText = `Missing or invalid parameters`) {
27
+ const errorParts = [];
28
+ for (const err of errors) {
29
+ const propName = err.path.split("/");
30
+ propName.shift();
31
+ if (!errorParts.includes(propName.join("."))) {
32
+ errorParts.push(propName.join("."));
33
+ }
34
+ }
35
+ throw defineError(400, {
36
+ statusMessage: `${prefixText}: [${errorParts.join(",")}]`,
37
+ data: errors
38
+ });
39
+ }
40
+ const errorResponseDefaults = {
41
+ 400: {
42
+ name: "BAD_REQUEST",
43
+ message: "Bad Request"
44
+ },
45
+ 401: {
46
+ name: "UNAUTHORIZED",
47
+ message: "Unauthorized"
48
+ },
49
+ 402: {
50
+ name: "PAYMENT_REQUIRED",
51
+ message: "Payment Required"
52
+ },
53
+ 403: {
54
+ name: "FORBIDDEN",
55
+ message: "Forbidden"
56
+ },
57
+ 404: {
58
+ name: "NOT_FOUND",
59
+ message: "Not Found"
60
+ },
61
+ 405: {
62
+ name: "METHOD_NOT_ALLOWED",
63
+ message: "Method Not Allowed"
64
+ },
65
+ 406: {
66
+ name: "NOT_ACCEPTABLE",
67
+ message: "Not Acceptable"
68
+ },
69
+ 407: {
70
+ name: "PROXY_AUTHENTICATION_REQUIRED",
71
+ message: "Proxy Authentication Required"
72
+ },
73
+ 408: {
74
+ name: "REQUEST_TIMEOUT",
75
+ message: "Request Timeout"
76
+ },
77
+ 409: {
78
+ name: "CONFLICT",
79
+ message: "Conflict"
80
+ },
81
+ 410: {
82
+ name: "GONE",
83
+ message: "Gone"
84
+ },
85
+ 411: {
86
+ name: "LENGTH_REQUIRED",
87
+ message: "Length Required"
88
+ },
89
+ 412: {
90
+ name: "PRECONDITION_FAILED",
91
+ message: "Precondition Failed"
92
+ },
93
+ 413: {
94
+ name: "REQUEST_TOO_LONG",
95
+ message: "Request Entity Too Large"
96
+ },
97
+ 414: {
98
+ name: "REQUEST_URI_TOO_LONG",
99
+ message: "Request-URI Too Long"
100
+ },
101
+ 415: {
102
+ name: "UNSUPPORTED_MEDIA_TYPE",
103
+ message: "Unsupported Media Type"
104
+ },
105
+ 416: {
106
+ name: "REQUESTED_RANGE_NOT_SATISFIABLE",
107
+ message: "Requested Range Not Satisfiable"
108
+ },
109
+ 417: {
110
+ name: "EXPECTATION_FAILED",
111
+ message: "Expectation Failed"
112
+ },
113
+ 418: {
114
+ name: "IM_A_TEAPOT",
115
+ message: "I'm a teapot"
116
+ },
117
+ 419: {
118
+ name: "INSUFFICIENT_SPACE_ON_RESOURCE",
119
+ message: "Insufficient Space on Resource"
120
+ },
121
+ 420: {
122
+ name: "METHOD_FAILURE",
123
+ message: "Method Failure"
124
+ },
125
+ 421: {
126
+ name: "MISDIRECTED_REQUEST",
127
+ message: "Misdirected Request"
128
+ },
129
+ 422: {
130
+ name: "UNPROCESSABLE_ENTITY",
131
+ message: "Unprocessable Entity"
132
+ },
133
+ 423: {
134
+ name: "LOCKED",
135
+ message: "Locked"
136
+ },
137
+ 424: {
138
+ name: "FAILED_DEPENDENCY",
139
+ message: "Failed Dependency"
140
+ },
141
+ 428: {
142
+ name: "PRECONDITION_REQUIRED",
143
+ message: "Precondition Required"
144
+ },
145
+ 429: {
146
+ name: "TOO_MANY_REQUESTS",
147
+ message: "Too Many Requests"
148
+ },
149
+ 431: {
150
+ name: "REQUEST_HEADER_FIELDS_TOO_LARGE",
151
+ message: "Request Header Fields Too Large"
152
+ },
153
+ 451: {
154
+ name: "UNAVAILABLE_FOR_LEGAL_REASONS",
155
+ message: "Unavailable For Legal Reasons"
156
+ },
157
+ 500: {
158
+ name: "INTERNAL_SERVER_ERROR",
159
+ message: "Internal Server Error"
160
+ },
161
+ 501: {
162
+ name: "NOT_IMPLEMENTED",
163
+ message: "Not Implemented"
164
+ },
165
+ 502: {
166
+ name: "BAD_GATEWAY",
167
+ message: "Bad Gateway"
168
+ },
169
+ 503: {
170
+ name: "SERVICE_UNAVAILABLE",
171
+ message: "Service Unavailable"
172
+ },
173
+ 504: {
174
+ name: "GATEWAY_TIMEOUT",
175
+ message: "Gateway Timeout"
176
+ },
177
+ 505: {
178
+ name: "HTTP_VERSION_NOT_SUPPORTED",
179
+ message: "HTTP Version Not Supported"
180
+ },
181
+ 507: {
182
+ name: "INSUFFICIENT_STORAGE",
183
+ message: "Insufficient Storage"
184
+ },
185
+ 511: {
186
+ name: "NETWORK_AUTHENTICATION_REQUIRED",
187
+ message: "Network Authentication Required"
188
+ }
189
+ };
190
+
191
+ const NUMBER_CHAR_RE = /\d/;
192
+ const STR_SPLITTERS = ["-", "_", "/", "."];
193
+ function isUppercase(char = "") {
194
+ if (NUMBER_CHAR_RE.test(char)) {
195
+ return void 0;
196
+ }
197
+ return char.toUpperCase() === char;
198
+ }
199
+ function splitByCase(string_, separators) {
200
+ const splitters = separators ?? STR_SPLITTERS;
201
+ const parts = [];
202
+ if (!string_ || typeof string_ !== "string") {
203
+ return parts;
204
+ }
205
+ let buff = "";
206
+ let previousUpper;
207
+ let previousSplitter;
208
+ for (const char of string_) {
209
+ const isSplitter = splitters.includes(char);
210
+ if (isSplitter === true) {
211
+ parts.push(buff);
212
+ buff = "";
213
+ previousUpper = void 0;
214
+ continue;
215
+ }
216
+ const isUpper = isUppercase(char);
217
+ if (previousSplitter === false) {
218
+ if (previousUpper === false && isUpper === true) {
219
+ parts.push(buff);
220
+ buff = char;
221
+ previousUpper = isUpper;
222
+ continue;
223
+ }
224
+ if (previousUpper === true && isUpper === false && buff.length > 1) {
225
+ const lastChar = buff[buff.length - 1];
226
+ parts.push(buff.slice(0, Math.max(0, buff.length - 1)));
227
+ buff = lastChar + char;
228
+ previousUpper = isUpper;
229
+ continue;
230
+ }
231
+ }
232
+ buff += char;
233
+ previousUpper = isUpper;
234
+ previousSplitter = isSplitter;
235
+ }
236
+ parts.push(buff);
237
+ return parts;
238
+ }
239
+ function upperFirst(string_) {
240
+ return !string_ ? "" : string_[0].toUpperCase() + string_.slice(1);
241
+ }
242
+ function lowerFirst(string_) {
243
+ return !string_ ? "" : string_[0].toLowerCase() + string_.slice(1);
244
+ }
245
+ function pascalCase(string_) {
246
+ return !string_ ? "" : (Array.isArray(string_) ? string_ : splitByCase(string_)).map((p) => upperFirst(p)).join("");
247
+ }
248
+ function camelCase(string_) {
249
+ return lowerFirst(pascalCase(string_));
250
+ }
251
+ function kebabCase(string_, joiner) {
252
+ return !string_ ? "" : (Array.isArray(string_) ? string_ : splitByCase(string_)).map((p) => p.toLowerCase()).join(joiner ?? "-");
253
+ }
254
+
255
+ function normalizeWhitespace(input) {
256
+ if (input.includes("\n\n")) {
257
+ return normalizeWhitespace(input.split("\n\n").join("\n"));
258
+ }
259
+ const lines = [];
260
+ for (const line of input.split("\n")) {
261
+ lines.push(line.trim());
262
+ }
263
+ const result = lines.join("\n").trim();
264
+ if (result.includes("\n\n")) {
265
+ return normalizeWhitespace(result.split("\n\n").join("\n"));
266
+ }
267
+ return result;
268
+ }
269
+ function isProcedureDef(input) {
270
+ if (typeof input !== "object") {
271
+ return false;
272
+ }
273
+ const expectedKeys = ["path", "method"];
274
+ const existingKeys = Object.keys(input);
275
+ for (const key of expectedKeys) {
276
+ if (!existingKeys.includes(key)) {
277
+ return false;
278
+ }
279
+ }
280
+ return isHttpMethod(input.method) && typeof input.path === "string" && (typeof input.params === "string" || typeof input.params === "undefined") && (typeof input.response === "string" || typeof input.response === "undefined");
281
+ }
282
+ function isServiceDef(input) {
283
+ if (typeof input !== "object") {
284
+ return false;
285
+ }
286
+ for (const key of Object.keys(input)) {
287
+ if (typeof input[key] !== "object") {
288
+ return false;
289
+ }
290
+ }
291
+ return true;
292
+ }
293
+ function isApplicationDef(input) {
294
+ if (typeof input !== "object") {
295
+ return false;
296
+ }
297
+ if (!("procedures" in input) || typeof input.procedures !== "object") {
298
+ return false;
299
+ }
300
+ if (!("models" in input) || typeof input.models !== "object") {
301
+ return false;
302
+ }
303
+ if (!("errors" in input) || typeof input.errors !== "object") {
304
+ return false;
305
+ }
306
+ return true;
307
+ }
308
+ function unflattenObject(data) {
309
+ if (Object(data) !== data || Array.isArray(data))
310
+ return data;
311
+ const regex = /\.?([^.[\]]+)|\[(\d+)\]/g;
312
+ const result = {};
313
+ for (const p in data) {
314
+ let cur = result;
315
+ let prop = "";
316
+ let m;
317
+ while (m = regex.exec(p)) {
318
+ cur = cur[prop] || (cur[prop] = m[2] ? [] : {});
319
+ prop = m[2] || m[1];
320
+ }
321
+ cur[prop] = data[p];
322
+ }
323
+ return result[""] || result;
324
+ }
325
+ function unflattenProcedures(procedures) {
326
+ return unflattenObject(procedures);
327
+ }
328
+ function setNestedObjectProperty(targetProp, value, object) {
329
+ const parts = targetProp.split(".");
330
+ let current = object;
331
+ for (let i = 0; i < parts.length; i++) {
332
+ const key = parts[i];
333
+ if (i === parts.length - 1) {
334
+ current[key] = value;
335
+ } else {
336
+ if (!current[key]) {
337
+ current[key] = {};
338
+ }
339
+ current = current[key];
340
+ }
341
+ }
342
+ return object;
343
+ }
344
+ const removeDisallowedChars = (input, disallowedChars) => {
345
+ let result = input;
346
+ for (const char of disallowedChars) {
347
+ if (result.includes(char)) {
348
+ result = result.split(char).join("");
349
+ }
350
+ }
351
+ return result;
352
+ };
353
+ const JsonSchemaScalarTypeValues = [
354
+ "integer",
355
+ "number",
356
+ "bigint",
357
+ "string",
358
+ "boolean",
359
+ "Date"
360
+ ];
361
+ const JsonSchemaNullTypeValues = ["null", "undefined"];
362
+ const JsonSchemaComplexTypeValues = [
363
+ "object",
364
+ "list",
365
+ "array",
366
+ "Uint8Array"
367
+ ];
368
+ function isJsonSchemaScalarType(input) {
369
+ if (typeof input !== "object") {
370
+ return false;
371
+ }
372
+ if (!("type" in input)) {
373
+ return false;
374
+ }
375
+ return JsonSchemaScalarTypeValues.includes(input.type);
376
+ }
377
+ function isJsonSchemaNullType(input) {
378
+ if (typeof input !== "object") {
379
+ return false;
380
+ }
381
+ if (!("type" in input)) {
382
+ return false;
383
+ }
384
+ return JsonSchemaNullTypeValues.includes(input.type);
385
+ }
386
+ const isJsonSchemaObject = (input) => {
387
+ if (typeof input !== "object") {
388
+ return false;
389
+ }
390
+ return "type" in input && input.type === "object" && "properties" in input && typeof input.properties === "object";
391
+ };
392
+ function isJsonSchemaArray(input) {
393
+ if (typeof input !== "object") {
394
+ return false;
395
+ }
396
+ if ("type" in input && input.type === "array") {
397
+ return true;
398
+ }
399
+ return false;
400
+ }
401
+ function isJsonSchemaEnum(input) {
402
+ if (typeof input !== "object") {
403
+ return false;
404
+ }
405
+ if (!("anyOf" in input) || !Array.isArray(input.anyOf)) {
406
+ return false;
407
+ }
408
+ let prevType;
409
+ for (const item of input.anyOf) {
410
+ if (!isJsonSchemaScalarType(item)) {
411
+ return false;
412
+ }
413
+ if (item.type !== "string" && item.type !== "integer" && item.type !== "number") {
414
+ return false;
415
+ }
416
+ if (!prevType) {
417
+ prevType = item.type;
418
+ } else if (prevType !== item.type) {
419
+ return false;
420
+ }
421
+ }
422
+ return true;
423
+ }
424
+
425
+ function typeboxSafeValidate(schema, coerce = false) {
426
+ const fn = (input) => {
427
+ const finalInput = coerce ? value.Value.Convert(schema, input) : input;
428
+ if (value.Value.Check(schema, finalInput)) {
429
+ return { success: true, value: finalInput };
430
+ }
431
+ const errs = [...value.Value.Errors(schema, finalInput)];
432
+ return {
433
+ success: false,
434
+ errors: errs
435
+ };
436
+ };
437
+ return fn;
438
+ }
439
+
440
+ function isRpc(input) {
441
+ if (typeof input !== "object") {
442
+ return false;
443
+ }
444
+ const anyInput = input;
445
+ if (!isHttpMethod(anyInput.method)) {
446
+ return false;
447
+ }
448
+ if (typeof anyInput.handler !== "function") {
449
+ return false;
450
+ }
451
+ return true;
452
+ }
453
+ function defineRpc(config) {
454
+ return config;
455
+ }
456
+ function createRpcDefinition(rpcName, httpPath, procedure) {
457
+ return {
458
+ description: procedure.description,
459
+ path: httpPath,
460
+ method: procedure.method ?? "post",
461
+ params: getRpcParamDefinition(rpcName, procedure),
462
+ response: getRpcResponseDefinition(rpcName, procedure)
463
+ };
464
+ }
465
+ function getRpcPath(rpcName, prefix = "") {
466
+ const path = rpcName.split(".").map(
467
+ (part) => removeDisallowedChars(
468
+ kebabCase(part),
469
+ `!@#$%^&*()+=[]{}|\\;:'"<>,./?`
470
+ )
471
+ ).join("/");
472
+ const finalPath = prefix ? `/${prefix}/${path}` : `/${path}`;
473
+ return finalPath;
474
+ }
475
+ function getRpcParamName(rpcName, procedure) {
476
+ if (!procedure.params) {
477
+ return null;
478
+ }
479
+ const nameParts = rpcName.split(".").map(
480
+ (part) => removeDisallowedChars(part, `!@#$%^&*()+=[]{}|\\;:'"<>,./?`)
481
+ );
482
+ const paramName = procedure.params.$id ?? pascalCase(`${nameParts.join(`_`)}_params`);
483
+ return paramName;
484
+ }
485
+ function getRpcParamDefinition(rpcName, procedure) {
486
+ if (!procedure.params) {
487
+ return void 0;
488
+ }
489
+ const name = getRpcParamName(rpcName, procedure);
490
+ if (!name) {
491
+ return void 0;
492
+ }
493
+ return name;
494
+ }
495
+ function getRpcResponseName(rpcName, procedure) {
496
+ if (!procedure.response) {
497
+ return null;
498
+ }
499
+ if (typebox.TypeGuard.TObject(procedure.response)) {
500
+ const nameParts = rpcName.split(".").map(
501
+ (part) => removeDisallowedChars(part, `!@#$%^&*()+=[]{}|\\;:'"<>,./?`)
502
+ );
503
+ const responseName = procedure.response.$id ?? pascalCase(`${nameParts.join("_")}_response`);
504
+ return responseName;
505
+ }
506
+ return null;
507
+ }
508
+ function getRpcResponseDefinition(rpcName, procedure) {
509
+ if (!procedure.response) {
510
+ return void 0;
511
+ }
512
+ const name = getRpcResponseName(rpcName, procedure);
513
+ if (!name) {
514
+ return void 0;
515
+ }
516
+ return name;
517
+ }
518
+ function registerRpc(router, path, procedure, middleware) {
519
+ const httpMethod = procedure.method ?? "post";
520
+ const handler = h3.eventHandler(async (event) => {
521
+ const context = {
522
+ event
523
+ };
524
+ if (h3.isPreflightRequest(event)) {
525
+ return "ok";
526
+ }
527
+ if (middleware.length) {
528
+ await Promise.all(middleware.map((m) => m(context)));
529
+ }
530
+ if (procedure.params) {
531
+ switch (httpMethod) {
532
+ case "get":
533
+ case "head": {
534
+ const parsedParams = await h3.getValidatedQuery(
535
+ event,
536
+ typeboxSafeValidate(procedure.params, true)
537
+ );
538
+ if (!parsedParams.success) {
539
+ const errorParts = [];
540
+ for (const err of parsedParams.errors) {
541
+ const propName = err.path.split("/");
542
+ propName.shift();
543
+ if (!errorParts.includes(propName.join("."))) {
544
+ errorParts.push(propName.join("."));
545
+ }
546
+ }
547
+ throw defineError(400, {
548
+ statusMessage: `Missing or invalid url query parameters: [${errorParts.join(
549
+ ","
550
+ )}]`,
551
+ data: parsedParams.errors
552
+ });
553
+ }
554
+ context.params = parsedParams.value;
555
+ break;
556
+ }
557
+ case "delete":
558
+ case "patch":
559
+ case "post":
560
+ case "put": {
561
+ const parsedParams = await h3.readValidatedBody(
562
+ event,
563
+ typeboxSafeValidate(procedure.params)
564
+ );
565
+ if (!parsedParams.success) {
566
+ let isObjectError = false;
567
+ const errorParts = [];
568
+ for (const err of parsedParams.errors) {
569
+ if (err.type === errors.ValueErrorType.Object) {
570
+ isObjectError = true;
571
+ }
572
+ const propName = err.path.split("/");
573
+ propName.shift();
574
+ if (!errorParts.includes(propName.join("."))) {
575
+ errorParts.push(propName.join("."));
576
+ }
577
+ }
578
+ if (isObjectError) {
579
+ throw defineError(400, {
580
+ statusMessage: `Invalid request body. Expected object.`
581
+ });
582
+ }
583
+ throw defineError(400, {
584
+ statusMessage: `Invalid request body. Affected properties [${errorParts.join(
585
+ ", "
586
+ )}]`,
587
+ data: parsedParams.errors
588
+ });
589
+ }
590
+ context.params = parsedParams.value;
591
+ break;
592
+ }
593
+ }
594
+ }
595
+ const response = await procedure.handler(context);
596
+ if (typeof response === "object") {
597
+ h3.setResponseHeader(event, "Content-Type", "application/json");
598
+ await h3.send(event, JSON.stringify(response));
599
+ } else {
600
+ await h3.send(event, response);
601
+ }
602
+ if (procedure.postHandler) {
603
+ context.response = response;
604
+ await procedure.postHandler(context);
605
+ }
606
+ return null;
607
+ });
608
+ switch (httpMethod) {
609
+ case "get":
610
+ router.get(path, handler);
611
+ break;
612
+ case "head":
613
+ router.head(path, handler);
614
+ break;
615
+ case "delete":
616
+ router.delete(path, handler);
617
+ break;
618
+ case "patch":
619
+ router.patch(path, handler);
620
+ break;
621
+ case "put":
622
+ router.put(path, handler);
623
+ break;
624
+ case "post":
625
+ default:
626
+ router.post(path, handler);
627
+ break;
628
+ }
629
+ }
630
+
631
+ function defineRoute(config) {
632
+ return config;
633
+ }
634
+ const defineMiddleware = (middleware) => middleware;
635
+ function registerRoute(router, route, middleware, prefix) {
636
+ const handler = async (event) => {
637
+ const context = {
638
+ event
639
+ };
640
+ if (middleware.length) {
641
+ for (const m of middleware) {
642
+ await m(context);
643
+ }
644
+ }
645
+ if (route.query) {
646
+ const result = await h3.getValidatedQuery(
647
+ event,
648
+ typeboxSafeValidate(route.query)
649
+ );
650
+ if (!result.success) {
651
+ throw errorResponseFromValidationErrors(
652
+ result.errors,
653
+ "Missing or invalid query parameters"
654
+ );
655
+ }
656
+ context.query = result.value;
657
+ }
658
+ if (route.body && route.method !== "get" && route.method !== "head") {
659
+ const result = await h3.readValidatedBody(
660
+ event,
661
+ typeboxSafeValidate(route.body)
662
+ );
663
+ if (!result.success) {
664
+ throw errorResponseFromValidationErrors(
665
+ result.errors,
666
+ "Missing or invalid body parameters"
667
+ );
668
+ }
669
+ context.body = result.value;
670
+ }
671
+ const response = await route.handler(context);
672
+ if (typeof response === "object") {
673
+ h3.setResponseHeader(event, "Content-Type", "application/json");
674
+ await h3.send(event, JSON.stringify(response));
675
+ } else {
676
+ await h3.send(event, response);
677
+ }
678
+ if (route.postHandler) {
679
+ context.response = response;
680
+ await route.postHandler(context);
681
+ }
682
+ return null;
683
+ };
684
+ const finalPath = (prefix ? `/${prefix}${route.path}` : route.path).split("//").join("/");
685
+ switch (route.method) {
686
+ case "get":
687
+ router.get(finalPath, handler);
688
+ break;
689
+ case "head":
690
+ router.head(finalPath, handler);
691
+ break;
692
+ case "delete":
693
+ router.delete(finalPath, handler);
694
+ break;
695
+ case "patch":
696
+ router.patch(finalPath, handler);
697
+ break;
698
+ case "post":
699
+ router.post(finalPath, handler);
700
+ break;
701
+ case "put":
702
+ router.put(finalPath, handler);
703
+ break;
704
+ }
705
+ }
706
+
707
+ var __defProp = Object.defineProperty;
708
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
709
+ var __publicField = (obj, key, value) => {
710
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
711
+ return value;
712
+ };
713
+ const DEV_ENDPOINT_ROOT = `/__arri_dev__`;
714
+ const DEV_DEFINITION_ENDPOINT = `${DEV_ENDPOINT_ROOT}/definition`;
715
+ class Arri {
716
+ constructor(opts) {
717
+ __publicField(this, "__isArri__", true);
718
+ __publicField(this, "h3App");
719
+ __publicField(this, "h3Router", h3.createRouter());
720
+ __publicField(this, "rpcDefinitionPath");
721
+ __publicField(this, "rpcRoutePrefix");
722
+ __publicField(this, "appInfo");
723
+ __publicField(this, "procedures", {});
724
+ __publicField(this, "models", {});
725
+ __publicField(this, "middlewares", []);
726
+ this.appInfo = opts?.appInfo;
727
+ this.h3App = h3.createApp({
728
+ debug: opts?.debug,
729
+ onAfterResponse: opts?.onAfterResponse,
730
+ onBeforeResponse: opts?.onBeforeResponse,
731
+ onError: async (err, event) => {
732
+ if (opts?.onError) {
733
+ await opts.onError(err, event);
734
+ }
735
+ h3.sendError(event, err);
736
+ },
737
+ onRequest: async (event) => {
738
+ if (opts?.onRequest) {
739
+ await opts.onRequest(event);
740
+ }
741
+ }
742
+ });
743
+ this.rpcRoutePrefix = opts?.rpcRoutePrefix ?? "";
744
+ this.rpcDefinitionPath = opts?.rpcDefinitionPath ?? "__definition";
745
+ this.h3Router.get(
746
+ this.rpcRoutePrefix ? `/${this.rpcRoutePrefix}/${this.rpcDefinitionPath}`.split("//").join("/") : `/${this.rpcDefinitionPath}`,
747
+ h3.eventHandler((_) => this.getAppDefinition())
748
+ );
749
+ this.h3App.use(this.h3Router);
750
+ if (process.env.ARRI_DEV_MODE === "true") {
751
+ this.h3Router.get(
752
+ DEV_DEFINITION_ENDPOINT,
753
+ h3.eventHandler((_) => this.getAppDefinition())
754
+ );
755
+ }
756
+ }
757
+ registerMiddleware(middleware) {
758
+ this.middlewares.push(middleware);
759
+ }
760
+ registerRpc(name, procedure) {
761
+ const path = getRpcPath(name, this.rpcRoutePrefix);
762
+ this.procedures[name] = createRpcDefinition(name, path, procedure);
763
+ if (typebox.TypeGuard.TObject(procedure.params)) {
764
+ const paramName = getRpcParamName(name, procedure);
765
+ if (paramName) {
766
+ this.models[paramName] = procedure.params;
767
+ }
768
+ }
769
+ if (typebox.TypeGuard.TObject(procedure.response)) {
770
+ const responseName = getRpcResponseName(name, procedure);
771
+ if (responseName) {
772
+ this.models[responseName] = procedure.response;
773
+ }
774
+ }
775
+ registerRpc(this.h3Router, path, procedure, this.middlewares);
776
+ }
777
+ registerRoute(route) {
778
+ registerRoute(this.h3Router, route, this.middlewares);
779
+ }
780
+ getAppDefinition() {
781
+ const appDef = {
782
+ arriSchemaVersion: "0.0.1",
783
+ info: this.appInfo,
784
+ procedures: {},
785
+ models: this.models,
786
+ errors: ErrorResponse
787
+ };
788
+ Object.keys(this.procedures).forEach((key) => {
789
+ const rpc = this.procedures[key];
790
+ appDef.procedures[key] = rpc;
791
+ });
792
+ return appDef;
793
+ }
794
+ getH3Instance() {
795
+ return this.h3App;
796
+ }
797
+ }
798
+ const HttpMethodValues = [
799
+ "get",
800
+ "post",
801
+ "put",
802
+ "patch",
803
+ "delete",
804
+ "head"
805
+ ];
806
+ const isHttpMethod = (input) => {
807
+ if (typeof input !== "string") {
808
+ return false;
809
+ }
810
+ return HttpMethodValues.includes(input);
811
+ };
812
+
813
+ exports.Arri = Arri;
814
+ exports.DEV_DEFINITION_ENDPOINT = DEV_DEFINITION_ENDPOINT;
815
+ exports.DEV_ENDPOINT_ROOT = DEV_ENDPOINT_ROOT;
816
+ exports.ErrorResponse = ErrorResponse;
817
+ exports.HttpMethodValues = HttpMethodValues;
818
+ exports.JsonSchemaComplexTypeValues = JsonSchemaComplexTypeValues;
819
+ exports.JsonSchemaNullTypeValues = JsonSchemaNullTypeValues;
820
+ exports.JsonSchemaScalarTypeValues = JsonSchemaScalarTypeValues;
821
+ exports.camelCase = camelCase;
822
+ exports.createRpcDefinition = createRpcDefinition;
823
+ exports.defineError = defineError;
824
+ exports.defineMiddleware = defineMiddleware;
825
+ exports.defineRoute = defineRoute;
826
+ exports.defineRpc = defineRpc;
827
+ exports.errorResponseFromValidationErrors = errorResponseFromValidationErrors;
828
+ exports.getRpcParamDefinition = getRpcParamDefinition;
829
+ exports.getRpcParamName = getRpcParamName;
830
+ exports.getRpcPath = getRpcPath;
831
+ exports.getRpcResponseName = getRpcResponseName;
832
+ exports.isApplicationDef = isApplicationDef;
833
+ exports.isHttpMethod = isHttpMethod;
834
+ exports.isJsonSchemaArray = isJsonSchemaArray;
835
+ exports.isJsonSchemaEnum = isJsonSchemaEnum;
836
+ exports.isJsonSchemaNullType = isJsonSchemaNullType;
837
+ exports.isJsonSchemaObject = isJsonSchemaObject;
838
+ exports.isJsonSchemaScalarType = isJsonSchemaScalarType;
839
+ exports.isProcedureDef = isProcedureDef;
840
+ exports.isRpc = isRpc;
841
+ exports.isServiceDef = isServiceDef;
842
+ exports.kebabCase = kebabCase;
843
+ exports.normalizeWhitespace = normalizeWhitespace;
844
+ exports.pascalCase = pascalCase;
845
+ exports.registerRoute = registerRoute;
846
+ exports.registerRpc = registerRpc;
847
+ exports.removeDisallowedChars = removeDisallowedChars;
848
+ exports.setNestedObjectProperty = setNestedObjectProperty;
849
+ exports.unflattenObject = unflattenObject;
850
+ exports.unflattenProcedures = unflattenProcedures;