@momentumcms/server-core 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/LICENSE +21 -0
- package/README.md +11 -0
- package/index.cjs +5254 -0
- package/package.json +40 -0
- package/src/index.d.ts +21 -0
- package/src/lib/api-keys.d.ts +105 -0
- package/src/lib/collection-access.d.ts +54 -0
- package/src/lib/field-access.d.ts +28 -0
- package/src/lib/field-hooks.d.ts +21 -0
- package/src/lib/graphql-handler.d.ts +25 -0
- package/src/lib/graphql-scalars.d.ts +6 -0
- package/src/lib/graphql-schema.d.ts +16 -0
- package/src/lib/import-export.d.ts +75 -0
- package/src/lib/momentum-api.d.ts +51 -0
- package/src/lib/momentum-api.types.d.ts +368 -0
- package/src/lib/openapi-generator.d.ts +45 -0
- package/src/lib/preview-renderer.d.ts +27 -0
- package/src/lib/publish-scheduler.d.ts +35 -0
- package/src/lib/rate-limiter.d.ts +20 -0
- package/src/lib/relationship-populator.d.ts +27 -0
- package/src/lib/seeding/index.d.ts +7 -0
- package/src/lib/seeding/seed-executor.d.ts +92 -0
- package/src/lib/seeding/seed-tracker.d.ts +72 -0
- package/src/lib/server-core.d.ts +71 -0
- package/src/lib/shared-server-utils.d.ts +20 -0
- package/src/lib/swagger-ui-html.d.ts +5 -0
- package/src/lib/upload-handler.d.ts +72 -0
- package/src/lib/version-operations.d.ts +33 -0
- package/src/lib/webhooks.d.ts +28 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { DatabaseAdapter, MomentumConfig } from '@momentumcms/core';
|
|
2
|
+
import type { MomentumAPIContext } from './momentum-api.types';
|
|
3
|
+
export type { DatabaseAdapter, MomentumConfig, ResolvedMomentumConfig } from '@momentumcms/core';
|
|
4
|
+
/**
|
|
5
|
+
* Query options for database operations.
|
|
6
|
+
*/
|
|
7
|
+
export interface QueryOptions {
|
|
8
|
+
limit?: number;
|
|
9
|
+
page?: number;
|
|
10
|
+
sort?: string;
|
|
11
|
+
where?: Record<string, unknown>;
|
|
12
|
+
[key: string]: unknown;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Momentum request object - framework-agnostic.
|
|
16
|
+
*/
|
|
17
|
+
export interface MomentumRequest {
|
|
18
|
+
method: 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
|
|
19
|
+
collectionSlug: string;
|
|
20
|
+
id?: string;
|
|
21
|
+
body?: Record<string, unknown>;
|
|
22
|
+
query?: QueryOptions;
|
|
23
|
+
/** User context for access control */
|
|
24
|
+
user?: MomentumAPIContext['user'];
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Validation error structure.
|
|
28
|
+
*/
|
|
29
|
+
export interface ValidationError {
|
|
30
|
+
field: string;
|
|
31
|
+
message: string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Momentum response object - framework-agnostic.
|
|
35
|
+
*/
|
|
36
|
+
export interface MomentumResponse {
|
|
37
|
+
status?: number;
|
|
38
|
+
docs?: Record<string, unknown>[];
|
|
39
|
+
doc?: Record<string, unknown>;
|
|
40
|
+
totalDocs?: number;
|
|
41
|
+
deleted?: boolean;
|
|
42
|
+
id?: string;
|
|
43
|
+
error?: string;
|
|
44
|
+
errors?: ValidationError[];
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Momentum handlers interface.
|
|
48
|
+
*/
|
|
49
|
+
export interface MomentumHandlers {
|
|
50
|
+
handleFind(request: MomentumRequest): Promise<MomentumResponse>;
|
|
51
|
+
handleFindById(request: MomentumRequest): Promise<MomentumResponse>;
|
|
52
|
+
handleCreate(request: MomentumRequest): Promise<MomentumResponse>;
|
|
53
|
+
handleUpdate(request: MomentumRequest): Promise<MomentumResponse>;
|
|
54
|
+
handleDelete(request: MomentumRequest): Promise<MomentumResponse>;
|
|
55
|
+
handleForceDelete(request: MomentumRequest): Promise<MomentumResponse>;
|
|
56
|
+
handleRestore(request: MomentumRequest): Promise<MomentumResponse>;
|
|
57
|
+
handleSearch(request: MomentumRequest): Promise<MomentumResponse>;
|
|
58
|
+
routeRequest(request: MomentumRequest): Promise<MomentumResponse>;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Creates Momentum CMS handlers.
|
|
62
|
+
* Framework-agnostic - can be used with Express, h3, Fastify, etc.
|
|
63
|
+
*
|
|
64
|
+
* Now delegates to the MomentumAPI singleton for consistent behavior
|
|
65
|
+
* between HTTP requests and direct API calls.
|
|
66
|
+
*/
|
|
67
|
+
export declare function createMomentumHandlers(config: MomentumConfig): MomentumHandlers;
|
|
68
|
+
/**
|
|
69
|
+
* In-memory database adapter for development/testing.
|
|
70
|
+
*/
|
|
71
|
+
export declare function createInMemoryAdapter(): DatabaseAdapter;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared server utilities used by both Express and Analog server adapters.
|
|
3
|
+
* Extracted to avoid duplication between server-express and server-analog.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Sanitize error messages to prevent leaking internal details (SQL, file paths, etc.).
|
|
7
|
+
* Returns the original message if it appears safe, otherwise returns the fallback.
|
|
8
|
+
*/
|
|
9
|
+
export declare function sanitizeErrorMessage(error: unknown, fallback: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* Sanitize a filename for use in Content-Disposition headers.
|
|
12
|
+
* Strips any characters that could enable header injection.
|
|
13
|
+
*/
|
|
14
|
+
export declare function sanitizeFilename(name: string): string;
|
|
15
|
+
/**
|
|
16
|
+
* Parses the `where` query parameter.
|
|
17
|
+
* Handles both JSON string format (?where={"slug":{"equals":"home"}})
|
|
18
|
+
* and pre-parsed object format from h3/qs/Express.
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseWhereParam(raw: unknown): Record<string, unknown> | undefined;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Upload Handler for Momentum CMS
|
|
3
|
+
* Framework-agnostic file upload handling
|
|
4
|
+
*/
|
|
5
|
+
import type { StorageAdapter, UploadedFile, MomentumConfig, MediaDocument } from '@momentumcms/core';
|
|
6
|
+
import { type MomentumAPIContext } from './momentum-api';
|
|
7
|
+
/**
|
|
8
|
+
* Upload request from the client.
|
|
9
|
+
*/
|
|
10
|
+
export interface UploadRequest {
|
|
11
|
+
/** Uploaded file data */
|
|
12
|
+
file: UploadedFile;
|
|
13
|
+
/** User context for access control */
|
|
14
|
+
user?: MomentumAPIContext['user'];
|
|
15
|
+
/** Alt text for the file (for images) */
|
|
16
|
+
alt?: string;
|
|
17
|
+
/** Target collection (defaults to 'media') */
|
|
18
|
+
collection?: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Upload response returned to the client.
|
|
22
|
+
*/
|
|
23
|
+
export interface UploadResponse {
|
|
24
|
+
/** Created media document */
|
|
25
|
+
doc?: MediaDocument;
|
|
26
|
+
/** Error message if upload failed */
|
|
27
|
+
error?: string;
|
|
28
|
+
/** HTTP status code */
|
|
29
|
+
status: number;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Upload configuration from MomentumConfig.
|
|
33
|
+
*/
|
|
34
|
+
export interface UploadConfig {
|
|
35
|
+
/** Storage adapter for file storage */
|
|
36
|
+
adapter: StorageAdapter;
|
|
37
|
+
/** Maximum file size in bytes */
|
|
38
|
+
maxFileSize?: number;
|
|
39
|
+
/** Allowed MIME types */
|
|
40
|
+
allowedMimeTypes?: string[];
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get upload configuration from MomentumConfig.
|
|
44
|
+
*/
|
|
45
|
+
export declare function getUploadConfig(config: MomentumConfig): UploadConfig | null;
|
|
46
|
+
/**
|
|
47
|
+
* Handle file upload.
|
|
48
|
+
*
|
|
49
|
+
* @param config - Upload configuration
|
|
50
|
+
* @param request - Upload request with file and user context
|
|
51
|
+
* @returns Upload response with created media document or error
|
|
52
|
+
*/
|
|
53
|
+
export declare function handleUpload(config: UploadConfig, request: UploadRequest): Promise<UploadResponse>;
|
|
54
|
+
/**
|
|
55
|
+
* Handle file deletion.
|
|
56
|
+
*
|
|
57
|
+
* @param adapter - Storage adapter
|
|
58
|
+
* @param path - Storage path of the file to delete
|
|
59
|
+
* @returns True if deleted, false if not found
|
|
60
|
+
*/
|
|
61
|
+
export declare function handleFileDelete(adapter: StorageAdapter, path: string): Promise<boolean>;
|
|
62
|
+
/**
|
|
63
|
+
* Handle file retrieval for serving.
|
|
64
|
+
*
|
|
65
|
+
* @param adapter - Storage adapter
|
|
66
|
+
* @param path - Storage path of the file
|
|
67
|
+
* @returns File buffer and metadata, or null if not found
|
|
68
|
+
*/
|
|
69
|
+
export declare function handleFileGet(adapter: StorageAdapter, path: string): Promise<{
|
|
70
|
+
buffer: Buffer;
|
|
71
|
+
mimeType?: string;
|
|
72
|
+
} | null>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Version Operations Implementation
|
|
3
|
+
*
|
|
4
|
+
* Provides version management operations for versioned collections.
|
|
5
|
+
*/
|
|
6
|
+
import type { DatabaseAdapter, CollectionConfig, DocumentStatus, DocumentVersionParsed, VersionQueryResult, RestoreVersionOptions, PublishOptions, SchedulePublishResult } from '@momentumcms/core';
|
|
7
|
+
import type { MomentumAPIContext, VersionOperations, VersionFindOptions } from './momentum-api.types';
|
|
8
|
+
/**
|
|
9
|
+
* Implementation of version operations for a collection.
|
|
10
|
+
*/
|
|
11
|
+
export declare class VersionOperationsImpl<T = Record<string, unknown>> implements VersionOperations<T> {
|
|
12
|
+
private readonly slug;
|
|
13
|
+
private readonly collectionConfig;
|
|
14
|
+
private readonly adapter;
|
|
15
|
+
private readonly context;
|
|
16
|
+
constructor(slug: string, collectionConfig: CollectionConfig, adapter: DatabaseAdapter, context: MomentumAPIContext);
|
|
17
|
+
findVersions(parentId: string, options?: VersionFindOptions): Promise<VersionQueryResult<T>>;
|
|
18
|
+
findVersionById(versionId: string): Promise<DocumentVersionParsed<T> | null>;
|
|
19
|
+
restore(options: RestoreVersionOptions): Promise<T>;
|
|
20
|
+
publish(docId: string, _options?: PublishOptions): Promise<T>;
|
|
21
|
+
unpublish(docId: string): Promise<T>;
|
|
22
|
+
saveDraft(docId: string, data: Partial<T>): Promise<DocumentVersionParsed<T>>;
|
|
23
|
+
getStatus(docId: string): Promise<DocumentStatus>;
|
|
24
|
+
compare(versionId1: string, versionId2: string): Promise<{
|
|
25
|
+
field: string;
|
|
26
|
+
oldValue: unknown;
|
|
27
|
+
newValue: unknown;
|
|
28
|
+
}[]>;
|
|
29
|
+
schedulePublish(docId: string, publishAt: string): Promise<SchedulePublishResult>;
|
|
30
|
+
cancelScheduledPublish(docId: string): Promise<void>;
|
|
31
|
+
private checkAccess;
|
|
32
|
+
private buildRequestContext;
|
|
33
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Webhook Dispatcher
|
|
3
|
+
*
|
|
4
|
+
* Dispatches webhook POST requests when collection events occur.
|
|
5
|
+
* Supports HMAC-SHA256 signature verification and configurable retries.
|
|
6
|
+
*/
|
|
7
|
+
import type { CollectionConfig, WebhookConfig, WebhookEvent, WebhookPayload } from '@momentumcms/core';
|
|
8
|
+
/**
|
|
9
|
+
* Sign a webhook payload with HMAC-SHA256.
|
|
10
|
+
* Returns the hex-encoded signature.
|
|
11
|
+
*/
|
|
12
|
+
declare function signPayload(payload: string, secret: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Send a single webhook request with retry support.
|
|
15
|
+
* Runs in the background (fire-and-forget).
|
|
16
|
+
*/
|
|
17
|
+
declare function sendWebhook(webhook: WebhookConfig, payload: WebhookPayload, attempt?: number): Promise<void>;
|
|
18
|
+
/**
|
|
19
|
+
* Dispatch webhooks for a collection event.
|
|
20
|
+
* Runs all matching webhooks in parallel, fire-and-forget.
|
|
21
|
+
*/
|
|
22
|
+
declare function dispatchWebhooks(webhooks: WebhookConfig[], event: WebhookEvent, payload: WebhookPayload): void;
|
|
23
|
+
/**
|
|
24
|
+
* Register webhook hooks for all collections that have webhook configs.
|
|
25
|
+
* Call this during server initialization, before initializeMomentumAPI().
|
|
26
|
+
*/
|
|
27
|
+
export declare function registerWebhookHooks(collections: CollectionConfig[]): void;
|
|
28
|
+
export { sendWebhook, dispatchWebhooks, signPayload };
|