Perform requests against a server.
const client = Client.ofUrl("...");
const record = await client.getRecord(...);
Client
ofEntity(entity)
: Client
Create a new client from a user’s entity.
entity
Entity
ofUrl(entityRecordUrl)
: Client
Create a new client from the URL of a user’s Entity record.
entityRecordUrl
string
ofRecord(entityRecord: EntityRecord)
: Client
authenticated(state: AuthenticationState)
: Client
Create a new authenticated client from an authentication state.
Such object is obtained through app registration and authorization flow.
discover(entity, signal?)
: Promise<EntityRecord>
Perform discovery on the provided entity.
entity
Entity
signal
AbortSignal optional
getEntityRecord(signal?)
: Promise<EntityRecord>
Fetch the user’s entity record.
(Fetched only once, and cached beyond that point).
signal
AbortSignal optional
getOwnRecord(linkModel, model, id, opts?)
Fetch a single of our user’s records by ID.
linkModel
Type
model
Type
id
RecordId
options
object optional
query
SingleQuery optional : Additional server parameters.
signal
AbortSignal optional : Signal to abort the request.
Promise<RecordResponse>
getRecord(linkModel, model, entity, id, opts?)
Fetch a single record by author entity and ID.
linkModel
Type
model
Type
entity
Entity
id
RecordId
options
object optional
query
SingleQuery optional : Additional server parameters.
signal
AbortSignal optional : Signal to abort the request.
Promise<RecordResponse>
getRecords(linkModel, model, query, signal?)
Fetch a list of records from a Query.
linkModel
Type
model
Type
query
Query
signal
AbortSignal optional
Promise<RecordsResponse>
postRecord(linkModel, model, record, signal?)
Create a new record.
linkModel
Type
model
Type
record
Record
signal
AbortSignal optional
Promise<RecordResponse>
putRecord(linkModel, model, record, signal?)
Update an existing record.
linkModel
Type
model
Type
record
Record
signal
AbortSignal optional
Promise<RecordResponse>
deleteRecord(linkModel, record, opts?)
Delete a record by updating it with a record tombstone.
linkModel
Type
record
NoContentRecord
signal
AbortSignal optional
Promise<RecordResponse>
uploadBlob(blob, signal?)
: Promise<BlobResponse>
Upload a single blob for use in future records.
blob
Blob
signal
AbortSignal optional
downloadBlob(record, blobLink, signal?)
: Promise<Blob>
Download a single blob from a record and blob link.
record
Record
blobLink
BlobLink
signal
AbortSignal optional
blobUrlBuilder()
: Promise<BlobUrlBuilder>
RecordResponse
RecordsResponse
BlobResponse
hash
BlobHash
size
int
expiresAt
Date
Building an authenticated client requires an authentication state obtained while registering an app. This state can be serialized and persisted for repeated use. Here we read an existing authentication state from the browser’s local storage and use it to build a client.
import {Client, AuthenticationState} from "@baqhub/sdk";
const authStateJson = localStorage.getItem("auth_state");
const authState = AuthenticationState.decodeJSON(authStateJson);
const client = Client.authenticated(authState);
// Next steps:
// Use the client to perform requests.
Once we have a client, we can use it to fetch a record by author entity and ID. Here we fetch a Post record and we ignore linked records altogether.
import {IO} from "@baqhub/sdk";
import {PostRecord} from "./baq/postRecord.js";
async function fetchPostRecord(client, entity, recordId) {
const {record} = await client.getRecord(
IO.unknown,
PostRecord,
entity,
recordId
);
return record;
}
Fetching a list of records works similarly. The difference is that we provide a Query instead. Here we fetch the 30 latest Post records.
import {IO} from "@baqhub/sdk";
import {PostRecord} from "./baq/postRecord.js";
async function fetchFeed(client) {
const {records, linkedRecords} = await client.getRecords(
IO.unknown,
PostRecord,
{
pageSize: 30,
filter: Q.type(PostRecord),
}
);
return {records};
}
Depending on the situation, we may need to also read linked records from the server’s response. The most common use-case for this is to get the records respective author’s Entity record which are included by default (unless it’s our own user’s).
In this example we fetch Post records and also specify that we expect EntityRecord
linked records.
import {EntityRecord, Q} from "@baqhub/sdk";
import {PostRecord} from "./baq/postRecord.js";
async function fetchFeed(client) {
const {records, linkedRecords} = await client.getRecords(
EntityRecord,
PostRecord,
{
pageSize: 30,
filter: Q.type(PostRecord),
}
);
return {records, linkedRecords};
}
For more advanced queries, we may want to specify multiple types for linked records. This can be done by combining them with IO.union
. In this example we also request the inclusion of the quote_post
link, which is itself a Post record.
import {EntityRecord, Q} from "@baqhub/sdk";
import {PostRecord} from "./baq/postRecord.js";
const LinkRecord = Q.union([EntityRecord, PostRecord]);
async function fetchFeed(client) {
const {records, linkedRecords} = await client.getRecords(
LinkRecord,
PostRecord,
{
pageSize: 30,
filter: Q.type(PostRecord),
includeLinks: ["entity", "content.quotePost"],
}
);
return {records, linkedRecords};
}
Creating a new record requires a Record object and its Type. Here we have a function that accepts a string as input for the new Post record, sends the record to the server, and returns the result.
import {IO} from "@baqhub/sdk";
import {PostRecord} from "./baq/postRecord.js";
async function createPostRecord(client, text) {
const postRecord = PostRecord.new("alice.baq.run", {text});
const {record} = await client.postRecord(
IO.unknown,
PostRecord,
postRecord
);
return record;
}
To add a blob to a record, we must first upload it. We can then create a BlobLink with additional metadata about our file, and finally add it to the record.
In this example we upload an image and add it as avatar to the Entity record for our user.
import {EntityRecord, BlobLink} from "@baqhub/sdk";
async function addAvatar(client, entityRecord, avatarBlob) {
const blobResponse = await client.uploadBlob(avatarBlob);
const blobLink = BlobLink.new(
blobResponse,
"image/jpeg",
"avatar.jpg"
);
return EntityRecord.update("alice.baq.run", entityRecord, {
...entityRecord.content,
avatar: blobLink,
});
}