77 * of patent rights can be found in the PATENTS file in the same directory.
88 */
99
10- // If a custom cache is provided, it must be of this type (a subset of ES6 Map).
11- type CacheMap < K , V > = {
12- get ( key : K ) : V | void ;
13- set ( key : K , value : V ) : any ;
14- delete ( key : K ) : any ;
15- clear ( ) : any ;
16- }
17-
18- // A Function, which when given an Array of keys, returns a Promise of an Array
19- // of values or Errors.
20- type BatchLoadFn < K , V > = ( keys : K [ ] ) => Promise < Array < V | Error > > ;
21-
22- // Optionally turn off batching or caching or provide a cache key function or a
23- // custom cache instance.
24- type Options < K , V > = {
25-
26- /**
27- * Default `true`. Set to `false` to disable batching,
28- * instead immediately invoking `batchLoadFn` with a
29- * single load key.
30- */
31- batch ?: boolean ,
32-
33- /**
34- * Default `Infinity`. Limits the number of items that get
35- * passed in to the `batchLoadFn`.
36- */
37- maxBatchSize ?: number ;
38-
39- /**
40- * Default `true`. Set to `false` to disable caching,
41- * instead creating a new Promise and new key in
42- * the `batchLoadFn` for every load.
43- */
44- cache ?: boolean ,
45-
46- /**
47- * A function to produce a cache key for a given load key.
48- * Defaults to `key => key`. Useful to provide when JavaScript
49- * objects are keys and two similarly shaped objects should
50- * be considered equivalent.
51- */
52- cacheKeyFn ?: ( key : any ) => any ,
53-
54- /**
55- * An instance of Map (or an object with a similar API) to
56- * be used as the underlying cache for this loader.
57- * Default `new Map()`.
58- */
59- cacheMap ?: CacheMap < K , Promise < V > > ;
60- }
61-
6210/**
6311 * DataLoader creates a public API for loading data from a particular
6412 * data back-end with unique keys such as the id column of a SQL table
@@ -69,9 +17,9 @@ type Options<K, V> = {
6917 * with different access permissions and consider creating a new instance
7018 * per web request.
7119 */
72- export class DataLoader < K , V > {
20+ declare class DataLoader < K , V > {
7321
74- constructor ( batchLoadFn : BatchLoadFn < K , V > , options ?: Options < K , V > ) ;
22+ constructor ( batchLoadFn : DataLoader . BatchLoadFn < K , V > , options ?: DataLoader . Options < K , V > ) ;
7523
7624 /**
7725 * Loads a key, returning a `Promise` for the value represented by that key.
@@ -112,3 +60,59 @@ export class DataLoader<K, V> {
11260 */
11361 prime ( key : K , value : V ) : DataLoader < K , V > ;
11462}
63+
64+ declare namespace DataLoader {
65+ // If a custom cache is provided, it must be of this type (a subset of ES6 Map).
66+ export type CacheMap < K , V > = {
67+ get ( key : K ) : V | void ;
68+ set ( key : K , value : V ) : any ;
69+ delete ( key : K ) : any ;
70+ clear ( ) : any ;
71+ }
72+
73+ // A Function, which when given an Array of keys, returns a Promise of an Array
74+ // of values or Errors.
75+ export type BatchLoadFn < K , V > = ( keys : K [ ] ) => Promise < Array < V | Error > > ;
76+
77+ // Optionally turn off batching or caching or provide a cache key function or a
78+ // custom cache instance.
79+ export type Options < K , V > = {
80+
81+ /**
82+ * Default `true`. Set to `false` to disable batching,
83+ * instead immediately invoking `batchLoadFn` with a
84+ * single load key.
85+ */
86+ batch ?: boolean ,
87+
88+ /**
89+ * Default `Infinity`. Limits the number of items that get
90+ * passed in to the `batchLoadFn`.
91+ */
92+ maxBatchSize ?: number ;
93+
94+ /**
95+ * Default `true`. Set to `false` to disable caching,
96+ * instead creating a new Promise and new key in
97+ * the `batchLoadFn` for every load.
98+ */
99+ cache ?: boolean ,
100+
101+ /**
102+ * A function to produce a cache key for a given load key.
103+ * Defaults to `key => key`. Useful to provide when JavaScript
104+ * objects are keys and two similarly shaped objects should
105+ * be considered equivalent.
106+ */
107+ cacheKeyFn ?: ( key : any ) => any ,
108+
109+ /**
110+ * An instance of Map (or an object with a similar API) to
111+ * be used as the underlying cache for this loader.
112+ * Default `new Map()`.
113+ */
114+ cacheMap ?: CacheMap < K , Promise < V > > ;
115+ }
116+ }
117+
118+ export = DataLoader ;
0 commit comments