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; } public async getTinkHealthyBoolean(): Promise { const response = await plugins.smartrequest.request( 'https://api.tink.com/api/v1/monitoring/healthy', {} ); return response.body === 'ok'; } // the request method for tink respecting platform specific stuff // e.g. certain headers if needed public async request(optionsArg: { urlArg: string, methodArg: 'POST' | 'GET', scopeArg: string, payloadArg: any, externalUserId: string }) { // check health if (!(await this.getTinkHealthyBoolean())) { throw new Error('TINK is not healthy right now. Please try again later.'); } else { console.log('tink is healthy, continuing...'); } // lets get an accessToken for the request const response = await plugins.smartrequest.postFormDataUrlEncoded( `${this.apiBaseUrl}/api/v1/oauth/token`, {}, [ { key: 'client_id', content: this.clientId, }, { key: 'client_secret', content: this.clientSecret, }, { key: 'grant_type', content: 'client_credentials', }, { key: 'scope', content: optionsArg.scopeArg, }, ] ); 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); } }