@ahoo-wang/fetcher-wow
Version:
Support for Wow(https://github.com/Ahoo-Wang/Wow) in Fetcher
100 lines • 3.84 kB
TypeScript
import { CommandRequest } from './commandRequest';
import { CommandResult, CommandResultEventStream } from './commandResult';
import { ApiMetadata, ApiMetadataCapable } from '@ahoo-wang/fetcher-decorator';
/**
* Command Client for sending commands to the server.
*
* The CommandClient is responsible for sending commands to the server and handling the responses.
* It provides methods for both regular command execution and streaming command results.
*
* @example
* ```typescript
* // Create a client options configuration
* const clientOptions: ClientOptions = {
* fetcher: new Fetcher({ baseURL: 'http://localhost:8080/' }),
* basePath: 'owner/{ownerId}/cart'
* };
*
* // Create a command client instance
* const commandClient = new CommandClient(clientOptions);
*
* // Define command endpoint
* const addCartItem = 'add_cart_item';
*
* // Create a command request
* const addCartItemCommand: CommandRequest<AddCartItem> = {
* method: HttpMethod.POST,
* headers: {
* [CommandHttpHeaders.WAIT_STAGE]: CommandStage.SNAPSHOT,
* },
* body: {
* productId: 'productId',
* quantity: 1,
* }
* };
*
* // Send command and get result
* const commandResult = await commandClient.send(addCartItem, addCartItemCommand);
*
* // Send command and get result as stream
* const commandResultStream = await commandClient.sendAndWaitStream(addCartItem, addCartItemCommand);
* for await (const commandResultEvent of commandResultStream) {
* console.log('Received:', commandResultEvent.data);
* }
* ```
*/
export declare class CommandClient<C extends object = object> implements ApiMetadataCapable {
readonly apiMetadata?: ApiMetadata | undefined;
constructor(apiMetadata?: ApiMetadata | undefined);
/**
* Send a command to the server and wait for the result.
*
* @param commandRequest - The command request to send
* @param attributes - Optional shared attributes that can be accessed by interceptors
* throughout the request lifecycle. These attributes allow passing
* custom data between different interceptors.
* @returns A promise that resolves to the command execution result
*
* @example
* ```typescript
* const commandResult = await commandClient.send('add_cart_item', {
* method: HttpMethod.POST,
* body: {
* productId: 'product-1',
* quantity: 2
* }
* });
* ```
*/
send(commandRequest: CommandRequest<C>, attributes?: Record<string, any>): Promise<CommandResult>;
/**
* Send a command to the server and wait for the result as a stream.
* This is useful for long-running commands that produce multiple events.
*
* @param commandRequest - The command request to send
* @param attributes - Optional shared attributes that can be accessed by interceptors
* throughout the request lifecycle. These attributes allow passing
* custom data between different interceptors.
* @returns A promise that resolves to a stream of command execution results
*
* @example
* ```typescript
* const commandResultStream = await commandClient.sendAndWaitStream('add_cart_item', {
* method: HttpMethod.POST,
* headers: {
* Accept: ContentTypeValues.TEXT_EVENT_STREAM
* },
* body: {
* productId: 'product-1',
* quantity: 2
* }
* });
*
* for await (const commandResultEvent of commandResultStream) {
* console.log('Received event:', commandResultEvent.data);
* }
* ```
*/
sendAndWaitStream(commandRequest: CommandRequest<C>, attributes?: Record<string, any>): Promise<CommandResultEventStream>;
}
//# sourceMappingURL=commandClient.d.ts.map