This commit is contained in:
2025-07-18 10:31:12 +00:00
parent 5abc4e7976
commit 193524f15c
33 changed files with 35866 additions and 3864 deletions

View File

@@ -1,11 +1,14 @@
import * as plugins from './bunq.plugins';
import * as paths from './bunq.paths';
import { BunqApiContext } from './bunq.classes.apicontext';
import { BunqMonetaryAccount } from './bunq.classes.monetaryaccount';
import { BunqUser } from './bunq.classes.user';
import { IBunqSessionServerResponse } from './bunq.interfaces';
export interface IBunqConstructorOptions {
deviceName: string;
apiKey: string;
environment: 'SANDBOX' | 'PRODUCTION';
permittedIps?: string[];
}
/**
@@ -13,100 +16,137 @@ export interface IBunqConstructorOptions {
*/
export class BunqAccount {
public options: IBunqConstructorOptions;
public bunqJSClient: plugins.bunqCommunityClient.default;
public encryptionKey: string;
public permittedIps = []; // bunq will use the current ip if omitted
/**
* user id is needed for doing stuff like listing accounts;
*/
public apiContext: BunqApiContext;
public userId: number;
public userType: 'UserPerson' | 'UserCompany' | 'UserApiKey';
private bunqUser: BunqUser;
constructor(optionsArg: IBunqConstructorOptions) {
this.options = optionsArg;
}
/**
* Initialize the bunq account
*/
public async init() {
this.encryptionKey = plugins.smartcrypto.nodeForge.util.bytesToHex(
plugins.smartcrypto.nodeForge.random.getBytesSync(16)
);
// Create API context
this.apiContext = new BunqApiContext({
apiKey: this.options.apiKey,
environment: this.options.environment,
deviceDescription: this.options.deviceName,
permittedIps: this.options.permittedIps
});
// lets setup bunq client
await plugins.smartfile.fs.ensureDir(paths.nogitDir);
await plugins.smartfile.fs.ensureFile(paths.bunqJsonProductionFile, '{}');
await plugins.smartfile.fs.ensureFile(paths.bunqJsonSandboxFile, '{}');
let apiKey: string;
// Initialize API context (handles installation, device registration, session)
await this.apiContext.init();
if (this.options.environment === 'SANDBOX') {
this.bunqJSClient = new plugins.bunqCommunityClient.default(
plugins.JSONFileStore(paths.bunqJsonSandboxFile)
);
apiKey = await this.bunqJSClient.api.sandboxUser.post();
console.log(apiKey);
} else {
this.bunqJSClient = new plugins.bunqCommunityClient.default(
plugins.JSONFileStore(paths.bunqJsonProductionFile)
);
apiKey = this.options.apiKey;
}
// run the bunq application with our API key
await this.bunqJSClient.run(
apiKey,
this.permittedIps,
this.options.environment,
this.encryptionKey
);
// install a new keypair
await this.bunqJSClient.install();
// register this device
await this.bunqJSClient.registerDevice(this.options.deviceName);
// register a new session
await this.bunqJSClient.registerSession();
await this.getUserId();
// Create user instance
this.bunqUser = new BunqUser(this.apiContext);
// Get user info
await this.getUserInfo();
}
/**
* lists all users
* Get user information and ID
*/
private async getUserId() {
const users = await this.bunqJSClient.api.user.list();
if (users.UserPerson) {
this.userId = users.UserPerson.id;
} else if (users.UserApiKey) {
this.userId = users.UserApiKey.id;
} else if (users.UserCompany) {
this.userId = users.UserCompany.id;
private async getUserInfo() {
const userInfo = await this.bunqUser.getInfo();
if (userInfo.UserPerson) {
this.userId = userInfo.UserPerson.id;
this.userType = 'UserPerson';
} else if (userInfo.UserCompany) {
this.userId = userInfo.UserCompany.id;
this.userType = 'UserCompany';
} else if (userInfo.UserApiKey) {
this.userId = userInfo.UserApiKey.id;
this.userType = 'UserApiKey';
} else {
console.log('could not determine user id');
throw new Error('Could not determine user type');
}
}
public async getAccounts() {
const apiMonetaryAccounts = await this.bunqJSClient.api.monetaryAccount
.list(this.userId, {})
.catch((e) => {
console.log(e);
});
/**
* Get all monetary accounts
*/
public async getAccounts(): Promise<BunqMonetaryAccount[]> {
await this.apiContext.ensureValidSession();
const response = await this.apiContext.getHttpClient().list(
`/v1/user/${this.userId}/monetary-account`
);
const accountsArray: BunqMonetaryAccount[] = [];
for (const apiAccount of apiMonetaryAccounts) {
accountsArray.push(BunqMonetaryAccount.fromAPIObject(this, apiAccount));
if (response.Response) {
for (const apiAccount of response.Response) {
accountsArray.push(BunqMonetaryAccount.fromAPIObject(this, apiAccount));
}
}
return accountsArray;
}
/**
* stops the instance
* Get a specific monetary account
*/
public async getAccount(accountId: number): Promise<BunqMonetaryAccount> {
await this.apiContext.ensureValidSession();
const response = await this.apiContext.getHttpClient().get(
`/v1/user/${this.userId}/monetary-account/${accountId}`
);
if (response.Response && response.Response[0]) {
return BunqMonetaryAccount.fromAPIObject(this, response.Response[0]);
}
throw new Error('Account not found');
}
/**
* Create a sandbox user (only works in sandbox environment)
*/
public async createSandboxUser(): Promise<string> {
if (this.options.environment !== 'SANDBOX') {
throw new Error('Creating sandbox users only works in sandbox environment');
}
const response = await this.apiContext.getHttpClient().post(
'/v1/sandbox-user-person',
{}
);
if (response.Response && response.Response[0] && response.Response[0].ApiKey) {
return response.Response[0].ApiKey.api_key;
}
throw new Error('Failed to create sandbox user');
}
/**
* Get the user instance
*/
public getUser(): BunqUser {
return this.bunqUser;
}
/**
* Get the HTTP client
*/
public getHttpClient() {
return this.apiContext.getHttpClient();
}
/**
* Stop the bunq account and clean up
*/
public async stop() {
if (this.bunqJSClient) {
this.bunqJSClient.setKeepAlive(false);
await this.bunqJSClient.destroyApiSession();
this.bunqJSClient = null;
if (this.apiContext) {
await this.apiContext.destroy();
this.apiContext = null;
}
}
}