Javascript SDK

Client

Perform requests against a server.

const client = Client.ofUrl("...");
const record = await client.getRecord(...);

Static methods

  • ofEntity(entity): Client

    • Create a new client from a user’s entity.

    • Parameters
      • entity Entity

        • Entity of the user.
  • ofUrl(entityRecordUrl): Client

    • Create a new client from the URL of a user’s Entity record.

    • Parameters
      • entityRecordUrl string

        • URL of the user’s Entity record.
  • ofRecord(entityRecord: EntityRecord): Client

    • Create a new client from a user’s Entity record.
  • authenticated(state: AuthenticationState): Client

Methods

  • discover(entity, signal?) : Promise<EntityRecord>

    • Perform discovery on the provided entity.

    • Parameters
      • entity Entity

        • Entity to perform discovery on.
      • signal AbortSignal optional

        • Signal to abort the request.
  • getEntityRecord(signal?): Promise<EntityRecord>

    • Fetch the user’s entity record.

    • (Fetched only once, and cached beyond that point).

    • Parameters
      • signal AbortSignal optional

        • Signal to abort the request.
  • getOwnRecord(linkModel, model, id, opts?)

    • Fetch a single of our user’s records by ID.

    • Parameters
      • linkModel Type

        • Expected record types of linked records.
      • model Type

        • Expected type of the requested record.
      • id RecordId

        • ID of the record to fetch.
      • options object optional

        • query SingleQuery optional : Additional server parameters.

        • signal AbortSignal optional : Signal to abort the request.

    • Return value
  • getRecord(linkModel, model, entity, id, opts?)

    • Fetch a single record by author entity and ID.

    • Parameters
      • linkModel Type

        • Expected record types of linked records.
      • model Type

        • Expected type of the requested record.
      • entity Entity

        • Author of the record to fetch.
      • id RecordId

        • ID of the record to fetch.
      • options object optional

        • query SingleQuery optional : Additional server parameters.

        • signal AbortSignal optional : Signal to abort the request.

    • Return value
  • getRecords(linkModel, model, query, signal?)

    • Fetch a list of records from a Query.

    • Parameters
      • linkModel Type

        • Expected record types of linked records.
      • model Type

        • Expected type of the requested records.
      • query Query

        • Query to narrow down what records should be fetched.
      • signal AbortSignal optional

        • Signal to abort the request.
    • Return value
  • postRecord(linkModel, model, record, signal?)

    • Create a new record.

    • Parameters
      • linkModel Type

        • Expected record types of linked records.
      • model Type

        • Type of the record to create.
      • record Record

        • Record to create.
      • signal AbortSignal optional

        • Signal to abort the request.
    • Return value
  • putRecord(linkModel, model, record, signal?)

    • Update an existing record.

    • Parameters
      • linkModel Type

        • Expected record types of linked records.
      • model Type

        • Type of the record to update.
      • record Record

        • Record to update.
      • signal AbortSignal optional

        • Signal to abort the request.
    • Return value
  • deleteRecord(linkModel, record, opts?)

    • Delete a record by updating it with a record tombstone­.

    • Parameters
      • linkModel Type

        • Expected record types of linked records.
      • record NoContentRecord

        • Record tombstone.
      • signal AbortSignal optional

        • Signal to abort the request.
    • Return value
  • uploadBlob(blob, signal?): Promise<BlobResponse>

    • Upload a single blob for use in future records.

    • Parameters
      • blob Blob

        • Blob to upload.
      • signal AbortSignal optional

        • Signal to abort the request.
  • downloadBlob(record, blobLink, signal?): Promise<Blob>

    • Download a single blob from a record and blob link.

    • Parameters
      • record Record

        • Record linking to the blob to download.
      • blobLink BlobLink

        • Link to the specific blob to download.
      • signal AbortSignal optional

        • Signal to abort the request.
  • blobUrlBuilder(): Promise<BlobUrlBuilder>

    • Get a function to synchronously build blob download URLs.

Properties

  • record Record

    • Requested record.
  • linkedRecords Record[]

    • Flat list of linked records.

Properties

  • records Record[]

    • Requested records.
  • linkedRecords Record[]

    • Flat list of linked records.

Properties

  • hash BlobHash

    • SHA256 hash of the uploaded blob.
  • size int

    • Size of the uploaded blob in bytes.
  • expiresAt Date

    • Flat list of linked records.

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,
  });
}
© 2024 Quentez