Skip to content

Commit

Permalink
Possibility to inject extra request headers when performing requests (#…
Browse files Browse the repository at this point in the history
…12)

* add extra request headers

* update readme
  • Loading branch information
gabrielmatei committed Mar 17, 2023
1 parent 3dcb878 commit a2fb20f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 27 deletions.
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 });
}
}

0 comments on commit a2fb20f

Please sign in to comment.