Skip to content

Commit 52231eb

Browse files
committed
Add types to finder methods
1 parent 6ff6af3 commit 52231eb

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

packages/api-client-core/src/GadgetRecordList.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ type PaginationConfig = {
1313
};
1414

1515
/** Represents a list of objects returned from the API. Facilitates iterating and paginating. */
16-
export class GadgetRecordList<Shape extends RecordShape> extends Array<GadgetRecord<Shape>> {
16+
export class GadgetRecordList<Shape extends RecordShape, RecordType extends GadgetRecord<Shape> = GadgetRecord<any>> extends Array<RecordType> {
1717
modelManager!: AnyModelManager | InternalModelManager<Shape>;
1818
pagination!: PaginationConfig;
1919

2020
/** Internal method used to create a list. Should not be used by applications. */
21-
static boot<Shape extends RecordShape>(
21+
static boot<Shape extends RecordShape, RecordType extends GadgetRecord<Shape> = GadgetRecord<any>>(
2222
modelManager: AnyModelManager | InternalModelManager<Shape>,
23-
records: GadgetRecord<Shape>[],
23+
records: RecordType[],
2424
pagination: PaginationConfig
2525
) {
26-
const list = new GadgetRecordList<Shape>();
26+
const list = new GadgetRecordList<Shape, RecordType>();
2727
list.push(...records);
2828
list.modelManager = modelManager;
2929
list.pagination = pagination;
@@ -71,7 +71,7 @@ export class GadgetRecordList<Shape extends RecordShape> extends Array<GadgetRec
7171
...options,
7272
after: this.pagination.pageInfo.endCursor,
7373
first: first || last,
74-
}) as Promise<GadgetRecordList<Shape>>;
74+
}) as Promise<GadgetRecordList<Shape, RecordType>>;
7575
return await nextPage;
7676
}
7777

@@ -86,7 +86,7 @@ export class GadgetRecordList<Shape extends RecordShape> extends Array<GadgetRec
8686
...options,
8787
before: this.pagination.pageInfo.startCursor,
8888
last: last || first,
89-
}) as Promise<GadgetRecordList<Shape>>;
89+
}) as Promise<GadgetRecordList<Shape, RecordType>>;
9090
return await prevPage;
9191
}
9292
}

packages/api-client-core/src/ModelManager.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ export type AnyModelFinderMetadata = {
2424
* This is a generic interface. Concrete ones are generated by Gadget, */
2525
export interface AnyModelManager {
2626
connection: GadgetConnection;
27-
findOne: ((id: string, options: any) => Promise<GadgetRecord<any>>) & AnyModelFinderMetadata;
28-
findMany: ((options: any) => Promise<GadgetRecordList<any>>) & AnyModelFinderMetadata;
29-
findFirst: ((options: any) => Promise<GadgetRecord<any>>) & AnyModelFinderMetadata;
30-
maybeFindFirst(options: any): Promise<GadgetRecord<any> | null>;
31-
maybeFindOne(id: string, options: any): Promise<GadgetRecord<any> | null>;
27+
findOne: (<RecordType extends GadgetRecord<any> = GadgetRecord<any>>(id: string, options: any) => Promise<RecordType>) & AnyModelFinderMetadata;
28+
findMany: (<RecordType extends GadgetRecord<any> = GadgetRecord<any>>(options: any) => Promise<GadgetRecordList<any, RecordType>>) & AnyModelFinderMetadata;
29+
findFirst: (<RecordType extends GadgetRecord<any> = GadgetRecord<any>>(options: any) => Promise<RecordType>) & AnyModelFinderMetadata;
30+
maybeFindFirst<RecordType extends GadgetRecord<any> = GadgetRecord<any>>(options: any): Promise<RecordType | null>;
31+
maybeFindOne<RecordType extends GadgetRecord<any> = GadgetRecord<any>>(id: string, options: any): Promise<RecordType| null>;
3232
}

0 commit comments

Comments
 (0)