fix(core): update

This commit is contained in:
Philipp Kunz 2022-02-18 12:55:56 +01:00
parent a0fa17fa1d
commit eb61d1abbf
4 changed files with 95 additions and 9 deletions

View File

@ -7,6 +7,16 @@ import * as tink from '../ts/index';
let tinkTestAccount: tink.TinkAccount;
tap.preTask('should delete existing users', async () => {
const preTinkAccount = tinkTestAccount = new tink.TinkAccount(
testQenv.getEnvVarOnDemand('TINK_CLIENT_ID'),
testQenv.getEnvVarOnDemand('TINK_CLIENT_SECRET')
);
expect(tinkTestAccount).toBeInstanceOf(tink.TinkAccount);
const tinkUser = new tink.TinkUser(preTinkAccount, null, 'user_1234_abc');
await tinkUser.delete();
})
tap.test('should create a valid tink account', async () => {
tinkTestAccount = new tink.TinkAccount(
testQenv.getEnvVarOnDemand('TINK_CLIENT_ID'),
@ -20,7 +30,7 @@ tap.test('should report tink as healthy', async () => {
});
tap.test('should create a valid request', async () => {
await tinkTestAccount.request('', 'POST', '', {});
await tinkTestAccount.createTinkUser("user_1234_abc");
})
tap.start();

View File

@ -1 +1,2 @@
export * from './tink.classes.tinkaccount';
export * from './tink.classes.tinkuser';

View File

@ -1,8 +1,13 @@
import * as plugins from './tink.plugins';
import { TinkUser } from './tink.classes.tinkuser'
export class TinkAccount {
private clientId: string;
private clientSecret: string;
private apiBaseUrl: string = 'https://api.tink.com';
constructor(clientIdArg: string, clientSecretArg: string) {
this.clientId = clientIdArg;
this.clientSecret = clientSecretArg;
@ -18,12 +23,13 @@ export class TinkAccount {
// the request method for tink respecting platform specific stuff
// e.g. certain headers if needed
public async request(
public async request(optionsArg: {
urlArg: string,
methodArg: 'POST' | 'GET',
scopeArg: string,
payloadArg: any
) {
payloadArg: any,
externalUserId: string
}) {
// check health
if (!(await this.getTinkHealthyBoolean())) {
throw new Error('TINK is not healthy right now. Please try again later.');
@ -32,7 +38,7 @@ export class TinkAccount {
}
// lets get an accessToken for the request
const response = await plugins.smartrequest.postFormDataUrlEncoded(
'https://api.tink.com/api/v1/oauth/token',
`${this.apiBaseUrl}/api/v1/oauth/token`,
{},
[
{
@ -49,11 +55,28 @@ export class TinkAccount {
},
{
key: 'scope',
content: 'user:read',
content: optionsArg.scopeArg,
},
]
);
console.log(response.statusCode);
console.log(response.body);
if (response.statusCode !== 200) {
throw new Error('there was an error aquiring an access token.');
}
const accessToken = response.body.access_token;
const response2 = await plugins.smartrequest.request(`${this.apiBaseUrl}${optionsArg.urlArg}`, {
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
},
method: optionsArg.methodArg,
requestBody: JSON.stringify(optionsArg.payloadArg)
})
console.log(response2.statusCode);
return response2.body;
}
public async createTinkUser(externalUserIdArg: string) {
const tinkuser = await TinkUser.createNewTinkUser(this, externalUserIdArg);
}
}

View File

@ -1 +1,53 @@
import * as plugins from './tink.plugins';
import { TinkAccount } from './tink.classes.tinkaccount';
export class TinkUser {
// STATIC
public static async createNewTinkUser (tinkaccountArg: TinkAccount, externalUserIdArg: string) {
const response = await tinkaccountArg.request({
externalUserId: null,
urlArg: '/api/v1/user/create',
methodArg: 'POST',
scopeArg: 'user:create',
payloadArg: {
"external_user_id": externalUserIdArg,
"market": "GB",
"locale": "en_US"
}
});
console.log(response);
}
// INSTANCE
public tinkAccountRef: TinkAccount;
public tinkUserId: string;
public externalUserIdArg: string;
public authorizationToken: string
constructor(tinkAccountrefArg: TinkAccount, tinkUserIdArg: string, externalUserIdArg: string) {
this.tinkAccountRef = tinkAccountrefArg;
this.tinkUserId = tinkUserIdArg;
this.externalUserIdArg = externalUserIdArg;
}
public async authorize() {
}
/**
* deletes the user
*/
public async delete() {
const response = await this.tinkAccountRef.request({
externalUserId: this.externalUserIdArg,
methodArg: 'POST',
payloadArg: {},
scopeArg: 'user:delete',
urlArg: '/api/v1/user/delete'
});
console.log(response);
}
}