insomnia/packages/insomnia-app/app/network/grpc/method.js
2020-11-10 13:30:27 +13:00

50 lines
1.2 KiB
JavaScript

// @flow
// https://grpc.github.io/grpc/node/grpc.html#~MethodDefinition
export type GrpcMethodDefinition = {
path: string,
originalName: string,
requestStream: boolean,
responseStream: boolean,
requestSerialize: Function,
responseDeserialize: Function,
};
export const GrpcMethodTypeEnum = {
unary: 'unary',
server: 'server',
client: 'client',
bidi: 'bidi',
};
export type GrpcMethodType = $Values<typeof GrpcMethodTypeEnum>;
export const getMethodType = ({
requestStream,
responseStream,
}: GrpcMethodDefinition): GrpcMethodType => {
if (requestStream) {
if (responseStream) {
return GrpcMethodTypeEnum.bidi;
} else {
return GrpcMethodTypeEnum.client;
}
} else {
if (responseStream) {
return GrpcMethodTypeEnum.server;
} else {
return GrpcMethodTypeEnum.unary;
}
}
};
export const GrpcMethodTypeName: { [GrpcMethodType]: string } = {
[GrpcMethodTypeEnum.unary]: 'Unary',
[GrpcMethodTypeEnum.server]: 'Server Streaming',
[GrpcMethodTypeEnum.client]: 'Client Streaming',
[GrpcMethodTypeEnum.bidi]: 'Bi-directional Streaming',
};
export const canClientStream = (methodType: GrpcMethodType) =>
methodType === GrpcMethodTypeEnum.client || methodType === GrpcMethodTypeEnum.bidi;