Skip to content

Commit dc623e0

Browse files
committed
refactor: Tweak asLegacyMiddleware and docs
1 parent 371a7d2 commit dc623e0

File tree

4 files changed

+25
-31
lines changed

4 files changed

+25
-31
lines changed

packages/json-rpc-engine/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ await server.handle(notification);
8181

8282
### Legacy compatibility
8383

84-
Use the `asLegacyMiddleware` function to use a `JsonRpcEngineV2` or V2 middleware as middleware in a legacy `JsonRpcEngine`:
84+
Use the `asLegacyMiddleware` function to use a `JsonRpcEngineV2` or V2 middleware as middleware in a legacy `JsonRpcEngine`.
85+
86+
#### Context propagation
87+
88+
In keeping with the conventions of the legacy engine, non-JSON-RPC string properties of the `context` will be
89+
copied over to the request once the V2 engine is done with the request. _Note that **only `string` keys** of
90+
the `context` will be copied over._
8591

8692
#### Converting a V2 engine
8793

@@ -105,8 +111,6 @@ legacyEngine.push(asLegacyMiddleware(v2Engine));
105111

106112
#### Converting V2 middleware
107113

108-
You can also directly convert one or more V2 middlewares without creating an engine:
109-
110114
```ts
111115
import {
112116
asLegacyMiddleware,
@@ -131,10 +135,6 @@ const legacyEngine2 = new JsonRpcEngine();
131135
legacyEngine2.push(asLegacyMiddleware(middleware1, middleware2));
132136
```
133137

134-
In keeping with the conventions of the legacy engine, non-JSON-RPC string properties of the `context` will be
135-
copied over to the request once the V2 engine is done with the request. _Note that **only `string` keys** of
136-
the `context` will be copied over._
137-
138138
### Middleware
139139

140140
Middleware functions can be sync or async.

packages/json-rpc-engine/src/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ engine.push(function (req, res, next, end) {
2626
Use the `asV2Middleware` function to use a `JsonRpcEngine` or legacy middleware as middleware in a
2727
`JsonRpcEngineV2`:
2828

29+
#### Context propagation
30+
31+
Non-JSON-RPC string properties on the request object will be copied over to the V2 engine's `context` object
32+
once the legacy engine is done with the request, _unless_ they already exist on the `context`, in which case
33+
they will be ignored.
34+
2935
#### Converting a legacy engine
3036

3137
```ts
@@ -67,12 +73,6 @@ const v2Engine2 = JsonRpcEngineV2.create({
6773
});
6874
```
6975

70-
#### Context propagation
71-
72-
Non-JSON-RPC string properties on the request object will be copied over to the V2 engine's `context` object
73-
once the legacy engine is done with the request, _unless_ they already exist on the `context`, in which case
74-
they will be ignored.
75-
7676
### Middleware
7777

7878
Requests are handled asynchronously, stepping down the stack until complete.

packages/json-rpc-engine/src/asV2Middleware.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
unserializeError,
2323
} from './v2/compatibility-utils';
2424
import type {
25-
// JsonRpcEngineV2 is used in docs.
25+
// Used in docs.
2626
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2727
JsonRpcEngineV2,
2828
JsonRpcMiddleware,
@@ -67,8 +67,7 @@ export function asV2Middleware<
6767
engineOrMiddleware: JsonRpcEngine | LegacyMiddleware<JsonRpcParams, Json>,
6868
...rest: LegacyMiddleware<JsonRpcParams, Json>[]
6969
): JsonRpcMiddleware<Request> {
70-
// Determine the legacy middleware function from input(s)
71-
const middleware =
70+
const legacyMiddleware =
7271
typeof engineOrMiddleware === 'function'
7372
? mergeMiddleware([engineOrMiddleware, ...rest])
7473
: engineOrMiddleware.asMiddleware();
@@ -98,7 +97,7 @@ export function asV2Middleware<
9897
const legacyNext = ((cb: JsonRpcEngineEndCallback) =>
9998
cb(end)) as JsonRpcEngineNextCallback;
10099

101-
middleware(req, res, legacyNext, end);
100+
legacyMiddleware(req, res, legacyNext, end);
102101
});
103102
propagateToContext(req, context);
104103

packages/json-rpc-engine/src/v2/asLegacyMiddleware.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@ import {
66
makeContext,
77
propagateToRequest,
88
} from './compatibility-utils';
9-
import type {
10-
JsonRpcEngineV2,
11-
JsonRpcMiddleware as V2Middleware,
12-
ResultConstraint,
13-
} from './JsonRpcEngineV2';
14-
import { JsonRpcEngineV2 as EngineV2 } from './JsonRpcEngineV2';
9+
import type { JsonRpcMiddleware, ResultConstraint } from './JsonRpcEngineV2';
10+
import { JsonRpcEngineV2 } from './JsonRpcEngineV2';
1511
import { createAsyncMiddleware } from '..';
1612
import type { JsonRpcMiddleware as LegacyMiddleware } from '..';
1713

@@ -38,7 +34,7 @@ export function asLegacyMiddleware<
3834
Params extends JsonRpcParams,
3935
Request extends JsonRpcRequest<Params>,
4036
>(
41-
...middleware: V2Middleware<Request, ResultConstraint<Request>>[]
37+
...middleware: JsonRpcMiddleware<Request, ResultConstraint<Request>>[]
4238
): LegacyMiddleware<Params, ResultConstraint<Request>>;
4339

4440
/**
@@ -54,23 +50,22 @@ export function asLegacyMiddleware<
5450
>(
5551
engineOrMiddleware:
5652
| JsonRpcEngineV2<Request>
57-
| V2Middleware<Request, ResultConstraint<Request>>,
58-
...rest: V2Middleware<Request, ResultConstraint<Request>>[]
53+
| JsonRpcMiddleware<Request, ResultConstraint<Request>>,
54+
...rest: JsonRpcMiddleware<Request, ResultConstraint<Request>>[]
5955
): LegacyMiddleware<Params, ResultConstraint<Request>> {
60-
// Determine the V2 middleware function from input(s)
61-
const middleware =
56+
const JsonRpcMiddleware =
6257
typeof engineOrMiddleware === 'function'
63-
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
64-
EngineV2.create<any>({
58+
? JsonRpcEngineV2.create({
6559
middleware: [engineOrMiddleware, ...rest],
6660
}).asMiddleware()
6761
: engineOrMiddleware.asMiddleware();
62+
6863
return createAsyncMiddleware(async (req, res, next) => {
6964
const request = fromLegacyRequest(req as Request);
7065
const context = makeContext(req);
7166
let modifiedRequest: Request | undefined;
7267

73-
const result = await middleware({
68+
const result = await JsonRpcMiddleware({
7469
request,
7570
context,
7671
next: (finalRequest) => {

0 commit comments

Comments
 (0)