Skip to content

Commit

Permalink
Merge pull request ProvableHQ#876 from Pauan/feat/header
Browse files Browse the repository at this point in the history
Adding in new X-Aleo-SDK-Version header
  • Loading branch information
Pauan authored May 15, 2024
2 parents 4cb84a6 + c25c41d commit 2781e07
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 10 deletions.
1 change: 1 addition & 0 deletions sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"sync-request": "^6.1.0"
},
"devDependencies": {
"@rollup/plugin-replace": "^5.0.5",
"@types/jest": "^29.5.5",
"@types/mime": "^3.0.1",
"@typescript-eslint/eslint-plugin": "^5.41.0",
Expand Down
9 changes: 9 additions & 0 deletions sdk/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import typescript from "rollup-plugin-typescript2";
import replace from "@rollup/plugin-replace";
import $package from "./package.json" assert { type: "json" };

export default {
input: {
Expand All @@ -23,6 +25,13 @@ export default {
"@aleohq/wasm",
],
plugins: [
replace({
preventAssignment: true,
delimiters: ['', ''],
values: {
'%%VERSION%%': $package.version,
},
}),
typescript({
tsconfig: "tsconfig.json",
clean: true,
Expand Down
29 changes: 24 additions & 5 deletions sdk/src/network-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import {

type ProgramImports = { [key: string]: string | Program };

interface AleoNetworkClientOptions {
headers?: { [key: string]: string };
}

/**
* Client library that encapsulates REST calls to publicly exposed endpoints of Aleo nodes. The methods provided in this
* allow users to query public information from the Aleo blockchain and submit transactions to the network.
Expand All @@ -27,10 +31,21 @@ type ProgramImports = { [key: string]: string | Program };
*/
class AleoNetworkClient {
host: string;
headers: { [key: string]: string };
account: Account | undefined;

constructor(host: string) {
constructor(host: string, options?: AleoNetworkClientOptions) {
this.host = host + "/testnet3";

if (options && options.headers) {
this.headers = options.headers;

} else {
this.headers = {
// This is replaced by the actual version by a Rollup plugin
"X-Aleo-SDK-Version": "%%VERSION%%",
};
}
}

/**
Expand Down Expand Up @@ -69,8 +84,12 @@ class AleoNetworkClient {
url = "/",
): Promise<Type> {
try {
const response = await get(this.host + url);
const response = await get(this.host + url, {
headers: this.headers
});

return await response.json();

} catch (error) {
throw new Error("Error fetching data.");
}
Expand Down Expand Up @@ -627,9 +646,9 @@ class AleoNetworkClient {
try {
const response = await post(this.host + "/transaction/broadcast", {
body: transaction_string,
headers: {
headers: Object.assign({}, this.headers, {
"Content-Type": "application/json",
},
}),
});

try {
Expand All @@ -644,4 +663,4 @@ class AleoNetworkClient {
}
}

export { AleoNetworkClient, ProgramImports }
export { AleoNetworkClient, AleoNetworkClientOptions, ProgramImports }
4 changes: 2 additions & 2 deletions sdk/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export async function get(url: URL | string) {
const response = await fetch(url);
export async function get(url: URL | string, options?: RequestInit) {
const response = await fetch(url, options);

if (!response.ok) {
throw new Error(response.status + " could not get URL " + url);
Expand Down
26 changes: 26 additions & 0 deletions sdk/tests/network-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ jest.retryTimes(3);

describe('NodeConnection', () => {
let connection: AleoNetworkClient;
let windowFetchSpy: jest.Spied<typeof fetch>;

beforeEach(() => {
connection = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
windowFetchSpy = jest.spyOn(globalThis, 'fetch');
});

afterEach(() => {
jest.restoreAllMocks();
windowFetchSpy = null as any;
});

describe('setAccount', () => {
Expand Down Expand Up @@ -76,6 +83,25 @@ describe('NodeConnection', () => {
const latestBlock = await connection.getLatestBlock();
expect(typeof (latestBlock as Block).block_hash).toBe('string');
}, 60000);

it('should set the X-Aleo-SDK-Version header', async () => {
expect(windowFetchSpy.mock.calls).toStrictEqual([]);

await connection.getLatestBlock();

expect(windowFetchSpy.mock.calls).toStrictEqual([
[
"https://api.explorer.aleo.org/v1/testnet3/latest/block",
{
"headers": {
// @TODO: Run the Jest tests on the compiled Rollup code,
// so that way the version is properly replaced.
"X-Aleo-SDK-Version": "%%VERSION%%"
}
}
],
]);
}, 60000);
});

describe('getLatestCommittee', () => {
Expand Down
39 changes: 36 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2088,6 +2088,14 @@
"@rollup/pluginutils" "^5.0.1"
magic-string "^0.30.3"

"@rollup/plugin-replace@^5.0.5":
version "5.0.5"
resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-5.0.5.tgz#33d5653dce6d03cb24ef98bef7f6d25b57faefdf"
integrity sha512-rYO4fOi8lMaTg/z5Jb+hKnrHHVn8j2lwkqwyS4kTRhKyWOLf2wST2sWXr4WzWiTcoHTp2sTjqUbqIj2E39slKQ==
dependencies:
"@rollup/pluginutils" "^5.0.1"
magic-string "^0.30.3"

"@rollup/pluginutils@^4.1.2":
version "4.2.1"
resolved "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-4.2.1.tgz"
Expand Down Expand Up @@ -8816,7 +8824,16 @@ string-length@^4.0.1:
char-regex "^1.0.2"
strip-ansi "^6.0.0"

"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -8883,7 +8900,14 @@ string_decoder@^1.1.1, string_decoder@~1.1.1:
dependencies:
safe-buffer "~5.1.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
version "6.0.1"
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand Down Expand Up @@ -9782,7 +9806,16 @@ [email protected]:
resolved "https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.2.tgz"
integrity sha512-xSBsCeh+g+dinoBv3GAOWM4LcVVO68wLXRanibtBSdUvkGWQRGeE9P7IwU9EmDDi4jA6L44lz15CGMwdw9N5+Q==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
version "7.0.0"
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand Down

0 comments on commit 2781e07

Please sign in to comment.