rpc-express-toolkit 4.1.0 → 4.2.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/README.md CHANGED
@@ -5,6 +5,7 @@
5
5
  [![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
6
6
  [![npm version](https://img.shields.io/npm/v/rpc-express-toolkit.svg)](https://www.npmjs.com/package/rpc-express-toolkit)
7
7
  [![npm downloads](https://img.shields.io/npm/dm/rpc-express-toolkit.svg)](https://www.npmjs.com/package/rpc-express-toolkit)
8
+ [![node](https://img.shields.io/node/v/rpc-express-toolkit.svg)](https://www.npmjs.com/package/rpc-express-toolkit)
8
9
 
9
10
  Enterprise-ready JSON-RPC 2.0 toolkit for Express.js with simplified APIs, middleware, schema validation, batch support, and optional safe type disambiguation.
10
11
 
@@ -61,6 +62,9 @@ Safe type disambiguation is disabled by default for maximum JSON-RPC 2.0 compati
61
62
 
62
63
  ### Safe Import (opt-in)
63
64
 
65
+ [![npm (proxy)](https://img.shields.io/npm/v/rpc-express-toolkit-safe.svg)](https://www.npmjs.com/package/rpc-express-toolkit-safe)
66
+ [![safe preset](https://img.shields.io/badge/safe%20preset-available-blue)](#safe-import-opt-in)
67
+
64
68
  When you control both client and server and want safer type round-trips, import the safe preset. It enables safe serialization by default (and strict mode on the server):
65
69
 
66
70
  ```js
@@ -80,6 +84,27 @@ const client = createSafeClient('http://localhost:3000/api');
80
84
 
81
85
  This keeps JSON-RPC 2.0 compliance as default for the main entrypoint, while offering a convenient safe-mode import for projects that prefer explicit type disambiguation.
82
86
 
87
+ Alternative: npm proxy package
88
+
89
+ If you prefer a dedicated package name, you can install the thin proxy `rpc-express-toolkit-safe` which simply re-exports the safe preset:
90
+
91
+ ```bash
92
+ npm install rpc-express-toolkit-safe
93
+ ```
94
+
95
+ ```js
96
+ // Server
97
+ const express = require('express');
98
+ const { createSafeEndpoint } = require('rpc-express-toolkit-safe');
99
+ const app = express();
100
+ app.use(express.json());
101
+ const rpc = createSafeEndpoint(app, {}, { endpoint: '/api' });
102
+
103
+ // Client
104
+ const { createSafeClient } = require('rpc-express-toolkit-safe');
105
+ const client = createSafeClient('http://localhost:3000/api');
106
+ ```
107
+
83
108
  ## Full Details
84
109
 
85
110
  For advanced configuration, middleware, structured logging, safe serialization, error handling, and more, see `README_ADVANCED.md`.
@@ -1,6 +1,6 @@
1
1
  const express = require('express');
2
2
  /* eslint-disable import/no-unresolved, prettier/prettier */
3
- const { createSafeEndpoint, createSafeClient } = require('rpc-express-toolkit/safe');
3
+ const { RpcSafeEndpoint, RpcSafeClient } = require('rpc-express-toolkit/safe');
4
4
  /* eslint-enable import/no-unresolved, prettier/prettier */
5
5
 
6
6
  async function main() {
@@ -8,7 +8,7 @@ async function main() {
8
8
  app.use(express.json());
9
9
 
10
10
  // Server with safe defaults (safeEnabled=true, strictMode=true)
11
- const rpc = createSafeEndpoint(app, {}, { endpoint: '/api' });
11
+ const rpc = new RpcSafeEndpoint(app, {}, { endpoint: '/api' });
12
12
 
13
13
  rpc.addMethod('sum', (req, ctx, params) => {
14
14
  // params are already deserialized according to the client's safe header
@@ -29,7 +29,7 @@ async function main() {
29
29
  console.log('Safe server listening on', url);
30
30
 
31
31
  // Safe client (sends header and expects safe response)
32
- const client = createSafeClient(url);
32
+ const client = new RpcSafeClient(url);
33
33
  const res = await client.call('sum', { a: '10n', b: '32n' });
34
34
  console.log('sum result (BigInt):', res.toString());
35
35
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rpc-express-toolkit",
3
- "version": "4.1.0",
3
+ "version": "4.2.0",
4
4
  "description": "Enterprise-ready JSON-RPC 2.0 toolkit for Express.js with simplified APIs, structured logging, middleware system, schema validation, comprehensive BigInt/Date support. JSON middleware validation and explicit configuration.",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
package/src/safe.d.ts CHANGED
@@ -2,6 +2,38 @@ import type { Router } from 'express';
2
2
  import type { RpcEndpoint, RpcClient, RpcEndpointOptions, RpcClientOptions } from './index';
3
3
 
4
4
  /**
5
+ * RPC Client with safe serialization enabled by default.
6
+ * Extends RpcClient with safeEnabled: true preset.
7
+ */
8
+ export class RpcSafeClient extends RpcClient {
9
+ constructor(
10
+ endpoint: string,
11
+ defaultHeaders?: Record<string, string>,
12
+ options?: RpcClientOptions
13
+ );
14
+ }
15
+
16
+ /**
17
+ * RPC Endpoint with safe serialization enabled by default.
18
+ * Extends RpcEndpoint with safeEnabled: true and strictMode: true presets.
19
+ */
20
+ export class RpcSafeEndpoint<C = any> extends RpcEndpoint<C> {
21
+ constructor(
22
+ router: Router,
23
+ context: C,
24
+ endpoint?: string,
25
+ options?: RpcEndpointOptions
26
+ );
27
+
28
+ constructor(
29
+ router: Router,
30
+ context: C,
31
+ options?: RpcEndpointOptions
32
+ );
33
+ }
34
+
35
+ /**
36
+ * @deprecated Use `new RpcSafeEndpoint(...)` instead.
5
37
  * Factory: create a RpcEndpoint with safe defaults (safeEnabled=true, strictMode=true).
6
38
  */
7
39
  export function createSafeEndpoint<C = any>(
@@ -18,6 +50,7 @@ export function createSafeEndpoint<C = any>(
18
50
  ): RpcEndpoint<C>;
19
51
 
20
52
  /**
53
+ * @deprecated Use `new RpcSafeClient(...)` instead.
21
54
  * Factory: create a RpcClient with safe defaults (safeEnabled=true).
22
55
  */
23
56
  export function createSafeClient(
package/src/safe.js CHANGED
@@ -1,14 +1,72 @@
1
+ /* eslint-disable max-classes-per-file */
1
2
  const Main = require('./index');
2
3
 
3
- // Default safe options applied unless explicitly overridden
4
- const defaultSafeOptions = {
5
- safeEnabled: true,
6
- strictMode: true,
7
- };
4
+ /**
5
+ * RPC Client with safe serialization enabled by default.
6
+ * Extends RpcClient with safeEnabled: true preset.
7
+ *
8
+ * This class automatically enables safe prefixes for strings (S:) and dates (D:)
9
+ * to avoid ambiguity with BigInt and ISO date strings.
10
+ *
11
+ * @example
12
+ * const client = new RpcSafeClient('http://localhost:3000/api');
13
+ * const result = await client.call('add', { a: 10n, b: 20n });
14
+ */
15
+ class RpcSafeClient extends Main.RpcClient {
16
+ /**
17
+ * @param {string} endpoint The JSON-RPC endpoint URL.
18
+ * @param {Object} [defaultHeaders={}] Optional default headers to include in requests.
19
+ * @param {Object} [options={}] Optional configuration options (safeEnabled will be set to true).
20
+ */
21
+ constructor(endpoint, defaultHeaders = {}, options = {}) {
22
+ super(endpoint, defaultHeaders, { safeEnabled: true, ...options });
23
+ }
24
+ }
25
+
26
+ /**
27
+ * RPC Endpoint with safe serialization enabled by default.
28
+ * Extends RpcEndpoint with safeEnabled: true and strictMode: true presets.
29
+ *
30
+ * This class automatically enables safe prefixes for strings (S:) and dates (D:)
31
+ * and enforces strict compatibility checking with clients.
32
+ *
33
+ * @example
34
+ * const app = express();
35
+ * const rpc = new RpcSafeEndpoint(app, {});
36
+ * rpc.addMethod('add', {
37
+ * handler: (req, ctx, params) => params.a + params.b,
38
+ * });
39
+ *
40
+ * @template C
41
+ */
42
+ class RpcSafeEndpoint extends Main.RpcEndpoint {
43
+ /**
44
+ * @param {import('express').Router} router Express router or app
45
+ * @param {C} context Context object passed to handlers
46
+ * @param {string|object} [endpointOrOptions='/api'] Endpoint path or options object
47
+ * @param {object} [options] Options object (when endpointOrOptions is a string)
48
+ */
49
+ constructor(router, context, endpointOrOptions = '/api', options = {}) {
50
+ const defaultSafeOptions = {
51
+ safeEnabled: true,
52
+ strictMode: true,
53
+ };
54
+
55
+ if (typeof endpointOrOptions === 'string') {
56
+ // String endpoint with options object
57
+ const finalOptions = { ...defaultSafeOptions, ...options };
58
+ super(router, context, endpointOrOptions, finalOptions);
59
+ } else {
60
+ // Options object form
61
+ const finalOptions = { ...defaultSafeOptions, ...endpointOrOptions };
62
+ super(router, context, finalOptions);
63
+ }
64
+ }
65
+ }
8
66
 
9
67
  /**
68
+ * @deprecated Use `new RpcSafeEndpoint(...)` instead.
10
69
  * Create a RpcEndpoint with safe defaults (safeEnabled=true, strictMode=true).
11
- * Preserves both constructor signatures supported by RpcEndpoint.
12
70
  *
13
71
  * @param {import('express').Router} router
14
72
  * @param {any} context
@@ -21,34 +79,34 @@ function createSafeEndpoint(
21
79
  endpointOrOptions = '/api',
22
80
  options = {}
23
81
  ) {
24
- if (typeof endpointOrOptions === 'string') {
25
- const finalOptions = { ...defaultSafeOptions, ...options };
26
- return new Main.RpcEndpoint(
27
- router,
28
- context,
29
- endpointOrOptions,
30
- finalOptions
31
- );
32
- }
33
- // options object form
34
- const final = { ...defaultSafeOptions, ...endpointOrOptions };
35
- return new Main.RpcEndpoint(router, context, final);
82
+ console.warn(
83
+ 'createSafeEndpoint() is deprecated. Use `new RpcSafeEndpoint(...)` instead.'
84
+ );
85
+ return new RpcSafeEndpoint(router, context, endpointOrOptions, options);
36
86
  }
37
87
 
38
88
  /**
89
+ * @deprecated Use `new RpcSafeClient(...)` instead.
39
90
  * Create a RpcClient with safe defaults (safeEnabled=true).
91
+ *
40
92
  * @param {string} endpoint
41
93
  * @param {Record<string,string>} [defaultHeaders]
42
94
  * @param {object} [options]
43
95
  */
44
96
  function createSafeClient(endpoint, defaultHeaders = {}, options = {}) {
45
- const finalOptions = { safeEnabled: true, ...options };
46
- return new Main.RpcClient(endpoint, defaultHeaders, finalOptions);
97
+ console.warn(
98
+ 'createSafeClient() is deprecated. Use `new RpcSafeClient(...)` instead.'
99
+ );
100
+ return new RpcSafeClient(endpoint, defaultHeaders, options);
47
101
  }
48
102
 
49
103
  module.exports = {
50
104
  // Re-export everything for convenience
51
105
  ...Main,
106
+ // New class-based API
107
+ RpcSafeClient,
108
+ RpcSafeEndpoint,
109
+ // Deprecated factory functions (kept for backward compatibility)
52
110
  createSafeEndpoint,
53
111
  createSafeClient,
54
112
  };