arri 0.13.0 → 0.16.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/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
- import { HttpMethod, RpcHttpMethod, RpcDefinition, AppDefinition, ClientGenerator } from 'arri-codegen-utils';
2
- import { AObjectSchema, ASchema, InferType, ARecordSchema, ADiscriminatorSchema, a, ValidationError } from 'arri-validate';
3
- import { H3EventContext, H3Event, Router, App, H3Error } from 'h3';
1
+ import { HttpMethod, RpcHttpMethod, RpcDefinition, SchemaFormProperties, SchemaFormDiscriminator, SchemaFormValues, AppDefinition, ClientGenerator } from 'arri-codegen-utils';
2
+ import { AObjectSchema, ASchema, InferType, a, ValidationError, ARecordSchema, ADiscriminatorSchema } from 'arri-validate';
3
+ import { H3EventContext, H3Event, Router, H3Error, App } from 'h3';
4
4
  export * from 'h3';
5
5
  import { BuildOptions } from 'esbuild';
6
6
 
@@ -44,20 +44,103 @@ type RouteOptions = Pick<ArriOptions, "onAfterResponse" | "onBeforeResponse" | "
44
44
  declare function registerRoute(router: Router, route: ArriRoute<any>, opts: RouteOptions): void;
45
45
  declare function handleRoute(router: Router, method: HttpMethod, route: ArriRoute<any>, opts: RouteOptions): void;
46
46
 
47
+ declare const ErrorResponse: AObjectSchema<{
48
+ statusCode: number;
49
+ statusMessage: string;
50
+ stack?: string | undefined;
51
+ data?: any;
52
+ }, false>;
53
+ type ErrorResponse = a.infer<typeof ErrorResponse>;
54
+ declare function defineError(statusCode: StatusCode, input?: Partial<Omit<ErrorResponse, "statusCode" | "stack">> & {
55
+ stack?: string;
56
+ }): H3Error;
57
+ declare function errorResponseFromValidationErrors(errors: ValidationError["errors"], prefixText?: string): H3Error;
58
+ type StatusCode = number | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 507 | 511;
59
+ declare function handleH3Error(err: unknown, event: H3Event, onError: ArriOptions["onError"]): Promise<void>;
60
+
61
+ declare function setSseHeaders(event: H3Event): void;
62
+ declare function defineEventStreamRpc<TParams extends RpcParamSchema | undefined = undefined, TResponse extends RpcParamSchema | undefined | never = undefined>(config: Omit<EventStreamRpc<TParams, TResponse>, "isEventStream">): EventStreamRpc<TParams, TResponse>;
63
+ declare function isEventStreamRpc(input: unknown): input is EventStreamRpc<any, any>;
64
+ interface EventStreamRpc<TParams extends RpcParamSchema | undefined = undefined, TResponse extends RpcParamSchema | undefined = undefined> extends Omit<Rpc<true, TParams, TResponse>, "handler" | "postHandler"> {
65
+ isEventStream: true;
66
+ handler: EventStreamRpcHandler<TParams extends RpcParamSchema ? InferType<TParams> : undefined, TResponse extends RpcParamSchema ? InferType<TResponse> : undefined>;
67
+ }
68
+ type EventStreamRpcHandler<TParams, TResponse> = (context: EventStreamRpcHandlerContext<TParams, TResponse>, event: EventStreamRpcEvent<TParams, TResponse>) => void | Promise<void>;
69
+ interface EventStreamRpcEvent<TParams, TResponse> extends RpcEvent<TParams> {
70
+ connection: EventStreamConnection<TResponse>;
71
+ }
72
+ interface EventStreamRpcHandlerContext<TParams = any, TResponse = any> extends RpcHandlerContext<TParams> {
73
+ connection: EventStreamConnection<TResponse>;
74
+ }
75
+ interface EventStreamConnectionOptions<TData> {
76
+ serializer: (input: TData) => string;
77
+ pingInterval?: number;
78
+ }
79
+ /**
80
+ * A server sent event
81
+ */
82
+ interface Sse {
83
+ id?: string;
84
+ event?: string;
85
+ data: string;
86
+ }
87
+ declare function formatSse({ id, data, event }: Sse): string;
88
+ declare function formatSseList(events: Sse[]): string;
89
+ declare class EventStreamConnection<TData> {
90
+ lastEventId: string | undefined;
91
+ private readonly writable;
92
+ private readonly readable;
93
+ private readonly writer;
94
+ private readonly encoder;
95
+ private readonly serializer;
96
+ private readonly h3Event;
97
+ private pingInterval;
98
+ private readonly pingIntervalMs;
99
+ constructor(event: H3Event, opts: EventStreamConnectionOptions<TData>);
100
+ /**
101
+ * Start sending the event stream to the client
102
+ */
103
+ start(): void;
104
+ /**
105
+ * Publish a new event. Events published with this hook will trigger the `onData()` hooks of any connected clients.
106
+ */
107
+ push(data: TData[], eventId?: string): Promise<void>;
108
+ push(data: TData, eventId?: string): Promise<void>;
109
+ /**
110
+ * Publish an error event. This will trigger the `onError` hooks of any connected clients.
111
+ */
112
+ pushError(error: ErrorResponse, eventId?: string): Promise<void>;
113
+ private publishEvents;
114
+ private publishEvent;
115
+ private cleanup;
116
+ /**
117
+ * Tell clients that the stream has ended and close the connection.
118
+ */
119
+ end(): Promise<void>;
120
+ on(event: "disconnect", callback: () => any): void;
121
+ on(event: "end", callback: () => any): void;
122
+ }
123
+ declare function registerEventStreamRpc(router: Router, path: string, procedure: EventStreamRpc<any, any> & {
124
+ name: string;
125
+ }, opts: RouteOptions): void;
126
+
47
127
  type RpcParamSchema<TObjectInner = any, TRecordInner extends ASchema = any, TDiscriminatorInner = any> = AObjectSchema<TObjectInner> | ARecordSchema<TRecordInner> | ADiscriminatorSchema<TDiscriminatorInner>;
48
128
  declare function isRpcParamSchema(input: unknown): input is RpcParamSchema;
49
- interface ArriProcedure<TParams extends RpcParamSchema | undefined, TResponse extends RpcParamSchema | undefined> {
50
- description?: string;
129
+ interface NamedRpc<TIsEventStream extends boolean = false, TParams extends RpcParamSchema | undefined = undefined, TResponse extends RpcParamSchema | undefined = undefined> extends Rpc<TIsEventStream, TParams, TResponse> {
130
+ name: string;
131
+ }
132
+ interface Rpc<TIsEventStream extends boolean = false, TParams extends RpcParamSchema | undefined = undefined, TResponse extends RpcParamSchema | undefined = undefined> {
51
133
  method?: RpcHttpMethod;
52
134
  path?: string;
135
+ description?: string;
53
136
  params: TParams;
54
137
  response: TResponse;
55
- handler: ArriProcedureHandler<TParams extends RpcParamSchema ? InferType<TParams> : undefined, TResponse extends RpcParamSchema ? InferType<TResponse> : void>;
56
- postHandler?: ArriProcedurePostHandler<TParams extends RpcParamSchema ? InferType<TParams> : undefined, TResponse extends RpcParamSchema ? InferType<TResponse> : undefined>;
57
- }
58
- interface ArriNamedProcedure<TParams extends RpcParamSchema | undefined, TResponse extends RpcParamSchema | undefined> extends ArriProcedure<TParams, TResponse> {
59
- name: string;
138
+ isEventStream?: TIsEventStream;
139
+ pingInterval?: TIsEventStream extends true ? number : undefined;
140
+ handler: TIsEventStream extends true ? EventStreamRpcHandler<TParams extends RpcParamSchema ? InferType<TParams> : undefined, TResponse extends RpcParamSchema ? InferType<TResponse> : undefined> : RpcHandler<TParams extends RpcParamSchema ? InferType<TParams> : undefined, TResponse extends RpcParamSchema ? InferType<TResponse> : void>;
141
+ postHandler?: TIsEventStream extends true ? undefined : RpcPostHandler<TParams extends RpcParamSchema ? InferType<TParams> : undefined, TResponse extends RpcParamSchema ? InferType<TResponse> : undefined>;
60
142
  }
143
+ type HttpRpc<TParams extends RpcParamSchema | undefined, TResponse extends RpcParamSchema | undefined> = Omit<Rpc<false, TParams, TResponse>, "isEventStream">;
61
144
  type HandlerContext = Record<string, any>;
62
145
  interface RpcHandlerContext<TParams = undefined> extends HandlerContext, Omit<H3EventContext, "params"> {
63
146
  rpcName: string;
@@ -72,31 +155,37 @@ interface RpcEvent<TParams = undefined> extends Omit<H3Event, "context"> {
72
155
  interface RpcPostEvent<TParams = undefined, TResponse = undefined> extends Omit<H3Event, "context"> {
73
156
  context: RpcPostHandlerContext<TParams, TResponse>;
74
157
  }
75
- type ArriProcedureHandler<TParams, TResponse> = (context: RpcHandlerContext<TParams>, event: RpcEvent<TParams>) => TResponse | Promise<TResponse>;
76
- type ArriProcedurePostHandler<TParams, TResponse> = (context: RpcPostHandlerContext<TParams, TResponse>, event: RpcPostEvent<TParams, TResponse>) => any;
77
- declare function isRpc(input: any): input is ArriProcedure<any, any>;
78
- declare function defineRpc<TParams extends RpcParamSchema | undefined = undefined, TResponse extends RpcParamSchema | undefined | never = undefined>(config: ArriProcedure<TParams, TResponse>): ArriProcedure<TParams, TResponse>;
79
- declare function createRpcDefinition(rpcName: string, httpPath: string, procedure: ArriProcedure<any, any>): RpcDefinition;
158
+ type RpcHandler<TParams, TResponse> = (context: RpcHandlerContext<TParams>, event: RpcEvent<TParams>) => TResponse | Promise<TResponse>;
159
+ type RpcPostHandler<TParams, TResponse> = (context: RpcPostHandlerContext<TParams, TResponse>, event: RpcPostEvent<TParams, TResponse>) => any;
160
+ declare function isRpc(input: unknown): input is Rpc<any, any>;
161
+ declare function defineRpc<TParams extends RpcParamSchema | undefined = undefined, TResponse extends RpcParamSchema | undefined | never = undefined>(config: HttpRpc<TParams, TResponse>): Rpc<false, TParams, TResponse>;
162
+ declare function createRpcDefinition(rpcName: string, httpPath: string, procedure: Rpc<any, any, any>): RpcDefinition;
80
163
  declare function getRpcPath(rpcName: string, prefix?: string): string;
81
- declare function getRpcParamName(rpcName: string, procedure: ArriProcedure<any, any>): string | undefined;
82
- declare function getRpcResponseName(rpcName: string, procedure: ArriProcedure<any, any>): string | undefined;
83
- declare function registerRpc(router: Router, path: string, procedure: ArriNamedProcedure<any, any>, opts: RouteOptions): void;
164
+ declare function getRpcParamName(rpcName: string, procedure: Rpc<any, any, any>): string | undefined;
165
+ declare function getRpcResponseName(rpcName: string, procedure: Rpc<any, any, any>): string | undefined;
166
+ declare function registerRpc(router: Router, path: string, procedure: NamedRpc<any, any, any>, opts: RouteOptions): void;
167
+ declare function validateRpcRequestInput(event: H3Event, httpMethod: RpcHttpMethod, schema: ASchema): Promise<void>;
84
168
 
85
169
  interface ArriRouterBase {
86
- rpc: <TParams extends AObjectSchema<any, any> | undefined, TResponse extends AObjectSchema<any, any> | undefined>(procedure: ArriNamedProcedure<TParams, TResponse>) => void;
170
+ rpc: <TIsEventStream extends boolean, TParams extends AObjectSchema<any, any> | undefined, TResponse extends AObjectSchema<any, any> | undefined>(procedure: NamedRpc<TIsEventStream, TParams, TResponse>) => void;
87
171
  route: <TPath extends string, TQuery extends AObjectSchema<any, any> = any, TBody extends ASchema<any> = any, TResponse = any>(route: ArriRoute<TPath, TQuery, TBody, TResponse>) => void;
172
+ registerModels: (models: ModelMap) => void;
88
173
  }
89
174
  declare class ArriRouter implements ArriRouterBase {
90
175
  private readonly procedures;
91
176
  private readonly routes;
92
- rpc<TParams extends AObjectSchema<any, any> | undefined, TResponse extends AObjectSchema<any, any> | undefined>(procedure: ArriNamedProcedure<TParams, TResponse>): void;
177
+ private readonly models;
178
+ rpc<TIsEventStream extends boolean = false, TParams extends AObjectSchema<any, any> | undefined = undefined, TResponse extends AObjectSchema<any, any> | undefined = undefined>(procedure: NamedRpc<TIsEventStream, TParams, TResponse>): void;
93
179
  route<TPath extends string, TQuery extends AObjectSchema<any, any> = any, TBody extends ASchema<any> = any, TResponse = any>(route: ArriRoute<TPath, TQuery, TBody, TResponse>): void;
94
- getProcedures(): ArriNamedProcedure<any, any>[];
180
+ registerModels(models: ModelMap): void;
181
+ getProcedures(): NamedRpc<any, any, any>[];
95
182
  getRoutes(): ArriRoute<any, any, any, any>[];
183
+ getModels(): ModelMap;
96
184
  }
97
185
 
98
186
  declare const DEV_ENDPOINT_ROOT = "/__arri_dev__";
99
187
  declare const DEV_DEFINITION_ENDPOINT = "/__arri_dev__/__definition";
188
+ type ModelMap = Record<string, SchemaFormProperties | SchemaFormDiscriminator | SchemaFormValues>;
100
189
  declare class ArriApp implements ArriRouterBase {
101
190
  __isArri__: boolean;
102
191
  readonly h3App: App;
@@ -113,8 +202,9 @@ declare class ArriApp implements ArriRouterBase {
113
202
  private readonly onError;
114
203
  constructor(opts?: ArriOptions);
115
204
  use(input: Middleware | ArriRouter): void;
116
- rpc<TParams extends AObjectSchema<any, any> | undefined, TResponse extends AObjectSchema<any, any> | undefined>(procedure: ArriNamedProcedure<TParams, TResponse>): void;
205
+ rpc<TIsEventStream extends boolean = false, TParams extends AObjectSchema<any, any> | undefined = undefined, TResponse extends AObjectSchema<any, any> | undefined = undefined>(procedure: NamedRpc<TIsEventStream, TParams, TResponse>): void;
117
206
  route<TPath extends string, TQuery extends AObjectSchema<any, any>, TBody extends ASchema<any>, TResponse = any>(route: ArriRoute<TPath, TQuery, TBody, TResponse>): void;
207
+ registerModels(models: ModelMap): void;
118
208
  getAppDefinition(): AppDefinition;
119
209
  }
120
210
  interface ArriOptions {
@@ -135,20 +225,6 @@ interface ArriOptions {
135
225
  onError?: (error: H3Error, event: MiddlewareEvent) => void | Promise<void>;
136
226
  }
137
227
 
138
- declare const ErrorResponse: AObjectSchema<{
139
- statusCode: number;
140
- statusMessage: string;
141
- stack?: string | undefined;
142
- data?: any;
143
- }, false>;
144
- type ErrorResponse = a.infer<typeof ErrorResponse>;
145
- declare function defineError(statusCode: StatusCode, input?: Partial<Omit<ErrorResponse, "statusCode" | "stack">> & {
146
- stack?: string;
147
- }): H3Error;
148
- declare function errorResponseFromValidationErrors(errors: ValidationError["errors"], prefixText?: string): H3Error;
149
- type StatusCode = number | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 507 | 511;
150
- declare function handleH3Error(err: unknown, event: H3Event, onError: ArriOptions["onError"]): Promise<void>;
151
-
152
228
  interface ArriConfig {
153
229
  port: number;
154
230
  entry: string;
@@ -163,6 +239,11 @@ interface ArriConfig {
163
239
  clientGenerators?: Array<ClientGenerator<any>>;
164
240
  buildDir?: string;
165
241
  esbuild?: Omit<BuildOptions, "outfile" | "outdir" | "entryNames" | "entryPoints">;
242
+ https?: {
243
+ cert: string;
244
+ key: string;
245
+ passphrase?: string;
246
+ } | boolean;
166
247
  }
167
248
  declare function isArriConfig(input: unknown): input is ArriConfig;
168
249
  declare function isResolvedArriConfig(input: unknown): input is ResolvedArriConfig;
@@ -170,4 +251,4 @@ type ResolvedArriConfig = Required<ArriConfig>;
170
251
  declare const defaultConfig: Required<ArriConfig>;
171
252
  declare function defineConfig(config: ArriConfig): ResolvedArriConfig;
172
253
 
173
- export { ArriApp, type ArriConfig, type ArriNamedProcedure, type ArriOptions, type ArriProcedure, type ArriProcedureHandler, type ArriProcedurePostHandler, type ArriRoute, ArriRouter, type ArriRouterBase, DEV_DEFINITION_ENDPOINT, DEV_ENDPOINT_ROOT, ErrorResponse, type ExtractParam, type ExtractParams, type HandlerContext, type Middleware, type MiddlewareEvent, type MiddlewareEventContext, type PostRouteEvent, type PostRouteEventContext, type ResolvedArriConfig, type RouteEvent, type RouteEventContext, type RouteOptions, type RpcEvent, type RpcHandlerContext, type RpcParamSchema, type RpcPostEvent, type RpcPostHandlerContext, type StatusCode, createRpcDefinition, defaultConfig, defineConfig, defineError, defineMiddleware, defineRoute, defineRpc, errorResponseFromValidationErrors, getRpcParamName, getRpcPath, getRpcResponseName, handleH3Error, handleRoute, isArriConfig, isResolvedArriConfig, isRpc, isRpcParamSchema, registerRoute, registerRpc };
254
+ export { ArriApp, type ArriConfig, type ArriOptions, type ArriRoute, ArriRouter, type ArriRouterBase, DEV_DEFINITION_ENDPOINT, DEV_ENDPOINT_ROOT, ErrorResponse, EventStreamConnection, type EventStreamConnectionOptions, type EventStreamRpc, type EventStreamRpcEvent, type EventStreamRpcHandler, type EventStreamRpcHandlerContext, type ExtractParam, type ExtractParams, type HandlerContext, type HttpRpc, type Middleware, type MiddlewareEvent, type MiddlewareEventContext, type ModelMap, type NamedRpc, type PostRouteEvent, type PostRouteEventContext, type ResolvedArriConfig, type RouteEvent, type RouteEventContext, type RouteOptions, type Rpc, type RpcEvent, type RpcHandler, type RpcHandlerContext, type RpcParamSchema, type RpcPostEvent, type RpcPostHandler, type RpcPostHandlerContext, type Sse, type StatusCode, createRpcDefinition, defaultConfig, defineConfig, defineError, defineEventStreamRpc, defineMiddleware, defineRoute, defineRpc, errorResponseFromValidationErrors, formatSse, formatSseList, getRpcParamName, getRpcPath, getRpcResponseName, handleH3Error, handleRoute, isArriConfig, isEventStreamRpc, isResolvedArriConfig, isRpc, isRpcParamSchema, registerEventStreamRpc, registerRoute, registerRpc, setSseHeaders, validateRpcRequestInput };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { HttpMethod, RpcHttpMethod, RpcDefinition, AppDefinition, ClientGenerator } from 'arri-codegen-utils';
2
- import { AObjectSchema, ASchema, InferType, ARecordSchema, ADiscriminatorSchema, a, ValidationError } from 'arri-validate';
3
- import { H3EventContext, H3Event, Router, App, H3Error } from 'h3';
1
+ import { HttpMethod, RpcHttpMethod, RpcDefinition, SchemaFormProperties, SchemaFormDiscriminator, SchemaFormValues, AppDefinition, ClientGenerator } from 'arri-codegen-utils';
2
+ import { AObjectSchema, ASchema, InferType, a, ValidationError, ARecordSchema, ADiscriminatorSchema } from 'arri-validate';
3
+ import { H3EventContext, H3Event, Router, H3Error, App } from 'h3';
4
4
  export * from 'h3';
5
5
  import { BuildOptions } from 'esbuild';
6
6
 
@@ -44,20 +44,103 @@ type RouteOptions = Pick<ArriOptions, "onAfterResponse" | "onBeforeResponse" | "
44
44
  declare function registerRoute(router: Router, route: ArriRoute<any>, opts: RouteOptions): void;
45
45
  declare function handleRoute(router: Router, method: HttpMethod, route: ArriRoute<any>, opts: RouteOptions): void;
46
46
 
47
+ declare const ErrorResponse: AObjectSchema<{
48
+ statusCode: number;
49
+ statusMessage: string;
50
+ stack?: string | undefined;
51
+ data?: any;
52
+ }, false>;
53
+ type ErrorResponse = a.infer<typeof ErrorResponse>;
54
+ declare function defineError(statusCode: StatusCode, input?: Partial<Omit<ErrorResponse, "statusCode" | "stack">> & {
55
+ stack?: string;
56
+ }): H3Error;
57
+ declare function errorResponseFromValidationErrors(errors: ValidationError["errors"], prefixText?: string): H3Error;
58
+ type StatusCode = number | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 507 | 511;
59
+ declare function handleH3Error(err: unknown, event: H3Event, onError: ArriOptions["onError"]): Promise<void>;
60
+
61
+ declare function setSseHeaders(event: H3Event): void;
62
+ declare function defineEventStreamRpc<TParams extends RpcParamSchema | undefined = undefined, TResponse extends RpcParamSchema | undefined | never = undefined>(config: Omit<EventStreamRpc<TParams, TResponse>, "isEventStream">): EventStreamRpc<TParams, TResponse>;
63
+ declare function isEventStreamRpc(input: unknown): input is EventStreamRpc<any, any>;
64
+ interface EventStreamRpc<TParams extends RpcParamSchema | undefined = undefined, TResponse extends RpcParamSchema | undefined = undefined> extends Omit<Rpc<true, TParams, TResponse>, "handler" | "postHandler"> {
65
+ isEventStream: true;
66
+ handler: EventStreamRpcHandler<TParams extends RpcParamSchema ? InferType<TParams> : undefined, TResponse extends RpcParamSchema ? InferType<TResponse> : undefined>;
67
+ }
68
+ type EventStreamRpcHandler<TParams, TResponse> = (context: EventStreamRpcHandlerContext<TParams, TResponse>, event: EventStreamRpcEvent<TParams, TResponse>) => void | Promise<void>;
69
+ interface EventStreamRpcEvent<TParams, TResponse> extends RpcEvent<TParams> {
70
+ connection: EventStreamConnection<TResponse>;
71
+ }
72
+ interface EventStreamRpcHandlerContext<TParams = any, TResponse = any> extends RpcHandlerContext<TParams> {
73
+ connection: EventStreamConnection<TResponse>;
74
+ }
75
+ interface EventStreamConnectionOptions<TData> {
76
+ serializer: (input: TData) => string;
77
+ pingInterval?: number;
78
+ }
79
+ /**
80
+ * A server sent event
81
+ */
82
+ interface Sse {
83
+ id?: string;
84
+ event?: string;
85
+ data: string;
86
+ }
87
+ declare function formatSse({ id, data, event }: Sse): string;
88
+ declare function formatSseList(events: Sse[]): string;
89
+ declare class EventStreamConnection<TData> {
90
+ lastEventId: string | undefined;
91
+ private readonly writable;
92
+ private readonly readable;
93
+ private readonly writer;
94
+ private readonly encoder;
95
+ private readonly serializer;
96
+ private readonly h3Event;
97
+ private pingInterval;
98
+ private readonly pingIntervalMs;
99
+ constructor(event: H3Event, opts: EventStreamConnectionOptions<TData>);
100
+ /**
101
+ * Start sending the event stream to the client
102
+ */
103
+ start(): void;
104
+ /**
105
+ * Publish a new event. Events published with this hook will trigger the `onData()` hooks of any connected clients.
106
+ */
107
+ push(data: TData[], eventId?: string): Promise<void>;
108
+ push(data: TData, eventId?: string): Promise<void>;
109
+ /**
110
+ * Publish an error event. This will trigger the `onError` hooks of any connected clients.
111
+ */
112
+ pushError(error: ErrorResponse, eventId?: string): Promise<void>;
113
+ private publishEvents;
114
+ private publishEvent;
115
+ private cleanup;
116
+ /**
117
+ * Tell clients that the stream has ended and close the connection.
118
+ */
119
+ end(): Promise<void>;
120
+ on(event: "disconnect", callback: () => any): void;
121
+ on(event: "end", callback: () => any): void;
122
+ }
123
+ declare function registerEventStreamRpc(router: Router, path: string, procedure: EventStreamRpc<any, any> & {
124
+ name: string;
125
+ }, opts: RouteOptions): void;
126
+
47
127
  type RpcParamSchema<TObjectInner = any, TRecordInner extends ASchema = any, TDiscriminatorInner = any> = AObjectSchema<TObjectInner> | ARecordSchema<TRecordInner> | ADiscriminatorSchema<TDiscriminatorInner>;
48
128
  declare function isRpcParamSchema(input: unknown): input is RpcParamSchema;
49
- interface ArriProcedure<TParams extends RpcParamSchema | undefined, TResponse extends RpcParamSchema | undefined> {
50
- description?: string;
129
+ interface NamedRpc<TIsEventStream extends boolean = false, TParams extends RpcParamSchema | undefined = undefined, TResponse extends RpcParamSchema | undefined = undefined> extends Rpc<TIsEventStream, TParams, TResponse> {
130
+ name: string;
131
+ }
132
+ interface Rpc<TIsEventStream extends boolean = false, TParams extends RpcParamSchema | undefined = undefined, TResponse extends RpcParamSchema | undefined = undefined> {
51
133
  method?: RpcHttpMethod;
52
134
  path?: string;
135
+ description?: string;
53
136
  params: TParams;
54
137
  response: TResponse;
55
- handler: ArriProcedureHandler<TParams extends RpcParamSchema ? InferType<TParams> : undefined, TResponse extends RpcParamSchema ? InferType<TResponse> : void>;
56
- postHandler?: ArriProcedurePostHandler<TParams extends RpcParamSchema ? InferType<TParams> : undefined, TResponse extends RpcParamSchema ? InferType<TResponse> : undefined>;
57
- }
58
- interface ArriNamedProcedure<TParams extends RpcParamSchema | undefined, TResponse extends RpcParamSchema | undefined> extends ArriProcedure<TParams, TResponse> {
59
- name: string;
138
+ isEventStream?: TIsEventStream;
139
+ pingInterval?: TIsEventStream extends true ? number : undefined;
140
+ handler: TIsEventStream extends true ? EventStreamRpcHandler<TParams extends RpcParamSchema ? InferType<TParams> : undefined, TResponse extends RpcParamSchema ? InferType<TResponse> : undefined> : RpcHandler<TParams extends RpcParamSchema ? InferType<TParams> : undefined, TResponse extends RpcParamSchema ? InferType<TResponse> : void>;
141
+ postHandler?: TIsEventStream extends true ? undefined : RpcPostHandler<TParams extends RpcParamSchema ? InferType<TParams> : undefined, TResponse extends RpcParamSchema ? InferType<TResponse> : undefined>;
60
142
  }
143
+ type HttpRpc<TParams extends RpcParamSchema | undefined, TResponse extends RpcParamSchema | undefined> = Omit<Rpc<false, TParams, TResponse>, "isEventStream">;
61
144
  type HandlerContext = Record<string, any>;
62
145
  interface RpcHandlerContext<TParams = undefined> extends HandlerContext, Omit<H3EventContext, "params"> {
63
146
  rpcName: string;
@@ -72,31 +155,37 @@ interface RpcEvent<TParams = undefined> extends Omit<H3Event, "context"> {
72
155
  interface RpcPostEvent<TParams = undefined, TResponse = undefined> extends Omit<H3Event, "context"> {
73
156
  context: RpcPostHandlerContext<TParams, TResponse>;
74
157
  }
75
- type ArriProcedureHandler<TParams, TResponse> = (context: RpcHandlerContext<TParams>, event: RpcEvent<TParams>) => TResponse | Promise<TResponse>;
76
- type ArriProcedurePostHandler<TParams, TResponse> = (context: RpcPostHandlerContext<TParams, TResponse>, event: RpcPostEvent<TParams, TResponse>) => any;
77
- declare function isRpc(input: any): input is ArriProcedure<any, any>;
78
- declare function defineRpc<TParams extends RpcParamSchema | undefined = undefined, TResponse extends RpcParamSchema | undefined | never = undefined>(config: ArriProcedure<TParams, TResponse>): ArriProcedure<TParams, TResponse>;
79
- declare function createRpcDefinition(rpcName: string, httpPath: string, procedure: ArriProcedure<any, any>): RpcDefinition;
158
+ type RpcHandler<TParams, TResponse> = (context: RpcHandlerContext<TParams>, event: RpcEvent<TParams>) => TResponse | Promise<TResponse>;
159
+ type RpcPostHandler<TParams, TResponse> = (context: RpcPostHandlerContext<TParams, TResponse>, event: RpcPostEvent<TParams, TResponse>) => any;
160
+ declare function isRpc(input: unknown): input is Rpc<any, any>;
161
+ declare function defineRpc<TParams extends RpcParamSchema | undefined = undefined, TResponse extends RpcParamSchema | undefined | never = undefined>(config: HttpRpc<TParams, TResponse>): Rpc<false, TParams, TResponse>;
162
+ declare function createRpcDefinition(rpcName: string, httpPath: string, procedure: Rpc<any, any, any>): RpcDefinition;
80
163
  declare function getRpcPath(rpcName: string, prefix?: string): string;
81
- declare function getRpcParamName(rpcName: string, procedure: ArriProcedure<any, any>): string | undefined;
82
- declare function getRpcResponseName(rpcName: string, procedure: ArriProcedure<any, any>): string | undefined;
83
- declare function registerRpc(router: Router, path: string, procedure: ArriNamedProcedure<any, any>, opts: RouteOptions): void;
164
+ declare function getRpcParamName(rpcName: string, procedure: Rpc<any, any, any>): string | undefined;
165
+ declare function getRpcResponseName(rpcName: string, procedure: Rpc<any, any, any>): string | undefined;
166
+ declare function registerRpc(router: Router, path: string, procedure: NamedRpc<any, any, any>, opts: RouteOptions): void;
167
+ declare function validateRpcRequestInput(event: H3Event, httpMethod: RpcHttpMethod, schema: ASchema): Promise<void>;
84
168
 
85
169
  interface ArriRouterBase {
86
- rpc: <TParams extends AObjectSchema<any, any> | undefined, TResponse extends AObjectSchema<any, any> | undefined>(procedure: ArriNamedProcedure<TParams, TResponse>) => void;
170
+ rpc: <TIsEventStream extends boolean, TParams extends AObjectSchema<any, any> | undefined, TResponse extends AObjectSchema<any, any> | undefined>(procedure: NamedRpc<TIsEventStream, TParams, TResponse>) => void;
87
171
  route: <TPath extends string, TQuery extends AObjectSchema<any, any> = any, TBody extends ASchema<any> = any, TResponse = any>(route: ArriRoute<TPath, TQuery, TBody, TResponse>) => void;
172
+ registerModels: (models: ModelMap) => void;
88
173
  }
89
174
  declare class ArriRouter implements ArriRouterBase {
90
175
  private readonly procedures;
91
176
  private readonly routes;
92
- rpc<TParams extends AObjectSchema<any, any> | undefined, TResponse extends AObjectSchema<any, any> | undefined>(procedure: ArriNamedProcedure<TParams, TResponse>): void;
177
+ private readonly models;
178
+ rpc<TIsEventStream extends boolean = false, TParams extends AObjectSchema<any, any> | undefined = undefined, TResponse extends AObjectSchema<any, any> | undefined = undefined>(procedure: NamedRpc<TIsEventStream, TParams, TResponse>): void;
93
179
  route<TPath extends string, TQuery extends AObjectSchema<any, any> = any, TBody extends ASchema<any> = any, TResponse = any>(route: ArriRoute<TPath, TQuery, TBody, TResponse>): void;
94
- getProcedures(): ArriNamedProcedure<any, any>[];
180
+ registerModels(models: ModelMap): void;
181
+ getProcedures(): NamedRpc<any, any, any>[];
95
182
  getRoutes(): ArriRoute<any, any, any, any>[];
183
+ getModels(): ModelMap;
96
184
  }
97
185
 
98
186
  declare const DEV_ENDPOINT_ROOT = "/__arri_dev__";
99
187
  declare const DEV_DEFINITION_ENDPOINT = "/__arri_dev__/__definition";
188
+ type ModelMap = Record<string, SchemaFormProperties | SchemaFormDiscriminator | SchemaFormValues>;
100
189
  declare class ArriApp implements ArriRouterBase {
101
190
  __isArri__: boolean;
102
191
  readonly h3App: App;
@@ -113,8 +202,9 @@ declare class ArriApp implements ArriRouterBase {
113
202
  private readonly onError;
114
203
  constructor(opts?: ArriOptions);
115
204
  use(input: Middleware | ArriRouter): void;
116
- rpc<TParams extends AObjectSchema<any, any> | undefined, TResponse extends AObjectSchema<any, any> | undefined>(procedure: ArriNamedProcedure<TParams, TResponse>): void;
205
+ rpc<TIsEventStream extends boolean = false, TParams extends AObjectSchema<any, any> | undefined = undefined, TResponse extends AObjectSchema<any, any> | undefined = undefined>(procedure: NamedRpc<TIsEventStream, TParams, TResponse>): void;
117
206
  route<TPath extends string, TQuery extends AObjectSchema<any, any>, TBody extends ASchema<any>, TResponse = any>(route: ArriRoute<TPath, TQuery, TBody, TResponse>): void;
207
+ registerModels(models: ModelMap): void;
118
208
  getAppDefinition(): AppDefinition;
119
209
  }
120
210
  interface ArriOptions {
@@ -135,20 +225,6 @@ interface ArriOptions {
135
225
  onError?: (error: H3Error, event: MiddlewareEvent) => void | Promise<void>;
136
226
  }
137
227
 
138
- declare const ErrorResponse: AObjectSchema<{
139
- statusCode: number;
140
- statusMessage: string;
141
- stack?: string | undefined;
142
- data?: any;
143
- }, false>;
144
- type ErrorResponse = a.infer<typeof ErrorResponse>;
145
- declare function defineError(statusCode: StatusCode, input?: Partial<Omit<ErrorResponse, "statusCode" | "stack">> & {
146
- stack?: string;
147
- }): H3Error;
148
- declare function errorResponseFromValidationErrors(errors: ValidationError["errors"], prefixText?: string): H3Error;
149
- type StatusCode = number | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 507 | 511;
150
- declare function handleH3Error(err: unknown, event: H3Event, onError: ArriOptions["onError"]): Promise<void>;
151
-
152
228
  interface ArriConfig {
153
229
  port: number;
154
230
  entry: string;
@@ -163,6 +239,11 @@ interface ArriConfig {
163
239
  clientGenerators?: Array<ClientGenerator<any>>;
164
240
  buildDir?: string;
165
241
  esbuild?: Omit<BuildOptions, "outfile" | "outdir" | "entryNames" | "entryPoints">;
242
+ https?: {
243
+ cert: string;
244
+ key: string;
245
+ passphrase?: string;
246
+ } | boolean;
166
247
  }
167
248
  declare function isArriConfig(input: unknown): input is ArriConfig;
168
249
  declare function isResolvedArriConfig(input: unknown): input is ResolvedArriConfig;
@@ -170,4 +251,4 @@ type ResolvedArriConfig = Required<ArriConfig>;
170
251
  declare const defaultConfig: Required<ArriConfig>;
171
252
  declare function defineConfig(config: ArriConfig): ResolvedArriConfig;
172
253
 
173
- export { ArriApp, type ArriConfig, type ArriNamedProcedure, type ArriOptions, type ArriProcedure, type ArriProcedureHandler, type ArriProcedurePostHandler, type ArriRoute, ArriRouter, type ArriRouterBase, DEV_DEFINITION_ENDPOINT, DEV_ENDPOINT_ROOT, ErrorResponse, type ExtractParam, type ExtractParams, type HandlerContext, type Middleware, type MiddlewareEvent, type MiddlewareEventContext, type PostRouteEvent, type PostRouteEventContext, type ResolvedArriConfig, type RouteEvent, type RouteEventContext, type RouteOptions, type RpcEvent, type RpcHandlerContext, type RpcParamSchema, type RpcPostEvent, type RpcPostHandlerContext, type StatusCode, createRpcDefinition, defaultConfig, defineConfig, defineError, defineMiddleware, defineRoute, defineRpc, errorResponseFromValidationErrors, getRpcParamName, getRpcPath, getRpcResponseName, handleH3Error, handleRoute, isArriConfig, isResolvedArriConfig, isRpc, isRpcParamSchema, registerRoute, registerRpc };
254
+ export { ArriApp, type ArriConfig, type ArriOptions, type ArriRoute, ArriRouter, type ArriRouterBase, DEV_DEFINITION_ENDPOINT, DEV_ENDPOINT_ROOT, ErrorResponse, EventStreamConnection, type EventStreamConnectionOptions, type EventStreamRpc, type EventStreamRpcEvent, type EventStreamRpcHandler, type EventStreamRpcHandlerContext, type ExtractParam, type ExtractParams, type HandlerContext, type HttpRpc, type Middleware, type MiddlewareEvent, type MiddlewareEventContext, type ModelMap, type NamedRpc, type PostRouteEvent, type PostRouteEventContext, type ResolvedArriConfig, type RouteEvent, type RouteEventContext, type RouteOptions, type Rpc, type RpcEvent, type RpcHandler, type RpcHandlerContext, type RpcParamSchema, type RpcPostEvent, type RpcPostHandler, type RpcPostHandlerContext, type Sse, type StatusCode, createRpcDefinition, defaultConfig, defineConfig, defineError, defineEventStreamRpc, defineMiddleware, defineRoute, defineRpc, errorResponseFromValidationErrors, formatSse, formatSseList, getRpcParamName, getRpcPath, getRpcResponseName, handleH3Error, handleRoute, isArriConfig, isEventStreamRpc, isResolvedArriConfig, isRpc, isRpcParamSchema, registerEventStreamRpc, registerRoute, registerRpc, setSseHeaders, validateRpcRequestInput };
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- export { A as ArriApp, p as ArriRouter, a as DEV_DEFINITION_ENDPOINT, D as DEV_ENDPOINT_ROOT, E as ErrorResponse, c as createRpcDefinition, n as defaultConfig, o as defineConfig, h as defineError, q as defineRoute, d as defineRpc, j as errorResponseFromValidationErrors, e as getRpcParamName, g as getRpcPath, f as getRpcResponseName, k as handleH3Error, t as handleRoute, l as isArriConfig, m as isResolvedArriConfig, b as isRpc, i as isRpcParamSchema, s as registerRoute, r as registerRpc } from './shared/arri.cc0da87a.mjs';
1
+ export { A as ArriApp, p as ArriRouter, a as DEV_DEFINITION_ENDPOINT, D as DEV_ENDPOINT_ROOT, E as ErrorResponse, B as EventStreamConnection, c as createRpcDefinition, n as defaultConfig, o as defineConfig, h as defineError, w as defineEventStreamRpc, q as defineRoute, d as defineRpc, j as errorResponseFromValidationErrors, y as formatSse, z as formatSseList, e as getRpcParamName, g as getRpcPath, f as getRpcResponseName, k as handleH3Error, t as handleRoute, l as isArriConfig, x as isEventStreamRpc, m as isResolvedArriConfig, b as isRpc, i as isRpcParamSchema, C as registerEventStreamRpc, s as registerRoute, r as registerRpc, u as setSseHeaders, v as validateRpcRequestInput } from './shared/arri.ff70fe08.mjs';
2
2
  export * from 'h3';
3
3
  import 'arri-validate';
4
4
  import 'arri-codegen-utils';