insomnia/packages/insomnia-app/app/network/grpc/method.ts
Dimitri Mitropoulos 5f4c19da35
[TypeScript] Phase 1 & 2 (#3370)
Co-authored-by: Opender Singh <opender.singh@konghq.com>
2021-05-12 18:35:00 +12: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;