-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
486 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.