insomnia/packages/insomnia-app/app/network/grpc/prepare.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

59 lines
1.7 KiB
TypeScript

import type { RenderedGrpcRequest, RenderedGrpcRequestBody } from '../../common/render';
import type { GrpcMethodType } from './method';
import * as models from '../../models';
import {
getRenderedGrpcRequest,
getRenderedGrpcRequestMessage,
RENDER_PURPOSE_SEND,
} from '../../common/render';
import { canClientStream } from './method';
export interface GrpcIpcRequestParams {
request: RenderedGrpcRequest;
}
export interface GrpcIpcMessageParams {
requestId: string;
body: RenderedGrpcRequestBody;
}
export const prepareGrpcRequest = async (
requestId: string,
environmentId: string,
methodType: GrpcMethodType,
): Promise<GrpcIpcRequestParams> => {
const req = await models.grpcRequest.getById(requestId);
const environment = await models.environment.getById(environmentId || 'n/a');
const request = await getRenderedGrpcRequest(
// @ts-expect-error -- TSCONVERSION req can be null but should not try to render if it is null
req,
environment,
RENDER_PURPOSE_SEND,
{},
canClientStream(methodType),
);
return {
request,
};
};
export const prepareGrpcMessage = async (
requestId: string,
environmentId: string,
): Promise<GrpcIpcMessageParams> => {
const req = await models.grpcRequest.getById(requestId);
const environment = await models.environment.getById(environmentId || 'n/a');
const requestBody = await getRenderedGrpcRequestMessage(
// @ts-expect-error -- TSCONVERSION req can be null but should not try to render if it is null
req,
environment,
RENDER_PURPOSE_SEND,
{},
);
return {
body: requestBody,
// @ts-expect-error -- TSCONVERSION req can be null
requestId: req._id,
};
};