UNPKG

ngx-sse-client

Version:

A simple SSE (Server Sent Events) client for Angular applications.

106 lines (100 loc) 3.42 kB
import { HttpHeaders, HttpParams, HttpContext, HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; import * as i0 from '@angular/core'; interface SseRequestOptions { body?: any; headers?: HttpHeaders | { [header: string]: string | string[]; }; params?: HttpParams | { [param: string]: string | string[]; }; context?: HttpContext; withCredentials?: boolean; } declare const defaultRequestOptions: { observe: string; reportProgress: boolean; responseType: string; }; declare class SseClient { private httpClient; constructor(httpClient: HttpClient); /** * Constructs a request which listen to the SSE and interprets the data as * events and returns the full event stream. * * @param url the endpoint URL. * @param options an object of `SseOption` * @param requestOptions the HTTP options to send with the request. * @param method the HTTP method * * @returns an observable of all events for the request, with the response body of type `Event`. */ stream(url: string, options?: { keepAlive?: boolean; reconnectionDelay?: number; responseType?: 'event'; }, requestOptions?: SseRequestOptions, method?: string): Observable<Event>; /** * Constructs a request which listen to the SSE and interprets the data as a * string text and returns the full event stream. * * @param url the endpoint URL. * @param options an object of `SseOption` * @param requestOptions the HTTP options to send with the request. * @param method the HTTP method * * @returns an observable of all events for the request, with the response body of type string. */ stream(url: string, options?: { keepAlive?: boolean; reconnectionDelay?: number; responseType?: 'text'; }, requestOptions?: SseRequestOptions, method?: string): Observable<string>; static ɵfac: i0.ɵɵFactoryDeclaration<SseClient, never>; static ɵprov: i0.ɵɵInjectableDeclaration<SseClient>; } interface SseErrorEvent extends ErrorEvent { /** * HTTP status code from the request error. */ status?: number; /** * HTTP status text from the request error. */ statusText?: string; } interface SseOptions { /** * `true` to automatically reconnect when the request is closed by an request * error (including timeout errors) or completed. * * In this case, to close the connection is necessary to unsubscribe manually. * * @default `true` */ keepAlive: boolean; /** * Delay before reconnecting with the server, this is only useful when * `keepAlive` is `true`. * * @default `3000` */ reconnectionDelay: number; /** * Defines the response type. * * When set to `event` a `MessageEvent` will be returned with the data and a * default `Event` with type error in case of erros. * * When set to `text` only the message data will be returned. In this case no * errors will be returned, only the data from successful requests. * * @default `text` */ responseType: 'event' | 'text'; } declare const defaultSseOptions: SseOptions; export { SseClient, defaultRequestOptions, defaultSseOptions }; export type { SseErrorEvent, SseOptions, SseRequestOptions };