Browse documentation

@motherbase/contracts is a small TypeScript package with one source file: packages/contracts/src/index.ts. It owns route constants, path builders, payload interfaces, realtime protocol types, and lightweight runtime guards. It has no runtime dependency.

HTTP routes

HTTP_ROUTES is an as const map:

KeyValueMain consumers
auth/api/authShared base constant
authSignIn/api/auth/sign-in/emailpackages/client/src/auth.ts
authSession/api/auth/get-sessionpackages/client/src/auth.ts
authSignOut/api/auth/sign-outpackages/client/src/auth.ts
authSignUp/api/auth/sign-up/emailpackages/client/src/auth.ts
authRequestPasswordReset/api/auth/email-otp/request-password-resetpackages/client/src/auth.ts
authResetPassword/api/auth/email-otp/reset-passwordpackages/client/src/auth.ts
authSendOtp/api/auth/email-otp/send-verification-otppackages/client/src/auth.ts
authSignInEmailOtp/api/auth/sign-in/email-otppackages/client/src/auth.ts
authSignInSocial/api/auth/sign-in/socialpackages/client/src/auth.ts
authUpdateUser/api/auth/update-userpackages/client/src/auth.ts
authChangePassword/api/auth/change-passwordpackages/client/src/auth.ts
authDeleteUser/api/auth/delete-userpackages/client/src/auth.ts
files/api/filespackages/client/src/storage.ts and apps/server/controllers/file.controller.ts
rest/rest/v1REST path construction
edge/api/edgeEdge-function path construction
realtimeBroadcast/realtime/v1/api/broadcastapps/server/controllers/realtime-broadcast.controller.ts

The server also imports the route map in apps/server/auth/routes.ts, apps/server/controllers/edge-invoke.controller.ts, and apps/server/controllers/rest-api.controller.ts so route ownership does not drift from the client.

Path builders

contractPath derives encoded paths from the constants:

contractPath.edge(name)
contractPath.file(id)
contractPath.file(id, 'raw')
contractPath.file(id, 'url')
contractPath.rest(model)
contractPath.realtime(model, 'created')
BuilderShapeConsumers
edge(name)/api/edge/:encodedNamepackages/client/src/functions.ts
file(id, action?)/api/files/:encodedId with optional /raw or /urlpackages/client/src/storage.ts
rest(model)/rest/v1/:encodedModelpackages/client/src/index.ts, packages/client/src/postgrest.ts, and packages/client/src/realtime.ts
realtime(model, mutation)lowercaseModel:mutationpackages/client/src/realtime.ts and apps/server/services/realtime/realtime-broadcast.service.ts

RealtimeMutation is the union 'created' | 'updated' | 'deleted' used by contractPath.realtime.

HTTP and storage payload contracts

ExportShapeConsumers
AuthUserContractRequired id and email plus arbitrary fieldsBase type for AuthSessionContract; no external source import
AuthSessionContracttoken and userPublic package contract; no external source import in this revision
FileContractRequired id plus arbitrary fieldsGeneric constraint for uploads
StorageUploadContract<TFile>{ file: TFile, url: string }apps/server/controllers/file.controller.ts and packages/client/src/types.ts
StorageRemoveContract{ deleted: true }apps/server/controllers/file.controller.ts, apps/server/services/storage/storage.service.ts, and packages/client/src/types.ts
ApiEnvelopeContract<T>status is 0 or 1, message, optional dataPublic envelope contract; the client keeps its parallel ApiEnvelope in packages/client/src/types.ts

StorageUploadContract defaults its generic to FileContract. The client specializes it with FileObject and re-exports the alias as StorageUploadResponse.

Runtime guards

isApiEnvelope(value) returns true only for a non-null object whose status is 0 or 1 and whose message is a string.

isStorageUpload(value) returns true only for an object with file.id and url strings.

The SDK/server contract integration test consumes both guards in apps/server/tests/sdk-server-contract.integration.test.ts. They are also available to external packages validating unknown JSON.

Legacy table realtime

REALTIME_SOCKET_EVENTS defines:

{
  subscribe: 'realtime:subscribe',
  unsubscribe: 'realtime:unsubscribe',
}

normalizeRealtimeModel(model) trims, lowercases, and accepts only a PostgreSQL-style identifier beginning with a letter or underscore and containing at most 63 letters, digits, or underscores. Invalid names return null.

The client uses both exports in packages/client/src/realtime.ts. The server uses them in apps/server/services/socket.service.ts and validates models again in apps/server/services/realtime/realtime-postgres.service.ts.

Topic realtime protocol

REALTIME_TOPIC_EVENTS defines join: 'rt:join', leave: 'rt:leave', and message: 'rt:msg'.

ExportShapeConsumers
RealtimeTopicMessageTypebroadcast, presence, postgres_changes, or systempackages/client/src/realtime.ts
RealtimePostgresChangeConfigEvent, optional schema, table, optional filterpackages/client/src/channel.ts and apps/server/services/realtime/realtime-postgres.service.ts
RealtimeJoinConfigOptional broadcast, presence, and Postgres-change configurationpackages/client/src/channel.ts, packages/client/src/realtime.ts, and apps/server/services/realtime/realtime-topic.service.ts
RealtimeJoinAckstatus is ok or error, optional subscription ids and errorClient realtime module and server topic service
RealtimeTopicEnvelopeTopic, message type, event, and unknown payloadpackages/client/src/channel.ts and packages/client/src/realtime.ts

Server-side event consumers include apps/server/services/realtime/realtime-topic.service.ts, apps/server/services/realtime/realtime-presence.service.ts, apps/server/services/realtime/realtime-postgres.service.ts, and apps/server/services/socket.service.ts.

isValidRealtimeTopic(topic) accepts strings of 1–255 printable ASCII characters and rejects control characters and newlines. realtimeTopicRoom(topic) prefixes a validated topic with rt: for its Socket.IO room. Both are used by the server topic/socket services; the broadcast controller also validates topics in apps/server/controllers/realtime-broadcast.controller.ts.

Note

RealtimeTopicEnvelope.payload is deliberately unknown. The client narrows it according to type before routing it to broadcast, presence, or Postgres-change callbacks.

  • Realtime — channels, broadcast and presence built on these contracts.
  • REST engine — the server side of the shared route paths.