updated API instantiation, and update user on verify

This commit is contained in:
dswbx
2025-01-29 14:44:32 +01:00
parent 86ba055f5e
commit c2b3316fcb
11 changed files with 250 additions and 57 deletions

View File

@@ -5,19 +5,74 @@ 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";
import { Api } from "bknd/client";
const api = new Api({
host: "..." // point to your bknd instance
});
const api = new Api();
// make sure to verify auth
// always make sure to verify auth
await api.verifyAuth();
```
The `Api` class is the main entry point for interacting with the bknd API. It provides methods
for all available modules described below.
## Setup
You can initialize an API instance by providing the `Request` object, or manually specifying the details such as `host` and `token`.
### Using the `Request` object
The recommended way to create an API instance is by passing the current `Request` object. This will automatically point the API to your current instance and extract the token from the headers (either from cookies or `Authorization` header):
```ts
import { Api } from "bknd/client";
// replace this with the actual request
let request: Request;
const api = new Api({ request });
```
If the authentication details are contained in the current request, but you're hosting your bknd instance somewhere else, you can specify a `host` option:
```ts
import { Api } from "bknd/client";
// replace this with the actual request
let request: Request;
const api = new Api({
host: "https://<your-endpoint>",
request,
});
```
### Using the `token` option
If you want to have an API instance that is using a different token, e.g. an admin token, you can create it by specifying the `host` and `token` option:
```ts
import { Api } from "bknd/client";
const api = new Api({
host: "https://<your-endpoint>",
token: "<your-token>"
});
```
### Using a local API
In case the place where you're using the API is the same as your bknd instance (e.g. when using it embedded in a React framework), you can specify a `fetcher` option to point to your bknd app. This way, requests won't travel over the network and instead processed locally:
```ts
import type { App } from "bknd";
import { Api } from "bknd/client";
// replace this with your actual `App` instance
let app: App;
const api = new Api({
fetcher: app.server.request as typeof fetch,
// specify `host` and `token` or `request`
});
```
## Data (`api.data`)
Access the `Data` specific API methods at `api.data`.
@@ -79,19 +134,25 @@ const { data } = await api.data.deleteOne("posts", 1);
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:
### `auth.strategies()`
To retrieve the available authentication strategies, use the `strategies` method:
```ts
const { data } = await api.auth.loginWithPassword({
const { data } = await api.auth.strategies();
```
### `auth.login([strategy], [input])`
To log in with a password, use the `login` method:
```ts
const { data } = await api.auth.login("password", {
email: "...",
password: "..."
});
```
### `auth.registerWithPassword([input])`
To register with a password, use the `registerWithPassword` method:
### `auth.register([strategy], [input])`
To register with a password, use the `register` method:
```ts
const { data } = await api.auth.registerWithPassword({
const { data } = await api.auth.register("password", {
email: "...",
password: "..."
});
@@ -103,8 +164,3 @@ To retrieve the current user, use the `me` method:
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();
```