Skip to content

Commit

Permalink
support custom client
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmaayan committed Oct 24, 2023
1 parent 50ec4d1 commit 20bc900
Show file tree
Hide file tree
Showing 10 changed files with 486 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to the Aptos TypeScript SDK will be captured in this file. T
- [`Breaking`] Changed all instances of `arguments` to `functionArguments` to avoid the reserved keyword in `strict` mode.
- Support publish move module API function
- Fix client config not being added to the request
- Support to config a custom client instance

## 0.0.0 (2023-10-18)

Expand Down
95 changes: 95 additions & 0 deletions examples/typescript/custom_client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Example to demostrate how one can config the SDK to use a custom client.
*
* The SDK by default supports axios client on web environment and go client on node environment
* with http2 support.
*
* Need to provide a function with the signature
* `<Req, Res>(requestOptions: ClientRequest<Req>): Promise<ClientResponse<Res>>;`
*
*/
import { Aptos, AptosConfig, ClientResponse, ClientRequest } from "aptos";
const superagent = require("superagent");

export async function fetchCustomClient<Req, Res>(requestOptions: ClientRequest<Req>): Promise<ClientResponse<Res>> {
const { params, method, url, headers, body } = requestOptions;

const customHeaders: any = {
...headers,
customClient: true,
};

const request = {
headers: customHeaders,
body: JSON.stringify(body),
method,
};

const response = await fetch(`${url}?${params}`, request);
const data = await response.json();
return {
status: response.status,
statusText: response.statusText,
data,
headers: response.headers,
config: response,
request,
};
}

export async function superagnetCustomClient<Req, Res>(
requestOptions: ClientRequest<Req>,
): Promise<ClientResponse<Res>> {
const { params, method, url, headers, body } = requestOptions;

const customHeaders: any = {
...headers,
customClient: true,
};

const request = {
headers: customHeaders,
body: JSON.stringify(body),
method,
};

const response = await superagent.get(`${url}?${params}`, request);
return {
status: response.status,
statusText: response.statusText,
data: response.text,
headers: response.headers,
config: response,
request,
};
}

const example = async () => {
console.log("This example demostrate how one can config for a custom client ot be used by the SDK");

async function withSuperagentClient() {
const config = new AptosConfig({ client: { provider: superagnetCustomClient } });
const aptos = new Aptos(config);

console.log(`\nclient being used ${config.client.provider.name}`);

const chainInfo = await aptos.getLedgerInfo();
console.log(`${chainInfo}`);
}

async function withFetchClient() {
const config = new AptosConfig({ client: { provider: fetchCustomClient } });
const aptos = new Aptos(config);

console.log(`\nclient being used ${config.client.provider.name}`);

const chainInfo = await aptos.getLedgerInfo();
console.log(`${JSON.stringify(chainInfo)}`);
}

// Call the inner functions
await withSuperagentClient();
await withFetchClient();
};

example();
6 changes: 4 additions & 2 deletions examples/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
"simple_transfer": "ts-node simple_transfer.ts",
"mint_nft": "ts-node mint_nft.ts",
"simple_sponsored_transaction": "ts-node simple_sponsored_transaction.ts",
"transfer_coin": "ts-node transfer_coin.ts"
"transfer_coin": "ts-node transfer_coin.ts",
"custom_client": "ts-node custom_client.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"aptos": "link:../.."
"aptos": "link:../..",
"superagent": "^8.1.2"
},
"devDependencies": {
"@types/node": "18.6.2",
Expand Down
237 changes: 237 additions & 0 deletions examples/typescript/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 20bc900

Please sign in to comment.