config#DEV_HOST TypeScript Examples

The following examples show how to use config#DEV_HOST. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: grpc.ts    From lightning-terminal with MIT License 6 votes vote down vote up
/**
   * Subscribes to a GRPC server-streaming endpoint and executes the `onMessage` handler
   * when a new message is received from the server
   * @param methodDescriptor the GRPC method to call on the service
   * @param request the GRPC request message to send
   * @param onMessage the callback function to execute when a new message is received
   * @param metadata headers to include with the request
   */
  subscribe<TReq extends ProtobufMessage, TRes extends ProtobufMessage>(
    methodDescriptor: MethodDefinition<TReq, TRes>,
    request: TReq,
    onMessage: (res: TRes) => void,
    metadata?: Metadata.ConstructorArg,
  ) {
    if (this.useSampleData) return;

    const method = `${methodDescriptor.methodName}`;
    const client = grpc.client(methodDescriptor, {
      host: DEV_HOST,
      transport: grpc.WebsocketTransport(),
    });
    client.onHeaders(headers => {
      log.debug(`${method} - headers`, headers);
    });
    client.onMessage(message => {
      log.debug(`${method} - message`, message.toObject());
      onMessage(message as TRes);
    });
    client.onEnd((status, statusMessage, trailers) => {
      log.debug(`${method} - status`, status, statusMessage);
      log.debug(`${method} - trailers`, trailers);
    });
    client.start(metadata);
    client.send(request);
  }
Example #2
Source File: grpc.ts    From lightning-terminal with MIT License 5 votes vote down vote up
/**
   * Executes a single GRPC request and returns a promise which will resolve with the response
   * @param methodDescriptor the GRPC method to call on the service
   * @param request The GRPC request message to send
   * @param metadata headers to include with the request
   */
  request<TReq extends ProtobufMessage, TRes extends ProtobufMessage>(
    methodDescriptor: UnaryMethodDefinition<TReq, TRes>,
    request: TReq,
    metadata?: Metadata.ConstructorArg,
  ): Promise<TRes> {
    return new Promise((resolve, reject) => {
      if (this.useSampleData) {
        const endpoint = `${methodDescriptor.service.serviceName}.${methodDescriptor.methodName}`;
        const data = sampleApiResponses[endpoint];
        // the calling function expects the return value to have a `toObject` function
        const response: any = { toObject: () => data };
        resolve(response);
        return;
      }

      const method = `${methodDescriptor.methodName}`;
      log.debug(`${method} request`, request.toObject());
      grpc.unary(methodDescriptor, {
        host: DEV_HOST,
        request,
        metadata,
        onEnd: ({ status, statusMessage, headers, message, trailers }) => {
          log.debug(`${method} status`, status, statusMessage);
          log.debug(`${method} headers`, headers);
          if (status === grpc.Code.OK && message) {
            log.debug(`${method} message`, message.toObject());
            resolve(message as TRes);
          } else if (status === grpc.Code.Unauthenticated) {
            reject(new AuthenticationError(statusMessage));
          } else {
            reject(new Error(statusMessage));
          }
          log.debug(`${method} trailers`, trailers);
        },
      });
    });
  }