Giter Site home page Giter Site logo

js-sdk's Introduction

PocketBase JavaScript SDK

Official JavaScript SDK (browser and node) for interacting with the PocketBase API.

Installation

Browser (manually via script tag)

<script src="/path/to/dist/pocketbase.umd.js"></script>

OR if you are using ES modules:

<script type="module">
    import PocketBase from '/path/to/dist/pocketbase.es.mjs'
    ...
</script>

Node.js (via npm)

npm install pocketbase --save
// Using ES modules (default)
import PocketBase from 'pocketbase'

// OR if you are using CommonJS modules
const PocketBase = require('pocketbase/cjs')

โš ๏ธ For Node < 17 you may need to add a fetch() polyfill.

I recommend cross-fetch: import 'cross-fetch/polyfill'

Examples

import PocketBase from 'pocketbase';

const client = new PocketBase('http://localhost:8090');

...

// list and filter "demo" collection records
const result = await client.Records.getList("demo", 1, 20, {
    filter: "status = true && totalComments > 0"
});

// authenticate as regular user
const userData = await client.Users.authViaEmail("[email protected]", "123456");


// or as admin
const adminData = await client.Admins.authViaEmail("[email protected]", "123456");

// and much more...

More detailed API docs and copy-paste examples could be found in the API documentation for each service.

Caveats

Errors handling

Each request error response is wrapped and in a normalized ClientResponseError object with the following fields:

ClientResponseError {
    url:           string,     // requested url
    status:        number,     // response status code
    data:          { ... },    // the api json error response
    isAbort:       boolean,    // is abort/cancellation error
    originalError: Error|null, // the original non-normalized error
}

Auto cancellation

The SDK client auto cancel duplicated pending requests. For example, if you have the following 3 duplicated calls, only the last one will be executed, while the first 2 will be cancelled with error null:

client.Records.getList("demo", 1, 20) // cancelled
client.Records.getList("demo", 1, 20) // cancelled
client.Records.getList("demo", 1, 20) // executed

To change this behavior, you could make use of 2 special query parameters:

  • $autoCancel - set it to false to disable auto cancellation for this request
  • $cancelKey - set it to a string that will be used as request identifier and based on which pending duplicated requests will be matched (default to HTTP_METHOD + url, eg. "get /api/users?page=1")

Example:

client.Records.getList("demo", 1, 20);                           // cancelled
client.Records.getList("demo", 1, 20);                           // executed
client.Records.getList("demo", 1, 20, { "$autoCancel": false }); // executed
client.Records.getList("demo", 1, 20, { "$autoCancel": false })  // executed
client.Records.getList("demo", 1, 20, { "$cancelKey": "test" })  // cancelled
client.Records.getList("demo", 1, 20, { "$cancelKey": "test" })  // executed

To manually cancel pending requests, you could use client.cancelAllRequests() or client.cancelRequest(cancelKey).

AuthStore

The SDK keeps track of the authenticated token and auth model for you via the client.AuthStore instance. It has the following public fields that you can use:

AuthStore {
    token:   string,     // the authenticated token
    model:   User|Admin, // the authenticated User or Admin model
    isValid: boolean,    // checks if the store has existing and unexpired token
    clear(),             // "logout" the authenticated User or Admin
    save(token, model),  // update the store with the new auth data
}

By default the SDK initialize a LocalAuthStore, which uses the browser's LocalStorage if available, otherwise - will fallback to runtime/memory store (aka. on page refresh or service restart you'll have to authenticate again).

To "logout" an authenticated user or admin, you can just call client.AuthStore.clear().

Definitions

Creating new client instance

const client = new PocketBase(
    baseUrl = '/',
    lang = 'en-US',
    authStore = LocalAuthStore
);

Instance methods

Each instance method returns the PocketBase instance allowing chaining.

Method Description
client.send(path, reqConfig = {}) Sends an api http request.
client.cancelAllRequests() Cancels all pending requests.
client.cancelRequest(cancelKey) Cancels single request by its cancellation token key.
client.buildUrl(path, reqConfig = {}) Builds a full client url by safely concatenating the provided path.

API services

Each service call returns a Promise object with the API response.

Resource Description
Admins
๐Ÿ”“client.Admins.authViaEmail(email, password, bodyParams = {}, queryParams = {}) Authenticate an admin account by its email and password and returns a new admin token and admin data.
๐Ÿ”client.Admins.refresh(bodyParams = {}, queryParams = {}) Refreshes the current admin authenticated instance and returns a new admin token and admin data.
๐Ÿ”“client.Admins.requestPasswordReset(email, bodyParams = {}, queryParams = {}) Sends admin password reset request.
๐Ÿ”“client.Admins.confirmPasswordReset(passwordResetToken, password, passwordConfirm, bodyParams = {}, queryParams = {}) Confirms admin password reset request.
๐Ÿ”client.Admins.getList(page = 1, perPage = 30, queryParams = {}) Returns paginated admins list.
๐Ÿ”client.Admins.getFullList(batchSize = 100, queryParams = {}) Returns a list with all admins batch fetched at once.
๐Ÿ”client.Admins.getOne(id, queryParams = {}) Returns single admin by its id.
๐Ÿ”client.Admins.create(bodyParams = {}, queryParams = {}) Creates a new admin.
๐Ÿ”client.Admins.update(id, bodyParams = {}, queryParams = {}) Updates an existing admin by its id.
๐Ÿ”client.Admins.delete(id, bodyParams = {}, queryParams = {}) Deletes an existing admin by its id.
Users
๐Ÿ”“client.Users.listAuthMethods(queryParams = {}) Returns all available application auth methods.
๐Ÿ”“client.Users.authViaEmail(email, password, bodyParams = {}, queryParams = {}) Authenticate a user account by its email and password and returns a new user token and user data.
๐Ÿ”“client.Users.authViaOAuth2(clientName, code, codeVerifier, redirectUrl, bodyParams = {}, queryParams = {}) Authenticate a user via OAuth2 client provider.
๐Ÿ”client.Users.refresh(bodyParams = {}, queryParams = {}) Refreshes the current user authenticated instance and returns a new user token and user data.
๐Ÿ”“client.Users.requestPasswordReset(email, bodyParams = {}, queryParams = {}) Sends user password reset request.
๐Ÿ”“client.Users.confirmPasswordReset(passwordResetToken, password, passwordConfirm, bodyParams = {}, queryParams = {}) Confirms user password reset request.
๐Ÿ”“client.Users.requestVerification(email, bodyParams = {}, queryParams = {}) Sends user verification email request.
๐Ÿ”“client.Users.confirmVerification(verificationToken, bodyParams = {}, queryParams = {}) Confirms user email verification request.
๐Ÿ”client.Users.requestEmailChange(newEmail, bodyParams = {}, queryParams = {}) Sends an email change request to the authenticated user.
๐Ÿ”“client.Users.confirmEmailChange(emailChangeToken, password, bodyParams = {}, queryParams = {}) Confirms user new email address.
๐Ÿ”client.Users.getList(page = 1, perPage = 30, queryParams = {}) Returns paginated users list.
๐Ÿ”client.Users.getFullList(batchSize = 100, queryParams = {}) Returns a list with all users batch fetched at once.
๐Ÿ”client.Users.getOne(id, queryParams = {}) Returns single user by its id.
๐Ÿ”client.Users.create(bodyParams = {}, queryParams = {}) Creates a new user.
๐Ÿ”client.Users.update(id, bodyParams = {}, queryParams = {}) Updates an existing user by its id.
๐Ÿ”client.Users.delete(id, bodyParams = {}, queryParams = {}) Deletes an existing user by its id.
Realtime
(for node environments you'll have to install an EventSource polyfill beforehand, eg. https://github.com/EventSource/eventsource)
๐Ÿ”“client.Realtime.subscribe(subscription, callback) Inits the sse connection (if not already) and register the subscription.
๐Ÿ”“client.Realtime.unsubscribe(subscription = "") Unsubscribe from a subscription (if empty - unsubscibe from all registered subscriptions).
Collections
๐Ÿ”client.Collections.getList(page = 1, perPage = 30, queryParams = {}) Returns paginated collections list.
๐Ÿ”client.Collections.getFullList(batchSize = 100, queryParams = {}) Returns a list with all collections batch fetched at once.
๐Ÿ”client.Collections.getOne(id, queryParams = {}) Returns single collection by its id.
๐Ÿ”client.Collections.create(bodyParams = {}, queryParams = {}) Creates a new collection.
๐Ÿ”client.Collections.update(id, bodyParams = {}, queryParams = {}) Updates an existing collection by its id.
๐Ÿ”client.Collections.delete(id, bodyParams = {}, queryParams = {}) Deletes an existing collection by its id.
Records
๐Ÿ”“client.Records.getList(collectionIdOrName, page = 1, perPage = 30, queryParams = {}) Returns paginated records list.
๐Ÿ”“client.Records.getFullList(collectionIdOrName, batchSize = 100, queryParams = {}) Returns a list with all records batch fetched at once.
๐Ÿ”“client.Records.getOne(collectionIdOrName, id, queryParams = {}) Returns single record by its id.
๐Ÿ”“client.Records.create(collectionIdOrName, bodyParams = {}, queryParams = {}) Creates a new record.
๐Ÿ”“client.Records.update(collectionIdOrName, id, bodyParams = {}, queryParams = {}) Updates an existing record by its id.
๐Ÿ”“client.Records.delete(collectionIdOrName, id, bodyParams = {}, queryParams = {}) Deletes an existing record by its id.
Logs
๐Ÿ”client.Logs.getRequestsList(page = 1, perPage = 30, queryParams = {}) Returns paginated request logs list.
๐Ÿ”client.Logs.getRequest(id, queryParams = {}) Returns a single request log by its id.
๐Ÿ”client.Logs.getRequestsStats(queryParams = {}) Returns request logs statistics.
Settings
๐Ÿ”client.Settings.getAll(queryParams = {}) Fetch all available app settings.
๐Ÿ”client.Settings.update(bodyParams = {}, queryParams = {}) Bulk updates app settings.

Development

# run unit tests
npm test

# build and minify for production
npm run build

js-sdk's People

Contributors

ganigeorgiev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.