@zap-js/server 0.0.4 → 0.0.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/index.d.ts +143 -0
- package/package.json +9 -4
package/index.d.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zap-js/server
|
|
3
|
+
*
|
|
4
|
+
* ZapJS Server Package - TypeScript Declarations
|
|
5
|
+
* High-performance fullstack React framework with Rust backend
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* RPC namespace for calling server functions via IPC
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { rpc } from '@zap-js/server';
|
|
14
|
+
*
|
|
15
|
+
* const result = await rpc.call<User | ApiError>('get_user', { id: '123' });
|
|
16
|
+
* if ('error' in result) {
|
|
17
|
+
* console.error(result.error);
|
|
18
|
+
* } else {
|
|
19
|
+
* console.log(result.name);
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare const rpc: {
|
|
24
|
+
/**
|
|
25
|
+
* Make a type-safe RPC call to a Rust server function
|
|
26
|
+
* Uses IPC with MessagePack serialization for ultra-fast communication (< 1ms)
|
|
27
|
+
*
|
|
28
|
+
* @template T - The return type (usually a union of success type | error type)
|
|
29
|
+
* @param functionName - Name of the Rust function marked with #[export]
|
|
30
|
+
* @param params - Parameters to pass (must be JSON-serializable)
|
|
31
|
+
* @returns Promise resolving to the function result
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* // Call a server function
|
|
36
|
+
* const user = await rpc.call<User>('get_user', { id: '123' });
|
|
37
|
+
*
|
|
38
|
+
* // With error handling (Result<T, E> pattern)
|
|
39
|
+
* const result = await rpc.call<User | ApiError>('get_user', { id: '123' });
|
|
40
|
+
* if ('error' in result) {
|
|
41
|
+
* // Handle error
|
|
42
|
+
* } else {
|
|
43
|
+
* // Use user data
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
call<T = unknown>(functionName: string, params?: Record<string, unknown>): Promise<T>;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Types namespace - Common type utilities
|
|
52
|
+
*/
|
|
53
|
+
export declare const types: {
|
|
54
|
+
[key: string]: any;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Request object passed to API route handlers
|
|
59
|
+
*/
|
|
60
|
+
export interface ZapRequest {
|
|
61
|
+
/** HTTP method (GET, POST, PUT, DELETE, etc.) */
|
|
62
|
+
method: string;
|
|
63
|
+
/** Full path including query string */
|
|
64
|
+
path: string;
|
|
65
|
+
/** Path without query string */
|
|
66
|
+
path_only: string;
|
|
67
|
+
/** Parsed query parameters */
|
|
68
|
+
query: Record<string, string>;
|
|
69
|
+
/** Dynamic route parameters */
|
|
70
|
+
params: Record<string, string>;
|
|
71
|
+
/** HTTP headers */
|
|
72
|
+
headers: Record<string, string>;
|
|
73
|
+
/** Request body as string */
|
|
74
|
+
body: string;
|
|
75
|
+
/** Parsed cookies */
|
|
76
|
+
cookies: Record<string, string>;
|
|
77
|
+
/** Unique request ID for tracing */
|
|
78
|
+
request_id?: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Response object returned from API route handlers
|
|
83
|
+
*/
|
|
84
|
+
export interface ZapResponse {
|
|
85
|
+
/** HTTP status code (default: 200) */
|
|
86
|
+
status?: number;
|
|
87
|
+
/** Response headers */
|
|
88
|
+
headers?: Record<string, string>;
|
|
89
|
+
/** Response body (will be JSON.stringify'd if object) */
|
|
90
|
+
body?: any;
|
|
91
|
+
/** Raw response data (alternative to body) */
|
|
92
|
+
data?: string;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Handler function for HTTP methods in API routes
|
|
97
|
+
*/
|
|
98
|
+
export type RouteHandler = (req: ZapRequest) => Promise<ZapResponse | any> | ZapResponse | any;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Async generator handler for streaming responses (SSE)
|
|
102
|
+
*/
|
|
103
|
+
export type StreamHandler = (req: ZapRequest) => AsyncGenerator<{ data: string }, void, unknown>;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* WebSocket connection interface
|
|
107
|
+
*/
|
|
108
|
+
export interface WsConnection {
|
|
109
|
+
/** Unique connection ID */
|
|
110
|
+
id: string;
|
|
111
|
+
/** Send a message to the client */
|
|
112
|
+
send(message: string | Uint8Array): void;
|
|
113
|
+
/** Close the connection */
|
|
114
|
+
close(code?: number, reason?: string): void;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* WebSocket handler interface
|
|
119
|
+
*/
|
|
120
|
+
export interface WsHandler {
|
|
121
|
+
/** Called when a client connects */
|
|
122
|
+
onConnect?: (connection: WsConnection) => Promise<void> | void;
|
|
123
|
+
/** Called when a message is received */
|
|
124
|
+
onMessage?: (connection: WsConnection, message: string | Uint8Array) => Promise<void> | void;
|
|
125
|
+
/** Called when the connection closes */
|
|
126
|
+
onClose?: (connection: WsConnection, code: number, reason: string) => Promise<void> | void;
|
|
127
|
+
/** Called when an error occurs */
|
|
128
|
+
onError?: (connection: WsConnection, error: Error) => Promise<void> | void;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* API route module exports
|
|
133
|
+
*/
|
|
134
|
+
export interface ApiRoute {
|
|
135
|
+
GET?: RouteHandler | StreamHandler;
|
|
136
|
+
POST?: RouteHandler;
|
|
137
|
+
PUT?: RouteHandler;
|
|
138
|
+
DELETE?: RouteHandler;
|
|
139
|
+
PATCH?: RouteHandler;
|
|
140
|
+
HEAD?: RouteHandler;
|
|
141
|
+
OPTIONS?: RouteHandler;
|
|
142
|
+
WEBSOCKET?: WsHandler;
|
|
143
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zap-js/server",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "High-performance fullstack React framework - Server package with Rust-powered backend",
|
|
5
5
|
"homepage": "https://github.com/saint0x/zapjs",
|
|
6
6
|
"repository": {
|
|
@@ -12,14 +12,19 @@
|
|
|
12
12
|
},
|
|
13
13
|
"type": "module",
|
|
14
14
|
"main": "./index.js",
|
|
15
|
+
"types": "./index.d.ts",
|
|
15
16
|
"exports": {
|
|
16
|
-
".":
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./index.d.ts",
|
|
19
|
+
"default": "./index.js"
|
|
20
|
+
}
|
|
17
21
|
},
|
|
18
22
|
"files": [
|
|
19
|
-
"index.js"
|
|
23
|
+
"index.js",
|
|
24
|
+
"index.d.ts"
|
|
20
25
|
],
|
|
21
26
|
"dependencies": {
|
|
22
|
-
"@zap-js/client": "^0.0.
|
|
27
|
+
"@zap-js/client": "^0.0.6"
|
|
23
28
|
},
|
|
24
29
|
"keywords": [
|
|
25
30
|
"zap",
|