Skip to content

Commit 632281c

Browse files
committed
feat: updating metadata wrapper type name
Related #500 Related #501 Related #502 [ci skip]
1 parent 427adac commit 632281c

4 files changed

Lines changed: 39 additions & 30 deletions

File tree

src/clientRPC/handlers/agentStatus.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type KeyRing from '../../keys/KeyRing';
22
import type CertManager from '../../keys/CertManager';
33
import type Logger from '@matrixai/logger';
44
import type { NodeIdEncoded } from '../../ids';
5-
import type { WithMetadata } from '../types';
5+
import type { RPCRequestParams, RPCResponseResult } from '../types';
66
import * as nodesUtils from '../../nodes/utils';
77
import * as keysUtils from '../../keys/utils';
88
import { UnaryHandler } from '../../RPC/handlers';
@@ -15,8 +15,8 @@ type StatusResult = {
1515
};
1616

1717
const agentStatusCaller = new UnaryCaller<
18-
WithMetadata,
19-
WithMetadata<StatusResult>
18+
RPCRequestParams,
19+
RPCResponseResult<StatusResult>
2020
>();
2121

2222
class AgentStatusHandler extends UnaryHandler<
@@ -25,10 +25,10 @@ class AgentStatusHandler extends UnaryHandler<
2525
certManager: CertManager;
2626
logger: Logger;
2727
},
28-
WithMetadata,
29-
WithMetadata<StatusResult>
28+
RPCRequestParams,
29+
RPCResponseResult<StatusResult>
3030
> {
31-
public async handle(): Promise<WithMetadata<StatusResult>> {
31+
public async handle(): Promise<RPCResponseResult<StatusResult>> {
3232
return {
3333
pid: process.pid,
3434
nodeId: nodesUtils.encodeNodeId(this.container.keyRing.getNodeId()),

src/clientRPC/handlers/agentUnlock.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import type Logger from '@matrixai/logger';
2-
import type { WithMetadata } from '../types';
2+
import type { RPCRequestParams, RPCResponseResult } from '../types';
33
import { UnaryHandler } from '../../RPC/handlers';
44
import { UnaryCaller } from '../../RPC/callers';
55

6-
const agentUnlockCaller = new UnaryCaller<WithMetadata, WithMetadata>();
6+
const agentUnlockCaller = new UnaryCaller<
7+
RPCRequestParams,
8+
RPCResponseResult
9+
>();
710

811
class AgentUnlockHandler extends UnaryHandler<
912
{ logger: Logger },
10-
WithMetadata,
11-
WithMetadata
13+
RPCRequestParams,
14+
RPCResponseResult
1215
> {
13-
public async handle(): Promise<WithMetadata> {
16+
public async handle(): Promise<RPCResponseResult> {
1417
// This is a NOP handler,
1518
// authentication and unlocking is handled via middleware.
1619
// Failure to authenticate will be an error from the middleware layer.

src/clientRPC/types.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ import type { JSONValue } from '../types';
33
// eslint-disable-next-line
44
type NoData = {};
55

6-
type WithMetadata<T extends Record<string, JSONValue> = NoData> = {
6+
type RPCRequestParams<T extends Record<string, JSONValue> = NoData> = {
77
metadata?: {
88
[Key: string]: JSONValue;
99
} & Partial<{ Authorization: string }>;
1010
} & Omit<T, 'metadata'>;
1111

12-
export type { WithMetadata, NoData };
12+
type RPCResponseResult<T extends Record<string, JSONValue> = NoData> = {
13+
metadata?: {
14+
[Key: string]: JSONValue;
15+
} & Partial<{ Authorization: string }>;
16+
} & Omit<T, 'metadata'>;
17+
18+
export type { RPCRequestParams, RPCResponseResult, NoData };

src/clientRPC/utils.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { SessionToken } from '../sessions/types';
22
import type KeyRing from '../keys/KeyRing';
33
import type SessionManager from '../sessions/SessionManager';
44
import type { Session } from '../sessions';
5-
import type { WithMetadata } from './types';
5+
import type { RPCResponseResult, RPCRequestParams } from './types';
66
import type {
77
JsonRpcRequest,
88
JsonRpcResponse,
@@ -25,7 +25,7 @@ import { promise } from '../utils';
2525
async function authenticate(
2626
sessionManager: SessionManager,
2727
keyRing: KeyRing,
28-
message: JsonRpcRequest<WithMetadata>,
28+
message: JsonRpcRequest<RPCRequestParams>,
2929
) {
3030
if (message.params == null) throw new clientErrors.ErrorClientAuthMissing();
3131
if (message.params.metadata == null) {
@@ -58,7 +58,7 @@ async function authenticate(
5858
return `Bearer ${token}`;
5959
}
6060

61-
function decodeAuth(messageParams: WithMetadata) {
61+
function decodeAuth(messageParams: RPCRequestParams) {
6262
const auth = messageParams.metadata?.Authorization;
6363
if (auth == null || !auth.startsWith('Bearer ')) {
6464
return;
@@ -75,19 +75,19 @@ function authenticationMiddlewareServer(
7575
sessionManager: SessionManager,
7676
keyRing: KeyRing,
7777
): MiddlewareFactory<
78-
JsonRpcRequest<WithMetadata>,
79-
JsonRpcRequest<WithMetadata>,
80-
JsonRpcResponse<WithMetadata>,
81-
JsonRpcResponse<WithMetadata>
78+
JsonRpcRequest<RPCRequestParams>,
79+
JsonRpcRequest<RPCRequestParams>,
80+
JsonRpcResponse<RPCResponseResult>,
81+
JsonRpcResponse<RPCResponseResult>
8282
> {
8383
return () => {
8484
let forwardFirst = true;
8585
let reverseController;
8686
let outgoingToken: string | null = null;
8787
return {
8888
forward: new TransformStream<
89-
JsonRpcRequest<WithMetadata>,
90-
JsonRpcRequest<WithMetadata>
89+
JsonRpcRequest<RPCRequestParams>,
90+
JsonRpcRequest<RPCRequestParams>
9191
>({
9292
transform: async (chunk, controller) => {
9393
if (forwardFirst) {
@@ -132,17 +132,17 @@ function authenticationMiddlewareServer(
132132
function authenticationMiddlewareClient(
133133
session: Session,
134134
): MiddlewareFactory<
135-
JsonRpcRequest<WithMetadata>,
136-
JsonRpcRequest<WithMetadata>,
137-
JsonRpcResponse<WithMetadata>,
138-
JsonRpcResponse<WithMetadata>
135+
JsonRpcRequest<RPCRequestParams>,
136+
JsonRpcRequest<RPCRequestParams>,
137+
JsonRpcResponse<RPCResponseResult>,
138+
JsonRpcResponse<RPCResponseResult>
139139
> {
140140
return () => {
141141
let forwardFirst = true;
142142
return {
143143
forward: new TransformStream<
144-
JsonRpcRequest<WithMetadata>,
145-
JsonRpcRequest<WithMetadata>
144+
JsonRpcRequest<RPCRequestParams>,
145+
JsonRpcRequest<RPCRequestParams>
146146
>({
147147
transform: async (chunk, controller) => {
148148
if (forwardFirst) {
@@ -164,8 +164,8 @@ function authenticationMiddlewareClient(
164164
},
165165
}),
166166
reverse: new TransformStream<
167-
JsonRpcResponse<WithMetadata>,
168-
JsonRpcResponse<WithMetadata>
167+
JsonRpcResponse<RPCResponseResult>,
168+
JsonRpcResponse<RPCResponseResult>
169169
>({
170170
transform: async (chunk, controller) => {
171171
controller.enqueue(chunk);

0 commit comments

Comments
 (0)