Skip to content

Commit

Permalink
[ts sdk-v2] Mini get/post request refactor (aptos-labs#10380)
Browse files Browse the repository at this point in the history
  • Loading branch information
heliuchuan authored and Poytr1 committed Oct 4, 2023
1 parent ac032ad commit 7f34101
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
6 changes: 3 additions & 3 deletions ecosystem/typescript/sdk_v2/src/client/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type GetRequestOptions = {
overrides?: ClientConfig;
};

export type GetAptosFullNodeRequestOptions = Omit<GetRequestOptions, "type">;
export type GetAptosRequestOptions = Omit<GetRequestOptions, "type">;

/**
* Main function to do a Get request
Expand Down Expand Up @@ -72,14 +72,14 @@ export async function get<Req, Res>(options: GetRequestOptions): Promise<AptosRe
}

export async function getAptosFullNode<Req, Res>(
options: GetAptosFullNodeRequestOptions,
options: GetAptosRequestOptions,
): Promise<AptosResponse<Req, Res>> {
return get<Req, Res>({ ...options, type: AptosApiType.FULLNODE });
}

/// This function is a helper for paginating using a function wrapping an API
export async function paginateWithCursor<Req extends Record<string, any>, Res extends any[]>(
options: GetAptosFullNodeRequestOptions,
options: GetAptosRequestOptions,
): Promise<Res> {
const out = [];
let cursor: string | undefined;
Expand Down
16 changes: 14 additions & 2 deletions ecosystem/typescript/sdk_v2/src/client/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export type PostRequestOptions = {
overrides?: ClientConfig;
};

export type PostAptosFullNodeRequestOptions = Omit<PostRequestOptions, "type">;
export type PostAptosRequestOptions = Omit<PostRequestOptions, "type">;

/**
* Main function to do a Post request
Expand Down Expand Up @@ -76,7 +76,19 @@ export async function post<Req, Res>(options: PostRequestOptions): Promise<Aptos
}

export async function postAptosFullNode<Req, Res>(
options: PostAptosFullNodeRequestOptions,
options: PostAptosRequestOptions,
): Promise<AptosResponse<Req, Res>> {
return post<Req, Res>({ ...options, type: AptosApiType.FULLNODE });
}

export async function postAptosIndexer<Req, Res>(
options: PostAptosRequestOptions,
): Promise<AptosResponse<Req, Res>> {
return post<Req, Res>({ ...options, type: AptosApiType.INDEXER });
}

export async function postAptosFaucet<Req, Res>(
options: PostAptosRequestOptions,
): Promise<AptosResponse<Req, Res>> {
return post<Req, Res>({ ...options, type: AptosApiType.FAUCET });
}
6 changes: 2 additions & 4 deletions ecosystem/typescript/sdk_v2/src/internal/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
*/

import { AptosConfig } from "../api/aptos_config";
import { getAptosFullNode, post, postAptosFullNode } from "../client";
import { getAptosFullNode, postAptosFullNode, postAptosIndexer } from "../client";
import { Block, GraphqlQuery, LedgerInfo, LedgerVersion, MoveValue, TableItemRequest, ViewRequest } from "../types";
import { AptosApiType } from "../utils/const";

export async function getLedgerInfo(args: { aptosConfig: AptosConfig }): Promise<LedgerInfo> {
const { aptosConfig } = args;
Expand Down Expand Up @@ -89,9 +88,8 @@ export async function queryIndexer<T>(args: {
originMethod?: string;
}): Promise<T> {
const { aptosConfig, query, originMethod } = args;
const { data } = await post<GraphqlQuery, T>({
const { data } = await postAptosIndexer<GraphqlQuery, T>({
aptosConfig,
type: AptosApiType.INDEXER,
originMethod: originMethod ?? "queryIndexer",
path: "",
body: query,
Expand Down

0 comments on commit 7f34101

Please sign in to comment.