diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fda0d31..ea5258c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/package.json b/package.json index 06fa7783..bce60592 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/transfers/DepositProvider.ts b/src/transfers/DepositProvider.ts index 033f4008..fe845b88 100644 --- a/src/transfers/DepositProvider.ts +++ b/src/transfers/DepositProvider.ts @@ -107,6 +107,7 @@ export class DepositProvider extends TransferProvider { public async startDeposit( params: DepositRequest, shouldUseNewEndpoints: boolean = false, + headers: { [key: string]: string } = {}, ): Promise { const request: DepositRequest & { account: string } = { ...params, @@ -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, }); } diff --git a/src/transfers/TransferProvider.ts b/src/transfers/TransferProvider.ts index 4fade97a..7dd176a9 100644 --- a/src/transfers/TransferProvider.ts +++ b/src/transfers/TransferProvider.ts @@ -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, ); } diff --git a/src/transfers/WithdrawProvider.ts b/src/transfers/WithdrawProvider.ts index 8127cb98..f4bbd7bf 100644 --- a/src/transfers/WithdrawProvider.ts +++ b/src/transfers/WithdrawProvider.ts @@ -100,6 +100,7 @@ export class WithdrawProvider extends TransferProvider { public async startWithdraw( params: WithdrawRequest, shouldUseNewEndpoints: boolean = false, + headers: { [key: string]: string } = {}, ): Promise { const request: WithdrawRequest & { account: string } = { ...params, @@ -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, }); }