arri 0.0.27 → 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.
- package/dist/chunks/index.cjs +66 -11
- package/dist/chunks/index.mjs +66 -11
- package/dist/cli.cjs +2 -2
- package/dist/cli.mjs +2 -2
- package/dist/codegen.cjs +2 -2
- package/dist/codegen.d.cts +2 -2
- package/dist/codegen.d.mts +2 -2
- package/dist/codegen.d.ts +2 -2
- package/dist/codegen.mjs +4 -4
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +1 -1
- package/dist/shared/{arri.f0cec5fb.d.ts → arri.1f0e8d09.d.cts} +32 -25
- package/dist/shared/{arri.f0cec5fb.d.cts → arri.1f0e8d09.d.mts} +32 -25
- package/dist/shared/{arri.f0cec5fb.d.mts → arri.1f0e8d09.d.ts} +32 -25
- package/dist/shared/{arri.198ae3ea.cjs → arri.50fd4bf9.cjs} +152 -124
- package/dist/shared/{arri.7f1296ca.mjs → arri.64252c62.mjs} +1 -1
- package/dist/shared/{arri.74885d3a.mjs → arri.e62071ea.mjs} +153 -125
- package/dist/shared/{arri.2826746b.cjs → arri.fa2beb45.cjs} +1 -1
- package/package.json +2 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TObject, TSchema, Static } from '@sinclair/typebox';
|
|
2
|
-
import {
|
|
2
|
+
import { H3Event, Router, App, H3Error } from 'h3';
|
|
3
3
|
|
|
4
4
|
interface ApplicationDef {
|
|
5
5
|
arriSchemaVersion: "0.0.1";
|
|
@@ -101,12 +101,13 @@ interface ArriRoute<TPath extends string, TMethod extends HttpMethod, TQuery ext
|
|
|
101
101
|
postHandler?: ArriRoutePostHandler<TPath, TQuery extends TObject ? Static<TQuery> : undefined, TBody extends TObject ? Static<TObject> : undefined, TResponse extends TSchema ? Static<TResponse> : TFallbackResponse>;
|
|
102
102
|
}
|
|
103
103
|
declare function defineRoute<TPath extends string, TMethod extends HttpMethod, TQuery extends TObject | undefined = any, TBody extends TSchema | undefined = any, TResponse extends TSchema | undefined = undefined, TFallbackResponse = any>(config: ArriRoute<TPath, TMethod, TQuery, TBody, TResponse, TFallbackResponse>): ArriRoute<TPath, TMethod, TQuery, TBody, TResponse, TFallbackResponse>;
|
|
104
|
-
type Middleware = (context: HandlerContext) => void | Promise<void>;
|
|
104
|
+
type Middleware = (context: HandlerContext, event: H3Event) => void | Promise<void>;
|
|
105
105
|
declare const defineMiddleware: (middleware: Middleware) => Middleware;
|
|
106
106
|
type ExtractParam<Path, NextPart> = Path extends `:${infer Param}` ? Record<Param, string> & NextPart : NextPart;
|
|
107
107
|
type ExtractParams<Path> = Path extends `${infer Segment}/${infer Rest}` ? ExtractParam<Segment, ExtractParams<Rest>> : ExtractParam<Path, {}>;
|
|
108
|
-
type ArriRouteHandler<TPath extends string, TQuery = undefined, TBody = undefined, TResponse = any> = (context: RouteHandlerContext<ExtractParams<TPath>, TQuery, TBody
|
|
108
|
+
type ArriRouteHandler<TPath extends string, TQuery = undefined, TBody = undefined, TResponse = any> = (context: RouteHandlerContext<ExtractParams<TPath>, TQuery, TBody>, event: H3Event) => TResponse extends undefined ? void | Promise<void> : TResponse | Promise<TResponse>;
|
|
109
109
|
interface RouteHandlerContext<TParams extends Record<string, string>, TQuery = undefined, TBody = undefined> extends HandlerContext {
|
|
110
|
+
type: "route";
|
|
110
111
|
params: TParams;
|
|
111
112
|
query: TQuery;
|
|
112
113
|
body: TBody;
|
|
@@ -114,8 +115,8 @@ interface RouteHandlerContext<TParams extends Record<string, string>, TQuery = u
|
|
|
114
115
|
interface RoutePostHandlerContext<TParams extends Record<string, string>, TQuery = undefined, TBody = undefined, TResponse = undefined> extends RouteHandlerContext<TParams, TQuery, TBody> {
|
|
115
116
|
response: TResponse;
|
|
116
117
|
}
|
|
117
|
-
type ArriRoutePostHandler<TPath extends string, TQuery = undefined, TBody = undefined, TResponse = undefined> = (context: RoutePostHandlerContext<ExtractParams<TPath>, TQuery, TBody, TResponse
|
|
118
|
-
declare function registerRoute<TPath extends string>(router: Router, route: ArriRoute<TPath, HttpMethod, any, any, any>, middleware: Middleware[], prefix?: string): void;
|
|
118
|
+
type ArriRoutePostHandler<TPath extends string, TQuery = undefined, TBody = undefined, TResponse = undefined> = (context: RoutePostHandlerContext<ExtractParams<TPath>, TQuery, TBody, TResponse>, event: H3Event) => any;
|
|
119
|
+
declare function registerRoute<TPath extends string>(router: Router, route: ArriRoute<TPath, HttpMethod, any, any, any>, middleware: Middleware[], prefix?: string, opts?: ArriOptions): void;
|
|
119
120
|
|
|
120
121
|
interface ArriProcedureBase {
|
|
121
122
|
method: HttpMethod;
|
|
@@ -132,18 +133,16 @@ interface ArriProcedure<TParams extends TObject | undefined, TResponse extends T
|
|
|
132
133
|
handler: ArriProcedureHandler<TParams extends TObject ? Static<TParams> : undefined, TResponse extends TObject ? Static<TResponse> : void>;
|
|
133
134
|
postHandler?: ArriProcedurePostHandler<TParams extends TObject ? Static<TParams> : undefined, TResponse extends TObject ? Static<TResponse> : undefined>;
|
|
134
135
|
}
|
|
135
|
-
|
|
136
|
-
[key: string]: any;
|
|
137
|
-
event: H3Event;
|
|
138
|
-
}
|
|
136
|
+
type HandlerContext = Record<string, any>;
|
|
139
137
|
interface RpcHandlerContext<TParams = undefined> extends HandlerContext {
|
|
138
|
+
type: "procedure";
|
|
140
139
|
params: TParams;
|
|
141
140
|
}
|
|
142
141
|
interface RpcPostHandlerContext<TParams = undefined, TResponse = undefined> extends RpcHandlerContext<TParams> {
|
|
143
142
|
response: TResponse;
|
|
144
143
|
}
|
|
145
|
-
type ArriProcedureHandler<TParams, TResponse> = (context: RpcHandlerContext<TParams
|
|
146
|
-
type ArriProcedurePostHandler<TParams, TResponse> = (
|
|
144
|
+
type ArriProcedureHandler<TParams, TResponse> = (context: RpcHandlerContext<TParams>, event: H3Event) => TResponse | Promise<TResponse>;
|
|
145
|
+
type ArriProcedurePostHandler<TParams, TResponse> = (context: RpcPostHandlerContext<TParams, TResponse>, event: H3Event) => any;
|
|
147
146
|
declare function isRpc(input: any): input is ArriProcedure<any, any>;
|
|
148
147
|
declare function defineRpc<TParams extends TObject | undefined = undefined, TResponse extends TObject | undefined | never = undefined>(config: ArriProcedure<TParams, TResponse>): ArriProcedure<TParams, TResponse>;
|
|
149
148
|
declare function createRpcDefinition(rpcName: string, httpPath: string, procedure: ArriProcedure<any, any>): ProcedureDef;
|
|
@@ -151,20 +150,8 @@ declare function getRpcPath(rpcName: string, prefix?: string): string;
|
|
|
151
150
|
declare function getRpcParamName(rpcName: string, procedure: ArriProcedure<any, any>): string | null;
|
|
152
151
|
declare function getRpcParamDefinition(rpcName: string, procedure: ArriProcedure<any, any>): ProcedureDef["params"];
|
|
153
152
|
declare function getRpcResponseName(rpcName: string, procedure: ArriProcedure<any, any>): string | null;
|
|
154
|
-
declare function registerRpc(router: Router, path: string, procedure: ArriProcedure<any, any>, middleware: Middleware[]): void;
|
|
153
|
+
declare function registerRpc(router: Router, path: string, procedure: ArriProcedure<any, any>, middleware: Middleware[], opts: ArriOptions): void;
|
|
155
154
|
|
|
156
|
-
interface ArriOptions extends AppOptions {
|
|
157
|
-
/**
|
|
158
|
-
* Metadata to display in the __definition.json file
|
|
159
|
-
*/
|
|
160
|
-
appInfo?: ApplicationDef["info"];
|
|
161
|
-
rpcRoutePrefix?: string;
|
|
162
|
-
/**
|
|
163
|
-
* Defaults to /__definitions
|
|
164
|
-
* This parameters also takes the rpcRoutePrefix option into account
|
|
165
|
-
*/
|
|
166
|
-
rpcDefinitionPath?: string;
|
|
167
|
-
}
|
|
168
155
|
declare const DEV_ENDPOINT_ROOT = "/__arri_dev__";
|
|
169
156
|
declare const DEV_DEFINITION_ENDPOINT = "/__arri_dev__/definition";
|
|
170
157
|
declare class Arri {
|
|
@@ -177,6 +164,9 @@ declare class Arri {
|
|
|
177
164
|
private procedures;
|
|
178
165
|
private models;
|
|
179
166
|
private readonly middlewares;
|
|
167
|
+
private readonly onAfterResponse;
|
|
168
|
+
private readonly onBeforeResponse;
|
|
169
|
+
private readonly onError;
|
|
180
170
|
constructor(opts?: ArriOptions);
|
|
181
171
|
registerMiddleware(middleware: Middleware): void;
|
|
182
172
|
registerRpc<TParams extends TObject | undefined, TResponse extends TObject | undefined>(name: string, procedure: ArriProcedure<TParams, TResponse>): void;
|
|
@@ -184,6 +174,23 @@ declare class Arri {
|
|
|
184
174
|
getAppDefinition(): ApplicationDef;
|
|
185
175
|
getH3Instance(): App;
|
|
186
176
|
}
|
|
177
|
+
interface ArriOptions {
|
|
178
|
+
debug?: boolean;
|
|
179
|
+
/**
|
|
180
|
+
* Metadata to display in the __definition.json file
|
|
181
|
+
*/
|
|
182
|
+
appInfo?: ApplicationDef["info"];
|
|
183
|
+
rpcRoutePrefix?: string;
|
|
184
|
+
/**
|
|
185
|
+
* Defaults to /__definitions
|
|
186
|
+
* This parameters also takes the rpcRoutePrefix option into account
|
|
187
|
+
*/
|
|
188
|
+
rpcDefinitionPath?: string;
|
|
189
|
+
onRequest?: (event: H3Event) => void | Promise<void>;
|
|
190
|
+
onAfterResponse?: (context: RpcPostHandlerContext | RoutePostHandlerContext<any>, event: H3Event) => void | Promise<void>;
|
|
191
|
+
onBeforeResponse?: (context: RpcPostHandlerContext | RoutePostHandlerContext<any>, event: H3Event) => void | Promise<void>;
|
|
192
|
+
onError?: (error: H3Error, context: RpcHandlerContext | RouteHandlerContext<any>, event: H3Event) => void | Promise<void>;
|
|
193
|
+
}
|
|
187
194
|
declare const HttpMethodValues: readonly ["get", "post", "put", "patch", "delete", "head"];
|
|
188
195
|
type HttpMethod = (typeof HttpMethodValues)[number];
|
|
189
196
|
declare const isHttpMethod: (input: any) => input is "get" | "post" | "put" | "patch" | "delete" | "head";
|
|
@@ -195,4 +202,4 @@ interface ClientGenerator<TOptions extends Record<string, any> | undefined> {
|
|
|
195
202
|
type ClientGeneratorPlugin<TOptions extends Record<string, any> | undefined> = (options: TOptions) => ClientGenerator<TOptions>;
|
|
196
203
|
declare function defineClientGeneratorPlugin<TOptions extends Record<string, any> | undefined>(plugin: ClientGeneratorPlugin<TOptions>): ClientGeneratorPlugin<TOptions>;
|
|
197
204
|
|
|
198
|
-
export {
|
|
205
|
+
export { type JsonSchemaScalarType as $, Arri as A, registerRoute as B, type ClientGenerator as C, DEV_ENDPOINT_ROOT as D, type ExtractParam as E, type ClientGeneratorPlugin as F, type ApplicationDef as G, HttpMethodValues as H, defineClientGeneratorPlugin as I, type JsonSchemaObject as J, normalizeWhitespace as K, isProcedureDef as L, type Middleware as M, isServiceDef as N, isApplicationDef as O, type ProcedureDef as P, unflattenObject as Q, type RpcHandlerContext as R, type ServiceDef as S, unflattenProcedures as T, setNestedObjectProperty as U, removeDisallowedChars as V, JsonSchemaScalarTypeValues as W, JsonSchemaNullTypeValues as X, JsonSchemaComplexTypeValues as Y, type JsonSchemaType as Z, type JsonSchemaTypeValue as _, DEV_DEFINITION_ENDPOINT as a, isJsonSchemaScalarType as a0, type JsonSchemaNullType as a1, isJsonSchemaNullType as a2, type JsonSchemaComplexType as a3, isJsonSchemaObject as a4, type JsonSchemaRecord as a5, isJsonSchemaRecord as a6, type JsonSchemaArray as a7, isJsonSchemaArray as a8, type JsonSchemaUint8Array as a9, type JsonSchemaEnum as aa, isJsonSchemaEnum as ab, type ArriOptions as b, type HttpMethod as c, type ArriProcedureBase as d, type ArriProcedure as e, type HandlerContext as f, type RpcPostHandlerContext as g, type ArriProcedureHandler as h, isHttpMethod as i, type ArriProcedurePostHandler as j, isRpc as k, defineRpc as l, createRpcDefinition as m, getRpcPath as n, getRpcParamName as o, getRpcParamDefinition as p, getRpcResponseName as q, registerRpc as r, type ArriRoute as s, defineRoute as t, defineMiddleware as u, type ExtractParams as v, type ArriRouteHandler as w, type RouteHandlerContext as x, type RoutePostHandlerContext as y, type ArriRoutePostHandler as z };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TObject, TSchema, Static } from '@sinclair/typebox';
|
|
2
|
-
import {
|
|
2
|
+
import { H3Event, Router, App, H3Error } from 'h3';
|
|
3
3
|
|
|
4
4
|
interface ApplicationDef {
|
|
5
5
|
arriSchemaVersion: "0.0.1";
|
|
@@ -101,12 +101,13 @@ interface ArriRoute<TPath extends string, TMethod extends HttpMethod, TQuery ext
|
|
|
101
101
|
postHandler?: ArriRoutePostHandler<TPath, TQuery extends TObject ? Static<TQuery> : undefined, TBody extends TObject ? Static<TObject> : undefined, TResponse extends TSchema ? Static<TResponse> : TFallbackResponse>;
|
|
102
102
|
}
|
|
103
103
|
declare function defineRoute<TPath extends string, TMethod extends HttpMethod, TQuery extends TObject | undefined = any, TBody extends TSchema | undefined = any, TResponse extends TSchema | undefined = undefined, TFallbackResponse = any>(config: ArriRoute<TPath, TMethod, TQuery, TBody, TResponse, TFallbackResponse>): ArriRoute<TPath, TMethod, TQuery, TBody, TResponse, TFallbackResponse>;
|
|
104
|
-
type Middleware = (context: HandlerContext) => void | Promise<void>;
|
|
104
|
+
type Middleware = (context: HandlerContext, event: H3Event) => void | Promise<void>;
|
|
105
105
|
declare const defineMiddleware: (middleware: Middleware) => Middleware;
|
|
106
106
|
type ExtractParam<Path, NextPart> = Path extends `:${infer Param}` ? Record<Param, string> & NextPart : NextPart;
|
|
107
107
|
type ExtractParams<Path> = Path extends `${infer Segment}/${infer Rest}` ? ExtractParam<Segment, ExtractParams<Rest>> : ExtractParam<Path, {}>;
|
|
108
|
-
type ArriRouteHandler<TPath extends string, TQuery = undefined, TBody = undefined, TResponse = any> = (context: RouteHandlerContext<ExtractParams<TPath>, TQuery, TBody
|
|
108
|
+
type ArriRouteHandler<TPath extends string, TQuery = undefined, TBody = undefined, TResponse = any> = (context: RouteHandlerContext<ExtractParams<TPath>, TQuery, TBody>, event: H3Event) => TResponse extends undefined ? void | Promise<void> : TResponse | Promise<TResponse>;
|
|
109
109
|
interface RouteHandlerContext<TParams extends Record<string, string>, TQuery = undefined, TBody = undefined> extends HandlerContext {
|
|
110
|
+
type: "route";
|
|
110
111
|
params: TParams;
|
|
111
112
|
query: TQuery;
|
|
112
113
|
body: TBody;
|
|
@@ -114,8 +115,8 @@ interface RouteHandlerContext<TParams extends Record<string, string>, TQuery = u
|
|
|
114
115
|
interface RoutePostHandlerContext<TParams extends Record<string, string>, TQuery = undefined, TBody = undefined, TResponse = undefined> extends RouteHandlerContext<TParams, TQuery, TBody> {
|
|
115
116
|
response: TResponse;
|
|
116
117
|
}
|
|
117
|
-
type ArriRoutePostHandler<TPath extends string, TQuery = undefined, TBody = undefined, TResponse = undefined> = (context: RoutePostHandlerContext<ExtractParams<TPath>, TQuery, TBody, TResponse
|
|
118
|
-
declare function registerRoute<TPath extends string>(router: Router, route: ArriRoute<TPath, HttpMethod, any, any, any>, middleware: Middleware[], prefix?: string): void;
|
|
118
|
+
type ArriRoutePostHandler<TPath extends string, TQuery = undefined, TBody = undefined, TResponse = undefined> = (context: RoutePostHandlerContext<ExtractParams<TPath>, TQuery, TBody, TResponse>, event: H3Event) => any;
|
|
119
|
+
declare function registerRoute<TPath extends string>(router: Router, route: ArriRoute<TPath, HttpMethod, any, any, any>, middleware: Middleware[], prefix?: string, opts?: ArriOptions): void;
|
|
119
120
|
|
|
120
121
|
interface ArriProcedureBase {
|
|
121
122
|
method: HttpMethod;
|
|
@@ -132,18 +133,16 @@ interface ArriProcedure<TParams extends TObject | undefined, TResponse extends T
|
|
|
132
133
|
handler: ArriProcedureHandler<TParams extends TObject ? Static<TParams> : undefined, TResponse extends TObject ? Static<TResponse> : void>;
|
|
133
134
|
postHandler?: ArriProcedurePostHandler<TParams extends TObject ? Static<TParams> : undefined, TResponse extends TObject ? Static<TResponse> : undefined>;
|
|
134
135
|
}
|
|
135
|
-
|
|
136
|
-
[key: string]: any;
|
|
137
|
-
event: H3Event;
|
|
138
|
-
}
|
|
136
|
+
type HandlerContext = Record<string, any>;
|
|
139
137
|
interface RpcHandlerContext<TParams = undefined> extends HandlerContext {
|
|
138
|
+
type: "procedure";
|
|
140
139
|
params: TParams;
|
|
141
140
|
}
|
|
142
141
|
interface RpcPostHandlerContext<TParams = undefined, TResponse = undefined> extends RpcHandlerContext<TParams> {
|
|
143
142
|
response: TResponse;
|
|
144
143
|
}
|
|
145
|
-
type ArriProcedureHandler<TParams, TResponse> = (context: RpcHandlerContext<TParams
|
|
146
|
-
type ArriProcedurePostHandler<TParams, TResponse> = (
|
|
144
|
+
type ArriProcedureHandler<TParams, TResponse> = (context: RpcHandlerContext<TParams>, event: H3Event) => TResponse | Promise<TResponse>;
|
|
145
|
+
type ArriProcedurePostHandler<TParams, TResponse> = (context: RpcPostHandlerContext<TParams, TResponse>, event: H3Event) => any;
|
|
147
146
|
declare function isRpc(input: any): input is ArriProcedure<any, any>;
|
|
148
147
|
declare function defineRpc<TParams extends TObject | undefined = undefined, TResponse extends TObject | undefined | never = undefined>(config: ArriProcedure<TParams, TResponse>): ArriProcedure<TParams, TResponse>;
|
|
149
148
|
declare function createRpcDefinition(rpcName: string, httpPath: string, procedure: ArriProcedure<any, any>): ProcedureDef;
|
|
@@ -151,20 +150,8 @@ declare function getRpcPath(rpcName: string, prefix?: string): string;
|
|
|
151
150
|
declare function getRpcParamName(rpcName: string, procedure: ArriProcedure<any, any>): string | null;
|
|
152
151
|
declare function getRpcParamDefinition(rpcName: string, procedure: ArriProcedure<any, any>): ProcedureDef["params"];
|
|
153
152
|
declare function getRpcResponseName(rpcName: string, procedure: ArriProcedure<any, any>): string | null;
|
|
154
|
-
declare function registerRpc(router: Router, path: string, procedure: ArriProcedure<any, any>, middleware: Middleware[]): void;
|
|
153
|
+
declare function registerRpc(router: Router, path: string, procedure: ArriProcedure<any, any>, middleware: Middleware[], opts: ArriOptions): void;
|
|
155
154
|
|
|
156
|
-
interface ArriOptions extends AppOptions {
|
|
157
|
-
/**
|
|
158
|
-
* Metadata to display in the __definition.json file
|
|
159
|
-
*/
|
|
160
|
-
appInfo?: ApplicationDef["info"];
|
|
161
|
-
rpcRoutePrefix?: string;
|
|
162
|
-
/**
|
|
163
|
-
* Defaults to /__definitions
|
|
164
|
-
* This parameters also takes the rpcRoutePrefix option into account
|
|
165
|
-
*/
|
|
166
|
-
rpcDefinitionPath?: string;
|
|
167
|
-
}
|
|
168
155
|
declare const DEV_ENDPOINT_ROOT = "/__arri_dev__";
|
|
169
156
|
declare const DEV_DEFINITION_ENDPOINT = "/__arri_dev__/definition";
|
|
170
157
|
declare class Arri {
|
|
@@ -177,6 +164,9 @@ declare class Arri {
|
|
|
177
164
|
private procedures;
|
|
178
165
|
private models;
|
|
179
166
|
private readonly middlewares;
|
|
167
|
+
private readonly onAfterResponse;
|
|
168
|
+
private readonly onBeforeResponse;
|
|
169
|
+
private readonly onError;
|
|
180
170
|
constructor(opts?: ArriOptions);
|
|
181
171
|
registerMiddleware(middleware: Middleware): void;
|
|
182
172
|
registerRpc<TParams extends TObject | undefined, TResponse extends TObject | undefined>(name: string, procedure: ArriProcedure<TParams, TResponse>): void;
|
|
@@ -184,6 +174,23 @@ declare class Arri {
|
|
|
184
174
|
getAppDefinition(): ApplicationDef;
|
|
185
175
|
getH3Instance(): App;
|
|
186
176
|
}
|
|
177
|
+
interface ArriOptions {
|
|
178
|
+
debug?: boolean;
|
|
179
|
+
/**
|
|
180
|
+
* Metadata to display in the __definition.json file
|
|
181
|
+
*/
|
|
182
|
+
appInfo?: ApplicationDef["info"];
|
|
183
|
+
rpcRoutePrefix?: string;
|
|
184
|
+
/**
|
|
185
|
+
* Defaults to /__definitions
|
|
186
|
+
* This parameters also takes the rpcRoutePrefix option into account
|
|
187
|
+
*/
|
|
188
|
+
rpcDefinitionPath?: string;
|
|
189
|
+
onRequest?: (event: H3Event) => void | Promise<void>;
|
|
190
|
+
onAfterResponse?: (context: RpcPostHandlerContext | RoutePostHandlerContext<any>, event: H3Event) => void | Promise<void>;
|
|
191
|
+
onBeforeResponse?: (context: RpcPostHandlerContext | RoutePostHandlerContext<any>, event: H3Event) => void | Promise<void>;
|
|
192
|
+
onError?: (error: H3Error, context: RpcHandlerContext | RouteHandlerContext<any>, event: H3Event) => void | Promise<void>;
|
|
193
|
+
}
|
|
187
194
|
declare const HttpMethodValues: readonly ["get", "post", "put", "patch", "delete", "head"];
|
|
188
195
|
type HttpMethod = (typeof HttpMethodValues)[number];
|
|
189
196
|
declare const isHttpMethod: (input: any) => input is "get" | "post" | "put" | "patch" | "delete" | "head";
|
|
@@ -195,4 +202,4 @@ interface ClientGenerator<TOptions extends Record<string, any> | undefined> {
|
|
|
195
202
|
type ClientGeneratorPlugin<TOptions extends Record<string, any> | undefined> = (options: TOptions) => ClientGenerator<TOptions>;
|
|
196
203
|
declare function defineClientGeneratorPlugin<TOptions extends Record<string, any> | undefined>(plugin: ClientGeneratorPlugin<TOptions>): ClientGeneratorPlugin<TOptions>;
|
|
197
204
|
|
|
198
|
-
export {
|
|
205
|
+
export { type JsonSchemaScalarType as $, Arri as A, registerRoute as B, type ClientGenerator as C, DEV_ENDPOINT_ROOT as D, type ExtractParam as E, type ClientGeneratorPlugin as F, type ApplicationDef as G, HttpMethodValues as H, defineClientGeneratorPlugin as I, type JsonSchemaObject as J, normalizeWhitespace as K, isProcedureDef as L, type Middleware as M, isServiceDef as N, isApplicationDef as O, type ProcedureDef as P, unflattenObject as Q, type RpcHandlerContext as R, type ServiceDef as S, unflattenProcedures as T, setNestedObjectProperty as U, removeDisallowedChars as V, JsonSchemaScalarTypeValues as W, JsonSchemaNullTypeValues as X, JsonSchemaComplexTypeValues as Y, type JsonSchemaType as Z, type JsonSchemaTypeValue as _, DEV_DEFINITION_ENDPOINT as a, isJsonSchemaScalarType as a0, type JsonSchemaNullType as a1, isJsonSchemaNullType as a2, type JsonSchemaComplexType as a3, isJsonSchemaObject as a4, type JsonSchemaRecord as a5, isJsonSchemaRecord as a6, type JsonSchemaArray as a7, isJsonSchemaArray as a8, type JsonSchemaUint8Array as a9, type JsonSchemaEnum as aa, isJsonSchemaEnum as ab, type ArriOptions as b, type HttpMethod as c, type ArriProcedureBase as d, type ArriProcedure as e, type HandlerContext as f, type RpcPostHandlerContext as g, type ArriProcedureHandler as h, isHttpMethod as i, type ArriProcedurePostHandler as j, isRpc as k, defineRpc as l, createRpcDefinition as m, getRpcPath as n, getRpcParamName as o, getRpcParamDefinition as p, getRpcResponseName as q, registerRpc as r, type ArriRoute as s, defineRoute as t, defineMiddleware as u, type ExtractParams as v, type ArriRouteHandler as w, type RouteHandlerContext as x, type RoutePostHandlerContext as y, type ArriRoutePostHandler as z };
|
|
@@ -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
|
-
|
|
528
|
+
type: "procedure",
|
|
529
|
+
params: void 0
|
|
529
530
|
};
|
|
530
531
|
if (h3.isPreflightRequest(event)) {
|
|
531
532
|
return "ok";
|
|
532
533
|
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
const
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
errorParts.
|
|
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
|
-
|
|
554
|
-
|
|
555
|
-
","
|
|
556
|
-
)}]`,
|
|
557
|
-
data: parsedParams.errors
|
|
558
|
-
});
|
|
562
|
+
context.params = parsedParams.value;
|
|
563
|
+
break;
|
|
559
564
|
}
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
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
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
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.
|
|
592
|
+
statusMessage: `Invalid request body. Affected properties [${errorParts.join(
|
|
593
|
+
", "
|
|
594
|
+
)}]`,
|
|
595
|
+
data: parsedParams.errors
|
|
587
596
|
});
|
|
588
597
|
}
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
", "
|
|
592
|
-
)}]`,
|
|
593
|
-
data: parsedParams.errors
|
|
594
|
-
});
|
|
598
|
+
context.params = parsedParams.value;
|
|
599
|
+
break;
|
|
595
600
|
}
|
|
596
|
-
|
|
597
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
659
|
+
type: "route",
|
|
660
|
+
params: event.context.params,
|
|
661
|
+
query: void 0,
|
|
662
|
+
body: void 0
|
|
645
663
|
};
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
664
|
+
try {
|
|
665
|
+
if (middleware.length) {
|
|
666
|
+
for (const m of middleware) {
|
|
667
|
+
await m(context, event);
|
|
668
|
+
}
|
|
649
669
|
}
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
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
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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.
|
|
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;
|