@ereo/rpc 0.1.6
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/README.md +357 -0
- package/dist/client.d.ts +11 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +433 -0
- package/dist/src/client.d.ts +56 -0
- package/dist/src/client.d.ts.map +1 -0
- package/dist/src/context-bridge.d.ts +79 -0
- package/dist/src/context-bridge.d.ts.map +1 -0
- package/dist/src/hooks.d.ts +80 -0
- package/dist/src/hooks.d.ts.map +1 -0
- package/dist/src/index.d.ts +18 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +616 -0
- package/dist/src/middleware.d.ts +123 -0
- package/dist/src/middleware.d.ts.map +1 -0
- package/dist/src/plugin.d.ts +62 -0
- package/dist/src/plugin.d.ts.map +1 -0
- package/dist/src/procedure.d.ts +98 -0
- package/dist/src/procedure.d.ts.map +1 -0
- package/dist/src/router.d.ts +66 -0
- package/dist/src/router.d.ts.map +1 -0
- package/dist/src/types.d.ts +166 -0
- package/dist/src/types.d.ts.map +1 -0
- package/package.json +50 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Common middleware helpers
|
|
3
|
+
*
|
|
4
|
+
* Pre-built middleware for common use cases like auth, rate limiting, and logging.
|
|
5
|
+
*/
|
|
6
|
+
import type { BaseContext, MiddlewareFn } from './types';
|
|
7
|
+
export interface LoggingOptions {
|
|
8
|
+
/** Log function (default: console.log) */
|
|
9
|
+
log?: (...args: unknown[]) => void;
|
|
10
|
+
/** Include timing information */
|
|
11
|
+
timing?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Logs RPC calls with optional timing
|
|
15
|
+
*/
|
|
16
|
+
export declare function logging(options?: LoggingOptions): MiddlewareFn<BaseContext, BaseContext>;
|
|
17
|
+
export interface RateLimitOptions {
|
|
18
|
+
/** Max requests per window */
|
|
19
|
+
limit: number;
|
|
20
|
+
/** Window size in milliseconds */
|
|
21
|
+
windowMs: number;
|
|
22
|
+
/** Key function to identify clients (default: IP address) */
|
|
23
|
+
keyFn?: (ctx: BaseContext) => string;
|
|
24
|
+
/** Error message */
|
|
25
|
+
message?: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Simple in-memory rate limiting
|
|
29
|
+
*
|
|
30
|
+
* For production, consider using Redis or similar.
|
|
31
|
+
*/
|
|
32
|
+
export declare function rateLimit(options: RateLimitOptions): MiddlewareFn<BaseContext, BaseContext>;
|
|
33
|
+
/**
|
|
34
|
+
* Clear the global rate limit store. Useful for testing.
|
|
35
|
+
*/
|
|
36
|
+
export declare function clearRateLimitStore(): void;
|
|
37
|
+
/**
|
|
38
|
+
* Create an auth middleware that extracts user from context
|
|
39
|
+
*
|
|
40
|
+
* Usage:
|
|
41
|
+
* const authMiddleware = createAuthMiddleware(async (ctx) => {
|
|
42
|
+
* const token = ctx.request.headers.get('Authorization');
|
|
43
|
+
* return verifyToken(token); // Returns user or null
|
|
44
|
+
* });
|
|
45
|
+
*/
|
|
46
|
+
export declare function createAuthMiddleware<TUser>(getUser: (ctx: BaseContext) => TUser | null | Promise<TUser | null>, options?: {
|
|
47
|
+
message?: string;
|
|
48
|
+
}): MiddlewareFn<BaseContext, BaseContext & {
|
|
49
|
+
user: TUser;
|
|
50
|
+
}>;
|
|
51
|
+
/**
|
|
52
|
+
* Require specific roles
|
|
53
|
+
*
|
|
54
|
+
* Usage:
|
|
55
|
+
* const adminOnly = requireRoles(['admin']);
|
|
56
|
+
* const adminProcedure = protectedProcedure.use(adminOnly);
|
|
57
|
+
*/
|
|
58
|
+
export declare function requireRoles<TContext extends BaseContext & {
|
|
59
|
+
user: {
|
|
60
|
+
role?: string;
|
|
61
|
+
};
|
|
62
|
+
}>(roles: string[], options?: {
|
|
63
|
+
message?: string;
|
|
64
|
+
}): MiddlewareFn<TContext, TContext>;
|
|
65
|
+
/**
|
|
66
|
+
* Add custom validation to a procedure
|
|
67
|
+
*
|
|
68
|
+
* Usage:
|
|
69
|
+
* const validatedProcedure = procedure.use(
|
|
70
|
+
* validate(async (ctx) => {
|
|
71
|
+
* if (ctx.ctx.maintenanceMode) {
|
|
72
|
+
* return { ok: false, error: { code: 'MAINTENANCE', message: 'System is under maintenance' } };
|
|
73
|
+
* }
|
|
74
|
+
* return { ok: true };
|
|
75
|
+
* })
|
|
76
|
+
* );
|
|
77
|
+
*/
|
|
78
|
+
export declare function validate<TContext extends BaseContext>(validator: (ctx: TContext) => Promise<{
|
|
79
|
+
ok: true;
|
|
80
|
+
} | {
|
|
81
|
+
ok: false;
|
|
82
|
+
error: {
|
|
83
|
+
code: string;
|
|
84
|
+
message: string;
|
|
85
|
+
};
|
|
86
|
+
}>): MiddlewareFn<TContext, TContext>;
|
|
87
|
+
/**
|
|
88
|
+
* Extend context with additional data
|
|
89
|
+
*
|
|
90
|
+
* Usage:
|
|
91
|
+
* const withDb = extend(async (ctx) => ({
|
|
92
|
+
* db: createDbConnection(),
|
|
93
|
+
* }));
|
|
94
|
+
*
|
|
95
|
+
* const dbProcedure = procedure.use(withDb);
|
|
96
|
+
*/
|
|
97
|
+
export declare function extend<TContext extends BaseContext, TExtension extends object>(extender: (ctx: TContext) => TExtension | Promise<TExtension>): MiddlewareFn<TContext, TContext & TExtension>;
|
|
98
|
+
export interface TimingContext {
|
|
99
|
+
timing: {
|
|
100
|
+
start: number;
|
|
101
|
+
getDuration: () => number;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Add timing information to context
|
|
106
|
+
*/
|
|
107
|
+
export declare function timing<TContext extends BaseContext>(): MiddlewareFn<TContext, TContext & TimingContext>;
|
|
108
|
+
/**
|
|
109
|
+
* Catch and transform errors
|
|
110
|
+
*
|
|
111
|
+
* Usage:
|
|
112
|
+
* const withErrorHandling = catchErrors((error) => {
|
|
113
|
+
* if (error instanceof DatabaseError) {
|
|
114
|
+
* return { code: 'DATABASE_ERROR', message: 'Database operation failed' };
|
|
115
|
+
* }
|
|
116
|
+
* throw error; // Re-throw unknown errors
|
|
117
|
+
* });
|
|
118
|
+
*/
|
|
119
|
+
export declare function catchErrors<TContext extends BaseContext>(handler: (error: unknown) => {
|
|
120
|
+
code: string;
|
|
121
|
+
message: string;
|
|
122
|
+
} | never): MiddlewareFn<TContext, TContext>;
|
|
123
|
+
//# sourceMappingURL=middleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../src/middleware.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAoB,MAAM,SAAS,CAAC;AAsE3E,MAAM,WAAW,cAAc;IAC7B,0CAA0C;IAC1C,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,CAAC;IACnC,iCAAiC;IACjC,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAC,OAAO,GAAE,cAAmB,GAAG,YAAY,CAAC,WAAW,EAAE,WAAW,CAAC,CAiB5F;AAMD,MAAM,WAAW,gBAAgB;IAC/B,8BAA8B;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,kCAAkC;IAClC,QAAQ,EAAE,MAAM,CAAC;IACjB,6DAA6D;IAC7D,KAAK,CAAC,EAAE,CAAC,GAAG,EAAE,WAAW,KAAK,MAAM,CAAC;IACrC,oBAAoB;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAOD;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,gBAAgB,GAAG,YAAY,CAAC,WAAW,EAAE,WAAW,CAAC,CAgC3F;AAED;;GAEG;AACH,wBAAgB,mBAAmB,IAAI,IAAI,CAE1C;AAMD;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EACxC,OAAO,EAAE,CAAC,GAAG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CACH;AAED;;GAEG;AACH,wBAAgB,MAAM,CAAC,QAAQ,SAAS,WAAW,KAAK,YAAY,CAAC,QAAQ,EAAE,QAAQ,GAAG,aAAa,CAAC,CAWvG;AAMD;;;;;;;;;;GAUG;AACH,wBAAgB,WAAW,CAAC,QAAQ,SAAS,WAAW,EACtD,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAAG,KAAK,GACrE,YAAY,CAAC,QAAQ,EAAE,QAAQ,CAAC,CASlC"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EreoJS plugin for RPC integration with WebSocket support
|
|
3
|
+
*
|
|
4
|
+
* Usage in ereo.config.ts:
|
|
5
|
+
* import { rpcPlugin } from '@ereo/rpc';
|
|
6
|
+
* import { api } from './api/router';
|
|
7
|
+
*
|
|
8
|
+
* export default defineConfig({
|
|
9
|
+
* plugins: [
|
|
10
|
+
* rpcPlugin({
|
|
11
|
+
* router: api,
|
|
12
|
+
* endpoint: '/api/rpc',
|
|
13
|
+
* }),
|
|
14
|
+
* ],
|
|
15
|
+
* });
|
|
16
|
+
*/
|
|
17
|
+
import type { RouterDef, Router, WSConnectionData } from './types';
|
|
18
|
+
/** Bun WebSocket configuration */
|
|
19
|
+
export interface BunWebSocketConfig<T> {
|
|
20
|
+
message: (ws: any, message: string | Buffer) => void | Promise<void>;
|
|
21
|
+
open?: (ws: any) => void | Promise<void>;
|
|
22
|
+
close?: (ws: any) => void | Promise<void>;
|
|
23
|
+
drain?: (ws: any) => void;
|
|
24
|
+
}
|
|
25
|
+
/** Minimal Plugin interface matching @ereo/core */
|
|
26
|
+
export interface RPCPlugin {
|
|
27
|
+
name: string;
|
|
28
|
+
runtimeMiddleware?: Array<(request: Request, context: any, next: () => Promise<Response>) => Response | Promise<Response>>;
|
|
29
|
+
virtualModules?: Record<string, string>;
|
|
30
|
+
}
|
|
31
|
+
export interface RPCPluginOptions<T extends RouterDef = RouterDef> {
|
|
32
|
+
/** The router instance */
|
|
33
|
+
router: Router<T>;
|
|
34
|
+
/** Endpoint path for HTTP and WebSocket (default: '/api/rpc') */
|
|
35
|
+
endpoint?: string;
|
|
36
|
+
}
|
|
37
|
+
export interface RPCPluginResult extends RPCPlugin {
|
|
38
|
+
/** Get WebSocket handler config for Bun.serve() */
|
|
39
|
+
getWebSocketConfig(): BunWebSocketConfig<WSConnectionData>;
|
|
40
|
+
/** Upgrade a request to WebSocket (call from your server setup) */
|
|
41
|
+
upgradeToWebSocket(server: any, request: Request, ctx: any): boolean;
|
|
42
|
+
}
|
|
43
|
+
export declare function rpcPlugin<T extends RouterDef>(options: RPCPluginOptions<T>): RPCPluginResult;
|
|
44
|
+
/**
|
|
45
|
+
* Helper to create server options that integrate RPC WebSocket support
|
|
46
|
+
*
|
|
47
|
+
* Usage:
|
|
48
|
+
* const rpc = rpcPlugin({ router: api });
|
|
49
|
+
*
|
|
50
|
+
* Bun.serve({
|
|
51
|
+
* port: 3000,
|
|
52
|
+
* fetch(request, server) {
|
|
53
|
+
* // Try WebSocket upgrade first
|
|
54
|
+
* if (rpc.upgradeToWebSocket(server, request, createContext(request))) {
|
|
55
|
+
* return; // Upgraded
|
|
56
|
+
* }
|
|
57
|
+
* // Handle normal requests...
|
|
58
|
+
* },
|
|
59
|
+
* websocket: rpc.getWebSocketConfig(),
|
|
60
|
+
* });
|
|
61
|
+
*/
|
|
62
|
+
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAKnE,kCAAkC;AAClC,MAAM,WAAW,kBAAkB,CAAC,CAAC;IACnC,OAAO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e,CAgFjB;AAED;;;;;;;;;;;;;;;;;GAiBG"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Procedure builder with chainable middleware
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* // Create base procedure
|
|
6
|
+
* const publicProcedure = procedure;
|
|
7
|
+
*
|
|
8
|
+
* // Add middleware for auth
|
|
9
|
+
* const protectedProcedure = procedure.use(async ({ ctx, next }) => {
|
|
10
|
+
* if (!ctx.ctx.user) {
|
|
11
|
+
* return { ok: false, error: { code: 'UNAUTHORIZED', message: 'Not logged in' } };
|
|
12
|
+
* }
|
|
13
|
+
* return next({ ...ctx, user: ctx.ctx.user });
|
|
14
|
+
* });
|
|
15
|
+
*
|
|
16
|
+
* // Use in router
|
|
17
|
+
* const api = createRouter({
|
|
18
|
+
* public: {
|
|
19
|
+
* health: publicProcedure.query(() => ({ status: 'ok' })),
|
|
20
|
+
* },
|
|
21
|
+
* protected: {
|
|
22
|
+
* me: protectedProcedure.query(({ user }) => user),
|
|
23
|
+
* },
|
|
24
|
+
* });
|
|
25
|
+
*/
|
|
26
|
+
import type { Schema, BaseContext, MiddlewareFn, MiddlewareDef, MiddlewareResult, QueryProcedure, MutationProcedure, SubscriptionProcedure, SubscriptionYield } from './types';
|
|
27
|
+
/**
|
|
28
|
+
* Procedure builder with accumulated middleware and context types
|
|
29
|
+
*/
|
|
30
|
+
export interface ProcedureBuilder<TContext extends BaseContext> {
|
|
31
|
+
/**
|
|
32
|
+
* Add middleware that can transform context or short-circuit
|
|
33
|
+
*/
|
|
34
|
+
use<TNewContext extends BaseContext>(middleware: MiddlewareFn<TContext, TNewContext>): ProcedureBuilder<TNewContext>;
|
|
35
|
+
/**
|
|
36
|
+
* Create a query procedure (no input)
|
|
37
|
+
*/
|
|
38
|
+
query<TOutput>(handler: (ctx: TContext) => TOutput | Promise<TOutput>): QueryProcedure<TContext, void, Awaited<TOutput>>;
|
|
39
|
+
/**
|
|
40
|
+
* Create a query procedure (with validated input)
|
|
41
|
+
*/
|
|
42
|
+
query<TInput, TOutput>(schema: Schema<TInput>, handler: (ctx: TContext & {
|
|
43
|
+
input: TInput;
|
|
44
|
+
}) => TOutput | Promise<TOutput>): QueryProcedure<TContext, TInput, Awaited<TOutput>>;
|
|
45
|
+
/**
|
|
46
|
+
* Create a mutation procedure (no input)
|
|
47
|
+
*/
|
|
48
|
+
mutation<TOutput>(handler: (ctx: TContext) => TOutput | Promise<TOutput>): MutationProcedure<TContext, void, Awaited<TOutput>>;
|
|
49
|
+
/**
|
|
50
|
+
* Create a mutation procedure (with validated input)
|
|
51
|
+
*/
|
|
52
|
+
mutation<TInput, TOutput>(schema: Schema<TInput>, handler: (ctx: TContext & {
|
|
53
|
+
input: TInput;
|
|
54
|
+
}) => TOutput | Promise<TOutput>): MutationProcedure<TContext, TInput, Awaited<TOutput>>;
|
|
55
|
+
/**
|
|
56
|
+
* Create a subscription procedure (no input)
|
|
57
|
+
*/
|
|
58
|
+
subscription<TOutput>(handler: (ctx: TContext) => SubscriptionYield<TOutput>): SubscriptionProcedure<TContext, void, TOutput>;
|
|
59
|
+
/**
|
|
60
|
+
* Create a subscription procedure (with validated input)
|
|
61
|
+
*/
|
|
62
|
+
subscription<TInput, TOutput>(schema: Schema<TInput>, handler: (ctx: TContext & {
|
|
63
|
+
input: TInput;
|
|
64
|
+
}) => SubscriptionYield<TOutput>): SubscriptionProcedure<TContext, TInput, TOutput>;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Base procedure builder - starting point for all procedures
|
|
68
|
+
*/
|
|
69
|
+
export declare const procedure: ProcedureBuilder<BaseContext>;
|
|
70
|
+
/**
|
|
71
|
+
* Execute middleware chain and return final context
|
|
72
|
+
*/
|
|
73
|
+
export declare function executeMiddleware<TContext extends BaseContext>(middlewares: MiddlewareDef<any, any>[], initialCtx: BaseContext): Promise<MiddlewareResult<TContext>>;
|
|
74
|
+
/**
|
|
75
|
+
* Create a query procedure without middleware
|
|
76
|
+
* @deprecated Use `procedure.query()` instead for middleware support
|
|
77
|
+
*/
|
|
78
|
+
export declare function query<TOutput>(handler: (ctx: BaseContext) => TOutput | Promise<TOutput>): QueryProcedure<BaseContext, void, Awaited<TOutput>>;
|
|
79
|
+
export declare function query<TInput, TOutput>(schema: Schema<TInput>, handler: (ctx: BaseContext & {
|
|
80
|
+
input: TInput;
|
|
81
|
+
}) => TOutput | Promise<TOutput>): QueryProcedure<BaseContext, TInput, Awaited<TOutput>>;
|
|
82
|
+
/**
|
|
83
|
+
* Create a mutation procedure without middleware
|
|
84
|
+
* @deprecated Use `procedure.mutation()` instead for middleware support
|
|
85
|
+
*/
|
|
86
|
+
export declare function mutation<TOutput>(handler: (ctx: BaseContext) => TOutput | Promise<TOutput>): MutationProcedure<BaseContext, void, Awaited<TOutput>>;
|
|
87
|
+
export declare function mutation<TInput, TOutput>(schema: Schema<TInput>, handler: (ctx: BaseContext & {
|
|
88
|
+
input: TInput;
|
|
89
|
+
}) => TOutput | Promise<TOutput>): MutationProcedure<BaseContext, TInput, Awaited<TOutput>>;
|
|
90
|
+
/**
|
|
91
|
+
* Create a subscription procedure without middleware
|
|
92
|
+
* @deprecated Use `procedure.subscription()` instead for middleware support
|
|
93
|
+
*/
|
|
94
|
+
export declare function subscription<TOutput>(handler: (ctx: BaseContext) => SubscriptionYield<TOutput>): SubscriptionProcedure<BaseContext, void, TOutput>;
|
|
95
|
+
export declare function subscription<TInput, TOutput>(schema: Schema<TInput>, handler: (ctx: BaseContext & {
|
|
96
|
+
input: TInput;
|
|
97
|
+
}) => SubscriptionYield<TOutput>): SubscriptionProcedure<BaseContext, TInput, TOutput>;
|
|
98
|
+
//# sourceMappingURL=procedure.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"procedure.d.ts","sourceRoot":"","sources":["../../src/procedure.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,KAAK,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createRouter({
|
|
6
|
+
* users: {
|
|
7
|
+
* me: protectedProcedure.query(({ user }) => user),
|
|
8
|
+
* },
|
|
9
|
+
* posts: {
|
|
10
|
+
* list: procedure.query(() => db.post.findMany()),
|
|
11
|
+
* onCreate: procedure.subscription(async function* () {
|
|
12
|
+
* for await (const post of postCreatedEvents) {
|
|
13
|
+
* yield post;
|
|
14
|
+
* }
|
|
15
|
+
* }),
|
|
16
|
+
* },
|
|
17
|
+
* });
|
|
18
|
+
*/
|
|
19
|
+
import type { RouterDef, WSConnectionData } from './types';
|
|
20
|
+
/** Bun WebSocket handler type */
|
|
21
|
+
export interface BunWebSocketHandler<T> {
|
|
22
|
+
message: (ws: BunServerWebSocket<T>, message: string | Buffer) => void | Promise<void>;
|
|
23
|
+
open?: (ws: BunServerWebSocket<T>) => void | Promise<void>;
|
|
24
|
+
close?: (ws: BunServerWebSocket<T>) => void | Promise<void>;
|
|
25
|
+
drain?: (ws: BunServerWebSocket<T>) => void;
|
|
26
|
+
}
|
|
27
|
+
/** Bun ServerWebSocket interface (simplified) */
|
|
28
|
+
interface BunServerWebSocket<T> {
|
|
29
|
+
data: T;
|
|
30
|
+
send(message: string | Buffer): number;
|
|
31
|
+
close(code?: number, reason?: string): void;
|
|
32
|
+
subscribe(topic: string): void;
|
|
33
|
+
unsubscribe(topic: string): void;
|
|
34
|
+
publish(topic: string, message: string | Buffer): void;
|
|
35
|
+
isSubscribed(topic: string): boolean;
|
|
36
|
+
readonly readyState: number;
|
|
37
|
+
readonly remoteAddress: string;
|
|
38
|
+
}
|
|
39
|
+
export interface Router<T extends RouterDef> {
|
|
40
|
+
_def: T;
|
|
41
|
+
/** HTTP request handler for queries and mutations */
|
|
42
|
+
handler: (request: Request, ctx: any) => Promise<Response>;
|
|
43
|
+
/** WebSocket handlers for Bun.serve() */
|
|
44
|
+
websocket: BunWebSocketHandler<WSConnectionData>;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Create a router from procedure definitions
|
|
48
|
+
*/
|
|
49
|
+
export declare function createRouter<T extends RouterDef>(def: T): Router<T>;
|
|
50
|
+
/**
|
|
51
|
+
* Custom RPC error class
|
|
52
|
+
*/
|
|
53
|
+
export declare class RPCError extends Error {
|
|
54
|
+
code: string;
|
|
55
|
+
status: number;
|
|
56
|
+
constructor(code: string, message: string, status?: number);
|
|
57
|
+
}
|
|
58
|
+
/** Common error factories */
|
|
59
|
+
export declare const errors: {
|
|
60
|
+
unauthorized: (message?: string) => RPCError;
|
|
61
|
+
forbidden: (message?: string) => RPCError;
|
|
62
|
+
notFound: (message?: string) => RPCError;
|
|
63
|
+
badRequest: (message: string) => RPCError;
|
|
64
|
+
};
|
|
65
|
+
export {};
|
|
66
|
+
//# sourceMappingURL=router.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../../src/router.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EACV,SAAS,EAOT,gBAAgB,EAEjB,MAAM,SAAS,CAAC;AAGjB,iCAAiC;AACjC,MAAM,WAAW,mBAAmB,CAAC,CAAC;IACpC,OAAO,EAAE,CAAC,EAAE,EAAE,kBAAkB,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACvF,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,kBAAkB,CAAC,CAAC,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3D,KAAK,CAAC,EAAE,CAAC,EAAE,EAAE,kBAAkB,CAAC,CAAC,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5D,KAAK,CAAC,EAAE,CAAC,EAAE,EAAE,kBAAkB,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;CAC7C;AAED,iDAAiD;AACjD,UAAU,kBAAk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+
* @ereo/rpc - Typed RPC layer for EreoJS
|
|
3
|
+
*
|
|
4
|
+
* Core type definitions for end-to-end type inference
|
|
5
|
+
*/
|
|
6
|
+
/** Schema interface (Zod-compatible but not Zod-dependent) */
|
|
7
|
+
export interface Schema<T> {
|
|
8
|
+
parse(data: unknown): T;
|
|
9
|
+
safeParse?(data: unknown): {
|
|
10
|
+
success: true;
|
|
11
|
+
data: T;
|
|
12
|
+
} | {
|
|
13
|
+
success: false;
|
|
14
|
+
error: unknown;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/** Base context available to all procedures */
|
|
18
|
+
export interface BaseContext {
|
|
19
|
+
/** Application context (from @ereo/core) */
|
|
20
|
+
ctx: any;
|
|
21
|
+
/** The original HTTP request */
|
|
22
|
+
request: Request;
|
|
23
|
+
}
|
|
24
|
+
/** Extended context after middleware runs */
|
|
25
|
+
export type ExtendedContext<TBase, TExtension> = TBase & TExtension;
|
|
26
|
+
/** Result of middleware execution */
|
|
27
|
+
export type MiddlewareResult<TContext> = {
|
|
28
|
+
ok: true;
|
|
29
|
+
ctx: TContext;
|
|
30
|
+
} | {
|
|
31
|
+
ok: false;
|
|
32
|
+
error: RPCErrorShape;
|
|
33
|
+
};
|
|
34
|
+
/** Middleware function signature */
|
|
35
|
+
export type MiddlewareFn<TContextIn, TContextOut> = (opts: {
|
|
36
|
+
ctx: TContextIn;
|
|
37
|
+
next: <T>(ctx: T) => MiddlewareResult<T>;
|
|
38
|
+
}) => MiddlewareResult<TContextOut> | Promise<MiddlewareResult<TContextOut>>;
|
|
39
|
+
/** Middleware definition stored on procedure builder */
|
|
40
|
+
export interface MiddlewareDef<TContextIn, TContextOut> {
|
|
41
|
+
fn: MiddlewareFn<TContextIn, TContextOut>;
|
|
42
|
+
}
|
|
43
|
+
export type ProcedureType = 'query' | 'mutation' | 'subscription';
|
|
44
|
+
/** Base procedure definition */
|
|
45
|
+
export interface ProcedureDef<TContext, TInput, TOutput> {
|
|
46
|
+
_type: ProcedureType;
|
|
47
|
+
_ctx: TContext;
|
|
48
|
+
_input: TInput;
|
|
49
|
+
_output: TOutput;
|
|
50
|
+
middlewares: MiddlewareDef<any, any>[];
|
|
51
|
+
inputSchema?: Schema<TInput>;
|
|
52
|
+
handler: (args: TContext & {
|
|
53
|
+
input: TInput;
|
|
54
|
+
}) => TOutput | Promise<TOutput>;
|
|
55
|
+
}
|
|
56
|
+
/** Query procedure */
|
|
57
|
+
export interface QueryProcedure<TContext, TInput, TOutput> extends ProcedureDef<TContext, TInput, TOutput> {
|
|
58
|
+
_type: 'query';
|
|
59
|
+
}
|
|
60
|
+
/** Mutation procedure */
|
|
61
|
+
export interface MutationProcedure<TContext, TInput, TOutput> extends ProcedureDef<TContext, TInput, TOutput> {
|
|
62
|
+
_type: 'mutation';
|
|
63
|
+
}
|
|
64
|
+
/** Subscription yield type */
|
|
65
|
+
export type SubscriptionYield<T> = AsyncGenerator<T, void, unknown>;
|
|
66
|
+
/** Subscription procedure */
|
|
67
|
+
export interface SubscriptionProcedure<TContext, TInput, TOutput> {
|
|
68
|
+
_type: 'subscription';
|
|
69
|
+
_ctx: TContext;
|
|
70
|
+
_input: TInput;
|
|
71
|
+
_output: TOutput;
|
|
72
|
+
middlewares: MiddlewareDef<any, any>[];
|
|
73
|
+
inputSchema?: Schema<TInput>;
|
|
74
|
+
handler: (args: TContext & {
|
|
75
|
+
input: TInput;
|
|
76
|
+
}) => SubscriptionYield<TOutput>;
|
|
77
|
+
}
|
|
78
|
+
export type AnyProcedure = QueryProcedure<any, any, any> | MutationProcedure<any, any, any> | SubscriptionProcedure<any, any, any>;
|
|
79
|
+
/** Router definition - nested object of procedures */
|
|
80
|
+
export type RouterDef = {
|
|
81
|
+
[key: string]: AnyProcedure | RouterDef;
|
|
82
|
+
};
|
|
83
|
+
/** Router instance */
|
|
84
|
+
export interface Router<T extends RouterDef> {
|
|
85
|
+
_def: T;
|
|
86
|
+
handler: (request: Request, ctx: any) => Promise<Response>;
|
|
87
|
+
websocket: any;
|
|
88
|
+
}
|
|
89
|
+
/** Infer client type from router definition */
|
|
90
|
+
export type InferClient<T extends RouterDef> = {
|
|
91
|
+
[K in keyof T]: T[K] extends QueryProcedure<any, infer TInput, infer TOutput> ? TInput extends void ? {
|
|
92
|
+
query: () => Promise<TOutput>;
|
|
93
|
+
} : {
|
|
94
|
+
query: (input: TInput) => Promise<TOutput>;
|
|
95
|
+
} : T[K] extends MutationProcedure<any, infer TInput, infer TOutput> ? TInput extends void ? {
|
|
96
|
+
mutate: () => Promise<TOutput>;
|
|
97
|
+
} : {
|
|
98
|
+
mutate: (input: TInput) => Promise<TOutput>;
|
|
99
|
+
} : T[K] extends SubscriptionProcedure<any, infer TInput, infer TOutput> ? TInput extends void ? {
|
|
100
|
+
subscribe: (callbacks: SubscriptionCallbacks<TOutput>) => Unsubscribe;
|
|
101
|
+
} : {
|
|
102
|
+
subscribe: (input: TInput, callbacks: SubscriptionCallbacks<TOutput>) => Unsubscribe;
|
|
103
|
+
} : T[K] extends RouterDef ? InferClient<T[K]> : never;
|
|
104
|
+
};
|
|
105
|
+
/** Subscription callbacks */
|
|
106
|
+
export interface SubscriptionCallbacks<T> {
|
|
107
|
+
onData: (data: T) => void;
|
|
108
|
+
onError?: (error: Error) => void;
|
|
109
|
+
onComplete?: () => void;
|
|
110
|
+
}
|
|
111
|
+
/** Unsubscribe function */
|
|
112
|
+
export type Unsubscribe = () => void;
|
|
113
|
+
/** RPC request over HTTP */
|
|
114
|
+
export interface RPCRequest {
|
|
115
|
+
path: string[];
|
|
116
|
+
type: 'query' | 'mutation';
|
|
117
|
+
input?: unknown;
|
|
118
|
+
}
|
|
119
|
+
/** WebSocket message types */
|
|
120
|
+
export type WSClientMessage = {
|
|
121
|
+
type: 'subscribe';
|
|
122
|
+
id: string;
|
|
123
|
+
path: string[];
|
|
124
|
+
input?: unknown;
|
|
125
|
+
} | {
|
|
126
|
+
type: 'unsubscribe';
|
|
127
|
+
id: string;
|
|
128
|
+
} | {
|
|
129
|
+
type: 'ping';
|
|
130
|
+
};
|
|
131
|
+
export type WSServerMessage = {
|
|
132
|
+
type: 'data';
|
|
133
|
+
id: string;
|
|
134
|
+
data: unknown;
|
|
135
|
+
} | {
|
|
136
|
+
type: 'error';
|
|
137
|
+
id: string;
|
|
138
|
+
error: RPCErrorShape;
|
|
139
|
+
} | {
|
|
140
|
+
type: 'complete';
|
|
141
|
+
id: string;
|
|
142
|
+
} | {
|
|
143
|
+
type: 'pong';
|
|
144
|
+
};
|
|
145
|
+
/** RPC error shape */
|
|
146
|
+
export interface RPCErrorShape {
|
|
147
|
+
code: string;
|
|
148
|
+
message: string;
|
|
149
|
+
details?: unknown;
|
|
150
|
+
}
|
|
151
|
+
/** RPC response */
|
|
152
|
+
export type RPCResponse<T = unknown> = {
|
|
153
|
+
ok: true;
|
|
154
|
+
data: T;
|
|
155
|
+
} | {
|
|
156
|
+
ok: false;
|
|
157
|
+
error: RPCErrorShape;
|
|
158
|
+
};
|
|
159
|
+
/** Per-connection WebSocket data */
|
|
160
|
+
export interface WSConnectionData {
|
|
161
|
+
subscriptions: Map<string, AbortController>;
|
|
162
|
+
ctx: any;
|
|
163
|
+
/** Original HTTP request that initiated the WebSocket upgrade */
|
|
164
|
+
originalRequest?: Request;
|
|
165
|
+
}
|
|
166
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAMH,8DAA8D;AAC9D,MAAM,WAAW,MAAM,CAAC,CAAC;IACvB,KAAK,CAAC,IAAI,EAAE,OAAO,GAAG,CAAC,CAAC;IACxB,SAAS,CAAC,CAAC,IAAI,EAAE,OAAO,GAAG;QAAE,OAAO,EAAE,IAAI,CAAC;QAAC,IAAI,EAAE,CAAC,CAAA;KAAE,GAAG;QAAE,OAAO,EAAE,KAAK,CAAC;QAAC,KAAK,EAAE,OAAO,CAAA;KAA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AAC;IAAC,KAAK,EAAE,aAAa,CAAA;CAAE,GACnD;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,GAChC;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAErB,sBAAsB;AACtB,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED,mBAAmB;AACnB,MAAM,MAAM,WAAW,CAAC,CAAC,GAAG,OAAO,IAC/B;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,CAAC,CAAA;CAAE,GACrB;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,aAAa,CAAA;CAAE,CAAC;AAMxC,oCAAoC;AACpC,MAAM,WAAW,gBAAgB;IAC/B,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IAC5C,GAAG,EAAE,GAAG,CAAC;IACT,iEAAiE;IACjE,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B"}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ereo/rpc",
|
|
3
|
+
"version": "0.1.6",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"author": "Ereo Team",
|
|
6
|
+
"homepage": "https://ereo.dev",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/ereojs/ereo.git",
|
|
10
|
+
"directory": "packages/rpc"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/ereojs/ereo/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./client": {
|
|
24
|
+
"types": "./dist/client.d.ts",
|
|
25
|
+
"import": "./dist/client.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "bun build ./src/index.ts ./client.ts --outdir ./dist --target bun --external @ereo/core --external react && bun run build:types",
|
|
33
|
+
"build:types": "tsc --emitDeclarationOnly --outDir dist",
|
|
34
|
+
"dev": "bun build ./src/index.ts ./client.ts --outdir ./dist --target bun --watch",
|
|
35
|
+
"test": "bun test",
|
|
36
|
+
"typecheck": "tsc --noEmit"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@ereo/core": "workspace:*"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"react": ">=18.0.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/bun": "^1.1.0",
|
|
46
|
+
"@types/react": "^18.2.0",
|
|
47
|
+
"react": "^18.2.0",
|
|
48
|
+
"typescript": "^5.4.0"
|
|
49
|
+
}
|
|
50
|
+
}
|