Skip to content
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ config.debugEnabled = true;

This will print out all sent messages and received responses to the console in a structured format.

### Timeout for requests to the bank

By default a request waits for the bank's answer as long as the connection lasts. To give up on a bank that doesn't answer, set a timeout in milliseconds; it applies to every request of every client:

```typescript
import { HttpClient } from 'lib-fints';

HttpClient.timeoutMs = 30_000;
```

A request that takes longer is aborted with a `TimeoutError`.

## Supported Transactions

The following table shows all transactions supported by the FinTSClient interface:
Expand Down
9 changes: 9 additions & 0 deletions src/httpClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { type CustomerMessage, type CustomerOrderMessage, Message } from './message.js';

export class HttpClient {
/**
* How long to wait for the bank's answer to one message, in milliseconds. Unset (the
* default) waits as long as the connection lasts; set it so that a bank that doesn't
* answer can't keep a dialog (and the caller) waiting forever.
*/
static timeoutMs: number | undefined = undefined;

constructor(
public url: string,
public debug = false,
Expand All @@ -25,6 +32,8 @@ export class HttpClient {
method: 'POST',
headers: { 'Content-Type': 'text/plain' },
body: requestBuffer.toString('base64'),
signal:
HttpClient.timeoutMs === undefined ? undefined : AbortSignal.timeout(HttpClient.timeoutMs),
});

if (response.ok) {
Expand Down
34 changes: 34 additions & 0 deletions src/tests/httpClientTimeout.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { HttpClient } from '../httpClient.js';
import type { CustomerMessage } from '../message.js';

const message = {
encode: () => "HNHBK:1:3'",
toString: () => 'HNHBK',
} as unknown as CustomerMessage;

describe('HttpClient.timeoutMs', () => {
afterEach(() => {
vi.unstubAllGlobals();
HttpClient.timeoutMs = undefined;
});

it('waits without a timeout unless one is set', async () => {
const fetchMock = vi.fn().mockRejectedValue(new Error('offline'));
vi.stubGlobal('fetch', fetchMock);
await expect(new HttpClient('http://bank.example').sendMessage(message)).rejects.toThrow(
'offline',
);
expect(fetchMock.mock.calls[0][1].signal).toBeUndefined();
});

it('gives every request the timeout that was set', async () => {
const fetchMock = vi.fn().mockRejectedValue(new DOMException('timed out', 'TimeoutError'));
vi.stubGlobal('fetch', fetchMock);
HttpClient.timeoutMs = 30_000;
await expect(new HttpClient('http://bank.example').sendMessage(message)).rejects.toThrow(
'timed out',
);
expect(fetchMock.mock.calls[0][1].signal).toBeInstanceOf(AbortSignal);
});
});