Skip to content

Commit

Permalink
Add types to finder methods
Browse files Browse the repository at this point in the history
  • Loading branch information
garymardell committed Jan 30, 2025
1 parent 6ff6af3 commit 2a98c90
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
14 changes: 7 additions & 7 deletions packages/api-client-core/src/GadgetRecordList.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-throw-literal */
/* eslint-disable @typescript-eslint/require-await */
import type { Jsonify } from "type-fest";
import type { GadgetRecord, RecordShape } from "./GadgetRecord.js";
import type { GadgetRecord, GadgetRecord_, RecordShape } from "./GadgetRecord.js";
import type { InternalModelManager } from "./InternalModelManager.js";
import type { AnyModelManager } from "./ModelManager.js";
import { GadgetClientError, GadgetOperationError } from "./support.js";
Expand All @@ -13,17 +13,17 @@ type PaginationConfig = {
};

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

/** Internal method used to create a list. Should not be used by applications. */
static boot<Shape extends RecordShape>(
static boot<Shape extends RecordShape, RecordType extends GadgetRecord_<Shape> = GadgetRecord<any>>(
modelManager: AnyModelManager | InternalModelManager<Shape>,
records: GadgetRecord<Shape>[],
records: RecordType[],
pagination: PaginationConfig
) {
const list = new GadgetRecordList<Shape>();
const list = new GadgetRecordList<Shape, RecordType>();
list.push(...records);
list.modelManager = modelManager;
list.pagination = pagination;
Expand Down Expand Up @@ -71,7 +71,7 @@ export class GadgetRecordList<Shape extends RecordShape> extends Array<GadgetRec
...options,
after: this.pagination.pageInfo.endCursor,
first: first || last,
}) as Promise<GadgetRecordList<Shape>>;
}) as Promise<GadgetRecordList<Shape, RecordType>>;
return await nextPage;
}

Expand All @@ -86,7 +86,7 @@ export class GadgetRecordList<Shape extends RecordShape> extends Array<GadgetRec
...options,
before: this.pagination.pageInfo.startCursor,
last: last || first,
}) as Promise<GadgetRecordList<Shape>>;
}) as Promise<GadgetRecordList<Shape, RecordType>>;
return await prevPage;
}
}
14 changes: 7 additions & 7 deletions packages/api-client-core/src/ModelManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { GadgetConnection } from "./GadgetConnection.js";
import type { GadgetRecord } from "./GadgetRecord.js";
import { GadgetRecord, GadgetRecord_, RecordShape } from "./GadgetRecord.js";
import type { GadgetRecordList } from "./GadgetRecordList.js";

export type AnyModelFinderMetadata = {
Expand All @@ -24,9 +24,9 @@ export type AnyModelFinderMetadata = {
* This is a generic interface. Concrete ones are generated by Gadget, */
export interface AnyModelManager {
connection: GadgetConnection;
findOne: ((id: string, options: any) => Promise<GadgetRecord<any>>) & AnyModelFinderMetadata;
findMany: ((options: any) => Promise<GadgetRecordList<any>>) & AnyModelFinderMetadata;
findFirst: ((options: any) => Promise<GadgetRecord<any>>) & AnyModelFinderMetadata;
maybeFindFirst(options: any): Promise<GadgetRecord<any> | null>;
maybeFindOne(id: string, options: any): Promise<GadgetRecord<any> | null>;
}
findOne: (<RecordType extends GadgetRecord_<any> = GadgetRecord<any>>(id: string, options: any) => Promise<RecordType>) & AnyModelFinderMetadata;
findMany: (<RecordType extends GadgetRecord_<any> = GadgetRecord<any>>(options: any) => Promise<GadgetRecordList<any, RecordType>>) & AnyModelFinderMetadata;
findFirst: (<RecordType extends GadgetRecord_<any> = GadgetRecord<any>>(options: any) => Promise<RecordType>) & AnyModelFinderMetadata;
maybeFindFirst<RecordType extends GadgetRecord_<any> = GadgetRecord<any>>(options: any): Promise<RecordType | null>;
maybeFindOne<RecordType extends GadgetRecord_<any> = GadgetRecord<any>>(id: string, options: any): Promise<RecordType| null>;
}

0 comments on commit 2a98c90

Please sign in to comment.