Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
[Transfer] Headers (#162)
Browse files Browse the repository at this point in the history
Add support for arbitrary headers (except Authorization ones) when starting deposits and withdrawals.
  • Loading branch information
Morley Zhi authored Jul 16, 2020
1 parent 4b9745a commit f66d7b7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Other changes:
third, optional parameter for
[ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) language codes. The
default value is "en".
- [Transfers] `DepositProvider#startDeposit` and
`WithdrawProvider#startWithdraw` can now add arbitrary headers to their
requests.

## [v0.0.9-rc.1](https://github.com/stellar/js-stellar-wallets/compare/v0.0.8-rc.1...v0.0.9-rc.1)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stellar/wallet-sdk",
"version": "0.1.0-rc.4",
"version": "0.1.0-rc.5",
"description": "Libraries to help you write Stellar-enabled wallets in Javascript",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
5 changes: 3 additions & 2 deletions src/transfers/DepositProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export class DepositProvider extends TransferProvider {
public async startDeposit(
params: DepositRequest,
shouldUseNewEndpoints: boolean = false,
headers: { [key: string]: string } = {},
): Promise<TransferResponse> {
const request: DepositRequest & { account: string } = {
...params,
Expand All @@ -130,13 +131,13 @@ export class DepositProvider extends TransferProvider {
{
method: "POST",
body,
headers: isAuthRequired ? this.getHeaders() : undefined,
headers: isAuthRequired ? this.getHeaders(headers) : undefined,
},
);
} else {
const qs = queryString.stringify(request);
response = await fetch(`${this.transferServer}/deposit?${qs}`, {
headers: isAuthRequired ? this.getHeaders() : undefined,
headers: isAuthRequired ? this.getHeaders(headers) : undefined,
});
}

Expand Down
14 changes: 12 additions & 2 deletions src/transfers/TransferProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,23 @@ export abstract class TransferProvider {
}
}

protected getHeaders(): Headers {
protected getHeaders(headers: { [key: string]: string } = {}): Headers {
// throw an error if headers include Authorization
// (provided headers will be wiped out)
if (headers.Authorization && this.authToken) {
throw new Error(
"You passed an `Authorization` header that will get wiped out by" +
" SEP-10 auth",
);
}

return new Headers(
this.authToken
? {
...headers,
Authorization: `Bearer ${this.authToken}`,
}
: {},
: headers,
);
}

Expand Down
5 changes: 3 additions & 2 deletions src/transfers/WithdrawProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export class WithdrawProvider extends TransferProvider {
public async startWithdraw(
params: WithdrawRequest,
shouldUseNewEndpoints: boolean = false,
headers: { [key: string]: string } = {},
): Promise<TransferResponse> {
const request: WithdrawRequest & { account: string } = {
...params,
Expand All @@ -123,13 +124,13 @@ export class WithdrawProvider extends TransferProvider {
{
method: "POST",
body,
headers: isAuthRequired ? this.getHeaders() : undefined,
headers: isAuthRequired ? this.getHeaders(headers) : undefined,
},
);
} else {
const qs = queryString.stringify(request);
response = await fetch(`${this.transferServer}/withdraw?${qs}`, {
headers: isAuthRequired ? this.getHeaders() : undefined,
headers: isAuthRequired ? this.getHeaders(headers) : undefined,
});
}

Expand Down

0 comments on commit f66d7b7

Please sign in to comment.