@sylphx/lens-server 1.2.0 → 1.3.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/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- import { ContextValue, EntityDef, EntityDefinition, EntityResolvers, EntityResolversDefinition, MutationDef, QueryDef, RelationDef, RelationTypeWithForeignKey, RouterDef } from "@sylphx/lens-core";
2
- import { EntityKey, Update, EmitCommand, InternalFieldUpdate, ArrayOperation } from "@sylphx/lens-core";
1
+ import { query, mutation, router, QueryBuilder, MutationBuilder, QueryDef as QueryDef2, MutationDef as MutationDef2, RouterDef as RouterDef2, RouterRoutes, ResolverFn, ResolverContext, InferRouterContext as InferRouterContext2 } from "@sylphx/lens-core";
2
+ import { ContextValue, EntityDef, EntityDefinition, EntityResolvers, EntityResolversDefinition, InferRouterContext, MutationDef, QueryDef, RelationDef, RelationTypeWithForeignKey, RouterDef } from "@sylphx/lens-core";
3
+ import { ArrayOperation, EmitCommand, EntityKey, InternalFieldUpdate, Update } from "@sylphx/lens-core";
3
4
  /** Client connection interface */
4
5
  interface StateClient {
5
6
  id: string;
@@ -228,20 +229,23 @@ type OperationsMap = {
228
229
  [key: string]: OperationMeta | OperationsMap;
229
230
  };
230
231
  /** Server configuration */
231
- interface LensServerConfig<TContext extends ContextValue = ContextValue> {
232
+ interface LensServerConfig<
233
+ TContext extends ContextValue = ContextValue,
234
+ TRouter extends RouterDef = RouterDef
235
+ > {
232
236
  /** Entity definitions */
233
237
  entities?: EntitiesMap;
234
238
  /** Relation definitions */
235
239
  relations?: RelationsArray;
236
- /** Router definition (namespaced operations) */
237
- router?: RouterDef;
240
+ /** Router definition (namespaced operations) - context type is inferred */
241
+ router?: TRouter;
238
242
  /** Query definitions (flat, legacy) */
239
243
  queries?: QueriesMap;
240
244
  /** Mutation definitions (flat, legacy) */
241
245
  mutations?: MutationsMap;
242
246
  /** Entity resolvers */
243
247
  resolvers?: EntityResolvers<EntityResolversDefinition>;
244
- /** Context factory */
248
+ /** Context factory - must return the context type expected by the router */
245
249
  context?: (req?: unknown) => TContext | Promise<TContext>;
246
250
  /** Server version */
247
251
  version?: string;
@@ -422,19 +426,78 @@ type InferApi<T extends LensServer> = T extends LensServerImpl<infer Q, infer M>
422
426
  mutations: M;
423
427
  } : never;
424
428
  /**
425
- * Create Lens server with Operations API + Optimization Layer
429
+ * Config helper type that infers context from router
426
430
  */
427
- declare function createServer<
431
+ type ServerConfigWithInferredContext<
432
+ TRouter extends RouterDef,
433
+ Q extends QueriesMap = QueriesMap,
434
+ M extends MutationsMap = MutationsMap
435
+ > = {
436
+ entities?: EntitiesMap;
437
+ relations?: RelationsArray;
438
+ router: TRouter;
439
+ queries?: Q;
440
+ mutations?: M;
441
+ resolvers?: EntityResolvers<EntityResolversDefinition>;
442
+ /** Context factory - type is inferred from router's procedures */
443
+ context?: (req?: unknown) => InferRouterContext<TRouter> | Promise<InferRouterContext<TRouter>>;
444
+ version?: string;
445
+ };
446
+ /**
447
+ * Config without router (legacy flat queries/mutations)
448
+ */
449
+ type ServerConfigLegacy<
428
450
  TContext extends ContextValue = ContextValue,
429
451
  Q extends QueriesMap = QueriesMap,
430
452
  M extends MutationsMap = MutationsMap
431
- >(config: LensServerConfig<TContext> & {
453
+ > = {
454
+ entities?: EntitiesMap;
455
+ relations?: RelationsArray;
456
+ router?: undefined;
432
457
  queries?: Q;
433
458
  mutations?: M;
434
- }): LensServer & {
459
+ resolvers?: EntityResolvers<EntityResolversDefinition>;
460
+ context?: (req?: unknown) => TContext | Promise<TContext>;
461
+ version?: string;
462
+ };
463
+ /**
464
+ * Create Lens server with Operations API + Optimization Layer
465
+ *
466
+ * When using a router with typed context (from initLens), the context
467
+ * function's return type is automatically enforced to match.
468
+ *
469
+ * @example
470
+ * ```typescript
471
+ * // Context type is inferred from router's procedures
472
+ * const server = createServer({
473
+ * router: appRouter, // RouterDef with MyContext
474
+ * context: () => ({
475
+ * db: prisma,
476
+ * user: null,
477
+ * }), // Must match MyContext!
478
+ * })
479
+ * ```
480
+ */
481
+ declare function createServer<
482
+ TRouter extends RouterDef,
483
+ Q extends QueriesMap = QueriesMap,
484
+ M extends MutationsMap = MutationsMap
485
+ >(config: ServerConfigWithInferredContext<TRouter, Q, M>): LensServer & {
486
+ _types: {
487
+ queries: Q;
488
+ mutations: M;
489
+ context: InferRouterContext<TRouter>;
490
+ };
491
+ };
492
+ declare function createServer<
493
+ TContext extends ContextValue = ContextValue,
494
+ Q extends QueriesMap = QueriesMap,
495
+ M extends MutationsMap = MutationsMap
496
+ >(config: ServerConfigLegacy<TContext, Q, M>): LensServer & {
435
497
  _types: {
436
498
  queries: Q;
437
499
  mutations: M;
500
+ context: TContext;
438
501
  };
439
502
  };
440
503
  /** SSE handler configuration */
@@ -507,4 +570,4 @@ declare class SSEHandler {
507
570
  * Create SSE handler (transport adapter)
508
571
  */
509
572
  declare function createSSEHandler(config: SSEHandlerConfig): SSEHandler;
510
- export { createServer, createSSEHandler, createGraphStateManager, WebSocketLike, Subscription, StateUpdateMessage, StateFullMessage, StateClient, ServerMetadata, LensServerConfig as ServerConfig, SelectionObject, SSEHandlerConfig, SSEHandler, SSEClientInfo, RelationsArray, QueriesMap, OperationsMap, OperationMeta, MutationsMap, LensServer, LensResult, LensOperation, InferOutput, InferInput, InferApi, GraphStateManagerConfig, GraphStateManager, EntityKey, EntitiesMap };
573
+ export { router, query, mutation, createServer, createSSEHandler, createGraphStateManager, WebSocketLike, Subscription, StateUpdateMessage, StateFullMessage, StateClient, ServerMetadata, LensServerConfig as ServerConfig, SelectionObject, SSEHandlerConfig, SSEHandler, SSEClientInfo, RouterRoutes, RouterDef2 as RouterDef, ResolverFn, ResolverContext, RelationsArray, QueryDef2 as QueryDef, QueryBuilder, QueriesMap, OperationsMap, OperationMeta, MutationsMap, MutationDef2 as MutationDef, MutationBuilder, LensServer, LensResult, LensOperation, InferRouterContext2 as InferRouterContext, InferOutput, InferInput, InferApi, GraphStateManagerConfig, GraphStateManager, EntityKey, EntitiesMap };
package/dist/index.js CHANGED
@@ -1,3 +1,10 @@
1
+ // src/index.ts
2
+ import {
3
+ query,
4
+ mutation,
5
+ router
6
+ } from "@sylphx/lens-core";
7
+
1
8
  // src/server/create.ts
2
9
  import {
3
10
  createContext,
@@ -12,8 +19,8 @@ import {
12
19
 
13
20
  // src/state/graph-state-manager.ts
14
21
  import {
15
- createUpdate,
16
22
  applyUpdate,
23
+ createUpdate,
17
24
  makeEntityKey
18
25
  } from "@sylphx/lens-core";
19
26
 
@@ -1403,6 +1410,9 @@ function createSSEHandler(config) {
1403
1410
  return new SSEHandler(config);
1404
1411
  }
1405
1412
  export {
1413
+ router,
1414
+ query,
1415
+ mutation,
1406
1416
  createServer,
1407
1417
  createSSEHandler,
1408
1418
  createGraphStateManager,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sylphx/lens-server",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "description": "Server runtime for Lens API framework",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -29,7 +29,7 @@
29
29
  "author": "SylphxAI",
30
30
  "license": "MIT",
31
31
  "dependencies": {
32
- "@sylphx/lens-core": "^1.2.0"
32
+ "@sylphx/lens-core": "^1.3.1"
33
33
  },
34
34
  "devDependencies": {
35
35
  "typescript": "^5.9.3",
package/src/index.ts CHANGED
@@ -5,6 +5,27 @@
5
5
  * Operations-based server with GraphStateManager for reactive updates.
6
6
  */
7
7
 
8
+ // =============================================================================
9
+ // Re-exports from Core (commonly used with server)
10
+ // =============================================================================
11
+
12
+ export {
13
+ // Operations
14
+ query,
15
+ mutation,
16
+ router,
17
+ // Types
18
+ type QueryBuilder,
19
+ type MutationBuilder,
20
+ type QueryDef,
21
+ type MutationDef,
22
+ type RouterDef,
23
+ type RouterRoutes,
24
+ type ResolverFn,
25
+ type ResolverContext,
26
+ type InferRouterContext,
27
+ } from "@sylphx/lens-core";
28
+
8
29
  // =============================================================================
9
30
  // Server
10
31
  // =============================================================================
@@ -10,13 +10,13 @@
10
10
 
11
11
  import {
12
12
  type ContextValue,
13
- type Emit,
14
13
  type EmitCommand,
15
14
  type EntityDef,
16
15
  type EntityDefinition,
17
16
  type EntityResolvers,
18
17
  type EntityResolversDefinition,
19
18
  type FieldType,
19
+ type InferRouterContext,
20
20
  type MutationDef,
21
21
  type QueryDef,
22
22
  type RelationDef,
@@ -72,20 +72,23 @@ export type OperationsMap = {
72
72
  };
73
73
 
74
74
  /** Server configuration */
75
- export interface LensServerConfig<TContext extends ContextValue = ContextValue> {
75
+ export interface LensServerConfig<
76
+ TContext extends ContextValue = ContextValue,
77
+ TRouter extends RouterDef = RouterDef,
78
+ > {
76
79
  /** Entity definitions */
77
80
  entities?: EntitiesMap;
78
81
  /** Relation definitions */
79
82
  relations?: RelationsArray;
80
- /** Router definition (namespaced operations) */
81
- router?: RouterDef;
83
+ /** Router definition (namespaced operations) - context type is inferred */
84
+ router?: TRouter;
82
85
  /** Query definitions (flat, legacy) */
83
86
  queries?: QueriesMap;
84
87
  /** Mutation definitions (flat, legacy) */
85
88
  mutations?: MutationsMap;
86
89
  /** Entity resolvers */
87
90
  resolvers?: EntityResolvers<EntityResolversDefinition>;
88
- /** Context factory */
91
+ /** Context factory - must return the context type expected by the router */
89
92
  context?: (req?: unknown) => TContext | Promise<TContext>;
90
93
  /** Server version */
91
94
  version?: string;
@@ -675,7 +678,10 @@ class LensServerImpl<
675
678
  const entityName = this.getEntityNameFromOutput(queryDef._output);
676
679
  if (entityName) {
677
680
  // For entity-typed outputs, use GraphStateManager
678
- const entities = this.extractEntities(entityName, command.type === "full" ? command.data : {});
681
+ const entities = this.extractEntities(
682
+ entityName,
683
+ command.type === "full" ? command.data : {},
684
+ );
679
685
  for (const { entity, id } of entities) {
680
686
  this.stateManager.processCommand(entity, id, command);
681
687
  }
@@ -1546,17 +1552,87 @@ export type InferApi<T extends LensServer> = T extends LensServerImpl<infer Q, i
1546
1552
  // Factory
1547
1553
  // =============================================================================
1548
1554
 
1555
+ /**
1556
+ * Config helper type that infers context from router
1557
+ */
1558
+ export type ServerConfigWithInferredContext<
1559
+ TRouter extends RouterDef,
1560
+ Q extends QueriesMap = QueriesMap,
1561
+ M extends MutationsMap = MutationsMap,
1562
+ > = {
1563
+ entities?: EntitiesMap;
1564
+ relations?: RelationsArray;
1565
+ router: TRouter;
1566
+ queries?: Q;
1567
+ mutations?: M;
1568
+ resolvers?: EntityResolvers<EntityResolversDefinition>;
1569
+ /** Context factory - type is inferred from router's procedures */
1570
+ context?: (req?: unknown) => InferRouterContext<TRouter> | Promise<InferRouterContext<TRouter>>;
1571
+ version?: string;
1572
+ };
1573
+
1574
+ /**
1575
+ * Config without router (legacy flat queries/mutations)
1576
+ */
1577
+ export type ServerConfigLegacy<
1578
+ TContext extends ContextValue = ContextValue,
1579
+ Q extends QueriesMap = QueriesMap,
1580
+ M extends MutationsMap = MutationsMap,
1581
+ > = {
1582
+ entities?: EntitiesMap;
1583
+ relations?: RelationsArray;
1584
+ router?: undefined;
1585
+ queries?: Q;
1586
+ mutations?: M;
1587
+ resolvers?: EntityResolvers<EntityResolversDefinition>;
1588
+ context?: (req?: unknown) => TContext | Promise<TContext>;
1589
+ version?: string;
1590
+ };
1591
+
1549
1592
  /**
1550
1593
  * Create Lens server with Operations API + Optimization Layer
1594
+ *
1595
+ * When using a router with typed context (from initLens), the context
1596
+ * function's return type is automatically enforced to match.
1597
+ *
1598
+ * @example
1599
+ * ```typescript
1600
+ * // Context type is inferred from router's procedures
1601
+ * const server = createServer({
1602
+ * router: appRouter, // RouterDef with MyContext
1603
+ * context: () => ({
1604
+ * db: prisma,
1605
+ * user: null,
1606
+ * }), // Must match MyContext!
1607
+ * })
1608
+ * ```
1551
1609
  */
1610
+ export function createServer<
1611
+ TRouter extends RouterDef,
1612
+ Q extends QueriesMap = QueriesMap,
1613
+ M extends MutationsMap = MutationsMap,
1614
+ >(
1615
+ config: ServerConfigWithInferredContext<TRouter, Q, M>,
1616
+ ): LensServer & { _types: { queries: Q; mutations: M; context: InferRouterContext<TRouter> } };
1617
+
1618
+ export function createServer<
1619
+ TContext extends ContextValue = ContextValue,
1620
+ Q extends QueriesMap = QueriesMap,
1621
+ M extends MutationsMap = MutationsMap,
1622
+ >(
1623
+ config: ServerConfigLegacy<TContext, Q, M>,
1624
+ ): LensServer & { _types: { queries: Q; mutations: M; context: TContext } };
1625
+
1552
1626
  export function createServer<
1553
1627
  TContext extends ContextValue = ContextValue,
1554
1628
  Q extends QueriesMap = QueriesMap,
1555
1629
  M extends MutationsMap = MutationsMap,
1556
1630
  >(
1557
1631
  config: LensServerConfig<TContext> & { queries?: Q; mutations?: M },
1558
- ): LensServer & { _types: { queries: Q; mutations: M } } {
1559
- const server = new LensServerImpl(config) as LensServerImpl<Q, M>;
1632
+ ): LensServer & { _types: { queries: Q; mutations: M; context: TContext } } {
1633
+ const server = new LensServerImpl(config) as LensServerImpl<Q, M, TContext>;
1560
1634
  // Attach type marker for inference (stripped at runtime)
1561
- return server as unknown as LensServer & { _types: { queries: Q; mutations: M } };
1635
+ return server as unknown as LensServer & {
1636
+ _types: { queries: Q; mutations: M; context: TContext };
1637
+ };
1562
1638
  }
@@ -10,13 +10,13 @@
10
10
  */
11
11
 
12
12
  import {
13
- type EntityKey,
14
- type Update,
13
+ type ArrayOperation,
15
14
  type EmitCommand,
15
+ type EntityKey,
16
16
  type InternalFieldUpdate,
17
- type ArrayOperation,
18
- createUpdate,
17
+ type Update,
19
18
  applyUpdate,
19
+ createUpdate,
20
20
  makeEntityKey,
21
21
  } from "@sylphx/lens-core";
22
22