@molecule/api-resource
Infrastructure · resource · API (Node) · v1.5.1 · Apache-2.0
CRUD resource framework
npm install @molecule/api-resourceHow it works
@molecule/api-resource is a infrastructure package for the API (Node) side (resource).
import { z } from 'zod'
import {
createRequestHandler,
create,
read,
update,
del,
query,
type MoleculeRequest,
} from '@molecule/api-resource'
import { types } from '@molecule/api-resource'
import { basePropsSchema } from '@molecule/api-resource/schema'
// Every resource schema extends basePropsSchema (id/createdAt/updatedAt).
const userSchema = basePropsSchema.extend({ name: z.string(), email: z.string() })
type UserProps = z.infer<typeof userSchema>
// Each CRUD factory takes the resource descriptor and returns an async
// operation, e.g. ({ props }) or ({ id }) => { statusCode, body }
// One descriptor, typed as Resource, drives every CRUD factory:
const userResource: types.Resource = { name: 'User', tableName: 'users', schema: userSchema }
const createUser = create<UserProps>(userResource)
const updateUser = update<UserProps>(userResource)
const readUser = read<UserProps>(userResource)
const deleteUser = del<UserProps>(userResource)
// Operations do NOT take (req, res) directly — adapt each one by mapping
// request fields (req.body, req.params.id) to its arguments, then wrap
// with createRequestHandler for Express
const requestHandlerMap = {
create: createRequestHandler(
async (req: MoleculeRequest) => await createUser({ props: req.body }),
),
read: createRequestHandler(async (req: MoleculeRequest) => await readUser({ id: req.params.id })),
// ...
}Works with: @molecule/api-bond, @molecule/api-database, @molecule/api-i18n, @molecule/api-locales-resource, @molecule/api-utilities-validation
Reference
Base resource patterns for molecule.dev.
Provides CRUD operation factories and request handler utilities for
building RESTful API resources. Every @molecule/api-resource-* package
(user, project, device, ...) is built on these factories.
A handler returns { statusCode, headers?, body } and NEVER writes to res — not through
res.json, not through res.send, not through res.end. createRequestHandler is the wrapper
that applies the returned object (res.set(headers), res.status(statusCode), res.send(body)),
and a handler returns null only when the request should fall through to next(). Calling a
handler directly from your own route is therefore only correct if you apply that return value
yourself:
const { statusCode, headers, body } = await handler(req, res)
if (headers) res.set(headers)
res.status(statusCode).send(body)
Monkey-patching res.json (or res.send/res.end) to re-shape a handler's answer intercepts
nothing the handler produces, so the request it was meant to answer is left unanswered and hangs.
A different request or response envelope is configuration. Pass
mapRequest and/or mapResponse as the second argument to createRequestHandler.
Both may be asynchronous. The existing handler still validates and authorizes
the adapted input. A response mapper sees the handler's errors and partial
responses too: preserve them and its headers instead of treating every result
as a success. adaptHandler(handler, options) offers the same mappings when
another HTTP adapter will send the structured response.
A request mapper can return { response: { statusCode, body } } to answer
before calling the handler, for example when extra validation fails or a
filter lookup finds nothing. That response still passes through mapResponse.
import { createRequestHandler } from '@molecule/api-resource'
import { handlers } from '@molecule/api-resource-user'
const register = createRequestHandler(handlers.create({ name: 'User', tableName: 'users' }), {
// Return overrides. The adapter retains framework methods and getters.
mapRequest: (req) => ({ body: req.body?.account }),
mapResponse: (result) =>
result.statusCode === 201
? { ...result, body: { account: result.body.props, accessToken: result.body.accessToken } }
: result,
})
// Keep the route's existing rate limiter and authorization around register.
// No mapping is inferred: a flat-body API keeps createRequestHandler(handler).
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Quick Start
import { z } from 'zod'
import {
createRequestHandler,
create,
read,
update,
del,
query,
type MoleculeRequest,
} from '@molecule/api-resource'
import { types } from '@molecule/api-resource'
import { basePropsSchema } from '@molecule/api-resource/schema'
// Every resource schema extends basePropsSchema (id/createdAt/updatedAt).
const userSchema = basePropsSchema.extend({ name: z.string(), email: z.string() })
type UserProps = z.infer<typeof userSchema>
// Each CRUD factory takes the resource descriptor and returns an async
// operation, e.g. ({ props }) or ({ id }) => { statusCode, body }
// One descriptor, typed as Resource, drives every CRUD factory:
const userResource: types.Resource = { name: 'User', tableName: 'users', schema: userSchema }
const createUser = create<UserProps>(userResource)
const updateUser = update<UserProps>(userResource)
const readUser = read<UserProps>(userResource)
const deleteUser = del<UserProps>(userResource)
// Operations do NOT take (req, res) directly — adapt each one by mapping
// request fields (req.body, req.params.id) to its arguments, then wrap
// with createRequestHandler for Express
const requestHandlerMap = {
create: createRequestHandler(
async (req: MoleculeRequest) => await createUser({ props: req.body }),
),
read: createRequestHandler(async (req: MoleculeRequest) => await readUser({ id: req.params.id })),
// ...
}
Type
infrastructure
Installation
npm install @molecule/api-resource @molecule/api-bond @molecule/api-database @molecule/api-i18n @molecule/api-locales-resource @molecule/api-utilities-validation zod
API
Interfaces
Response
Structured response object returned by resource handlers. Contains the HTTP status code, optional headers, and the response body payload.
interface Response {
/**
* HTTP status code to return.
*/
statusCode: number
/**
* Optional map of HTTP header keys to values.
*/
headers?: Record<string, string>
/**
* The response payload to return.
*/
body: any
}
RequestHandlerOptions
Per-route wire-format adapters. Validation and authorization still belong to the original handler. Omit these options to retain its native contract.
interface RequestHandlerOptions {
/**
* Return the fields to replace before the handler validates the request.
* The adapter preserves the original request's methods and getters, and does
* not mutate it. Returning `{ body }` or `{ query }` is sufficient, including
* frameworks whose request properties have read-only accessors.
* Return `{ response: { statusCode, headers?, body } }` to answer before the
* handler runs. That result still goes through mapResponse, with the original request.
*/
mapRequest?: (
req: MoleculeRequest,
res: MoleculeResponse,
) => RequestMappingResult | Promise<RequestMappingResult>
/**
* Adapt the returned status, headers or body. This sees error and partial
* responses too: narrow the success shape before changing it, and preserve
* headers with `{ ...response, body }`. It never runs for fall-through.
*/
mapResponse?: (
response: Response,
req: MoleculeRequest,
res: MoleculeResponse,
) => Response | Promise<Response>
}
MoleculeRequest
Framework-agnostic HTTP request interface. Structurally compatible with Express's
Request — Express objects satisfy this interface without adapters, but the type
doesn't depend on Express, allowing use with Fastify, Hono, or Lambda.
params is Record<string, any> on purpose. Express 5 declares
ParamsDictionary as { [key: string]: string | string[] } — a wildcard or a
repeated parameter yields an array — so Record<string, string> is NOT a
supertype of it, and every Express handler passing its own Request to a
function declared on this interface (authorization.set(req, …)) failed with
TS2345 and was told to cast. This is the same shape body and query already
use. A reader that needs one segment as a string checks it
(typeof req.params.id === 'string') rather than trusting the annotation;
the packages that own the routes do exactly that where it matters (see
@molecule/api-bonds-default-express's getParamId).
interface MoleculeRequest {
body: any
params: Record<string, any>
query: Record<string, any>
headers: Record<string, string | string[] | undefined>
cookies?: Record<string, string>
/**
* Client IP address. Populated by Express (honoring `trust proxy`) and other
* frameworks; optional because it isn't guaranteed in every adapter. Used for
* IP-keyed rate limiting on auth endpoints.
*/
ip?: string
on?(event: string, listener: (...args: unknown[]) => void): void
}
MoleculeResponse
Framework-agnostic HTTP response interface. Structurally compatible with Express's
Response — includes status(), json(), send(), set(), cookie methods,
streaming (write), and locals for per-request data sharing.
interface MoleculeResponse {
status(code: number): this
json(body: unknown): void
send(body: unknown): void
end(): void
set(headers: Record<string, string>): void
setHeader(name: string, value: string): void
cookie(name: string, value: string, options?: Record<string, unknown>): void
/** Clear a cookie on adapters that support cookies. */
clearCookie?(name: string, options?: Record<string, unknown>): void
/** Remove an outgoing header when the adapter supports it. */
removeHeader?(name: string): void
write(chunk: string | Buffer): boolean
flushHeaders?(): void
locals: Record<string, any>
}
Query
The available resource query options.
interface Query {
/**
* The maximum number of query results.
*/
limit?: number
/**
* An indexed property to order the results by.
*/
orderBy?: `createdAt` | `updatedAt`
/**
* The direction to order the results.
*/
orderDirection?: `asc` | `desc`
/**
* Constrains the results to values less than these properties, when provided.
*/
before?: {
/**
* Constrains the results to resources created before the provided timestamp.
*
* Usually an ISO 8601 timestamp.
*/
createdAt?: string
/**
* Constrains the results to resources updated before the provided timestamp.
*
* Usually an ISO 8601 timestamp.
*/
updatedAt?: string
}
/**
* Constrains the results to values greater than these properties, when provided.
*/
after?: {
/**
* Constrains the results to resources created after the provided timestamp.
*
* Usually an ISO 8601 timestamp.
*/
createdAt?: string
/**
* Constrains the results to resources updated after the provided timestamp.
*
* Usually an ISO 8601 timestamp.
*/
updatedAt?: string
}
}
ErrorFallback
Fallback HTTP response used when a caught error is NOT a molecule-tagged error (i.e. a genuinely-unexpected failure).
interface ErrorFallback {
/** HTTP status for the fallback (typically 500). */
status: number
/** Human-readable fallback message (already localized by the caller). */
message: string
/** Machine-readable key the frontend maps to a localized message. */
errorKey: string
}
Types
Handler
A resource handler function that processes a request and returns a structured
response object. If it returns null or undefined, the request is passed
to the next middleware via next().
type Handler = (
req: MoleculeRequest,
res: MoleculeResponse,
) => Promise<Response | null | undefined> | Response | null | undefined
req— The incoming HTTP request.res— The HTTP response (available but handlers should return a Response instead of callingresdirectly).
RequestMappingResult
Request overrides, or an explicit response that stops before the handler. The response form supports extra validation and unresolved external filters without writing data or inventing a placeholder identifier.
type RequestMappingResult = Partial<MoleculeRequest> | { response: Response }
MoleculeNextFunction
Framework-agnostic next function. Call with no arguments to pass to the next middleware, or with an error to trigger error-handling middleware.
type MoleculeNextFunction = (err?: unknown) => void
err— Optional error to forward to error-handling middleware.
MoleculeRequestHandler
Framework-agnostic middleware function signature matching Express's
(req, res, next) => void pattern.
type MoleculeRequestHandler = (
req: MoleculeRequest,
res: MoleculeResponse,
next: MoleculeNextFunction,
) => void | Promise<void>
Functions
create(resource)
Creates a handler to create a resource.
Uses the bonded DataStore from @molecule/api-database for database-agnostic operations.
Example usage:
import { create as resourceCreate } from '@molecule/api-resource'
import type * as types from '../../types.js'
export const create = ({ name, tableName, schema }: types.Resource) => {
const create = resourceCreate({ name, tableName, schema })
return async (req: MoleculeRequest) => {
const props = req.body
return await create({ props })
}
}
function create<
CreateProps extends Record<string, unknown> = Record<string, unknown>,
CreatedProps extends types.Props & CreateProps = types.Props & CreateProps,
>({
name,
tableName,
schema,
}: types.Resource): ({
props: createProps,
id,
}: {
props: CreateProps
id?: CreatedProps['id']
}) => Promise<
| { statusCode: number; body: { error: string; errorKey: string; props?: undefined } }
| { statusCode: number; body: { props: CreatedProps; error?: undefined; errorKey?: undefined } }
>
resource— The resource descriptor.resource.name— Human-readable resource name used in error messages (e.g.'User').resource.tableName— Database table to insert the new row into.resource.schema— Zod schema to validate the incoming props against.
Returns: A curried async function that accepts { props, id? } and returns a { statusCode, body } response.
adaptHandler(handler, options)
Compose wire-format adapters around a structured resource handler without sending its response. The response mapper receives the adapted request. Exceptions propagate to the caller's normal error boundary.
function adaptHandler(handler: Handler, options?: RequestHandlerOptions): Handler
handler— The existing resource handler, including its checks.options— Request and response mappings for this route only.
Returns: A structured handler that keeps null/undefined fall-through intact.
createRequestHandler(handler, options)
Wraps a resource handler into Express-compatible middleware. Calls the handler,
then applies statusCode, headers, and body to the response. If the handler
returns null/undefined, calls next(). Catches errors and forwards them as
i18n-translated error messages.
function createRequestHandler(
handler: Handler,
options?: RequestHandlerOptions,
): (req: MoleculeRequest, res: MoleculeResponse, next: MoleculeNextFunction) => Promise<void>
handler— The resource handler function to wrap.options— Optional wire-format mappings; neither mutates the shared handler.
Returns: An Express-compatible middleware function (req, res, next) => void.
del(resource)
Creates a handler to delete a resource by its id.
Note: We use
deleverywhere instead ofdeletesincedeleteis a reserved keyword in JavaScript.
Uses the bonded DataStore from @molecule/api-database for database-agnostic operations.
Example usage:
import { del as resourceDel } from '@molecule/api-resource'
import type * as types from '../../types.js'
export const del = ({ name, tableName, schema }: types.Resource) => {
const del = resourceDel({ name, tableName, schema })
return async (req: MoleculeRequest) => {
const id = req.params.id
return await del({ id })
}
}
function del<
Props extends types.Props = types.Props,
DeletedResource extends types.Resource = types.Resource,
>({
name,
tableName,
}: DeletedResource): ({ id }: { id: Props['id'] }) => Promise<
| { statusCode: number; body: { error: string; errorKey: string; props?: undefined } }
| {
statusCode: number
body: { props: { id: Props['id'] }; error?: undefined; errorKey?: undefined }
}
>
resource— The resource descriptor.resource.name— Human-readable resource name used in error messages (e.g.'User').resource.tableName— Database table to delete the row from.
Returns: A curried async function that accepts { id } and returns a { statusCode, body } response.
query(resource)
Creates a request handler to be used to query resources.
Uses the bonded DataStore from @molecule/api-database for database-agnostic operations.
function query<
Props extends types.Props = types.Props,
QueriedResource extends types.Resource = types.Resource,
>({
tableName,
}: QueriedResource): (
req: MoleculeRequest,
) => Promise<
| { statusCode: number; body: { error: string; errorKey: string } }
| { statusCode: number; body: Props[] }
>
resource— The resource descriptor.resource.tableName— Database table to query resources from.
Returns: A curried async handler that accepts a MoleculeRequest (with query params for pagination/sorting) and returns a { statusCode, body } response.
read(resource)
Creates a handler to read a resource by its id.
Uses the bonded DataStore from @molecule/api-database for database-agnostic operations.
Example usage:
import { read as resourceRead } from '@molecule/api-resource'
import type * as types from '../../types.js'
export const read = ({ name, tableName, schema }: types.Resource) => {
const read = resourceRead({ name, tableName, schema })
return async (req: MoleculeRequest) => {
const id = req.params.id
return await read({ id })
}
}
function read<
ReadProps extends types.Props = types.Props,
ReadResource extends types.Resource = types.Resource,
>({
tableName,
}: ReadResource): ({
id,
props,
}: {
id: ReadProps['id']
props?: ReadProps
}) => Promise<
| { statusCode: number; body: { props: ReadProps; error?: undefined; errorKey?: undefined } }
| { statusCode: number; body: { error: string; errorKey: string; props?: undefined } }
>
resource— The resource descriptor.resource.tableName— Database table to query the resource from.
Returns: A curried async function that accepts { id, props? } and returns a { statusCode, body } response.
respondError(res, error, fallback)
Respond to a caught handler error, honoring molecule's "tagged error" convention.
A tagged error carries BOTH a numeric statusCode AND a string errorKey — the same
contract @molecule/api-server-default-express's error middleware (classifyTaggedError)
uses. Providers throw these for expected, user-actionable conditions — e.g. a
config-missing throw (503 + 'config.notConfigured') when STRIPE_SECRET_KEY is unset.
Handlers that catch errors MUST funnel through this instead of a blanket
res.status(500), so a tagged error surfaces its REAL status + errorKey rather than
being flattened to a generic 500. Otherwise the app looks broken at the exact moment the
user tries to use the feature — the worst possible time for a payment (the conversion
path). A swallowed tag is why the middleware-only fix was insufficient: ~185 handlers
catch-and-500, so the tag never reached the middleware.
Tagged errors are logged at warn (expected/actionable, not a server fault); everything
else is logged at error and returns the caller's fallback. Requiring BOTH fields is
deliberate: it keeps arbitrary library errors that merely carry a .statusCode (e.g. an
AWS SDK 403) from being surfaced with a status molecule never chose.
function respondError(res: MoleculeResponse, error: unknown, fallback: ErrorFallback): void
res— The response object.error— The caught error.fallback— Status/message/errorKey to use whenerrorisn't a tagged error.
getUserId(res)
The signed-in user's id, from the session the auth middleware puts on res.locals.session.
function getUserId(res: MoleculeResponse): string | null
res— The response of the request.
Returns: The user id, or null when the request has no session (the auth middleware did not run, or the request is anonymous).
requireUser(res)
Like getUserId, but answers 401 when the request has no session. It is for a route
function that answers through res itself (a package's own Express routes.ts); a handler
wrapped by createRequestHandler returns its answer instead and reads getUserId. Call
it at the top of the route function and return when it gives null:
const userId = requireUser(res)
if (!userId) return
function requireUser(res: MoleculeResponse): string | null
res— The response of the request.
Returns: The user id, or null after the 401 is sent.
authenticateResourceRoute(_req, res, next)
Route middleware that requires a session carrying an authenticated user ID.
function authenticateResourceRoute(
_req: MoleculeRequest,
res: MoleculeResponse,
next: MoleculeNextFunction,
): void
getParamId(req, name)
One route parameter as a string. Express 5 types a parameter as string | string[] (a
repeated or wildcard segment is an array), so this takes the first value of an array.
function getParamId(req: MoleculeRequest, name?: string): string
req— The request.name— The parameter's name (defaultid).
Returns: The parameter's value.
queryString(value)
Read a single non-empty trimmed string from a query or route param value.
function queryString(value: unknown): string | undefined
value— Raw query or route param value.
Returns: The trimmed string, or undefined when absent or blank.
queryLimit(value, max)
Read a bounded positive integer page size (1..max) from a query param value.
function queryLimit(value: unknown, max?: number): number | undefined
value— Raw query value.max— Upper bound (default100).
Returns: The clamped integer, or undefined when absent or non-numeric.
queryOffset(value)
Read a non-negative integer pagination offset from a query param value.
function queryOffset(value: unknown): number | undefined
value— Raw query value.
Returns: The clamped integer (>= 0), or undefined when absent or non-numeric.
sendError(res, status, key, fallback)
Send a translated JSON error response ({ error, errorKey }).
function sendError(res: MoleculeResponse, status: number, key: string, fallback: string): void
res— Response to write to.status— HTTP status code.key— Translation key and stable error identifier.fallback— English fallback text.
sendUnauthorized(res)
Answer a request that requires a signed-in viewer (401).
function sendUnauthorized(res: MoleculeResponse): void
res— Response to write to.
sendNotFound(res, key, fallback)
Answer a request for a missing or inaccessible resource (404).
function sendNotFound(res: MoleculeResponse, key?: string, fallback?: string): void
res— Response to write to.key— Translation key (default'resource.error.notFound').fallback— Fallback English message (default'Not found.').
sendForbidden(res, key, fallback)
Answer an unauthorized mutation attempt (403).
function sendForbidden(res: MoleculeResponse, key?: string, fallback?: string): void
res— Response to write to.key— Translation key (default'resource.error.forbidden').fallback— Fallback English message (default'Forbidden.').
sendValidationError(res, key, issues)
Format Zod validation issues as a 400 JSON error response.
function sendValidationError(
res: MoleculeResponse,
key: string,
issues: ReadonlyArray<{ path: PropertyKey[]; message: string }>,
): void
res— Response to write to.key— Stable error identifier (e.g.'article.error.validationFailed').issues— Validation issues returned byschema.safeParse.
sendServerError(res, key, message, error, context)
Log an internal error through the bonded logger and send a sanitized 500 JSON error.
function sendServerError(
res: MoleculeResponse,
key: string,
message: string,
error: unknown,
context?: Record<string, unknown>,
): void
res— Response to write to.key— Stable error identifier.message— Safe public fallback message.error— Original failure for structured logging.context— Optional extra log fields.
rateLimitResourceRoute(scope, actor)
Throttles a resource route using the configured rate-limit bond. Buckets are isolated by route scope. Use the framework's client IP for public or credential-check routes and a server-authenticated actor when available.
A deployment without a bonded limiter, or with an unavailable limiter, continues to serve the route and logs why throttling could not be applied.
function rateLimitResourceRoute(
scope: string,
actor?: (res: MoleculeResponse) => string | null,
): MoleculeRequestHandler
scope— Fixed scope for the route, such asshare:resolve.actor— Optional extractor for a server-authenticated actor ID.
Returns: Middleware that responds with 429 for an exhausted bucket.
update(resource)
Creates a handler to update a resource by its id and some props.
Uses the bonded DataStore from @molecule/api-database for database-agnostic operations.
Example usage:
import { update as resourceUpdate } from '@molecule/api-resource'
import type * as types from '../../types.js'
export const update = ({ name, tableName, schema }: types.Resource) => {
const update = resourceUpdate({ name, tableName, schema })
return async (req: MoleculeRequest) => {
const id = req.params.id
const props = req.body
return await update({ id, props })
}
}
function update<
UpdateProps extends Record<string, unknown> = Record<string, unknown>,
UpdatedProps extends Pick<types.Props, `updatedAt`> & UpdateProps = Pick<
types.Props,
`updatedAt`
> &
UpdateProps,
>({
name,
tableName,
schema,
}: types.Resource): ({
id,
props: updateProps,
}: {
id: UpdatedProps['id'] | types.Props['id']
props: UpdateProps
}) => Promise<
| { statusCode: number; body: { error: string; errorKey: string; props?: undefined } }
| { statusCode: number; body: { props: UpdatedProps; error?: undefined; errorKey?: undefined } }
>
resource— The resource descriptor.resource.name— Human-readable resource name used in error messages (e.g.'User').resource.tableName— Database table to update the row in.resource.schema— Zod schema to validate the incoming props against.
Returns: A curried async function that accepts { id, props } and returns a { statusCode, body } response.
Constants
i18nRegistered
Sentinel value confirming that locale translations for the resource package
have been registered (or attempted). Always true after this module loads.
const i18nRegistered: true
querySchema
The resource query Zod schema.
const querySchema: z.ZodObject<
{
limit: z.ZodDefault<z.ZodCoercedNumber<unknown>>
orderBy: z.ZodDefault<z.ZodEnum<{ createdAt: 'createdAt'; updatedAt: 'updatedAt' }>>
orderDirection: z.ZodDefault<z.ZodEnum<{ asc: 'asc'; desc: 'desc' }>>
before: z.ZodOptional<
z.ZodObject<
{ createdAt: z.ZodOptional<z.ZodString>; updatedAt: z.ZodOptional<z.ZodString> },
z.core.$strip
>
>
after: z.ZodOptional<
z.ZodObject<
{ createdAt: z.ZodOptional<z.ZodString>; updatedAt: z.ZodOptional<z.ZodString> },
z.core.$strip
>
>
},
z.core.$strip
>
idParamSchema
The :id route parameter: any non-empty string. Pair it with a params validator
(validateParams(idParamSchema) from @molecule/api-middleware-validation, whose own
idParamSchema requires a UUID).
const idParamSchema: z.ZodObject<{ id: z.ZodString }, z.core.$strip>
DEFAULT_MAX_LIST_LIMIT
Default upper bound for paginated list endpoints.
const DEFAULT_MAX_LIST_LIMIT: 100
Namespaces
schema
Re-exported as a namespace — reach every member below through it (e.g. schema.<name>).
schema.basePropsSchema
Base Zod schema with the three fields every resource row must have:
id (UUID), createdAt (ISO datetime), and updatedAt (ISO datetime).
const basePropsSchema: z.ZodObject<
{ id: z.ZodString; createdAt: z.ZodString; updatedAt: z.ZodString },
z.core.$strip
>
schema.BaseProps
TypeScript type inferred from basePropsSchema, containing id, createdAt, and updatedAt.
type BaseProps = z.infer<typeof basePropsSchema>
schema.responseSchema(propsSchema)
Creates a Zod schema for the standard { statusCode, body } response
returned by resource CRUD operations.
function responseSchema<T extends z.ZodTypeAny>(propsSchema: T): z.ZodTypeAny
propsSchema— The Zod schema for the resource's props (e.g.userPropsSchema).
Returns: A Zod schema that validates { statusCode: number, body: { props? } | T[] }.
schema.resourceSchema(_propsSchema)
Creates a Zod schema that validates a resource descriptor object
({ name, tableName, schema }).
function resourceSchema<T extends z.ZodTypeAny>(_propsSchema: T): z.ZodTypeAny
_propsSchema— The Zod schema for the resource's props (used for type inference only).
Returns: A Zod schema that validates the resource descriptor shape.
schema.validate(schema, data)
Validates data against a Zod schema, throwing an Error with a
human-readable message listing all validation failures if invalid.
function validate<T>(schema: z.ZodSchema<T>, data: unknown): T
schema— The Zod schema to validate against.data— The data to validate.
Returns: The parsed and typed data if validation succeeds.
schema.safeParse(schema, data)
Validates data against a Zod schema without throwing. Returns a discriminated
union: { success: true, data } on success or { success: false, error } on failure.
function safeParse<T>(
schema: z.ZodSchema<T>,
data: unknown,
): { success: true; data: T } | { success: false; error: z.ZodError }
schema— The Zod schema to validate against.data— The data to validate.
Returns: A discriminated union indicating success with parsed data, or failure with a ZodError.
schema.partial(schema)
Creates a schema where all fields are optional, suitable for validating partial update payloads where only changed fields are sent.
function partial<T extends z.ZodRawShape>(
schema: z.ZodObject<T>,
): z.ZodObject<{ [K in keyof T]: z.ZodOptional<T[K]> }>
schema— The Zod object schema to make partial.
Returns: A new Zod schema with all fields marked optional.
schema.pick(schema, keys)
Creates a schema with only the specified fields, useful for create payloads where only a subset of the full props should be provided by the client.
function pick<T extends z.ZodRawShape, K extends keyof T>(
schema: z.ZodObject<T>,
keys: readonly K[],
): z.ZodObject<Pick<T, K>>
schema— The Zod object schema to pick from.keys— The field names to include in the new schema.
Returns: A new Zod schema containing only the picked fields.
types
Re-exported as a namespace — reach every member below through it (e.g. types.<name>).
types.BaseProps
TypeScript type inferred from basePropsSchema, containing id, createdAt, and updatedAt.
type BaseProps = z.infer<typeof basePropsSchema>
types.Props
The resource's properties to be stored in the database.
Used by CRUD operations for type inference.
interface Props {
/**
* Usually a UUID.
*/
id: string
/**
* When the resource was created.
*
* Usually an ISO 8601 timestamp.
*/
createdAt: string
/**
* When the resource was last updated.
*
* Usually an ISO 8601 timestamp.
*/
updatedAt: string
}
types.Resource
An object describing the resource.
The schema can be a Zod schema (z.ZodTypeAny) for new code, or any other schema type for backward compatibility.
interface Resource<T = unknown> {
/**
* The name of the resource.
*
* @example User
*/
name: string
/**
* The database table name for the resource.
*
* @example users
*/
tableName: string
/**
* The schema used when validating props.
*
* Can be a Zod schema (z.ZodTypeAny) or other schema formats.
*/
schema: T
}
types.ZodResource
An object describing the resource with a Zod schema.
Use this type when you want to enforce Zod schema usage.
interface ZodResource<T extends z.ZodTypeAny = z.ZodTypeAny> {
name: string
tableName: string
schema: T
}
types.Response
Standard response from resource operations.
interface Response<T = unknown> {
/**
* HTTP status code to return.
*/
statusCode: number
/**
* The response payload.
*/
body:
| {
props?: T
error?: string
errorKey?: string
}
| T[]
}
utilities
Re-exported as a namespace — reach every member below through it (e.g. utilities.<name>).
utilities.ZodSchema (deprecated)
Deprecated: Use
z.ZodType
type ZodSchema<
Output = unknown,
Input = unknown,
Internals extends core.$ZodTypeInternals<Output, Input> = core.$ZodTypeInternals<Output, Input>,
> = ZodType<Output, Input, Internals>
utilities.ZodError
An Error-like class used to store Zod validation issues.
interface ZodError<T = unknown> extends $ZodError<T> {
/** @deprecated Use the `z.treeifyError(err)` function instead. */
format(): core.$ZodFormattedError<T>
format<U>(mapper: (issue: core.$ZodIssue) => U): core.$ZodFormattedError<T, U>
/** @deprecated Use the `z.treeifyError(err)` function instead. */
flatten(): core.$ZodFlattenedError<T>
flatten<U>(mapper: (issue: core.$ZodIssue) => U): core.$ZodFlattenedError<T, U>
/** @deprecated Push directly to `.issues` instead. */
addIssue(issue: core.$ZodIssue): void
/** @deprecated Push directly to `.issues` instead. */
addIssues(issues: core.$ZodIssue[]): void
/** @deprecated Check `err.issues.length === 0` instead. */
isEmpty: boolean
}
utilities.getValidProps(options)
Validates resource props against a Zod schema, returning typed valid props or throwing a descriptive error with field-level messages prefixed by the resource name.
function getValidProps<Props>({
name,
schema,
props,
}: {
/**
* The name of the resource (used in error messages).
*/
name: string
/**
* The Zod schema used when validating props.
*/
schema: ZodSchema<Props>
/**
* The `props` to be validated.
*/
props: unknown
}): Props
options— Validation options.options.name— The resource name (used in error messages, e.g.,"User.email: Required").options.schema— The Zod schema to validate against.options.props— The raw props to validate.
Returns: The validated and typed props.
utilities.safeParse(schema, data)
Validates data against a Zod schema without throwing, returning a discriminated union.
function safeParse<T>(
schema: ZodSchema<T>,
data: unknown,
): { success: true; data: T } | { success: false; error: ZodError }
schema— The Zod schema to validate against.data— The raw data to validate.
Returns: { success: true, data } on success, or { success: false, error } with a ZodError.
utilities.validate(schema, data)
Validates data against a Zod schema, throwing a ZodError on failure.
function validate<T>(schema: ZodSchema<T>, data: unknown): T
schema— The Zod schema to validate against.data— The raw data to validate.
Returns: The validated and typed data.
utilities.isEmail(string, strict)
Basic regex for determining whether or not a string is an email address.
function isEmail(string: string, strict?: boolean): boolean
string— The string.strict— Whether strict.
Returns: Whether email.
utilities.isUuid(uuid)
Basic regex for determining whether or not a string is a UUID.
function isUuid(uuid: string): boolean
uuid— The uuid.
Returns: Whether uuid.
Injection Notes
Requirements
Peer dependencies:
@molecule/api-bond^1.0.1@molecule/api-database^1.0.1@molecule/api-i18n^1.0.1@molecule/api-locales-resource^1.0.1@molecule/api-utilities-validation^1.0.1
Runtime Dependencies
@molecule/api-bond@molecule/api-database@molecule/api-i18n@molecule/api-locales-resource@molecule/api-utilities-validationzod
Working WITH installed @molecule/api-resource-* packages (auth getUserId,
table ownership, migrations) — what generated code most often breaks:
- Auth inside a handler is
const userId = getUserId(res), imported from@molecule/api-bonds-default-express— NOT from this package (this package'sutilitiesnamespace holds only validation helpers). Return 401 when it is missing. There is NOrequireAuthmiddleware or file to import or create; auth is applied at the router level. A route's authorizer may also stash the already-loaded row on the response context and hand it toread({ id, props })to skip a second database fetch. - A selected
@molecule/api-resource-*package OWNS its table AND its already-wired routes with a FIXED schema (e.g.api-resource-projectownsprojectskeyed byuser_idwith a live/projectshandler). Do NOT write a migration that re-creates such a table with a different schema — yourCREATE TABLEshadows theirs (or vice-versa) and the package's still-wired handler then 500s (column "user_id" does not exist). To model a different shape, use a DIFFERENT table name (e.g.workspace_projects) + your own handler. Checkapi/migrations/*+api/__setup__/for tables packages already provide. - Adding a resource package AFTER scaffold? Copy its migration. Such a
package ships its table DDL at
node_modules/@molecule/<pkg>/src/__setup__/*.sql— copy it intoapi/migrations/<next-number>_<name>.sql. Wiring its routes/handler alone does NOT create the table: every request 500s with "relation does not exist" even though the build type-checks clean. (Only packages present at scaffold time get their migration auto-created.) - Zod/validation helpers are NAMESPACED exports, not top-level:
schema.basePropsSchema/schema.validate/schema.safeParse/schema.partial/schema.pick, andutilities.getValidProps(re-exported from@molecule/api-utilities-validation). Import the namespace:import { schema, utilities } from '@molecule/api-resource'.
Translations
Translation strings are provided by @molecule/api-locales-resource.