Skip to content
This repository was archived by the owner on Apr 23, 2021. It is now read-only.

Commit 082033b

Browse files
add model interface files
1 parent c6369e9 commit 082033b

15 files changed

+178
-113
lines changed

.editorconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; Top-most .editorcofing file
2+
root = true
3+
4+
; Unix-style newlines
5+
[*]
6+
charset = utf-8
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
indent_size = 4
11+
indent_style = space
12+
13+
[dockerfile]
14+
indent_size = 4
15+
indent_style = tab
16+
17+
[*.md]
18+
trim_trailing_whitespace = false

.vscode/launch.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616
"env": {
1717
"client_id": "test",
1818
"client_secret": "secret",
19-
"redirect_uri": "http://localhost:5000/truelayer-redirect",
20-
"nonce": "foo",
21-
"state": "bar",
22-
"scope": "offline_access info accounts transactions balance"
19+
"redirect_uri": "http://localhost:5000/truelayer-redirect"
2320
}
2421
}
2522
]
26-
}
23+
}

app.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import IOptions from "./src/IOptions";
77
const client_id: string = process.env.client_id;
88
const client_secret: string = process.env.client_secret;
99
const redirect_uri: string = process.env.redirect_uri;
10-
// const nonce: string = process.env.nonce;
11-
// const state: string = process.env.state;
12-
// const scope: string = process.env.scope;
1310

1411
// Build 'options' to pass to APIClient
1512
const options: IOptions = {
@@ -22,11 +19,12 @@ const client = new trueLayer.V1.ApiClient(options);
2219
const clientAuth = client.auth;
2320
const clientData = client.data;
2421

22+
// Create express instance
2523
const app = express();
2624

2725
// Redirect to the auth server
2826
app.get("/", (req, res) => {
29-
const authURL = clientAuth.getAuthUrl();
27+
const authURL = clientAuth.getAuthUrl("offline_access info accounts transactions balance", "abc", true);
3028
res.redirect(authURL);
3129
});
3230

@@ -51,18 +49,18 @@ app.post("/truelayer-redirect", async (req, res) => {
5149
const accountInfo = await clientData.accountInfo(tokens.access_token, accountsList[0].account_id);
5250
const transactions = await clientData.transactions(tokens.access_token, accountsList[0].account_id);
5351
const balance = await clientData.balance(tokens.access_token, accountsList[0].account_id);
52+
/* tslint:disable:no-console */
5453
console.log("Info " + JSON.stringify(info));
5554
console.log("Me " + JSON.stringify(me));
5655
console.log("Accounts " + JSON.stringify(accounts));
5756
console.log("Account info " + JSON.stringify(accountInfo));
5857
console.log("transactions " + JSON.stringify(transactions));
5958
console.log("balance " + JSON.stringify(balance));
6059

61-
6260
res.set("Content-Type", "text/plain");
6361
res.send(`You sent: ${JSON.stringify(tokens.access_token)} to Express`);
6462
});
6563

6664
app.listen(5000, () => {
67-
console.log("Example app listening on port 5000...");
65+
// console.log("Example app listening on port 5000...");
6866
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"@types/node": "^7.0.21",
1313
"@types/request-promise": "^4.1.33",
1414
"tslint": "^5.3.2",
15+
"tslint-eslint-rules": "^4.1.0",
1516
"typescript": "^2.3.3"
1617
},
1718
"dependencies": {

src/IOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ interface IOptions {
66
redirect_uri: string;
77
}
88

9-
export default IOptions;
9+
export default IOptions;

src/auth.ts

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,29 @@ export default class Auth {
2727
/**
2828
* Builds a correctly formatted authentication url
2929
*
30-
* @param {any} [scope=this.default_scope]
31-
* @param {string} [nonce=this.default_nonce]
32-
* @param {string} [state=this.default_state]
33-
* @param {boolean} [mock=false]
30+
* @param {string} scope
31+
* @param {string} nonce
32+
* @param {boolean} [mock=C.MOCK]
33+
* @param {string} [state]
3434
* @returns {string}
35-
*
36-
* @memberof Auth
3735
*/
38-
// TODO: remove nonce and state default
39-
public getAuthUrl(scope = C.SCOPE, nonce: string = C.NONCE, state?: string, mock: boolean = C.MOCK): string {
40-
36+
public getAuthUrl(scope: string, nonce: string, mock: boolean = C.MOCK, state?: string): string {
4137
return `https://${C.AUTH_HOST}/?` +
42-
`response_type=code&` +
43-
`response_mode=form_post&` +
44-
`client_id=${this.options.client_id}&` +
45-
`redirect_uri=${this.options.redirect_uri}&` +
46-
`scope=${scope}&` +
47-
`nonce=${nonce}&` +
48-
`state=${state}&` +
49-
`enable_mock=${mock}`;
50-
};
38+
`response_type=code&` +
39+
`response_mode=form_post&` +
40+
`client_id=${this.options.client_id}&` +
41+
`redirect_uri=${this.options.redirect_uri}&` +
42+
`scope=${scope}&` +
43+
`nonce=${nonce}&` +
44+
`state=${state}&` +
45+
`enable_mock=${mock}`;
46+
}
5147

5248
/**
5349
* Exchanges an auth code for an access token
5450
*
5551
* @param {string} code
5652
* @returns {Promise<IAccessTokens>}
57-
*
58-
* @memberof Auth
5953
*/
6054
public async exchangeCodeForToken(code: string): Promise<IAccessTokens> {
6155
const requestOptions: request.Options = {
@@ -79,15 +73,13 @@ export default class Auth {
7973
access_token: parsedResponse.access_token,
8074
refresh_token: parsedResponse.refresh_token
8175
};
82-
};
76+
}
8377

8478
/**
8579
* Exchanges a refresh token for a fresh access token
8680
*
8781
* @param {string} refreshToken
8882
* @returns {Promise<IAccessTokens>}
89-
*
90-
* @memberof Auth
9183
*/
9284
public async refreshAccessToken(refreshToken: string): Promise<IAccessTokens> {
9385
const requestOptions: request.Options = {

src/constants.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@ export default class Constants {
22
// Constants
33
public static readonly AUTH_HOST: string = "auth.truelayer.com";
44
public static readonly MOCK: boolean = true;
5-
public static readonly NONCE: string = "foo";
6-
public static readonly SCOPE: string = "offline_access info accounts transactions balance";
7-
}
5+
}

src/data.ts

Lines changed: 35 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
// Imports
12
import * as request from "request-promise";
23
import IOptions from "./IOptions";
34
import C from "./constants";
45

6+
// Endpoint interfaces
7+
import IAccount from "./model/info";
8+
import IBalance from "./model/info";
9+
import IInfo from "./model/info";
10+
import IMe from "./model/info";
11+
import ITransaction from "./model/info";
12+
513
interface IResponse<T> {
614
success: boolean;
715
error?: IError;
@@ -13,63 +21,6 @@ interface IError {
1321
message?: string;
1422
}
1523

16-
interface IInfo {
17-
full_name: string;
18-
update_timestamp?: string;
19-
date_of_birth?: string;
20-
addresses?: [IAddressInfo];
21-
emails?: [string];
22-
phones?: [string];
23-
}
24-
25-
interface IAddressInfo {
26-
address?: string;
27-
city?: string;
28-
state?: string;
29-
zip?: string;
30-
country?: string;
31-
}
32-
33-
interface IAccount {
34-
update_timestamp: string;
35-
account_id: string;
36-
account_type: string;
37-
display_name?: string;
38-
description: string;
39-
currency: string;
40-
account_number: IAccountNumber;
41-
}
42-
43-
interface IAccountNumber {
44-
iban: string;
45-
swift_bic: string;
46-
number: string;
47-
sort_code: string;
48-
}
49-
50-
interface IMe {
51-
provider_id: string;
52-
credentials_id: string;
53-
client_id: string;
54-
}
55-
56-
interface ITransaction {
57-
timestamp: string;
58-
description: string;
59-
transaction_type: string;
60-
amount: number;
61-
currency: string;
62-
balance: IBalance;
63-
meta: object;
64-
}
65-
66-
interface IBalance {
67-
currency: string;
68-
available: number;
69-
current: number;
70-
update_timestamp: string;
71-
}
72-
7324
export default class Data {
7425

7526
// Private
@@ -80,6 +31,17 @@ export default class Data {
8031
this.options = options;
8132
}
8233

34+
/**
35+
* Generic api calling function
36+
*
37+
* @private
38+
* @template T
39+
* @param {string} accessToken
40+
* @param {string} path
41+
* @returns {Promise<IResponse<T>>}
42+
*
43+
* @memberof Data
44+
*/
8345
private async callAPI<T>(accessToken: string, path: string): Promise<IResponse<T>> {
8446
const requestOptions: request.Options = {
8547
uri: path,
@@ -96,60 +58,66 @@ export default class Data {
9658

9759
/**
9860
* Call to /info API.
99-
* @param accessToken
100-
* @returns {Promise<IResponse>}
61+
*
62+
* @param {string} accessToken
63+
* @returns {Promise<IResponse<IInfo>>}
10164
*/
102-
// TODO handle input validation and errors
65+
// TODO: handle input validation and errors
10366
public async info(accessToken: string): Promise<IResponse<IInfo>> {
104-
return this.callAPI<IInfo>(accessToken, `https://api.truelayer.com/data/v1/info`);
67+
return this.callAPI<IInfo>(accessToken, `https://${C.AUTH_HOST}/data/v1/info`);
10568
}
10669

10770
/**
10871
* Call to /me API.
72+
*
10973
* @param accessToken
11074
* @returns {Promise<IResponse<IMe>>}
11175
*/
11276
public async me(accessToken: string): Promise<IResponse<IMe>> {
113-
return this.callAPI<IMe>(accessToken, `https://api.truelayer.com/data/v1/me`);
77+
return this.callAPI<IMe>(accessToken, `https://${C.AUTH_HOST}/data/v1/me`);
11478
}
11579

11680
/**
11781
* Call to /accounts API.
82+
*
11883
* @param accessToken
11984
* @returns {Promise<IResponse<IAccount>>}
12085
*/
12186
public async accounts(accessToken: string): Promise<IResponse<IAccount>> {
122-
return this.callAPI<IAccount>(accessToken, `https://api.truelayer.com/data/v1/accounts`);
87+
return this.callAPI<IAccount>(accessToken, `https://${C.AUTH_HOST}/data/v1/accounts`);
12388
}
12489

12590
/**
12691
* Call to /accounts/account_id API.
92+
*
12793
* @param accessToken
12894
* @param accountId
12995
* @returns {Promise<IResponse<IAccount>>}
13096
*/
13197
public async accountInfo(accessToken: string, accountId: string): Promise<IResponse<IAccount>> {
132-
return this.callAPI<IAccount>(accessToken, `https://api.truelayer.com/data/v1/accounts/${accountId}`);
98+
return this.callAPI<IAccount>(accessToken, `https://${C.AUTH_HOST}/data/v1/accounts/${accountId}`);
13399
}
134100

135101
/**
136102
* Call to /accounts/account_id/transactions API
103+
*
137104
* @param accessToken
138105
* @param accountId
139106
* @returns {Promise<IResponse<ITransaction>>}
140107
*/
141-
// // TODO add to from params
142108
public async transactions(accessToken: string, accountId: string): Promise<IResponse<ITransaction>> {
143-
return this.callAPI<ITransaction>(accessToken, `https://api.truelayer.com/data/v1/accounts/${accountId}/transactions`);
109+
// TODO add to from params
110+
return this.callAPI<ITransaction>(accessToken, `https://${C.AUTH_HOST}/data/v1/accounts/${accountId}/transactions`);
144111
}
145112

146113
/**
147114
* Call to /accounts/account_id/balance API
115+
*
148116
* @param accessToken
149117
* @param accountId
150118
* @returns {Promise<IResponse<IBalance>>}
151119
*/
152120
public async balance(accessToken: string, accountId: string): Promise<IResponse<IBalance>> {
153-
return this.callAPI<IBalance>(accessToken, `https://api.truelayer.com/data/v1/accounts/${accountId}/balance`);
121+
return this.callAPI<IBalance>(accessToken, `https://${C.AUTH_HOST}/data/v1/accounts/${accountId}/balance`);
154122
}
155123
}

src/model/account.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
interface IAccount {
2+
update_timestamp: string;
3+
account_id: string;
4+
account_type: string;
5+
display_name?: string;
6+
description: string;
7+
currency: string;
8+
account_number: IAccountNumber;
9+
}
10+
11+
interface IAccountNumber {
12+
iban: string;
13+
swift_bic: string;
14+
number: string;
15+
sort_code: string;
16+
}
17+
18+
export default IAccount;

src/model/balance.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
interface IBalance {
2+
currency: string;
3+
available: number;
4+
current: number;
5+
update_timestamp: string;
6+
}
7+
8+
export default IBalance;

src/model/info.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
interface IInfo {
2+
full_name: string;
3+
update_timestamp?: string;
4+
date_of_birth?: string;
5+
addresses?: [IAddressInfo];
6+
emails?: [string];
7+
phones?: [string];
8+
}
9+
10+
interface IAddressInfo {
11+
address?: string;
12+
city?: string;
13+
state?: string;
14+
zip?: string;
15+
country?: string;
16+
}
17+
18+
export default IInfo;

0 commit comments

Comments
 (0)