From c300f5e107706cb7be1157fb0c63cc6c83e733bc Mon Sep 17 00:00:00 2001 From: dswbx Date: Tue, 19 Nov 2024 15:45:59 +0100 Subject: [PATCH] doc update: added a brief description for the ts sdk --- docs/mint.json | 2 +- docs/sdk.mdx | 107 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 docs/sdk.mdx diff --git a/docs/mint.json b/docs/mint.json index 0e32f6d..b9f94b3 100644 --- a/docs/mint.json +++ b/docs/mint.json @@ -72,7 +72,7 @@ "navigation": [ { "group": "Getting Started", - "pages": ["introduction", "setup"] + "pages": ["introduction", "setup", "sdk"] }, { "group": "Modules", diff --git a/docs/sdk.mdx b/docs/sdk.mdx new file mode 100644 index 0000000..ac5e065 --- /dev/null +++ b/docs/sdk.mdx @@ -0,0 +1,107 @@ +--- +title: 'SDK (TypeScript)' +description: 'Use the bknd SDK in TypeScript' +--- + +To start using the bknd API, start by creating a new API instance: +```ts +import { Api } from "bknd"; + +const api = new API({ + host: "..." // point to your bknd instance +}); +``` + +The `Api` class is the main entry point for interacting with the bknd API. It provides methods +for all available modules described below. + +## Data (`api.data`) +Access the `Data` specific API methods at `api.data`. + +### `data.readMany([entity], [query])` +To retrieve a list of records from an entity, use the `readMany` method: +```ts +const { data } = await api.data.readMany("posts"); +``` + +You can also add additional query instructions: +```ts +const { data } = await api.data.readMany("posts", { + limit: 10, + offset: 0, + select: ["id", "title", "views"], + with: ["comments"], + where: { + title: "Hello, World!", + views: { + $gt: 100 + } + }, + sort: { by: "views", order: "desc" } +}); +``` +The `with` property automatically adds the related entries to the response. + +### `data.readOne([entity], [id])` +To retrieve a single record from an entity, use the `readOne` method: +```ts +const { data } = await api.data.readOne("posts", 1); +``` + +### `data.createOne([entity], [data])` +To create a single record of an entity, use the `createOne` method: +```ts +const { data } = await api.data.createOne("posts", { + title: "Hello, World!", + content: "This is a test post.", + views: 0 +}); +``` + +### `data.updateOne([entity], [id], [data])` +To update a single record of an entity, use the `updateOne` method: +```ts +const { data } = await api.data.updateOne("posts", 1, { + views: 1 +}); +``` + +### `data.deleteOne([entity], [id])` +To delete a single record of an entity, use the `deleteOne` method: +```ts +const { data } = await api.data.deleteOne("posts", 1); +``` + +## Auth (`api.auth`) +Access the `Auth` specific API methods at `api.auth`. If there is successful authentication, the +API will automatically save the token and use it for subsequent requests. + +### `auth.loginWithPassword([input])` +To log in with a password, use the `loginWithPassword` method: +```ts +const { data } = await api.auth.loginWithPassword({ + email: "...", + password: "..." +}); +``` + +### `auth.registerWithPassword([input])` +To register with a password, use the `registerWithPassword` method: +```ts +const { data } = await api.auth.registerWithPassword({ + email: "...", + password: "..." +}); +``` + +### `auth.me()` +To retrieve the current user, use the `me` method: +```ts +const { data } = await api.auth.me(); +``` + +### `auth.strategies()` +To retrieve the available authentication strategies, use the `strategies` method: +```ts +const { data } = await api.auth.strategies(); +``` \ No newline at end of file