nyte 1.3.2 → 1.3.3

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.
@@ -1,8 +1,29 @@
1
+ export type RpcClientFn = <TReturn = unknown>(...args: any[]) => Promise<TReturn>;
2
+ type DropFirst<T extends any[]> = T extends [any, ...infer R] ? R : T;
3
+ type ClientArgs<Fn> = Fn extends (...args: infer A) => any ? DropFirst<A> : any[];
4
+ type ClientReturn<Fn> = Fn extends (...args: any[]) => infer R ? Awaited<R> : unknown;
5
+ export type RpcClient<TApi extends Record<string, any> = Record<string, any>> = {
6
+ /** Debug helper to inspect which file was passed to importServer() */
7
+ __file: string;
8
+ } & {
9
+ [K in keyof TApi]: TApi[K] extends (...args: any[]) => any ? <TReturn = ClientReturn<TApi[K]>>(...args: ClientArgs<TApi[K]>) => Promise<TReturn> : RpcClientFn;
10
+ } & {
11
+ [key: string]: RpcClientFn;
12
+ };
1
13
  /**
2
14
  * `importServer("src/backend/index.ts")` returns a Proxy where every property is
3
15
  * a function that performs a POST to `/api/rpc`.
4
16
  *
17
+ * Typing:
18
+ * - Without a generic: `const api = importServer('...')` -> `api.anyFn<MyReturn>()`
19
+ * - With a generic: `importServer<typeof import('../../backend/helper')>('...')`
20
+ * gives argument + default return types, while still allowing overrides.
21
+ *
22
+ * Note: server functions can be defined as `(req, ...args)`; the first arg is injected
23
+ * by the server, so the client signature automatically drops that first parameter.
24
+ *
5
25
  * Security note: the server will still validate allowlisted directories.
6
26
  * @param {string} file
7
27
  */
8
- export declare function importServer<T extends Record<string, any> = Record<string, any>>(file: string): T;
28
+ export declare function importServer<TApi extends Record<string, any> = Record<string, any>>(file: string): RpcClient<TApi>;
29
+ export {};
@@ -32,6 +32,14 @@ function asErrorMessage(err) {
32
32
  * `importServer("src/backend/index.ts")` returns a Proxy where every property is
33
33
  * a function that performs a POST to `/api/rpc`.
34
34
  *
35
+ * Typing:
36
+ * - Without a generic: `const api = importServer('...')` -> `api.anyFn<MyReturn>()`
37
+ * - With a generic: `importServer<typeof import('../../backend/helper')>('...')`
38
+ * gives argument + default return types, while still allowing overrides.
39
+ *
40
+ * Note: server functions can be defined as `(req, ...args)`; the first arg is injected
41
+ * by the server, so the client signature automatically drops that first parameter.
42
+ *
35
43
  * Security note: the server will still validate allowlisted directories.
36
44
  * @param {string} file
37
45
  */
@@ -47,7 +55,7 @@ function importServer(file) {
47
55
  if (prop === 'then')
48
56
  return undefined; // prevent await Proxy issues
49
57
  const fnName = String(prop);
50
- return async (...args) => {
58
+ const rpcFn = async (...args) => {
51
59
  const payload = {
52
60
  file,
53
61
  fn: fnName,
@@ -91,6 +99,7 @@ function importServer(file) {
91
99
  }
92
100
  throw new Error(data.error || 'RPC Error');
93
101
  };
102
+ return rpcFn;
94
103
  }
95
104
  };
96
105
  return new Proxy({}, handler);
@@ -172,7 +172,14 @@ async function executeRpc(ctx, payload) {
172
172
  }
173
173
  // ------------------------------------------
174
174
  const rpcRequest = ctx.request ? new http_1.NyteRequest(ctx.request) : buildRpcRequestFromPayload(payload);
175
- const result = await fnValue(rpcRequest, ...payload.args);
175
+ // If the function declares a first parameter, we assume it's the injected request.
176
+ // Otherwise, call it only with payload args.
177
+ // NOTE: `.length` counts only required params; if the function is declared as (req?: NyteRequest, ...)
178
+ // it might be 0, so we treat length>=1 as "expects req".
179
+ const expectsReq = fnValue.length >= 1;
180
+ const result = expectsReq
181
+ ? await fnValue(rpcRequest, ...payload.args)
182
+ : await fnValue(...payload.args);
176
183
  return { success: true, return: result };
177
184
  }
178
185
  catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nyte",
3
- "version": "1.3.2",
3
+ "version": "1.3.3",
4
4
  "description": "Nyte.js is a high-level framework for building web applications with ease and speed. It provides a robust set of tools and features to streamline development and enhance productivity.",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "itsmuzin",