Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to inject extra request headers when performing requests #12

Merged
merged 2 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,9 @@ When initializing the client object, an optional config can also be specified wi
// Useful in the situations where the server connects directly to a node to fetch block info
// and would like a predictable shard for the validation of tokens
blockHashShard: 0;

// Optional, to put custom HTTP headers in the request that is made to the api
// Useful in situations where a private api is used and is protected by a JTW token
extraRequestHeaders?: { [key: string]: string };
}
```
25 changes: 2 additions & 23 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiversx/sdk-native-auth-client",
"version": "1.0.0",
"version": "1.0.1",
"description": "Native authentication client-side",
"main": "lib/src/index.js",
"types": "lib/src/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/entities/native.auth.client.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export class NativeAuthClientConfig {
expirySeconds: number = 60 * 60 * 24;
blockHashShard?: number;
gatewayUrl?: string;
extraRequestHeaders?: { [key: string]: string };
}
10 changes: 7 additions & 3 deletions src/native.auth.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class NativeAuthClient {
private async getCurrentBlockHashWithGateway(): Promise<string> {
const round = await this.getCurrentRound();
const url = `${this.config.gatewayUrl}/blocks/by-round/${round}`;
const response = await axios.get(url);
const response = await this.get(url);
const blocks = response.data.data.blocks;
const block = blocks.filter((block: { shard: number }) => block.shard === this.config.blockHashShard)[0];
return block.hash;
Expand All @@ -49,7 +49,7 @@ export class NativeAuthClient {
}

const url = `${this.config.gatewayUrl}/network/status/${this.config.blockHashShard}`;
const response = await axios.get(url);
const response = await this.get(url);
const status = response.data.data.status;
return status.erd_current_round;
}
Expand All @@ -60,7 +60,7 @@ export class NativeAuthClient {
url += `&shard=${this.config.blockHashShard}`;
}

const response = await axios.get(url);
const response = await this.get(url);
return response.data[0].hash;
}

Expand All @@ -71,4 +71,8 @@ export class NativeAuthClient {
private escape(str: string) {
return str.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
}

private async get(url: string): Promise<any> {
return await axios.get(url, { headers: this.config.extraRequestHeaders });
}
}