From efa8ce55a66e363d83c2890371d7cc1924ae702e Mon Sep 17 00:00:00 2001 From: Dimitri Date: Mon, 16 Dec 2024 21:03:11 +0700 Subject: [PATCH] feat: do not fetch stars per blueprint on list blueprint --- package.json | 2 +- src/blueprint.ts | 26 +++++++++----------------- src/types/blueprint.ts | 12 +++--------- 3 files changed, 13 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index dea9b7d..e6b679d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@zk-email/sdk", - "version": "0.0.84", + "version": "0.0.85", "description": "ZK Email SDK for TypeScript", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/src/blueprint.ts b/src/blueprint.ts index 53ce64d..1c6efcb 100644 --- a/src/blueprint.ts +++ b/src/blueprint.ts @@ -5,7 +5,6 @@ import { BlueprintResponse, DownloadUrls, ListBlueprintsOptions, - ListBlueprintsOptionsRequest, Status, ZkFramework, } from "./types/blueprint"; @@ -152,6 +151,7 @@ export class Blueprint { chain: response.verifier_contract_chain, }, version: response.version, + stars: response.stars, }; return props; @@ -290,23 +290,19 @@ export class Blueprint { public static async listBlueprints( baseUrl: string, options?: ListBlueprintsOptions, - auth?: Auth, - fetchStars = true + auth?: Auth ): Promise { - const requestOptions: ListBlueprintsOptionsRequest = { - skip: options?.skip, - limit: options?.limit, - sort: options?.sort, - status: options?.status, - is_public: options?.isPublic, - search: options?.search, - }; + if (options?.sortBy) { + // Backend accepts snake case only + // @ts-ignore + options.sortBy = options.sortBy === "updatedAt" ? "updated_at" : options.sortBy; + } let response: { blueprints?: BlueprintResponse[] }; try { response = await get<{ blueprints?: BlueprintResponse[] }>( `${baseUrl}/blueprint`, - requestOptions, + options, auth ); } catch (err) { @@ -323,10 +319,6 @@ export class Blueprint { return new Blueprint(blueprintProps, baseUrl, auth); }); - if (fetchStars) { - await Promise.all(blueprints.map((bp) => bp.getStars())); - } - return blueprints; } @@ -624,7 +616,7 @@ export class Blueprint { const { stars } = await get<{ stars: number }>( `${this.baseUrl}/blueprint/${encodeURIComponent(this.props.slug!)}/stars` ); - this.stars = stars || 0; + this.props.stars = stars || 0; return stars || 0; } catch (err) { console.error("Failed calling POST on /blueprint/${id}/stars in addStar: ", err); diff --git a/src/types/blueprint.ts b/src/types/blueprint.ts index 5a3ff73..1bb0dca 100644 --- a/src/types/blueprint.ts +++ b/src/types/blueprint.ts @@ -24,6 +24,7 @@ export type BlueprintProps = { status?: Status; verifierContract?: VerifierContract; version?: number; + stars?: number; }; export type DecomposedRegex = { @@ -128,6 +129,7 @@ export type BlueprintResponse = { verifier_contract_address: string; verifier_contract_chain: number; version: number; + stars: number; }; export type ServerDate = { @@ -156,18 +158,10 @@ export type ListBlueprintsOptions = { skip?: number; limit?: number; sort?: -1 | 1; + sortBy?: "updatedAt" | "stars"; status?: Status[]; isPublic?: boolean; search?: string; }; -export type ListBlueprintsOptionsRequest = { - skip?: number; - limit?: number; - sort?: -1 | 1; - status?: Status[]; - is_public?: boolean; - search?: string; -}; - export type DownloadUrls = Record;