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

feat: added now required api-key #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ _Get this from https://login.secure.investec.com/io/programmable-banking/cards/o

`secret` - _required_

_Get this from https://login.secure.investec.com/io/programmable-banking/cards/overview_
<br />
<br />

`apiKey` - _required_

_Get this from https://login.secure.investec.com/io/programmable-banking/cards/overview_

---
Expand Down
11 changes: 7 additions & 4 deletions deno_lib/InvestecOpenAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ class InvestecOpenAPI {
private proxyUrl?: string
private clientId?: string
private secret?: string
private apiKey?: string
private accessToken?: string
private accessTokenExpiry: number = -1
private errorCallback: (err: Error) => void = console.error

configure({ proxyUrl, clientId, secret, errorCallback }: Config) {
configure({ proxyUrl, clientId, secret, apiKey, errorCallback }: Config) {
this.proxyUrl = proxyUrl ?? ''
this.clientId = clientId
this.secret = secret
this.apiKey = apiKey
if (errorCallback) this.errorCallback = errorCallback
}

Expand Down Expand Up @@ -110,9 +112,9 @@ class InvestecOpenAPI {
}

private async getAccessToken(): Promise<string | undefined> {
if (!this.clientId || !this.secret)
if (!this.clientId || !this.secret || !this.apiKey)
throw new Error(
'Investec Open API not configured yet, please call `investecOpenAPI.configure({ ... })` first.\n `clientId` and `secret` fields are required!'
'Investec Open API not configured yet, please call `investecOpenAPI.configure({ ... })` first.\n `clientId`, `secret` and `apiKey` fields are required!'
)

if (this.accessToken && this.accessTokenExpiry > Date.now())
Expand All @@ -126,6 +128,7 @@ class InvestecOpenAPI {
headers: {
Authorization: `Basic ${btoa(this.clientId + ':' + this.secret)}`,
'Content-Type': 'application/x-www-form-urlencoded',
'x-api-Key': this.apiKey,
},
method: 'POST',
}
Expand All @@ -147,7 +150,7 @@ class InvestecOpenAPI {
this.accessTokenExpiry = Date.now() + (expires_in - 30) * 1000
return access_token
} catch (ex) {
this.errorCallback(ex)
this.errorCallback(ex as Error)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions deno_lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface Config {
errorCallback?: (error: Error) => void
clientId: string
secret: string
apiKey: string
}

export interface GetAccessTokenResponse {
Expand Down
Loading