Dependencies & builds
Install npm packages into functions, write functions in TypeScript, share code between functions and choose how sources are built.
npm dependencies
Each function has its own package.json and node_modules. Install packages from the panel's Dependencies box, with the CLI, or over the API:
curl -X POST https://api.example.com/api/function/<functionId>/dependencies \
-H "Authorization: APIKEY <key>" \
-H "Content-Type: application/json" \
-d '{"name": ["@spica-devkit/bucket@0.22.1", "dayjs@^1.11.0"]}'curl https://api.example.com/api/function/<functionId>/dependencies \
-H "Authorization: APIKEY <key>"[{"name": "@spica-devkit/bucket", "version": "^0.22.1"}, {"name": "dayjs", "version": "^1.11.0"}]Remove a package with DELETE /function/<functionId>/dependencies/<name>. Scoped names work as they are, for example DELETE /function/<id>/dependencies/@spica-devkit/bucket.
In a CLI project, dependencies live in function/<name>/package.json. spica apply and spica dev install what changed.
{
"name": "hello",
"version": "0.0.1",
"dependencies": {
"@spica-devkit/bucket": "^0.22.1",
"dayjs": "^1.11.0"
}
}TypeScript
Set language: typescript and write index.ts. Spica compiles it when you save, and rejects code with type or syntax errors with 422 and a list of diagnostics with line and column. JavaScript is saved without checks, so a syntax error only shows up when the function runs:
import * as Bucket from "@spica-devkit/bucket";
interface Post {
_id: string;
title: string;
status: "draft" | "published";
}
export default async function (req: any): Promise<Post[]> {
Bucket.initialize({apikey: process.env.SPICA_API_KEY});
return Bucket.data.getAll<Post>(process.env.POSTS_BUCKET_ID, {
queryParams: {filter: 'document.status == "published"', limit: 10}
});
}Generate types for your buckets with spica bucket orm and import them into your functions.
Sharing code between functions
Any function can be imported by another one as @spica-fn/<functionName>:
{"name": ["@spica-fn/text-utils"]}import * as TextUtils from "@spica-fn/text-utils";The dependency is recorded as a local package (file:../text-utils). When you change the imported function, Spica rebuilds the functions that depend on it. Keep shared helpers in functions without triggers so they don't run on their own.
Builders
The API turns function sources into runnable code with one of two builders, set by --function-builder:
| Builder | What it does | Use it when |
|---|---|---|
legacy (default) | Compiles the entry file and loads dependencies from node_modules at runtime. | You want the long-standing behaviour. |
rollup | Bundles the entry file with its local imports and dependencies into one file. | You want faster cold starts and self-contained builds. |
Bundling large dependency graphs needs memory. Raise --function-builder-max-memory (in MB) when builds fail with out-of-memory errors, and keep it below your container's memory limit.
Where sources are stored
Function sources and their installed packages are kept on disk under the API's persistent path, and can also be stored in a shared location so that every API replica sees the same code:
--function-asset-storage-strategy | Location |
|---|---|
default | --function-asset-path. Point every replica at the same shared volume. |
awss3 | An S3 bucket: --function-asset-awss3-bucket-name, credentials from --function-asset-awss3-credentials-path or the AWS default chain. |
gcs | A Google Cloud Storage bucket: --function-asset-gcs-bucket-name and --function-asset-gcs-service-account-path. |
See the configuration reference.