NodeJS API using express and pg-promise.
You can browse the OpenAPI definition for the main branch here, and for the live API here.
Live at https://fallingfruit.org/api/0.3 and powers https://fallingfruit.org (falling-fruit-web).
-
Clone the repo and change into the new directory.
git clone https://github.com/falling-fruit/falling-fruit-api cd falling-fruit-api -
Install the
nodeversion specified in the.nvmrcfile. This is easiest usingnvm.nvm install nvm use
-
yarnis already provided (see.yarn/releases). Use it to install dependencies.yarn
-
Copy
.env.exampleto.envand update the values as needed.
cp .env.example .env-
Install Docker (with Compose). It is not required to run the API itself, but is used to provision a disposable PostGIS database for the end-to-end tests (
yarn test:e2e) and for generating database types (yarn codegen). -
If you want to use the e2e database in the API, set DATABASE_URL to the value in
.env.testand runyarn e2edb:up. -
You can also provision your own PostGIS database - see
.github/workflows/ci.ymlfor a setup to recreate.
yarn startVisit http://localhost:3300.
Tests use the built-in node --test runner, split by file naming convention:
yarn test: run everything (unit and e2e).yarn test:unit: run unit tests only (test/**/*.test.ts).yarn test:e2e: run e2e tests only (test/**/*.e2e.ts).
e2e tests require a database. yarn test:e2e starts the dockerized PostGIS
database (yarn e2edb:up), migrates it, and runs them. Tear it down afterwards
with yarn e2edb:down.
Migrations are managed with dbmate and located in ./db/migrations.
Create a new migration with:
yarn dbmate new <migration_name>Add SQL statements in the -- migrate:up and (optional) -- migrate:down sections, then run yarn codegen.
Row types in src/db/generated/ are derived from the live Postgres schema by
kanel (configured in
.kanelrc.cjs). Regenerate after any schema change (migration):
yarn codegenThe OpenAPI spec is generated from Zod schemas, which are the single source of truth for both runtime validation and the documentation:
src/schemas/: Zod component schemas, parameters, and security schemes, registered with@asteasolutions/zod-to-openapi.src/routes/*.ts: Each route declares its OpenAPI operation inline via a compact spec alongside its handler. The same schemas drive runtime request validation and compile-time response typing: a handler's return type is bound to its documented 2xx response schema.docs/base.yml: Static metadata (info,tags,servers).
scripts/build-openapi.ts registers every schema and route operation, merges
base.yml, and writes the bundled docs/openapi.yml.
Build and validate the spec:
yarn docsType-check the TypeScript sources:
yarn typecheck