arri 0.0.28 → 0.0.29

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.
@@ -521,95 +521,110 @@ function getRpcResponseDefinition(rpcName, procedure) {
521
521
  }
522
522
  return name;
523
523
  }
524
- function registerRpc(router, path, procedure, middleware) {
524
+ function registerRpc(router, path, procedure, middleware, opts) {
525
525
  const httpMethod = procedure.method ?? "post";
526
526
  const handler = h3.eventHandler(async (event) => {
527
527
  const context = {
528
- event
528
+ type: "procedure",
529
+ params: void 0
529
530
  };
530
531
  if (h3.isPreflightRequest(event)) {
531
532
  return "ok";
532
533
  }
533
- if (middleware.length) {
534
- await Promise.all(middleware.map((m) => m(context)));
535
- }
536
- if (procedure.params) {
537
- switch (httpMethod) {
538
- case "get":
539
- case "head": {
540
- const parsedParams = await h3.getValidatedQuery(
541
- event,
542
- typeboxSafeValidate(procedure.params, true)
543
- );
544
- if (!parsedParams.success) {
545
- const errorParts = [];
546
- for (const err of parsedParams.errors) {
547
- const propName = err.path.split("/");
548
- propName.shift();
549
- if (!errorParts.includes(propName.join("."))) {
550
- errorParts.push(propName.join("."));
534
+ try {
535
+ if (middleware.length) {
536
+ await Promise.all(middleware.map((m) => m(context, event)));
537
+ }
538
+ if (procedure.params) {
539
+ switch (httpMethod) {
540
+ case "get":
541
+ case "head": {
542
+ const parsedParams = await h3.getValidatedQuery(
543
+ event,
544
+ typeboxSafeValidate(procedure.params, true)
545
+ );
546
+ if (!parsedParams.success) {
547
+ const errorParts = [];
548
+ for (const err of parsedParams.errors) {
549
+ const propName = err.path.split("/");
550
+ propName.shift();
551
+ if (!errorParts.includes(propName.join("."))) {
552
+ errorParts.push(propName.join("."));
553
+ }
551
554
  }
555
+ throw defineError(400, {
556
+ statusMessage: `Missing or invalid url query parameters: [${errorParts.join(
557
+ ","
558
+ )}]`,
559
+ data: parsedParams.errors
560
+ });
552
561
  }
553
- throw defineError(400, {
554
- statusMessage: `Missing or invalid url query parameters: [${errorParts.join(
555
- ","
556
- )}]`,
557
- data: parsedParams.errors
558
- });
562
+ context.params = parsedParams.value;
563
+ break;
559
564
  }
560
- context.params = parsedParams.value;
561
- break;
562
- }
563
- case "delete":
564
- case "patch":
565
- case "post":
566
- case "put": {
567
- const parsedParams = await h3.readValidatedBody(
568
- event,
569
- typeboxSafeValidate(procedure.params)
570
- );
571
- if (!parsedParams.success) {
572
- let isObjectError = false;
573
- const errorParts = [];
574
- for (const err of parsedParams.errors) {
575
- if (err.type === errors.ValueErrorType.Object) {
576
- isObjectError = true;
565
+ case "delete":
566
+ case "patch":
567
+ case "post":
568
+ case "put": {
569
+ const parsedParams = await h3.readValidatedBody(
570
+ event,
571
+ typeboxSafeValidate(procedure.params)
572
+ );
573
+ if (!parsedParams.success) {
574
+ let isObjectError = false;
575
+ const errorParts = [];
576
+ for (const err of parsedParams.errors) {
577
+ if (err.type === errors.ValueErrorType.Object) {
578
+ isObjectError = true;
579
+ }
580
+ const propName = err.path.split("/");
581
+ propName.shift();
582
+ if (!errorParts.includes(propName.join("."))) {
583
+ errorParts.push(propName.join("."));
584
+ }
577
585
  }
578
- const propName = err.path.split("/");
579
- propName.shift();
580
- if (!errorParts.includes(propName.join("."))) {
581
- errorParts.push(propName.join("."));
586
+ if (isObjectError) {
587
+ throw defineError(400, {
588
+ statusMessage: `Invalid request body. Expected object.`
589
+ });
582
590
  }
583
- }
584
- if (isObjectError) {
585
591
  throw defineError(400, {
586
- statusMessage: `Invalid request body. Expected object.`
592
+ statusMessage: `Invalid request body. Affected properties [${errorParts.join(
593
+ ", "
594
+ )}]`,
595
+ data: parsedParams.errors
587
596
  });
588
597
  }
589
- throw defineError(400, {
590
- statusMessage: `Invalid request body. Affected properties [${errorParts.join(
591
- ", "
592
- )}]`,
593
- data: parsedParams.errors
594
- });
598
+ context.params = parsedParams.value;
599
+ break;
595
600
  }
596
- context.params = parsedParams.value;
597
- break;
601
+ default:
602
+ break;
598
603
  }
599
604
  }
600
- }
601
- const response = await procedure.handler(context);
602
- if (typeof response === "object") {
603
- h3.setResponseHeader(event, "Content-Type", "application/json");
604
- await h3.send(event, JSON.stringify(response));
605
- } else {
606
- await h3.send(event, response);
607
- }
608
- if (procedure.postHandler) {
605
+ const response = await procedure.handler(context, event);
609
606
  context.response = response;
610
- await procedure.postHandler(context);
607
+ if (opts.onBeforeResponse) {
608
+ await opts.onBeforeResponse(context, event);
609
+ }
610
+ if (typeof response === "object") {
611
+ h3.setResponseHeader(event, "Content-Type", "application/json");
612
+ await h3.send(event, JSON.stringify(response));
613
+ } else {
614
+ await h3.send(event, response ?? "");
615
+ }
616
+ if (opts.onAfterResponse) {
617
+ await opts.onAfterResponse(context, event);
618
+ }
619
+ if (procedure.postHandler) {
620
+ await procedure.postHandler(context, event);
621
+ }
622
+ } catch (err) {
623
+ if (opts.onError) {
624
+ await opts.onError(err, context, event);
625
+ }
611
626
  }
612
- return null;
627
+ return "";
613
628
  });
614
629
  switch (httpMethod) {
615
630
  case "get":
@@ -638,55 +653,70 @@ function defineRoute(config) {
638
653
  return config;
639
654
  }
640
655
  const defineMiddleware = (middleware) => middleware;
641
- function registerRoute(router, route, middleware, prefix) {
642
- const handler = async (event) => {
656
+ function registerRoute(router, route, middleware, prefix, opts) {
657
+ const handler = h3.eventHandler(async (event) => {
643
658
  const context = {
644
- event
659
+ type: "route",
660
+ params: event.context.params,
661
+ query: void 0,
662
+ body: void 0
645
663
  };
646
- if (middleware.length) {
647
- for (const m of middleware) {
648
- await m(context);
664
+ try {
665
+ if (middleware.length) {
666
+ for (const m of middleware) {
667
+ await m(context, event);
668
+ }
649
669
  }
650
- }
651
- if (route.query) {
652
- const result = await h3.getValidatedQuery(
653
- event,
654
- typeboxSafeValidate(route.query)
655
- );
656
- if (!result.success) {
657
- throw errorResponseFromValidationErrors(
658
- result.errors,
659
- "Missing or invalid query parameters"
670
+ if (route.query) {
671
+ const result = await h3.getValidatedQuery(
672
+ event,
673
+ typeboxSafeValidate(route.query)
660
674
  );
675
+ if (!result.success) {
676
+ throw errorResponseFromValidationErrors(
677
+ result.errors,
678
+ "Missing or invalid query parameters"
679
+ );
680
+ }
681
+ context.query = result.value;
661
682
  }
662
- context.query = result.value;
663
- }
664
- if (route.body && route.method !== "get" && route.method !== "head") {
665
- const result = await h3.readValidatedBody(
666
- event,
667
- typeboxSafeValidate(route.body)
668
- );
669
- if (!result.success) {
670
- throw errorResponseFromValidationErrors(
671
- result.errors,
672
- "Missing or invalid body parameters"
683
+ if (route.body && route.method !== "get" && route.method !== "head") {
684
+ const result = await h3.readValidatedBody(
685
+ event,
686
+ typeboxSafeValidate(route.body)
673
687
  );
688
+ if (!result.success) {
689
+ throw errorResponseFromValidationErrors(
690
+ result.errors,
691
+ "Missing or invalid body parameters"
692
+ );
693
+ }
694
+ context.body = result.value;
674
695
  }
675
- context.body = result.value;
676
- }
677
- const response = await route.handler(context);
678
- if (typeof response === "object") {
679
- h3.setResponseHeader(event, "Content-Type", "application/json");
680
- await h3.send(event, JSON.stringify(response));
681
- } else {
682
- await h3.send(event, response);
683
- }
684
- if (route.postHandler) {
696
+ const response = await route.handler(context, event);
685
697
  context.response = response;
686
- await route.postHandler(context);
698
+ if (opts?.onBeforeResponse) {
699
+ await opts.onBeforeResponse(context, event);
700
+ }
701
+ if (typeof response === "object") {
702
+ h3.setResponseHeader(event, "Content-Type", "application/json");
703
+ await h3.send(event, JSON.stringify(response));
704
+ } else {
705
+ await h3.send(event, response ?? "");
706
+ }
707
+ if (opts?.onAfterResponse) {
708
+ await opts.onAfterResponse(context, event);
709
+ }
710
+ if (route.postHandler) {
711
+ await route.postHandler(context, event);
712
+ }
713
+ } catch (err) {
714
+ if (opts?.onError) {
715
+ await opts.onError(err, context, event);
716
+ }
687
717
  }
688
- return null;
689
- };
718
+ return "";
719
+ });
690
720
  const finalPath = (prefix ? `/${prefix}${route.path}` : route.path).split("//").join("/");
691
721
  switch (route.method) {
692
722
  case "get":
@@ -719,7 +749,7 @@ var __publicField = (obj, key, value) => {
719
749
  const DEV_ENDPOINT_ROOT = `/__arri_dev__`;
720
750
  const DEV_DEFINITION_ENDPOINT = `${DEV_ENDPOINT_ROOT}/definition`;
721
751
  class Arri {
722
- constructor(opts) {
752
+ constructor(opts = {}) {
723
753
  __publicField(this, "__isArri__", true);
724
754
  __publicField(this, "h3App");
725
755
  __publicField(this, "h3Router", h3.createRouter());
@@ -729,23 +759,17 @@ class Arri {
729
759
  __publicField(this, "procedures", {});
730
760
  __publicField(this, "models", {});
731
761
  __publicField(this, "middlewares", []);
762
+ __publicField(this, "onAfterResponse");
763
+ __publicField(this, "onBeforeResponse");
764
+ __publicField(this, "onError");
732
765
  this.appInfo = opts?.appInfo;
733
766
  this.h3App = h3.createApp({
734
767
  debug: opts?.debug,
735
- onAfterResponse: opts?.onAfterResponse,
736
- onBeforeResponse: opts?.onBeforeResponse,
737
- onError: async (err, event) => {
738
- if (opts?.onError) {
739
- await opts.onError(err, event);
740
- }
741
- h3.sendError(event, err);
742
- },
743
- onRequest: async (event) => {
744
- if (opts?.onRequest) {
745
- await opts.onRequest(event);
746
- }
747
- }
768
+ onRequest: opts?.onRequest
748
769
  });
770
+ this.onError = opts.onError;
771
+ this.onAfterResponse = opts.onAfterResponse;
772
+ this.onBeforeResponse = opts.onBeforeResponse;
749
773
  this.rpcRoutePrefix = opts?.rpcRoutePrefix ?? "";
750
774
  this.rpcDefinitionPath = opts?.rpcDefinitionPath ?? "__definition";
751
775
  this.h3Router.get(
@@ -778,7 +802,11 @@ class Arri {
778
802
  this.models[responseName] = procedure.response;
779
803
  }
780
804
  }
781
- registerRpc(this.h3Router, path, procedure, this.middlewares);
805
+ registerRpc(this.h3Router, path, procedure, this.middlewares, {
806
+ onError: this.onError,
807
+ onAfterResponse: this.onAfterResponse,
808
+ onBeforeResponse: this.onBeforeResponse
809
+ });
782
810
  }
783
811
  registerRoute(route) {
784
812
  registerRoute(this.h3Router, route, this.middlewares);
@@ -1,6 +1,6 @@
1
1
  import { execSync } from 'child_process';
2
2
  import { writeFileSync } from 'fs';
3
- import { h as unflattenProcedures, i as isServiceDef, p as pascalCase, j as camelCase, a as isProcedureDef, d as isJsonSchemaObject, f as isJsonSchemaEnum, c as isJsonSchemaNullType, b as isJsonSchemaScalarType, e as isJsonSchemaRecord, g as isJsonSchemaArray } from './arri.74885d3a.mjs';
3
+ import { h as unflattenProcedures, i as isServiceDef, p as pascalCase, j as camelCase, a as isProcedureDef, d as isJsonSchemaObject, f as isJsonSchemaEnum, c as isJsonSchemaNullType, b as isJsonSchemaScalarType, e as isJsonSchemaRecord, g as isJsonSchemaArray } from './arri.e62071ea.mjs';
4
4
 
5
5
  function defineClientGeneratorPlugin(plugin) {
6
6
  return plugin;