@graffy/core 0.16.20-alpha.5 → 0.16.20-alpha.7

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.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/types/index.d.ts +47 -9
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graffy/core",
3
3
  "description": "The main module for Graffy, a library for intuitive real-time data APIs.",
4
4
  "author": "aravind (https://github.com/aravindet)",
5
- "version": "0.16.20-alpha.5",
5
+ "version": "0.16.20-alpha.7",
6
6
  "main": "./index.cjs",
7
7
  "exports": {
8
8
  "import": "./index.mjs",
@@ -16,8 +16,8 @@
16
16
  },
17
17
  "license": "Apache-2.0",
18
18
  "dependencies": {
19
- "@graffy/common": "0.16.20-alpha.5",
20
- "@graffy/stream": "0.16.20-alpha.5",
19
+ "@graffy/common": "0.16.20-alpha.7",
20
+ "@graffy/stream": "0.16.20-alpha.7",
21
21
  "debug": "^4.3.7"
22
22
  }
23
23
  }
package/types/index.d.ts CHANGED
@@ -9,6 +9,15 @@ type AnyFunction = (...args: any[]) => any;
9
9
  // biome-ignore lint/suspicious/noExplicitAny: Keys can be anything.
10
10
  type Key = any;
11
11
 
12
+ // biome-ignore lint/suspicious/noExplicitAny: Any value is for results.
13
+ type AnyValue = any;
14
+
15
+ type AnyProjection =
16
+ | boolean
17
+ | { $key: Key }
18
+ | { [key: string]: AnyProjection }
19
+ | AnyProjection[];
20
+
12
21
  export type GraffyCollection<CollectionSchema extends AnyObject> = {
13
22
  [name: string]: CollectionSchema;
14
23
  } & { __brand: 'GraffyCollection' };
@@ -27,6 +36,15 @@ export default class Graffy<S> {
27
36
  options?: GraffyReadOptions,
28
37
  ): Promise<ReadResult<S, Q>>;
29
38
 
39
+ // Generic one, when the path is not known at compile time.
40
+ // Should we have this?
41
+ //
42
+ // read<Q extends AnyProjection>(
43
+ // path: string | Key[],
44
+ // projection: Q,
45
+ // options?: GraffyReadOptions,
46
+ // ): Promise<BlindReadResult<Q>>;
47
+
30
48
  on: AnyFunction;
31
49
  call: AnyFunction;
32
50
  use: AnyFunction;
@@ -63,21 +81,41 @@ export type Project<S> = S extends AnyLeaf
63
81
  ? boolean
64
82
  : S extends GraffyCollection<AnyObject>
65
83
  ?
66
- | (Project<S[string]> & { $key: Key })
67
- | (Project<S[string]> & { $key: Key })[]
84
+ | { [key: string]: Project<S[string]> } // Object form
85
+ | (Project<S[string]> & { $key: Key }) // Single $key
86
+ | (Project<S[string]> & { $key: Key })[] // Array $key
68
87
  : S extends AnyObject
69
- ? Partial<{ [K in keyof S]: Project<S[K]> }> | boolean
88
+ ? 'string' extends keyof S // No named properties?
89
+ ? AnyProjection
90
+ : Partial<{ [K in keyof S]: Project<S[K]> }> | boolean
70
91
  : never;
71
92
 
72
93
  // biome-ignore lint/suspicious/noExplicitAny: <explanation>
73
94
  type ResultArray<R> = R[] & { $page: any; $next: any; $prev: any };
74
95
 
75
96
  type ReadResult<S, Q> = S extends GraffyCollection<AnyObject>
76
- ? Q extends Array<AnyObject> | { $key: Key }
77
- ? ResultArray<S[string] & { $key: Key }>
78
- : S[string] & { $key: Key }
79
- : Q extends AnyObject
80
- ? { [K in keyof S & keyof Q]: ReadResult<S[K], Q[K]> }
81
- : S;
97
+ ? Q extends Array<infer QItem>
98
+ ? ResultArray<PlainReadResult<S[string], QItem> & { $key: Key }> // Array $key
99
+ : Q extends { $key: Key }
100
+ ? ResultArray<PlainReadResult<S[string], Q> & { $key: Key }> // Single $key
101
+ : { [K in keyof Q]: PlainReadResult<S[string], Q[K]> } // Object form
102
+ : PlainReadResult<S, Q>;
103
+
104
+ // Ignore $key in Q
105
+ type PlainReadResult<S, Q> = Q extends AnyObject
106
+ ? { [K in keyof S & keyof Q]: ReadResult<S[K], Q[K]> }
107
+ : S;
108
+
109
+ // What can we tell about ReadResult when schema isn’t known?
110
+ // type BlindReadResult<Q> = Q extends Array<infer QItem>
111
+ // ? ResultArray<BlindPlainReadResult<QItem>>
112
+ // : Q extends { $key: Key }
113
+ // ? ResultArray<BlindPlainReadResult<Q>>
114
+ // : BlindPlainReadResult<Q>;
115
+
116
+ // // Ignore $key in Q
117
+ // type BlindPlainReadResult<Q> = Q extends AnyObject
118
+ // ? { [K in keyof Q]: BlindReadResult<Q[K]> }
119
+ // : AnyValue;
82
120
 
83
121
  type GraffyReadOptions = AnyObject;