Client library

Client reference

Every method of @thuum/client, the flag and validation helpers, token helpers, errors and exported types.

ThuumClient#

MethodReturns
new ThuumClient({ baseUrl, fetch? })
exchangeToken(domain, appKey, { scope?, ttl_seconds? }){ token, token_type, expires_in }
domain(domain, token, { onError? })DomainClient
request(method, path, { token?, body? })the response's data (undefined for 204)

DomainClient#

slug is the Domain. me() returns Me.

eventBroker#

MethodRouteReturns
data.publish(topic, { type, data })POST /event/data/{topic}BrokerEvent
data.list(topic, { type?, since?, until?, limit? })GET /event/data/{topic}BrokerEvent[]
data.purge(topic, { type?, since?, until? })DELETE /event/data/{topic}{ deleted }
topics.list()GET /event/topicsTopic[]
topics.get(mainTopic)GET /event/topics/{topic}Topic
topics.put(mainTopic, { max_events?, ttl_seconds? })PUT /event/topics/{topic}Topic
topics.delete(mainTopic)DELETE /event/topics/{topic}—
connect({ WebSocket? })WebSocketEventBrokerSocket
claims.list() / get(id)GET /event/claims[/{id}]EventClaim[] / EventClaim
claims.create({ name, description?, topic_flags?, fallback_data_flags?, topics? })POST /event/claimsEventClaim
claims.update(id, fields) / delete(id)PATCH / DELETE /event/claims/{id}EventClaim / —
claims.topics.put(id, mainTopic, flags) / delete(id, mainTopic)…/claims/{id}/topics/{topic}EventClaim / —
claims.keys.link(id, selector) / unlink(id, selector)…/claims/{id}/keys/{selector}EventClaim / —
keys.list()GET /event/keysEventKey[]

stateBroker#

MethodRouteReturns
data.get(topic, key)GET /state/data/{topic}/{key}BrokerState
data.list(topic, { prefix?, after?, after_topic?, limit? })GET /state/data/{topic}BrokerState[]
data.set(topic, key, value)PUT /state/data/{topic}/{key}BrokerState
data.merge(topic, key, patch)PATCH /state/data/{topic}/{key}BrokerState
data.delete(topic, key)DELETE /state/data/{topic}/{key}—
data.echo(topic, key)POST /state/data/{topic}/{key}/echoBrokerState
topics.list() / get / put(mainTopic, { ttl_seconds?, max_states? }) / delete/state/topics…StateTopic
connect({ WebSocket? })WebSocketStateBrokerSocket
claims.*, keys.list()as for the Event Broker, under /stateStateClaim, StateKey

domain#

MethodRouteReturns
keys.list() / get(selector)GET /domain/keys[/{selector}]AppKey[] / AppKey
keys.create({ name, token_ttl_seconds? })POST /domain/keysCreatedAppKey (with key)
keys.delete(selector)DELETE /domain/keys/{selector}—
claims.list() / get(id)GET /domain/claims[/{id}]DomainClaim[] / DomainClaim
claims.create({ name, description?, protocols? })POST /domain/claimsDomainClaim
claims.update(id, { name?, description? }) / delete(id)PATCH / DELETE /domain/claims/{id}DomainClaim / —
claims.protocols.put(id, protocol, flags) / delete(id, protocol)…/protocols/{protocol}DomainClaim / —
claims.keys.link(id, selector) / unlink(id, selector)…/keys/{selector}DomainClaim / —

Flags#

TypeScript
import {
	PUBLISH,
	WRITE,
	SUBSCRIBE,
	READ,
	combine,
	has,
	DATA_FLAGS,
	MANAGEMENT_FLAGS
} from '@thuum/client';
ExportValue / use
PUBLISH, WRITE, SUBSCRIBE, READ8, 4, 2, 1
combine(...bits)combine(PUBLISH, READ) === 9
has(flags, bit)Whether flags includes every bit of bit.
DATA_FLAGS[{ bit, letter, label }] for P W S R, in order.
MANAGEMENT_FLAGSThe same for W R only.

Rules#

Validate before a round trip, with the same rules as the API:

ExportChecks
isTopic, TOPIC_PATTERN1–8 levels of a-z A-Z 0-9 _ - joined by :, ≤ 128 characters.
isMainTopic, MAIN_TOPIC_PATTERNOne level.
isEventType, EVENT_TYPE_PATTERN1–64 of a-z A-Z 0-9 _ . : -.
isStateKey, STATE_KEY_PATTERN1–128 of a-z A-Z 0-9 _ . : -.
isDomainSlug, isAppKey, isSelectorA Domain slug, an 80-hex App Key, a 16-hex selector.
MAX_TOPIC_LEVELS, MAX_TOPIC_LENGTH8, 128.
DEFAULT_TOKEN_TTL_SECONDS, MAX_TOKEN_TTL_SECONDS30, 86400.
mainTopic(topic)'a:b:c' ⇒ 'a'.
superTopics(topic)'a:b:c' ⇒ ['a:b:c', 'a:b', 'a']: everyone a publish reaches.
isInSubtree(topic, root)Whether a subscription to root receives topic.
resolveDataFlags(claim, topic){ flags, source: 'topic' | 'fallback', mainTopic }, as the server resolves it.
selectorOf(appKey)The first 16 characters.
PROTOCOLS['domain', 'event', 'state'].

Tokens#

ExportUse
decodeToken(token){ selector, expiresAt, issuedAt } (epoch ms), or null. Not verified.
isExpired(claims, now?)Whether decoded claims are past their expiry.
TypeScript
const claims = decodeToken(token);
if (!claims || isExpired(claims)) token = await fetchNewToken();

Types#

All request and response shapes are exported as types: BrokerEvent, BrokerState, StateChange, StateValue, Topic, StateTopic, EventFilters, PurgeFilters, StateFilters, PublishEvent, Me, AppKey, CreatedAppKey, DomainClaim, EventClaim, StateClaim, EventKey, StateKey, Protocol, TokenResponse, ApiErrors, and the option types of every class.