tsrpc-browser
Version:
TSRPC Client for browser
64 lines (55 loc) • 2.33 kB
TypeScript
/*!
* TSRPC Browser v3.4.17
* -----------------------------------------
* Copyright (c) King Wang.
* MIT License
* https://github.com/k8w/tsrpc-browser
*/
import { ApiReturn } from 'tsrpc-proto';
import { BaseHttpClient } from 'tsrpc-base-client';
import { BaseHttpClientOptions } from 'tsrpc-base-client';
import { BaseServiceType } from 'tsrpc-proto';
import { BaseWsClient } from 'tsrpc-base-client';
import { BaseWsClientOptions } from 'tsrpc-base-client';
import { ServiceProto } from 'tsrpc-proto';
import { TransportOptions } from 'tsrpc-base-client';
import { TsrpcError } from 'tsrpc-proto';
/**
* HTTP Client for TSRPC.
* It uses XMLHttpRequest to send requests.
* @typeParam ServiceType - `ServiceType` from generated `proto.ts`
*/
export declare class HttpClient<ServiceType extends BaseServiceType> extends BaseHttpClient<ServiceType> {
readonly options: Readonly<HttpClientOptions>;
constructor(proto: ServiceProto<ServiceType>, options?: Partial<HttpClientOptions>);
callApi<T extends string & keyof ServiceType['api']>(apiName: T, req: ServiceType['api'][T]['req'], options?: HttpClientTransportOptions): Promise<ApiReturn<ServiceType['api'][T]['res']>>;
sendMsg<T extends string & keyof ServiceType['msg']>(msgName: T, msg: ServiceType['msg'][T], options?: HttpClientTransportOptions): Promise<{
isSucc: true;
} | {
isSucc: false;
err: TsrpcError;
}>;
}
export declare interface HttpClientOptions extends BaseHttpClientOptions {
}
export declare interface HttpClientTransportOptions extends TransportOptions {
/**
* Event when progress of data sent is changed
* @param ratio - 0~1
*/
onProgress?: (ratio: number) => void;
}
/**
* Client for TSRPC WebSocket Server.
* @typeParam ServiceType - `ServiceType` from generated `proto.ts`
*/
export declare class WsClient<ServiceType extends BaseServiceType> extends BaseWsClient<ServiceType> {
readonly options: Readonly<WsClientOptions>;
constructor(proto: ServiceProto<ServiceType>, options?: Partial<WsClientOptions>);
}
export declare interface WsClientOptions extends BaseWsClientOptions {
/** As the 3rd parameter for `new WebSocket()` */
caUrl?: string;
}
export * from "tsrpc-proto";
export { }