insomnia/packages/insomnia-app/app/network/grpc/method.ts
Opender Singh 7ffc391428
add eslint rules for semi colons (#3989)
* add rules for semi colons

* run lint fix

* remove invalid eslint disable
2021-09-01 10:50:26 -04:00

51 lines
1.4 KiB
TypeScript

import { MethodDefinition } from '@grpc/grpc-js';
import { ValueOf } from 'type-fest';
// TODO(TSCONVERSION) remove this alias and type MethodDefinition correctly
export type GrpcMethodDefinition = MethodDefinition<any, any>;
export const GrpcMethodTypeEnum = {
unary: 'unary',
server: 'server',
client: 'client',
bidi: 'bidi',
} as const;
export type GrpcMethodType = ValueOf<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 = {
[GrpcMethodTypeEnum.unary]: 'Unary',
[GrpcMethodTypeEnum.server]: 'Server Streaming',
[GrpcMethodTypeEnum.client]: 'Client Streaming',
[GrpcMethodTypeEnum.bidi]: 'Bi-directional Streaming',
} as const;
export const GrpcMethodTypeAcronym = {
[GrpcMethodTypeEnum.unary]: 'U',
[GrpcMethodTypeEnum.server]: 'SS',
[GrpcMethodTypeEnum.client]: 'CS',
[GrpcMethodTypeEnum.bidi]: 'BD',
} as const;
export const canClientStream = (methodType?: GrpcMethodType) =>
methodType === GrpcMethodTypeEnum.client || methodType === GrpcMethodTypeEnum.bidi;