Common
npm install @civet/common
The common module provides general functions that can be used for a quick start where no extensive configuration is required.
FetchProvider
DataProvider implementation that uses the fetch API.
- Usage
- Import
const provider = new FetchProvider({ baseURL: 'https://example.com' });
import { FetchProvider } from "@civet/common";
Constructor
| Arguments | Description |
|---|---|
options: object | Fetch provider options |
Fetch provider options
| Name | Type | Description |
|---|---|---|
| baseURL | string | Base URL to use for requests |
| modifyRequest | (url: URL, request: RequestInit, meta: Meta) => Promise<void> | Function for customizing requests, for example for adding authentication details |
| getResponse | (url: URL, request: RequestInit, response: Response, meta: Meta) => Promise<any | any[]> | Function for customizing how to resolve the response |
| handleError | (url: URL, request: RequestInit, response: Response, meta: Meta) => Promise<any | any[]> | Function for customizing error handling |
Class members
| Name | Arguments | Return Type | Description |
|---|---|---|---|
| handleGet | url: string, query: RequestInit, options: FetchOptions, meta: Meta, abortSignal: AbortSignal | Promise<any | any[]> | |
| request | url: string, query: RequestInit, options: FetchOptions, meta: Meta, abortSignal: AbortSignal | Promise<any | any[]> | Performs the specified request and returns the response |
Fetch options
| Name | Type | Description |
|---|---|---|
| json | boolean | Whether the response should always be parsed as JSON (regardless of the Content-Type) |
| noJson | boolean | Whether the response should never be parsed as JSON (regardless of the Content-Type) |
| noText | boolean | Whether the response should never be returned as plain text |
| getResponse | (url: URL, request: RequestInit, response: Response, meta: Meta) => Promise<any | any[]> | Function for customizing how to resolve the response (takes priority over fetch provider options) |
| handleError | (url: URL, request: RequestInit, response: Response, meta: Meta) => Promise<any | any[]> | Function for customizing error handling (takes priority over fetch provider options) |
SSEReceiver
EventReceiver implementation for server-sent events.
- Usage
- Import
const eventSource = new EventSource('https://example.com');
const receiver = new SSEReceiver(eventSource);
import { SSEReceiver } from "@civet/common";
Constructor
| Arguments | Description |
|---|---|
eventSource: EventSource | EventSource to be used |
options: object | SSE receiver options |
SSE receiver options
| Name | Type | Description |
|---|---|---|
| events | string[] | Event types to listen for (defaults to unnamed events: ['message']) |
| getEvents | (resource: ResourceContext, type: string, event: MessageEvent) => Promise<Event[]> | Function for customizing how to resolve events |
Class members
| Name | Arguments | Return Type | Description |
|---|---|---|---|
| handleSubscribe | resource: Notifier<ResourceContext>, options: SSEOptions | () => void |
Fetch options
| Name | Type | Description |
|---|---|---|
| events | string[] | Event types to listen for (defaults to unnamed events: ['message']) (takes priority over sse receiver options) |
| getEvents | (resource: ResourceContext, type: string, event: MessageEvent) => Promise<Event[]> | Function for customizing how to resolve events (takes priority over sse receiver options) |