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.
| Package | Use it to |
|---|---|
@spica-devkit/bucket | Manage buckets and read, write and stream bucket data. |
@spica-devkit/auth | Sign users up and in, verify emails and phones, reset passwords, passwordless login. |
@spica-devkit/identity | Log identities in, manage identities and two-factor authentication. |
@spica-devkit/storage | Upload, download and manage files. |
@spica-devkit/database | Access MongoDB directly from functions. |
@spica-devkit/sync | Plan and apply resource files from your own services. |
@spica-devkit/testing | Start disposable Spica instances for end-to-end tests. |
npm install @spica-devkit/bucket @spica-devkit/auth @spica-devkit/storageUse 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:
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"});| Credential | Sent as | Use it |
|---|---|---|
apikey | APIKEY <key> | On servers and in functions. |
identity | IDENTITY <jwt> | In internal tools used by your team. |
user | USER <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:
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:
await Bucket.data.get(bucketId, documentId, {headers: {"Accept-Language": "tr_TR"}});Errors
Failed requests reject with the API's error, for example:
{"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.