benchling_typescript_sdk
Version:
Typescript SDK for Benchling API
46 lines (40 loc) • 1.37 kB
text/typescript
import { BaseClient } from "../BaseClient";
import { chunkQueries } from "../superModules/chunkQueries";
import type { Box, BoxPathGetParamQuery } from "../types";
type LimitedListQueries = keyof NonNullable<BoxPathGetParamQuery>;
const limitedSearches: LimitedListQueries[] = [
"names.anyOf",
"names.anyOf.caseSensitive",
"barcodes",
"ids",
"storageContentsIds",
"creatorIds",
];
export class Boxes {
private client: BaseClient;
constructor(client: BaseClient) {
this.client = client;
}
public async *listBoxesNoLimits(paramQuery: BoxPathGetParamQuery): AsyncGenerator<Box[]> {
if (!paramQuery || typeof paramQuery !== "object") {
throw new Error("Invalid query parameters provided.");
}
// Generate the list of chunked queries so that no list of queries is longer than 100
let paramQueries: BoxPathGetParamQuery[] = chunkQueries<NonNullable<BoxPathGetParamQuery>>(
paramQuery,
100,
limitedSearches
);
// Iterate over each chunked query
for (const query of paramQueries) {
// Call list for the current query
for await (const batch of this.listBoxes(query)) {
// Yield each batch
yield batch;
}
}
}
public listBoxes(parameters: BoxPathGetParamQuery): AsyncGenerator<Box[]> {
return this.client.fetchPagesIterator<Box>("boxes", parameters);
}
}