benchling_typescript_sdk
Version:
Typescript SDK for Benchling API
89 lines (76 loc) • 2.83 kB
text/typescript
import { BaseClient } from "../BaseClient";
import type { CustomEntity, CustomEntityPathGetParamQuery } from "../types";
import { chunkQueries } from "../superModules/chunkQueries";
type LimitedListQueries = keyof NonNullable<CustomEntityPathGetParamQuery>;
const limitedSearches: LimitedListQueries[] = [
"names.anyOf",
"names.anyOf.caseSensitive",
"entityRegistryIds.anyOf",
"authorIds.anyOf",
"ids",
"creatorIds",
"mentions",
];
export class CustomEntities {
private client: BaseClient;
constructor(client: BaseClient) {
this.client = client;
}
public async getCustomEntity(customEntityId: string): Promise<CustomEntity | null> {
if (!customEntityId) {
throw new Error("Custom Entity ID is required but was not provided.");
}
return this.client.fetchData<CustomEntity>(`custom-entities/${customEntityId}`);
}
public listCustomEntities(
parameters: CustomEntityPathGetParamQuery
): AsyncGenerator<CustomEntity[]> {
return this.client.fetchPagesIterator<CustomEntity>("custom-entities", parameters);
}
public async testListNoLimits(paramQuery: CustomEntityPathGetParamQuery) {
let d = this.listCustomEntitiesNoLimits(paramQuery);
// console.log(d);
}
public async *listCustomEntitiesNoLimits(
paramQuery: CustomEntityPathGetParamQuery
): AsyncGenerator<CustomEntity[]> {
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: CustomEntityPathGetParamQuery[] = chunkQueries<
NonNullable<CustomEntityPathGetParamQuery>
>(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.listCustomEntities(query)) {
// Yield each batch
yield batch;
}
}
}
public async *listCustomEntitiesFromNamesAnyOf(
namesAnyOf: string[],
schema_id: string
): AsyncGenerator<CustomEntity[]> {
const chunkSize = 100;
// Split the namesAnyOf array into chunks of 100
for (let i = 0; i < namesAnyOf.length; i += chunkSize) {
const chunk = namesAnyOf.slice(i, i + chunkSize);
// Call listCustomEntities for the current chunk
const parameters: CustomEntityPathGetParamQuery = {
"names.anyOf": chunk.join(","),
schemaId: schema_id,
};
// Yield results from the inner generator
for await (const entities of this.listCustomEntities(parameters)) {
yield entities;
}
}
}
}
// let entities = superClient.benchling.customEntities.listCustomEntities({
// schemaId,
// "names.anyOf": dc.uniqueValues.join(","),
// });