Skip to content

Commit 82e277b

Browse files
committed
Add types to finder methods
1 parent 6ff6af3 commit 82e277b

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-throw-literal */
22
/* eslint-disable @typescript-eslint/require-await */
33
import type { Jsonify } from "type-fest";
4-
import type { GadgetRecord, RecordShape } from "./GadgetRecord.js";
4+
import type { GadgetRecord, GadgetRecord_, RecordShape } from "./GadgetRecord.js";
55
import type { InternalModelManager } from "./InternalModelManager.js";
66
import type { AnyModelManager } from "./ModelManager.js";
77
import { GadgetClientError, GadgetOperationError } from "./support.js";
@@ -13,17 +13,20 @@ 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<
17+
Shape extends RecordShape,
18+
RecordType extends GadgetRecord_<Shape> = GadgetRecord<any>
19+
> extends Array<RecordType> {
1720
modelManager!: AnyModelManager | InternalModelManager<Shape>;
1821
pagination!: PaginationConfig;
1922

2023
/** Internal method used to create a list. Should not be used by applications. */
21-
static boot<Shape extends RecordShape>(
24+
static boot<Shape extends RecordShape, RecordType extends GadgetRecord_<Shape> = GadgetRecord<any>>(
2225
modelManager: AnyModelManager | InternalModelManager<Shape>,
23-
records: GadgetRecord<Shape>[],
26+
records: RecordType[],
2427
pagination: PaginationConfig
2528
) {
26-
const list = new GadgetRecordList<Shape>();
29+
const list = new GadgetRecordList<Shape, RecordType>();
2730
list.push(...records);
2831
list.modelManager = modelManager;
2932
list.pagination = pagination;
@@ -71,7 +74,7 @@ export class GadgetRecordList<Shape extends RecordShape> extends Array<GadgetRec
7174
...options,
7275
after: this.pagination.pageInfo.endCursor,
7376
first: first || last,
74-
}) as Promise<GadgetRecordList<Shape>>;
77+
}) as Promise<GadgetRecordList<Shape, RecordType>>;
7578
return await nextPage;
7679
}
7780

@@ -86,7 +89,7 @@ export class GadgetRecordList<Shape extends RecordShape> extends Array<GadgetRec
8689
...options,
8790
before: this.pagination.pageInfo.startCursor,
8891
last: last || first,
89-
}) as Promise<GadgetRecordList<Shape>>;
92+
}) as Promise<GadgetRecordList<Shape, RecordType>>;
9093
return await prevPage;
9194
}
9295
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { GadgetConnection } from "./GadgetConnection.js";
2-
import type { GadgetRecord } from "./GadgetRecord.js";
2+
import { GadgetRecord, GadgetRecord_ } from "./GadgetRecord.js";
33
import type { GadgetRecordList } from "./GadgetRecordList.js";
44

55
export type AnyModelFinderMetadata = {
@@ -24,9 +24,11 @@ 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>) &
28+
AnyModelFinderMetadata;
29+
findMany: (<RecordType extends GadgetRecord_<any> = GadgetRecord<any>>(options: any) => Promise<GadgetRecordList<any, RecordType>>) &
30+
AnyModelFinderMetadata;
31+
findFirst: (<RecordType extends GadgetRecord_<any> = GadgetRecord<any>>(options: any) => Promise<RecordType>) & AnyModelFinderMetadata;
32+
maybeFindFirst<RecordType extends GadgetRecord_<any> = GadgetRecord<any>>(options: any): Promise<RecordType | null>;
33+
maybeFindOne<RecordType extends GadgetRecord_<any> = GadgetRecord<any>>(id: string, options: any): Promise<RecordType | null>;
3234
}

0 commit comments

Comments
 (0)