Devkit

Typed JavaScript packages for calling Spica from functions, browsers, Node.js services, CI and tests.

The devkit is a set of npm packages that wrap Spica's APIs with typed functions. They work in Spica functions, browsers and Node.js.

PackageUse it to
@spica-devkit/bucketManage buckets and read, write and stream bucket data.
@spica-devkit/authSign users up and in, verify emails and phones, reset passwords, passwordless login.
@spica-devkit/identityLog identities in, manage identities and two-factor authentication.
@spica-devkit/storageUpload, download and manage files.
@spica-devkit/databaseAccess MongoDB directly from functions.
@spica-devkit/syncPlan and apply resource files from your own services.
@spica-devkit/testingStart disposable Spica instances for end-to-end tests.
Terminal
npm install @spica-devkit/bucket @spica-devkit/auth @spica-devkit/storage

Use the same version as your Spica instance.

Initialize

Every package except database, sync and testing has an initialize function. Call it once with credentials and the API's public URL:

TypeScript
import * as Bucket from "@spica-devkit/bucket";

Bucket.initialize({apikey: "<key>", publicUrl: "https://api.example.com/api"});
Bucket.initialize({identity: "<identity jwt>", publicUrl: "https://api.example.com/api"});
Bucket.initialize({user: "<user jwt>", publicUrl: "https://api.example.com/api"});
CredentialSent asUse it
apikeyAPIKEY <key>On servers and in functions.
identityIDENTITY <jwt>In internal tools used by your team.
userUSER <jwt>In your product's clients. Rules apply.

@spica-devkit/auth can also be initialized with only publicUrl, for calls such as signIn that don't need credentials.

Inside functions

Functions know where the API is, so publicUrl can be left out. Initialize once at module level:

JavaScript
import * as Bucket from "@spica-devkit/bucket";

Bucket.initialize({apikey: process.env.SPICA_API_KEY});

export default async function () {
  return Bucket.data.getAll(process.env.POSTS_BUCKET_ID, {queryParams: {limit: 5}});
}

Install the devkit as a function dependency and keep the API key in a secret.

Per-call headers

Most functions accept a last headers argument, for example to send Accept-Language or to call with another user's token:

TypeScript
await Bucket.data.get(bucketId, documentId, {headers: {"Accept-Language": "tr_TR"}});

Errors

Failed requests reject with the API's error, for example:

JSON
{"statusCode": 403, "message": "You do not have sufficient permissions to do bucket:data:index on resource 6b0b1968a5611972c29c25f7", "error": "Forbidden"}

Engines

The devkit types use _id, as MongoDB instances do. On PostgreSQL instances, documents and resources are identified by id and by the bucket's primary property, so read those fields instead. See MongoDB and PostgreSQL.