Skip to main content

Common

NPM | GitHub

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.

const provider = new FetchProvider({ baseURL: 'https://example.com' });

Constructor

ArgumentsDescription
options: objectFetch provider options

Fetch provider options

NameTypeDescription
baseURLstringBase 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 TypeDescription
handleGeturl: string, query: RequestInit, options: FetchOptions, meta: Meta, abortSignal: AbortSignalPromise<any | any[]>
requesturl: string, query: RequestInit, options: FetchOptions, meta: Meta, abortSignal: AbortSignalPromise<any | any[]>Performs the specified request and returns the response

Fetch options

NameTypeDescription
jsonbooleanWhether the response should always be parsed as JSON (regardless of the Content-Type)
noJsonbooleanWhether the response should never be parsed as JSON (regardless of the Content-Type)
noTextbooleanWhether 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.

const eventSource = new EventSource('https://example.com');
const receiver = new SSEReceiver(eventSource);

Constructor

ArgumentsDescription
eventSource: EventSourceEventSource to be used 
options: objectSSE receiver options

SSE receiver options

NameTypeDescription
eventsstring[]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 TypeDescription
handleSubscriberesource: Notifier<ResourceContext>, options: SSEOptions() => void

Fetch options

NameTypeDescription
eventsstring[]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)