Skip to content

Commit 2a98c90

Browse files
committed
Add types to finder methods
1 parent 6ff6af3 commit 2a98c90

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

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

Lines changed: 7 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,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: 7 additions & 7 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_, RecordShape } from "./GadgetRecord.js";
33
import type { GadgetRecordList } from "./GadgetRecordList.js";
44

55
export type AnyModelFinderMetadata = {
@@ -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>;
32-
}
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>;
32+
}

0 commit comments

Comments
 (0)