benchling_typescript_sdk
Version:
Typescript SDK for Benchling API
85 lines (77 loc) • 2.98 kB
text/typescript
import { UnexpectedBenchlingConfigError } from "../../utils/clientError";
import { BaseClient } from "../BaseClient";
import { chunkQueries } from "../superModules/chunkQueries";
import type {
AssayResultsGetParamQuery,
AssayResult,
AssayResultCreate,
AssayResultsCreateResponse,
AssayResultIdsResponse,
AssayResultsArchive,
AsyncTaskLink,
AsyncTask,
} from "../types";
type LimitedListQueries = keyof NonNullable<AssayResultsGetParamQuery>;
const limitedSearches: LimitedListQueries[] = ["entityIds", "storageIds", "assayRunIds", "ids"];
export class AssayResults {
private client: BaseClient;
constructor(client: BaseClient) {
this.client = client;
}
public async *listAssayResultsNoLimits(
paramQuery: AssayResultsGetParamQuery
): AsyncGenerator<AssayResult[]> {
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: AssayResultsGetParamQuery[] = chunkQueries<
NonNullable<AssayResultsGetParamQuery>
>(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.getAssayResults(query)) {
// Yield each batch
yield batch;
}
}
}
public getAssayResults(parameters: AssayResultsGetParamQuery): AsyncGenerator<AssayResult[]> {
return this.client.fetchPagesIterator<AssayResult>("assay-results", parameters);
}
public async postAssayResults(body: AssayResultCreate[]): Promise<AssayResultsCreateResponse> {
return await this.client.postData<AssayResultsCreateResponse>(
"assay-results",
{ assayResults: body },
{}
);
}
public async getAssayResultById(assayResultId: string): Promise<AssayResult> {
return await this.client.fetchData<AssayResult>(`assay-results/${assayResultId}`, {});
}
public async assayResultsArchive(body: AssayResultsArchive): Promise<AssayResultIdsResponse> {
return await this.client.postData<AssayResultIdsResponse>("assay-results:archive", body, {});
}
public async waitForBulkAssayCreate(
assayResults: AssayResultCreate[]
): Promise<Record<string, never> | undefined> {
let task = await this.client.postData<AsyncTaskLink>(
"assay-results:bulk-create",
assayResults,
{}
);
if (!task || !task.taskId) {
console.warn("Unexpected Task Response:", task);
throw new UnexpectedBenchlingConfigError({
message: "AssayResults.waitForBulkAssayCreate: taskId is missing in response",
body: JSON.stringify(task),
url: "assay-results:bulk-create",
});
}
let res: AsyncTask = await this.client.awaitForTask(task.taskId);
return res.response;
}
}
// public async assayResultsBulkGet() {}
// public async assayResultsUnarchive() {}