SDK API-Referenz
Installation
bash
npm install @prilog/sdkPrilogModuleContext
Das zentrale Kontext-Objekt das jedes Modul in register() erhält.
typescript
interface PrilogModuleContext {
tenantId: string;
auth: AuthContext;
db: ScopedDb;
cache: ModuleCache;
events: ModuleEventBus;
queue: ModuleQueue;
billing: ModuleBilling;
featureFlags: ModuleFeatureFlags;
routes: ModuleRouteRegistrar;
}ctx.cache
Tenant-isolierter Redis-Cache. Keys werden automatisch mit prilog:mod:{moduleId}:{tenantId}: prefixed.
typescript
await ctx.cache.set('feed', data, { ttl: 300 }); // 5 Min TTL
const feed = await ctx.cache.get('feed');
await ctx.cache.del('feed');ctx.events
typescript
// Core-Event abonnieren
ctx.events.on('space.created', async (event) => {
const { spaceId, tenantId, name } = event.payload;
// ...
});
// Eigenes Event publizieren (wird automatisch mit moduleId prefixed)
await ctx.events.publish('task.created', { taskId: '123' });
// → Event-Typ wird: "prilog-mein-modul.task.created"ctx.billing
typescript
// Subscription: Einmal pro Monat aufrufen
await ctx.billing.reportActive();
// Pay-per-Use: Bei jedem Nutzungsereignis
await ctx.billing.reportUsage({ type: 'sms', quantity: 1 });
// Aktuelle Nutzung abfragen
const usage = await ctx.billing.getUsage();
// [{ eventType: 'sms', quantity: 42 }]ctx.queue
typescript
// Async Job einreihen (für Operationen > 100ms)
await ctx.queue.add('caldav-sync', { calendarId: '...' });INFO
Die Queue ist aktuell ein Stub (inline-Ausführung). Bull-Integration wird hinzugefügt wenn ein Modul echte async Jobs braucht.
Core-Events
typescript
import { CORE_EVENTS } from '@prilog/sdk';| Konstante | Event-Typ | Payload |
|---|---|---|
CORE_EVENTS.USER_CREATED | user.created | { userId, tenantId, role? } |
CORE_EVENTS.USER_JOINED | user.joined | { userId, spaceId, tenantId } |
CORE_EVENTS.USER_LEFT | user.left | { userId, spaceId, tenantId } |
CORE_EVENTS.SPACE_CREATED | space.created | { spaceId, tenantId, name } |
CORE_EVENTS.SPACE_DELETED | space.deleted | { spaceId, tenantId } |
CORE_EVENTS.ROOM_CREATED | room.created | { roomId, spaceId, tenantId } |
CORE_EVENTS.MODULE_ACTIVATED | module.activated | { moduleId, tenantId } |
CORE_EVENTS.MODULE_DEACTIVATED | module.deactivated | { moduleId, tenantId } |
CORE_EVENTS.BILLING_PERIOD_END | billing.period.end | { tenantId, periodEnd } |
Berechtigungen
typescript
import { ALL_PERMISSIONS } from '@prilog/sdk';| Berechtigung | Typ | Beschreibung |
|---|---|---|
users:read | Lesend | Nutzerliste lesen (ohne Passwörter) |
spaces:read | Lesend | Space-Struktur lesen |
spaces:write | Schreibend | Spaces erstellen und bearbeiten |
messages:read | Lesend | Chat-Nachrichten lesen |
events:publish | Schreibend | Events auf dem Event-Bus veröffentlichen |
events:subscribe | Lesend | Auf Core-Events hören |
files:read | Lesend | Dateien lesen |
files:write | Schreibend | Dateien schreiben |
billing:report | System | Nutzungsereignisse melden |
notifications:send | Schreibend | Benachrichtigungen senden |
external:http | Extern | HTTP an externe URLs (Whitelist!) |
Hilfsfunktionen
defineModule()
Type-safe Modul-Definition:
typescript
import { defineModule } from '@prilog/sdk';
export default defineModule({
async register(ctx) { /* ... */ },
async unregister(ctx) { /* ... */ },
async cleanup(tenantId) { /* ... */ },
});validateManifest()
Programmatische Manifest-Validierung:
typescript
import { validateManifest } from '@prilog/sdk';
const result = validateManifest(manifest);
// { valid: boolean, errors: string[], warnings: string[] }