@cattle-grid/account-api

cattle_grid account api sdk

The aim here is to create a client sdk for the cattle grid account api. This package is autogenerated using @hey-api/openapi-ts.

For background on cattle_grid, see here.

This API does not support event sources directly. For how to use them see Event Sources

Import the relevant methods and configure the client with the baseUrl.

import { signin, accountInfo } from "@cattle-grid/account-api";
import { client } from "@cattle-grid/account-api/client";

client.setConfig({
baseUrl: "http://localhost:3001/fe",
});

One can sign in into an account with the account name and the corresponding password. For further requests, you will need the bearer token.

let result = await signin({ body: { name: "js", password: "js" } });

const bearerToken = result.data.token;

This is done via:

client.interceptors.request.use(async (request) => { 
request.headers.set("Authorization", "Bearer " + bearerToken);
return request;
});

This can be done via

const result = await accountInfo();

The response data is in result.data the status code can be retrieved from result.response.status.

Furthermore methods can be discovered via sdk.gen.

As we use Bearer authentication, you cannot use a vanilla event source as provided by the browser. Instead, you have to use a polyfill such as extended-eventsource.

Once you have this, usage is like this

import { EventSource } from "extended-eventsource";

const eventSource = new EventSource(`/fe/account/stream/${eventType}`, {
headers: {
Authorization: `Bearer ${token}`,
},
});


eventSource.addEventListener("message", (event) => {
const parsed = JSON.parse(event.data);
console.log(parsed);
});

Here eventType is one of incoming, outgoing, or error.