@almadar/server 1.0.0
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/LICENSE +72 -0
- package/dist/index--XhXIuTh.d.ts +120 -0
- package/dist/index-D8fohXsO.d.ts +178 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +1015 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/index.d.ts +4 -0
- package/dist/lib/index.js +279 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/middleware/index.d.ts +71 -0
- package/dist/middleware/index.js +308 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/services/index.d.ts +2 -0
- package/dist/services/index.js +632 -0
- package/dist/services/index.js.map +1 -0
- package/dist/utils/index.d.ts +78 -0
- package/dist/utils/index.js +106 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +84 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
Business Source License 1.1
|
|
2
|
+
|
|
3
|
+
Parameters
|
|
4
|
+
|
|
5
|
+
Licensor: Almadar FZE
|
|
6
|
+
Licensed Work: KFlow Builder / Almadar
|
|
7
|
+
The Licensed Work is (c) 2025-2026 Almadar FZE.
|
|
8
|
+
Additional Use Grant: You may make production use of the Licensed Work for
|
|
9
|
+
non-commercial purposes and for internal evaluation.
|
|
10
|
+
Production use for commercial purposes requires a
|
|
11
|
+
commercial license from the Licensor.
|
|
12
|
+
Change Date: 2030-02-01
|
|
13
|
+
Change License: Apache License, Version 2.0
|
|
14
|
+
|
|
15
|
+
Terms
|
|
16
|
+
|
|
17
|
+
The Licensor hereby grants you the right to copy, modify, create derivative
|
|
18
|
+
works, redistribute, and make non-production use of the Licensed Work. The
|
|
19
|
+
Licensor may make an Additional Use Grant, above, permitting limited
|
|
20
|
+
production use.
|
|
21
|
+
|
|
22
|
+
Effective on the Change Date, or the fourth anniversary of the first publicly
|
|
23
|
+
available distribution of a specific version of the Licensed Work under this
|
|
24
|
+
License, whichever comes first, the Licensor hereby grants you rights under
|
|
25
|
+
the terms of the Change License, and the rights granted in the paragraph
|
|
26
|
+
above terminate.
|
|
27
|
+
|
|
28
|
+
If your use of the Licensed Work does not comply with the requirements
|
|
29
|
+
currently in effect as described in this License, you must purchase a
|
|
30
|
+
commercial license from the Licensor, its affiliated entities, or authorized
|
|
31
|
+
resellers, or you must refrain from using the Licensed Work.
|
|
32
|
+
|
|
33
|
+
All copies of the original and modified Licensed Work, and derivative works
|
|
34
|
+
of the Licensed Work, are subject to this License. This License applies
|
|
35
|
+
separately for each version of the Licensed Work and the Change Date may vary
|
|
36
|
+
for each version of the Licensed Work released by Licensor.
|
|
37
|
+
|
|
38
|
+
You must conspicuously display this License on each original or modified copy
|
|
39
|
+
of the Licensed Work. If you receive the Licensed Work in original or
|
|
40
|
+
modified form from a third party, the terms and conditions set forth in this
|
|
41
|
+
License apply to your use of that work.
|
|
42
|
+
|
|
43
|
+
Any use of the Licensed Work in violation of this License will automatically
|
|
44
|
+
terminate your rights under this License for the current and all other
|
|
45
|
+
versions of the Licensed Work.
|
|
46
|
+
|
|
47
|
+
This License does not grant you any right in any trademark or logo of
|
|
48
|
+
Licensor or its affiliates (provided that you may use a trademark or logo of
|
|
49
|
+
Licensor as expressly required by this License).
|
|
50
|
+
|
|
51
|
+
TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
|
|
52
|
+
AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
|
|
53
|
+
EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
|
|
54
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
|
|
55
|
+
TITLE.
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved.
|
|
60
|
+
"Business Source License" is a trademark of MariaDB Corporation Ab.
|
|
61
|
+
|
|
62
|
+
ADDITIONAL TERMS:
|
|
63
|
+
|
|
64
|
+
Documentation (builder/packages/website/docs/) is licensed under CC BY 4.0.
|
|
65
|
+
|
|
66
|
+
TRADEMARKS:
|
|
67
|
+
|
|
68
|
+
"Orbital", "KFlow", "Almadar", and the Almadar logo are trademarks of
|
|
69
|
+
Almadar FZE. You may not use these trademarks without prior written
|
|
70
|
+
permission from Almadar FZE.
|
|
71
|
+
|
|
72
|
+
For licensing inquiries: licensing@almadar.io
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import admin from 'firebase-admin';
|
|
2
|
+
import { WebSocketServer } from 'ws';
|
|
3
|
+
import { Server } from 'http';
|
|
4
|
+
|
|
5
|
+
declare const env: {
|
|
6
|
+
NODE_ENV: "development" | "production" | "test";
|
|
7
|
+
PORT: number;
|
|
8
|
+
CORS_ORIGIN: string | string[];
|
|
9
|
+
API_PREFIX: string;
|
|
10
|
+
USE_MOCK_DATA: boolean;
|
|
11
|
+
DATABASE_URL?: string | undefined;
|
|
12
|
+
FIREBASE_PROJECT_ID?: string | undefined;
|
|
13
|
+
FIREBASE_CLIENT_EMAIL?: string | undefined;
|
|
14
|
+
FIREBASE_PRIVATE_KEY?: string | undefined;
|
|
15
|
+
FIREBASE_SERVICE_ACCOUNT_PATH?: string | undefined;
|
|
16
|
+
FIRESTORE_EMULATOR_HOST?: string | undefined;
|
|
17
|
+
FIREBASE_AUTH_EMULATOR_HOST?: string | undefined;
|
|
18
|
+
MOCK_SEED?: number | undefined;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
declare const logger: {
|
|
22
|
+
debug: (message: string, meta?: unknown) => void;
|
|
23
|
+
info: (message: string, meta?: unknown) => void;
|
|
24
|
+
warn: (message: string, meta?: unknown) => void;
|
|
25
|
+
error: (message: string, meta?: unknown) => void;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Server EventBus - Singleton for server-side cross-trait communication
|
|
30
|
+
*
|
|
31
|
+
* This EventBus enables:
|
|
32
|
+
* - Server-side trait event emission after CRUD operations
|
|
33
|
+
* - Server-side trait listeners responding to events
|
|
34
|
+
* - Cross-client event broadcast via WebSocket
|
|
35
|
+
*
|
|
36
|
+
* @packageDocumentation
|
|
37
|
+
*/
|
|
38
|
+
type EventHandler = (payload: unknown, meta?: Record<string, unknown>) => void;
|
|
39
|
+
/**
|
|
40
|
+
* Simple EventBus implementation for server-side events
|
|
41
|
+
*/
|
|
42
|
+
declare class EventBus {
|
|
43
|
+
private handlers;
|
|
44
|
+
private debug;
|
|
45
|
+
constructor(options?: {
|
|
46
|
+
debug?: boolean;
|
|
47
|
+
});
|
|
48
|
+
on(event: string, handler: EventHandler): () => void;
|
|
49
|
+
off(event: string, handler: EventHandler): void;
|
|
50
|
+
emit(event: string, payload?: unknown, meta?: Record<string, unknown>): void;
|
|
51
|
+
clear(): void;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Singleton EventBus instance for server-side event communication.
|
|
55
|
+
*/
|
|
56
|
+
declare const serverEventBus: EventBus;
|
|
57
|
+
/**
|
|
58
|
+
* Type-safe event emission helper
|
|
59
|
+
*/
|
|
60
|
+
declare function emitEntityEvent(entityType: string, action: 'CREATED' | 'UPDATED' | 'DELETED', payload: Record<string, unknown>): void;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Database Configuration
|
|
64
|
+
*
|
|
65
|
+
* Supports both Prisma (SQL) and Firebase Admin (Firestore)
|
|
66
|
+
* Generated apps can use either depending on the database option
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Get Firestore instance
|
|
71
|
+
*/
|
|
72
|
+
declare function getFirestore(): admin.firestore.Firestore;
|
|
73
|
+
/**
|
|
74
|
+
* Get Firebase Auth instance
|
|
75
|
+
*/
|
|
76
|
+
declare function getAuth(): admin.auth.Auth;
|
|
77
|
+
|
|
78
|
+
declare const db: admin.firestore.Firestore;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* WebSocket Event Broadcast - Cross-client event synchronization
|
|
82
|
+
*
|
|
83
|
+
* Broadcasts server-side events to all connected clients via WebSocket.
|
|
84
|
+
* This enables real-time updates across multiple browser clients.
|
|
85
|
+
*
|
|
86
|
+
* @packageDocumentation
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Setup WebSocket server for event broadcasting.
|
|
91
|
+
*
|
|
92
|
+
* Listens to all server events via wildcard and broadcasts to connected clients.
|
|
93
|
+
*
|
|
94
|
+
* @param server - HTTP server to attach WebSocket to
|
|
95
|
+
* @param path - WebSocket endpoint path (default: '/ws/events')
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```typescript
|
|
99
|
+
* import { createServer } from 'http';
|
|
100
|
+
* import { setupEventBroadcast } from '@/lib/websocket';
|
|
101
|
+
*
|
|
102
|
+
* const server = createServer(app);
|
|
103
|
+
* setupEventBroadcast(server);
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
declare function setupEventBroadcast(server: Server, path?: string): WebSocketServer;
|
|
107
|
+
/**
|
|
108
|
+
* Get the WebSocket server instance (for testing or advanced usage)
|
|
109
|
+
*/
|
|
110
|
+
declare function getWebSocketServer(): WebSocketServer | null;
|
|
111
|
+
/**
|
|
112
|
+
* Close the WebSocket server
|
|
113
|
+
*/
|
|
114
|
+
declare function closeWebSocketServer(): Promise<void>;
|
|
115
|
+
/**
|
|
116
|
+
* Get connected client count
|
|
117
|
+
*/
|
|
118
|
+
declare function getConnectedClientCount(): number;
|
|
119
|
+
|
|
120
|
+
export { EventBus as E, env as a, getConnectedClientCount as b, closeWebSocketServer as c, db as d, emitEntityEvent as e, getFirestore as f, getAuth as g, getWebSocketServer as h, setupEventBroadcast as i, logger as l, serverEventBus as s };
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { ParsedFilter } from './utils/index.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* MockDataService - In-memory data store with faker-based mock generation
|
|
5
|
+
*
|
|
6
|
+
* Provides a stateful mock data layer that supports all CRUD operations.
|
|
7
|
+
* Uses @faker-js/faker for realistic data generation based on field types.
|
|
8
|
+
*
|
|
9
|
+
* @packageDocumentation
|
|
10
|
+
*/
|
|
11
|
+
interface FieldSchema {
|
|
12
|
+
name: string;
|
|
13
|
+
type: 'string' | 'number' | 'boolean' | 'date' | 'enum' | 'relation' | 'array';
|
|
14
|
+
required?: boolean;
|
|
15
|
+
enumValues?: string[];
|
|
16
|
+
min?: number;
|
|
17
|
+
max?: number;
|
|
18
|
+
fakerMethod?: string;
|
|
19
|
+
relatedEntity?: string;
|
|
20
|
+
}
|
|
21
|
+
interface EntitySchema {
|
|
22
|
+
fields: FieldSchema[];
|
|
23
|
+
seedCount?: number;
|
|
24
|
+
}
|
|
25
|
+
interface BaseEntity$1 {
|
|
26
|
+
id: string;
|
|
27
|
+
createdAt: Date;
|
|
28
|
+
updatedAt: Date;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* In-memory mock data store with CRUD operations and faker-based seeding.
|
|
32
|
+
*/
|
|
33
|
+
declare class MockDataService {
|
|
34
|
+
private stores;
|
|
35
|
+
private schemas;
|
|
36
|
+
private idCounters;
|
|
37
|
+
constructor();
|
|
38
|
+
/**
|
|
39
|
+
* Initialize store for an entity.
|
|
40
|
+
*/
|
|
41
|
+
private getStore;
|
|
42
|
+
/**
|
|
43
|
+
* Generate next ID for an entity.
|
|
44
|
+
*/
|
|
45
|
+
private nextId;
|
|
46
|
+
/**
|
|
47
|
+
* Register an entity schema.
|
|
48
|
+
*/
|
|
49
|
+
registerSchema(entityName: string, schema: EntitySchema): void;
|
|
50
|
+
/**
|
|
51
|
+
* Seed an entity with mock data.
|
|
52
|
+
*/
|
|
53
|
+
seed(entityName: string, fields: FieldSchema[], count?: number): void;
|
|
54
|
+
/**
|
|
55
|
+
* Generate a single mock item based on field schemas.
|
|
56
|
+
*/
|
|
57
|
+
private generateMockItem;
|
|
58
|
+
/**
|
|
59
|
+
* Generate a mock value for a field based on its schema.
|
|
60
|
+
*/
|
|
61
|
+
private generateFieldValue;
|
|
62
|
+
/**
|
|
63
|
+
* Generate a string value based on field name heuristics.
|
|
64
|
+
* Generic name/title fields use entity-aware format (e.g., "Project Name 1").
|
|
65
|
+
* Specific fields (email, phone, etc.) use faker.
|
|
66
|
+
*/
|
|
67
|
+
private generateStringValue;
|
|
68
|
+
/**
|
|
69
|
+
* Capitalize first letter of a string.
|
|
70
|
+
*/
|
|
71
|
+
private capitalizeFirst;
|
|
72
|
+
/**
|
|
73
|
+
* Generate a date value based on field name heuristics.
|
|
74
|
+
*/
|
|
75
|
+
private generateDateValue;
|
|
76
|
+
/**
|
|
77
|
+
* List all items of an entity.
|
|
78
|
+
*/
|
|
79
|
+
list<T>(entityName: string): T[];
|
|
80
|
+
/**
|
|
81
|
+
* Get a single item by ID.
|
|
82
|
+
*/
|
|
83
|
+
getById<T>(entityName: string, id: string): T | null;
|
|
84
|
+
/**
|
|
85
|
+
* Create a new item.
|
|
86
|
+
*/
|
|
87
|
+
create<T extends BaseEntity$1>(entityName: string, data: Partial<T>): T;
|
|
88
|
+
/**
|
|
89
|
+
* Update an existing item.
|
|
90
|
+
*/
|
|
91
|
+
update<T extends BaseEntity$1>(entityName: string, id: string, data: Partial<T>): T | null;
|
|
92
|
+
/**
|
|
93
|
+
* Delete an item.
|
|
94
|
+
*/
|
|
95
|
+
delete(entityName: string, id: string): boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Clear all data for an entity.
|
|
98
|
+
*/
|
|
99
|
+
clear(entityName: string): void;
|
|
100
|
+
/**
|
|
101
|
+
* Clear all data.
|
|
102
|
+
*/
|
|
103
|
+
clearAll(): void;
|
|
104
|
+
/**
|
|
105
|
+
* Get count of items for an entity.
|
|
106
|
+
*/
|
|
107
|
+
count(entityName: string): number;
|
|
108
|
+
}
|
|
109
|
+
declare const mockDataService: MockDataService;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* DataService - Unified data access abstraction
|
|
113
|
+
*
|
|
114
|
+
* Provides a common interface for data operations that can be backed by
|
|
115
|
+
* either MockDataService (for development) or Firebase (for production).
|
|
116
|
+
*
|
|
117
|
+
* @packageDocumentation
|
|
118
|
+
*/
|
|
119
|
+
|
|
120
|
+
interface BaseEntity {
|
|
121
|
+
id: string;
|
|
122
|
+
createdAt: Date;
|
|
123
|
+
updatedAt: Date;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Pagination options for list queries
|
|
127
|
+
*/
|
|
128
|
+
interface PaginationOptions {
|
|
129
|
+
/** Page number (1-indexed) */
|
|
130
|
+
page?: number;
|
|
131
|
+
/** Number of items per page */
|
|
132
|
+
pageSize?: number;
|
|
133
|
+
/** Search term to filter results */
|
|
134
|
+
search?: string;
|
|
135
|
+
/** Fields to search in (defaults to all string fields) */
|
|
136
|
+
searchFields?: string[];
|
|
137
|
+
/** Sort field */
|
|
138
|
+
sortBy?: string;
|
|
139
|
+
/** Sort direction */
|
|
140
|
+
sortOrder?: 'asc' | 'desc';
|
|
141
|
+
/** Filters parsed from query params */
|
|
142
|
+
filters?: ParsedFilter[];
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Paginated response structure
|
|
146
|
+
*/
|
|
147
|
+
interface PaginatedResult<T> {
|
|
148
|
+
data: T[];
|
|
149
|
+
total: number;
|
|
150
|
+
page: number;
|
|
151
|
+
pageSize: number;
|
|
152
|
+
totalPages: number;
|
|
153
|
+
}
|
|
154
|
+
interface DataService {
|
|
155
|
+
list<T>(collection: string): Promise<T[]>;
|
|
156
|
+
listPaginated<T>(collection: string, options?: PaginationOptions): Promise<PaginatedResult<T>>;
|
|
157
|
+
getById<T>(collection: string, id: string): Promise<T | null>;
|
|
158
|
+
create<T extends BaseEntity>(collection: string, data: Partial<T>): Promise<T>;
|
|
159
|
+
update<T extends BaseEntity>(collection: string, id: string, data: Partial<T>): Promise<T | null>;
|
|
160
|
+
delete(collection: string, id: string): Promise<boolean>;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Singleton data service instance.
|
|
164
|
+
* Use this in handlers for all data operations.
|
|
165
|
+
*/
|
|
166
|
+
declare const dataService: DataService;
|
|
167
|
+
interface EntitySeedConfig {
|
|
168
|
+
name: string;
|
|
169
|
+
fields: FieldSchema[];
|
|
170
|
+
seedCount: number;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Seed mock data for multiple entities.
|
|
174
|
+
* Only works when USE_MOCK_DATA is enabled.
|
|
175
|
+
*/
|
|
176
|
+
declare function seedMockData(entities: EntitySeedConfig[]): void;
|
|
177
|
+
|
|
178
|
+
export { type DataService as D, type EntitySchema as E, type FieldSchema as F, MockDataService as M, type PaginatedResult as P, type EntitySeedConfig as a, type PaginationOptions as b, dataService as d, mockDataService as m, seedMockData as s };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { E as EventBus, c as closeWebSocketServer, d as db, e as emitEntityEvent, a as env, g as getAuth, b as getConnectedClientCount, f as getFirestore, h as getWebSocketServer, l as logger, s as serverEventBus, i as setupEventBroadcast } from './index--XhXIuTh.js';
|
|
2
|
+
export { AppError, ConflictError, ForbiddenError, NotFoundError, UnauthorizedError, ValidationError, asyncHandler, authenticateFirebase, errorHandler, notFoundHandler, validateBody, validateParams, validateQuery } from './middleware/index.js';
|
|
3
|
+
export { D as DataService, E as EntitySchema, a as EntitySeedConfig, F as FieldSchema, M as MockDataService, P as PaginatedResult, b as PaginationOptions, d as dataService, m as mockDataService, s as seedMockData } from './index-D8fohXsO.js';
|
|
4
|
+
export { FirestoreWhereFilterOp, PaginationParams, ParsedFilter, applyFiltersToQuery, extractPaginationParams, parseQueryFilters } from './utils/index.js';
|
|
5
|
+
export { default as admin } from 'firebase-admin';
|
|
6
|
+
import 'ws';
|
|
7
|
+
import 'http';
|
|
8
|
+
import 'express';
|
|
9
|
+
import 'zod';
|