Skip to content

Commit 0631ee7

Browse files
authored
Merge pull request #306 from murgatroid99/typescript_interface_updates
Add loadPackageDefinition and interceptor APIs to .d.ts file
2 parents 4885390 + 85c154c commit 0631ee7

File tree

1 file changed

+207
-1
lines changed

1 file changed

+207
-1
lines changed

packages/grpc-native-core/index.d.ts

Lines changed: 207 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ declare module "grpc" {
7878
*/
7979
export function load<T = GrpcObject>(filename: Filename, format?: "proto" | "json", options?: LoadOptions): T;
8080

81+
/**
82+
* Load a gRPC package definition as a gRPC object hierarchy
83+
* @param packageDef The package definition object
84+
* @return The resulting gRPC object
85+
*/
86+
export function loadPackageDefinition(packageDefinition: PackageDefinition): GrpcObject;
87+
8188
/**
8289
* A filename
8390
*/
@@ -235,12 +242,18 @@ declare module "grpc" {
235242

236243
/**
237244
* An object that completely defines a service.
238-
* @typedef {Object.<string, grpc~MethodDefinition>} grpc~ServiceDefinition
239245
*/
240246
export type ServiceDefinition<ImplementationType> = {
241247
readonly [I in keyof ImplementationType]: MethodDefinition<any, any>;
242248
}
243249

250+
/**
251+
* An object that defines a package containing multiple services
252+
*/
253+
export type PackageDefinition = {
254+
readonly [fullyQualifiedName: string]: ServiceDefinition<any>;
255+
}
256+
244257
/**
245258
* An object that completely defines a service method signature.
246259
*/
@@ -1259,4 +1272,197 @@ declare module "grpc" {
12591272
* @param clientObj The client to close
12601273
*/
12611274
export function closeClient(clientObj: Client): void;
1275+
1276+
/**
1277+
* A builder for gRPC status objects
1278+
*/
1279+
export class StatusBuilder {
1280+
constructor()
1281+
1282+
/**
1283+
* Adds a status code to the builder
1284+
* @param code The status code
1285+
*/
1286+
withCode(code: number): this;
1287+
1288+
/**
1289+
* Adds details to the builder
1290+
* @param details A status message
1291+
*/
1292+
withDetails(details: string): this;
1293+
1294+
/**
1295+
* Adds metadata to the builder
1296+
* @param metadata The gRPC status metadata
1297+
*/
1298+
withMetadata(metadata: Metadata): this;
1299+
1300+
/**
1301+
* Builds the status object
1302+
* @return A gRPC status
1303+
*/
1304+
build(): StatusObject;
1305+
}
1306+
1307+
export type MetadataListener = (metadata: Metadata, next: function) => void;
1308+
1309+
export type MessageListener = (message: any, next: function) => void;
1310+
1311+
export type StatusListener = (status: StatusObject, next: function) => void;
1312+
1313+
export interface Listener {
1314+
onReceiveMetadata?: MetadataListener;
1315+
onReceiveMessage?: MessageListener;
1316+
onReceiveStatus?: StatusListener;
1317+
}
1318+
1319+
/**
1320+
* A builder for listener interceptors
1321+
*/
1322+
export class ListenerBuilder {
1323+
constructor();
1324+
1325+
/**
1326+
* Adds onReceiveMetadata method to the builder
1327+
* @param onReceiveMetadata A listener method for receiving metadata
1328+
*/
1329+
withOnReceiveMetadata(onReceiveMetadata: MetadataListener): this;
1330+
1331+
/**
1332+
* Adds onReceiveMessage method to the builder
1333+
* @param onReceiveMessage A listener method for receiving message
1334+
*/
1335+
withOnReceiveMessage(onReceiveMessage: MessageListener): this;
1336+
1337+
/**
1338+
* Adds onReceiveStatus method to the builder
1339+
* @param onReceiveStatus A listener method for receiving status
1340+
*/
1341+
withOnReceiveStatus(onReceiveStatus: StatusListener): this;
1342+
1343+
/**
1344+
* Builds the call listener
1345+
*/
1346+
build(): Listener;
1347+
}
1348+
1349+
export type MetadataRequester = (metadata: Metadata, listener: Listener, next: function) => void;
1350+
1351+
export type MessageRequester = (message: any, next: function) => void;
1352+
1353+
export type CloseRequester = (next: function) => void;
1354+
1355+
export type CancelRequester = (next: function) => void;
1356+
1357+
export type GetPeerRequester = (next: function) => string;
1358+
1359+
export interface Requester {
1360+
start?: MetadataRequester;
1361+
sendMessage?: MessageRequester;
1362+
halfClose?: CloseRequester;
1363+
cancel?: CancelRequester;
1364+
getPeer?: GetPeerRequester;
1365+
}
1366+
1367+
/**
1368+
* A builder for the outbound methods of an interceptor
1369+
*/
1370+
export class RequesterBuilder {
1371+
constructor();
1372+
1373+
/**
1374+
* Add a metadata requester to the builder
1375+
* @param start A requester method for handling metadata
1376+
*/
1377+
withStart(start: MetadataRequester): this;
1378+
1379+
/**
1380+
* Add a message requester to the builder.
1381+
* @param sendMessage A requester method for handling
1382+
* messages.
1383+
*/
1384+
withSendMessage(sendMessage: MessageRequester): this;
1385+
1386+
/**
1387+
* Add a close requester to the builder.
1388+
* @param halfClose A requester method for handling client
1389+
* close.
1390+
*/
1391+
withHalfClose(halfClose: CloseRequester): this;
1392+
1393+
/**
1394+
* Add a cancel requester to the builder.
1395+
* @param cancel A requester method for handling `cancel`
1396+
*/
1397+
withCancel(cancel: CancelRequester): this;
1398+
1399+
/**
1400+
* Builds the requester's interceptor methods.
1401+
*/
1402+
build(): Requester;
1403+
}
1404+
1405+
/**
1406+
* A chainable gRPC call proxy which will delegate to an optional requester
1407+
* object. By default, interceptor methods will chain to nextCall. If a
1408+
* requester is provided which implements an interceptor method, that
1409+
* requester method will be executed as part of the chain.
1410+
* operations.
1411+
*/
1412+
export class InterceptingCall {
1413+
/**
1414+
* @param next_Call The next call in the chain
1415+
* @param requester Interceptor methods to handle request
1416+
*/
1417+
constructor(nextCall: InterceptingCall|null, requester?: Requester);
1418+
1419+
/**
1420+
* Starts a call through the outbound interceptor chain and adds an element to
1421+
* the reciprocal inbound listener chain.
1422+
*/
1423+
start(metadata: Metadata, listener: Listener): void;
1424+
1425+
/**
1426+
* Pass a message through the interceptor chain.
1427+
*/
1428+
sendMessage(message: any): void;
1429+
1430+
/**
1431+
* Run a close operation through the interceptor chain
1432+
*/
1433+
halfClose(): void;
1434+
1435+
/**
1436+
* Run a cancel operation through the interceptor chain
1437+
*/
1438+
cancel(): void;
1439+
1440+
/**
1441+
* Run a cancelWithStatus operation through the interceptor chain.
1442+
* @param status
1443+
* @param message
1444+
*/
1445+
cancelWithStatus(status: StatusObject, message: string): void;
1446+
1447+
/**
1448+
* Pass a getPeer call down to the base gRPC call (should not be intercepted)
1449+
*/
1450+
getPeer(): object;
1451+
1452+
/**
1453+
* For streaming calls, we need to transparently pass the stream's context
1454+
* through the interceptor chain. Passes the context between InterceptingCalls
1455+
* but hides it from any requester implementations.
1456+
* @param context Carries objects needed for streaming operations.
1457+
* @param message The message to send.
1458+
*/
1459+
sendMessageWithContext(context: object, message: any): void;
1460+
1461+
/**
1462+
* For receiving streaming messages, we need to seed the base interceptor with
1463+
* the streaming context to create a RECV_MESSAGE batch.
1464+
* @param context Carries objects needed for streaming operations
1465+
*/
1466+
recvMessageWithContext(context: object): void;
1467+
}
12621468
}

0 commit comments

Comments
 (0)