@moodinary/sdk
v0.1.0
Published
Typed client for the Moodinary public API
Readme
@moodinary/sdk
A typed server-side client generated from Moodinary's canonical OpenAPI contract. Uses openapi-fetch, with no automatic retries. Keep organization-scoped API keys on your server, never in browser bundles or logs.
import { createMoodinaryClient } from "@moodinary/sdk";
const token = process.env.MOODINARY_API_KEY;
if (!token) throw new Error("Missing Moodinary API key");
const moodinary = createMoodinaryClient({ token });
const { data, error, response } = await moodinary.GET("/org");
if (error) throw new Error(`Moodinary returned ${response.status}`);The default base URL is https://api.moodinary.com/v1. Override baseUrl for a local server or pass fetch for a custom runtime or tests. Paths are relative to the version prefix, such as /org, not /v1/org.
Read resources
Typed GET covers /org, /teams, /pulses, /stats, /stats/daily, /alerts, /actions, and /comments. Query parameters, bodies, path parameters, and response types come from the generated schema.
const page = await moodinary.GET("/pulses", {
params: { query: { limit: 50 } },
});
if (page.data?.nextCursor) {
const nextPage = await moodinary.GET("/pulses", {
params: { query: { limit: 50, cursor: page.data.nextCursor } },
});
}
const teams = await moodinary.GET("/teams");
const stats = await moodinary.GET("/stats");
const daily = await moodinary.GET("/stats/daily");
const alerts = await moodinary.GET("/alerts");
const actions = await moodinary.GET("/actions");
const comments = await moodinary.GET("/comments");Create and update
Create requests require a caller-generated Idempotency-Key. Generate one UUID for each logical operation and persist it alongside the request before sending. If you reconcile or retry that same operation yourself, reuse its key and exact body. Never silently retry a mutation with a new key. The SDK rejects missing or blank create keys before sending.
const teamKey = crypto.randomUUID();
const team = await moodinary.POST("/teams", {
params: { header: { "Idempotency-Key": teamKey } },
body: { name: "Support" },
});
const actionKey = crypto.randomUUID();
const action = await moodinary.POST("/actions", {
params: { header: { "Idempotency-Key": actionKey } },
body: { title: "Schedule weekly check-ins" },
});
const updated = await moodinary.PATCH("/actions/{id}", {
params: { path: { id: "your-action-id" } },
body: { title: "Schedule monthly check-ins" },
});Non-2xx responses preserve the API error in error and the original HTTP response in response. Network failures reject the promise. There is no automatic retry for either case. Rate-limit headers and other response headers are available through response.headers.
Contract generation
From packages/sdk, run bun run generate after the server's canonical apps/server/openapi.json changes. This invokes openapi-typescript to regenerate src/schema.gen.ts. Commit the generated file together with the spec. Do not hand-edit it. Run bun run test for mocked transport tests; type checking runs in CI.
This workspace package currently exports TypeScript source, matching the monorepo convention. It is not yet a precompiled standalone npm distribution.
