Skip to content

Commit 6c1a772

Browse files
committed
latest build
1 parent 47377e8 commit 6c1a772

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

dist/index.d.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,38 @@ export interface ComponentContext extends Record<string, unknown> {
1313
export type ContextFunction = ((context: Record<string, unknown>) => any);
1414
export interface IDataSource {
1515
name: string;
16+
[key: string | symbol]: any;
1617
}
18+
/**
19+
* Type for implementing data sources
20+
* When defining a data source class, methods should accept context as their first parameter
21+
* @example
22+
* class MyDataSource {
23+
* name = 'MyDataSource';
24+
*
25+
* // Context is required as first parameter when implementing
26+
* getData(context: ComponentContext, id: string) {
27+
* return { id };
28+
* }
29+
* }
30+
*/
31+
export type DataSourceDefinition<T> = {
32+
[P in keyof T]: T[P] extends Function ? (context: ComponentContext, ...args: any[]) => any : T[P];
33+
};
34+
/**
35+
* Type for consuming data sources in resolvers
36+
* When using a data source method, the context is automatically injected
37+
* @example
38+
* // In a resolver:
39+
* Query: {
40+
* getData(_, { id }, context) {
41+
* // Context is automatically injected, so you don't pass it
42+
* return context.dataSources.MyDataSource.getData(id);
43+
* }
44+
* }
45+
*/
1746
export type DataSource<T> = {
18-
[P in keyof T]: T[P] extends (context: ComponentContext, ...p: infer P) => infer R ? (...p: P) => R : never;
47+
[P in keyof T]: T[P] extends (context: ComponentContext, ...p: infer P) => infer R ? (...p: P) => R : T[P];
1948
};
2049
export type DataSourceMap = {
2150
[key: string]: IDataSource;

0 commit comments

Comments
 (0)