@nomba/node-sdk 0.1.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +77 -16
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/interfaces/v1/accounts/IAccountsService.d.ts +46 -8
- package/dist/interfaces/v1/betting/IBettingService.d.ts +11 -2
- package/dist/interfaces/v1/cable/ICableService.d.ts +12 -2
- package/dist/interfaces/v1/charge/IChargeService.d.ts +80 -19
- package/dist/interfaces/v1/checkout/ICheckoutService.d.ts +64 -15
- package/dist/interfaces/v1/data/IDataService.d.ts +6 -1
- package/dist/interfaces/v1/direct-debits/IDirectDebitsService.d.ts +37 -14
- package/dist/interfaces/v1/electricity/IElectricityService.d.ts +11 -2
- package/dist/interfaces/v1/globals/collections/IGlobalCollectionsService.d.ts +22 -5
- package/dist/interfaces/v1/globals/payouts/IGlobalPayoutService.d.ts +76 -13
- package/dist/interfaces/v1/requery/IRequeryService.d.ts +19 -5
- package/dist/interfaces/v1/terminals/ITerminalsService.d.ts +43 -10
- package/dist/interfaces/v1/transactions/ITransactionsService.d.ts +71 -12
- package/dist/interfaces/v1/virtual-accounts/IVirtualAccountsService.d.ts +50 -12
- package/dist/interfaces/v1/webhooks/IWebhooksService.d.ts +29 -10
- package/dist/interfaces/v2/airtime/IAirtimeV2Service.d.ts +21 -2
- package/dist/interfaces/v2/betting/IBettingV2Service.d.ts +21 -2
- package/dist/interfaces/v2/cable/ICableV2Service.d.ts +21 -2
- package/dist/interfaces/v2/data/IDataV2Service.d.ts +21 -2
- package/dist/interfaces/v2/electricity/IElectricityV2Service.d.ts +22 -2
- package/dist/interfaces/v2/transfers/ITransfersService.d.ts +57 -6
- package/dist/lib/errors.d.ts +19 -0
- package/dist/lib/errors.js +23 -0
- package/dist/lib/outcome.d.ts +30 -5
- package/dist/lib/outcome.js +31 -6
- package/dist/lib/sdk-version.generated.d.ts +1 -1
- package/dist/lib/sdk-version.generated.js +1 -1
- package/dist/lib/util.d.ts +0 -1
- package/dist/lib/util.js +0 -6
- package/dist/nomba.d.ts +38 -5
- package/dist/nomba.js +38 -5
- package/dist/services/http/http-client.service.d.ts +40 -6
- package/dist/services/http/http-client.service.js +44 -9
- package/dist/services/v1/accounts/accounts.service.d.ts +46 -8
- package/dist/services/v1/accounts/accounts.service.js +46 -8
- package/dist/services/v1/betting/betting.service.d.ts +12 -2
- package/dist/services/v1/betting/betting.service.js +12 -2
- package/dist/services/v1/cable/cable.service.d.ts +13 -2
- package/dist/services/v1/cable/cable.service.js +13 -2
- package/dist/services/v1/charge/charge.service.d.ts +80 -19
- package/dist/services/v1/charge/charge.service.js +88 -26
- package/dist/services/v1/checkout/checkout.service.d.ts +63 -15
- package/dist/services/v1/checkout/checkout.service.js +70 -24
- package/dist/services/v1/data/data.service.d.ts +7 -1
- package/dist/services/v1/data/data.service.js +7 -1
- package/dist/services/v1/direct-debits/direct-debits.service.d.ts +38 -14
- package/dist/services/v1/direct-debits/direct-debits.service.js +39 -17
- package/dist/services/v1/electricity/electricity.service.d.ts +12 -2
- package/dist/services/v1/electricity/electricity.service.js +12 -2
- package/dist/services/v1/globals/collections.service.d.ts +23 -5
- package/dist/services/v1/globals/collections.service.js +29 -7
- package/dist/services/v1/globals/payout.service.d.ts +77 -13
- package/dist/services/v1/globals/payout.service.js +83 -17
- package/dist/services/v1/requery/requery.service.d.ts +23 -5
- package/dist/services/v1/requery/requery.service.js +23 -5
- package/dist/services/v1/terminals/terminals.service.d.ts +42 -14
- package/dist/services/v1/terminals/terminals.service.js +44 -27
- package/dist/services/v1/transactions/transactions.service.d.ts +70 -12
- package/dist/services/v1/transactions/transactions.service.js +70 -12
- package/dist/services/v1/virtual-accounts/virtual-accounts.service.d.ts +52 -12
- package/dist/services/v1/virtual-accounts/virtual-accounts.service.js +55 -15
- package/dist/services/v1/webhooks/webhooks.service.d.ts +31 -10
- package/dist/services/v1/webhooks/webhooks.service.js +35 -14
- package/dist/services/v2/airtime/airtime.service.d.ts +26 -2
- package/dist/services/v2/airtime/airtime.service.js +27 -7
- package/dist/services/v2/betting/betting.service.d.ts +27 -3
- package/dist/services/v2/betting/betting.service.js +28 -8
- package/dist/services/v2/cable/cable.service.d.ts +27 -3
- package/dist/services/v2/cable/cable.service.js +28 -8
- package/dist/services/v2/data/data.service.d.ts +26 -2
- package/dist/services/v2/data/data.service.js +27 -7
- package/dist/services/v2/electricity/electricity.service.d.ts +28 -3
- package/dist/services/v2/electricity/electricity.service.js +29 -8
- package/dist/services/v2/transfers/transfers.service.d.ts +51 -6
- package/dist/services/v2/transfers/transfers.service.js +55 -25
- package/dist/types/common/common.types.d.ts +10 -13
- package/dist/types/v1/globals/collections.types.d.ts +3 -3
- package/dist/types/v1/globals/payout.types.d.ts +3 -3
- package/package.json +1 -1
|
@@ -5,8 +5,28 @@
|
|
|
5
5
|
import type { CallOptions } from "../../../types/common/common.types.js";
|
|
6
6
|
import type { ElectricityPaymentParams, ElectricityPaymentV2Result } from "../../../types/v2/electricity/electricity.types.js";
|
|
7
7
|
export interface IElectricityV2Service {
|
|
8
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* `POST /v2/bill/electricity`. Pays an electricity bill from the parent account. Prepaid meters get the
|
|
10
|
+
* token in `meta.phcnToken`.
|
|
11
|
+
* @param params The disco, meter number and amount, and the `merchantTxRef` used for idempotency.
|
|
12
|
+
* @param options Outcome closures and per-call overrides; without them, success resolves the result and
|
|
13
|
+
* a rejection or pending outcome throws.
|
|
14
|
+
* @returns The payment result, or whatever `options.onSuccess`/`onPending`/`onFailed` returns.
|
|
15
|
+
* @throws {NombaError} The payment was rejected (4xx).
|
|
16
|
+
* @throws {OutcomeUnknownError} The outcome could not be confirmed as success or failure.
|
|
17
|
+
* @throws {UnconfirmedOutcomeError} The result is pending and no `onPending` handler was given.
|
|
18
|
+
*/
|
|
9
19
|
payElectricity<R = ElectricityPaymentV2Result>(params: ElectricityPaymentParams, options?: CallOptions<ElectricityPaymentV2Result, R>): Promise<ElectricityPaymentV2Result | R>;
|
|
10
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* `POST /v2/bill/electricity/{subAccountId}`. Pays from a sub account.
|
|
22
|
+
* @param subAccountId The sub account to pay from.
|
|
23
|
+
* @param params The disco, meter number and amount, and the `merchantTxRef` used for idempotency.
|
|
24
|
+
* @param options Outcome closures and per-call overrides; without them, success resolves the result and
|
|
25
|
+
* a rejection or pending outcome throws.
|
|
26
|
+
* @returns The payment result, or whatever `options.onSuccess`/`onPending`/`onFailed` returns.
|
|
27
|
+
* @throws {NombaError} The payment was rejected (4xx).
|
|
28
|
+
* @throws {OutcomeUnknownError} The outcome could not be confirmed as success or failure.
|
|
29
|
+
* @throws {UnconfirmedOutcomeError} The result is pending and no `onPending` handler was given.
|
|
30
|
+
*/
|
|
11
31
|
payElectricityFromSubAccount<R = ElectricityPaymentV2Result>(subAccountId: string, params: ElectricityPaymentParams, options?: CallOptions<ElectricityPaymentV2Result, R>): Promise<ElectricityPaymentV2Result | R>;
|
|
12
32
|
}
|
|
@@ -5,16 +5,67 @@
|
|
|
5
5
|
import type { CallOptions } from "../../../types/common/common.types.js";
|
|
6
6
|
import type { Bank, BankAccountLookupParams, BankAccountLookupResult, BankTransferParams, BankTransferResult, WalletTransferParams, WalletTransferResult } from "../../../types/v2/transfers/transfers.types.js";
|
|
7
7
|
export interface ITransfersService {
|
|
8
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* `GET /v1/transfers/banks`. Lists the banks usable as a transfer destination.
|
|
10
|
+
* @returns The supported banks, each with its name and code.
|
|
11
|
+
* @throws {NombaError} A non-2xx response.
|
|
12
|
+
*/
|
|
9
13
|
listBanks(): Promise<Bank[]>;
|
|
10
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* `POST /v1/transfers/bank/lookup`. Resolves a bank account number to its account name.
|
|
16
|
+
* @param params The bank code and account number to look up.
|
|
17
|
+
* @returns The resolved account name.
|
|
18
|
+
* @throws {NombaError} The lookup was rejected, e.g. an invalid account number.
|
|
19
|
+
*/
|
|
11
20
|
lookupBankAccount(params: BankAccountLookupParams): Promise<BankAccountLookupResult>;
|
|
12
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* `POST /v2/transfers/bank`. Sends a bank transfer from the parent account.
|
|
23
|
+
* @param params The destination bank, account number and amount, and the `merchantTxRef` used for
|
|
24
|
+
* idempotency.
|
|
25
|
+
* @param options Outcome closures and per-call overrides; without them, success resolves the result and
|
|
26
|
+
* a rejection or pending outcome throws.
|
|
27
|
+
* @returns The transfer result, or whatever `options.onSuccess`/`onPending`/`onFailed` returns.
|
|
28
|
+
* @throws {NombaError} The transfer was rejected (4xx) and never happened.
|
|
29
|
+
* @throws {OutcomeUnknownError} The outcome could not be confirmed as success or failure.
|
|
30
|
+
* @throws {UnconfirmedOutcomeError} The result is pending and no `onPending` handler was given.
|
|
31
|
+
*/
|
|
13
32
|
bankTransfer<R = BankTransferResult>(params: BankTransferParams, options?: CallOptions<BankTransferResult, R>): Promise<BankTransferResult | R>;
|
|
14
|
-
/**
|
|
33
|
+
/**
|
|
34
|
+
* `POST /v2/transfers/bank/{subAccountId}`. Sends a bank transfer from a sub account. You must ask
|
|
35
|
+
* Nomba to profile you for sub account transfers.
|
|
36
|
+
* @param subAccountId The sub account to transfer from.
|
|
37
|
+
* @param params The destination bank, account number and amount, and the `merchantTxRef` used for
|
|
38
|
+
* idempotency.
|
|
39
|
+
* @param options Outcome closures and per-call overrides; without them, success resolves the result and
|
|
40
|
+
* a rejection or pending outcome throws.
|
|
41
|
+
* @returns The transfer result, or whatever `options.onSuccess`/`onPending`/`onFailed` returns.
|
|
42
|
+
* @throws {NombaError} The transfer was rejected (4xx) and never happened.
|
|
43
|
+
* @throws {OutcomeUnknownError} The outcome could not be confirmed as success or failure.
|
|
44
|
+
* @throws {UnconfirmedOutcomeError} The result is pending and no `onPending` handler was given.
|
|
45
|
+
*/
|
|
15
46
|
bankTransferFromSubAccount<R = BankTransferResult>(subAccountId: string, params: BankTransferParams, options?: CallOptions<BankTransferResult, R>): Promise<BankTransferResult | R>;
|
|
16
|
-
/**
|
|
47
|
+
/**
|
|
48
|
+
* `POST /v2/transfers/wallet`. Sends a wallet-to-wallet transfer from the parent account to another
|
|
49
|
+
* Nomba account.
|
|
50
|
+
* @param params The destination wallet and amount, and the `merchantTxRef` used for idempotency.
|
|
51
|
+
* @param options Outcome closures and per-call overrides; without them, success resolves the result and
|
|
52
|
+
* a rejection or pending outcome throws.
|
|
53
|
+
* @returns The transfer result, or whatever `options.onSuccess`/`onPending`/`onFailed` returns.
|
|
54
|
+
* @throws {NombaError} The transfer was rejected (4xx) and never happened.
|
|
55
|
+
* @throws {OutcomeUnknownError} The outcome could not be confirmed as success or failure.
|
|
56
|
+
* @throws {UnconfirmedOutcomeError} The result is pending and no `onPending` handler was given.
|
|
57
|
+
*/
|
|
17
58
|
walletTransfer<R = WalletTransferResult>(params: WalletTransferParams, options?: CallOptions<WalletTransferResult, R>): Promise<WalletTransferResult | R>;
|
|
18
|
-
/**
|
|
59
|
+
/**
|
|
60
|
+
* `POST /v2/transfers/wallet/{subAccountId}`. Sends a wallet-to-wallet transfer from a sub account.
|
|
61
|
+
* @param subAccountId The sub account to transfer from.
|
|
62
|
+
* @param params The destination wallet and amount, and the `merchantTxRef` used for idempotency.
|
|
63
|
+
* @param options Outcome closures and per-call overrides; without them, success resolves the result and
|
|
64
|
+
* a rejection or pending outcome throws.
|
|
65
|
+
* @returns The transfer result, or whatever `options.onSuccess`/`onPending`/`onFailed` returns.
|
|
66
|
+
* @throws {NombaError} The transfer was rejected (4xx) and never happened.
|
|
67
|
+
* @throws {OutcomeUnknownError} The outcome could not be confirmed as success or failure.
|
|
68
|
+
* @throws {UnconfirmedOutcomeError} The result is pending and no `onPending` handler was given.
|
|
69
|
+
*/
|
|
19
70
|
walletTransferFromSubAccount<R = WalletTransferResult>(subAccountId: string, params: WalletTransferParams, options?: CallOptions<WalletTransferResult, R>): Promise<WalletTransferResult | R>;
|
|
20
71
|
}
|
package/dist/lib/errors.d.ts
CHANGED
|
@@ -60,3 +60,22 @@ export declare const isPending: (e: unknown) => e is OutcomeUnknownError | Uncon
|
|
|
60
60
|
export declare class TokenError extends Error {
|
|
61
61
|
constructor(cause: unknown);
|
|
62
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Thrown when a money-moving request is attempted without a valid idempotency key. The SDK refuses to
|
|
65
|
+
* generate a random key for money-moving operations because doing so can lead to double disbursement:
|
|
66
|
+
* if the request times out and is retried, a new random key would create a duplicate transaction.
|
|
67
|
+
*
|
|
68
|
+
* Always provide a stable, caller-controlled idempotency key (e.g., `merchantTxRef`, `orderReference`,
|
|
69
|
+
* or `idempotencyKey`) so the same reference can be used to safely retry or requery the transaction.
|
|
70
|
+
*/
|
|
71
|
+
export declare class IdempotencyKeyAbsentError extends Error {
|
|
72
|
+
/** The field name that must be set in the request body. */
|
|
73
|
+
readonly field: string;
|
|
74
|
+
/** The API endpoint being called. */
|
|
75
|
+
readonly path: string;
|
|
76
|
+
constructor(
|
|
77
|
+
/** The field name that must be set in the request body. */
|
|
78
|
+
field: string,
|
|
79
|
+
/** The API endpoint being called. */
|
|
80
|
+
path: string);
|
|
81
|
+
}
|
package/dist/lib/errors.js
CHANGED
|
@@ -90,3 +90,26 @@ export class TokenError extends Error {
|
|
|
90
90
|
this.name = "TokenError";
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Thrown when a money-moving request is attempted without a valid idempotency key. The SDK refuses to
|
|
95
|
+
* generate a random key for money-moving operations because doing so can lead to double disbursement:
|
|
96
|
+
* if the request times out and is retried, a new random key would create a duplicate transaction.
|
|
97
|
+
*
|
|
98
|
+
* Always provide a stable, caller-controlled idempotency key (e.g., `merchantTxRef`, `orderReference`,
|
|
99
|
+
* or `idempotencyKey`) so the same reference can be used to safely retry or requery the transaction.
|
|
100
|
+
*/
|
|
101
|
+
export class IdempotencyKeyAbsentError extends Error {
|
|
102
|
+
field;
|
|
103
|
+
path;
|
|
104
|
+
constructor(
|
|
105
|
+
/** The field name that must be set in the request body. */
|
|
106
|
+
field,
|
|
107
|
+
/** The API endpoint being called. */
|
|
108
|
+
path) {
|
|
109
|
+
super(`Idempotency key required: set "${field}" in the request body for ${path}. ` +
|
|
110
|
+
`The SDK does not generate random keys for money-moving operations to prevent double disbursement.`);
|
|
111
|
+
this.field = field;
|
|
112
|
+
this.path = path;
|
|
113
|
+
this.name = "IdempotencyKeyAbsentError";
|
|
114
|
+
}
|
|
115
|
+
}
|
package/dist/lib/outcome.d.ts
CHANGED
|
@@ -3,16 +3,41 @@
|
|
|
3
3
|
* @description Classifies a money-moving call as success, pending or failed, using only documented values
|
|
4
4
|
*/
|
|
5
5
|
import type { IHttpClient } from "../interfaces/http/IHttpClient.js";
|
|
6
|
-
import type { HttpMethod, HttpRequestOptions
|
|
6
|
+
import type { HttpMethod, HttpRequestOptions } from "../types/common/common.types.js";
|
|
7
7
|
import type { Outcome, OutcomeHandlers, OutcomeTable } from "../types/common/outcome.types.js";
|
|
8
8
|
/** Documented 4xx body codes (docs/api-basics/response-codes): definite rejections, safe to report as failed. */
|
|
9
9
|
export declare const REJECTED: Readonly<Record<string, "failed">>;
|
|
10
10
|
/** Anything the table does not list is pending: an undocumented value is never acted on. */
|
|
11
11
|
export declare const classify: (table: OutcomeTable, code: string, status: string | undefined) => Outcome;
|
|
12
12
|
/**
|
|
13
|
-
* Makes one money-moving call and settles it into exactly one outcome.
|
|
13
|
+
* Makes one money-moving call and settles it into exactly one outcome: success, pending, or failed.
|
|
14
|
+
*
|
|
14
15
|
* With a closure for that outcome, resolves to what the closure returns. Without one:
|
|
15
|
-
* success resolves `data
|
|
16
|
-
*
|
|
16
|
+
* - **success**: resolves `data`
|
|
17
|
+
* - **pending**: throws `UnconfirmedOutcomeError` (undocumented code/status, or documented pending)
|
|
18
|
+
* - **failed**: throws `NombaError` (documented rejection)
|
|
19
|
+
*
|
|
20
|
+
* Transport failures (timeout, network error, 5xx) throw `OutcomeUnknownError`.
|
|
21
|
+
*
|
|
22
|
+
* @template T The response data type.
|
|
23
|
+
* @template R The return type when closures transform the result.
|
|
24
|
+
* @param http The HTTP client to use.
|
|
25
|
+
* @param method The HTTP method (`POST` for money-moving calls).
|
|
26
|
+
* @param path The API endpoint path.
|
|
27
|
+
* @param request The request body and query; `okCodes`, `envelope`, and `idempotencyKey` are set internally.
|
|
28
|
+
* @param table Maps body codes and `data.status` values to outcomes; unlisted values are pending.
|
|
29
|
+
* @param idempotency `value` is the idempotency key, derived from params (e.g., `merchantTxRef`). Tracked
|
|
30
|
+
* in errors so callers know which transaction to requery; not sent in headers. Must be non-empty.
|
|
31
|
+
* `field` is the name of the field in the request body that holds it (e.g., `"merchantTxRef"`). Used in
|
|
32
|
+
* error messages when the key is missing.
|
|
33
|
+
* @param options Outcome closures: `onSuccess`, `onPending`, `onFailed`. Each transforms the result or error.
|
|
34
|
+
* @returns The response `data`, or what the matching closure returns.
|
|
35
|
+
* @throws {IdempotencyKeyAbsentError} The idempotency key is missing or empty.
|
|
36
|
+
* @throws {NombaError} A documented rejection (4xx body code).
|
|
37
|
+
* @throws {OutcomeUnknownError} Transport failure — outcome unknown; requery before retrying.
|
|
38
|
+
* @throws {UnconfirmedOutcomeError} Pending or undocumented response — do not retry without requerying.
|
|
17
39
|
*/
|
|
18
|
-
export declare function settle<T, R = T>(http: IHttpClient, method: HttpMethod, path: string, request: Omit<HttpRequestOptions, "okCodes" | "envelope" | "idempotencyKey">, table: OutcomeTable,
|
|
40
|
+
export declare function settle<T, R = T>(http: IHttpClient, method: HttpMethod, path: string, request: Omit<HttpRequestOptions, "okCodes" | "envelope" | "idempotencyKey">, table: OutcomeTable, { field: idempotencyField, value: idempotencyKey, }: {
|
|
41
|
+
field: string;
|
|
42
|
+
value: string;
|
|
43
|
+
}, options?: OutcomeHandlers<T, R>): Promise<T | R>;
|
package/dist/lib/outcome.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @module nomba/lib/outcome
|
|
3
3
|
* @description Classifies a money-moving call as success, pending or failed, using only documented values
|
|
4
4
|
*/
|
|
5
|
-
import { isPending, NombaError, OutcomeUnknownError, UnconfirmedOutcomeError, } from "./errors.js";
|
|
5
|
+
import { IdempotencyKeyAbsentError, isPending, NombaError, OutcomeUnknownError, UnconfirmedOutcomeError, } from "./errors.js";
|
|
6
6
|
/** Documented 4xx body codes (docs/api-basics/response-codes): definite rejections, safe to report as failed. */
|
|
7
7
|
export const REJECTED = {
|
|
8
8
|
"400": "failed",
|
|
@@ -28,13 +28,38 @@ const defaultStatusOf = (data) => {
|
|
|
28
28
|
return typeof s === "string" ? s : undefined;
|
|
29
29
|
};
|
|
30
30
|
/**
|
|
31
|
-
* Makes one money-moving call and settles it into exactly one outcome.
|
|
31
|
+
* Makes one money-moving call and settles it into exactly one outcome: success, pending, or failed.
|
|
32
|
+
*
|
|
32
33
|
* With a closure for that outcome, resolves to what the closure returns. Without one:
|
|
33
|
-
* success resolves `data
|
|
34
|
-
*
|
|
34
|
+
* - **success**: resolves `data`
|
|
35
|
+
* - **pending**: throws `UnconfirmedOutcomeError` (undocumented code/status, or documented pending)
|
|
36
|
+
* - **failed**: throws `NombaError` (documented rejection)
|
|
37
|
+
*
|
|
38
|
+
* Transport failures (timeout, network error, 5xx) throw `OutcomeUnknownError`.
|
|
39
|
+
*
|
|
40
|
+
* @template T The response data type.
|
|
41
|
+
* @template R The return type when closures transform the result.
|
|
42
|
+
* @param http The HTTP client to use.
|
|
43
|
+
* @param method The HTTP method (`POST` for money-moving calls).
|
|
44
|
+
* @param path The API endpoint path.
|
|
45
|
+
* @param request The request body and query; `okCodes`, `envelope`, and `idempotencyKey` are set internally.
|
|
46
|
+
* @param table Maps body codes and `data.status` values to outcomes; unlisted values are pending.
|
|
47
|
+
* @param idempotency `value` is the idempotency key, derived from params (e.g., `merchantTxRef`). Tracked
|
|
48
|
+
* in errors so callers know which transaction to requery; not sent in headers. Must be non-empty.
|
|
49
|
+
* `field` is the name of the field in the request body that holds it (e.g., `"merchantTxRef"`). Used in
|
|
50
|
+
* error messages when the key is missing.
|
|
51
|
+
* @param options Outcome closures: `onSuccess`, `onPending`, `onFailed`. Each transforms the result or error.
|
|
52
|
+
* @returns The response `data`, or what the matching closure returns.
|
|
53
|
+
* @throws {IdempotencyKeyAbsentError} The idempotency key is missing or empty.
|
|
54
|
+
* @throws {NombaError} A documented rejection (4xx body code).
|
|
55
|
+
* @throws {OutcomeUnknownError} Transport failure — outcome unknown; requery before retrying.
|
|
56
|
+
* @throws {UnconfirmedOutcomeError} Pending or undocumented response — do not retry without requerying.
|
|
35
57
|
*/
|
|
36
|
-
export async function settle(http, method, path, request, table, options = {}) {
|
|
37
|
-
|
|
58
|
+
export async function settle(http, method, path, request, table, { field: idempotencyField, value: idempotencyKey, }, options = {}) {
|
|
59
|
+
if (typeof idempotencyKey !== "string" || !idempotencyKey.trim()) {
|
|
60
|
+
throw new IdempotencyKeyAbsentError(idempotencyField, path);
|
|
61
|
+
}
|
|
62
|
+
const { onSuccess, onPending, onFailed } = options;
|
|
38
63
|
const report = (outcome, code, status, key = idempotencyKey) => http.reportOutcome({
|
|
39
64
|
method,
|
|
40
65
|
path,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "0.
|
|
1
|
+
export declare const SDK_VERSION = "0.4.0";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const SDK_VERSION = "0.
|
|
1
|
+
export const SDK_VERSION = "0.4.0";
|
package/dist/lib/util.d.ts
CHANGED
|
@@ -23,7 +23,6 @@ export declare const pageQuery: <T extends {
|
|
|
23
23
|
cursor: string | undefined;
|
|
24
24
|
};
|
|
25
25
|
export declare const sleep: (ms: number) => Promise<void>;
|
|
26
|
-
export declare const assertKey: (key: string) => void;
|
|
27
26
|
/** A cursor-paginated list response: `{ results, cursor }`, with `cursor` empty on the last page. */
|
|
28
27
|
export type Page<T> = {
|
|
29
28
|
results?: T[];
|
package/dist/lib/util.js
CHANGED
|
@@ -23,9 +23,3 @@ export const pageQuery = (p) => ({
|
|
|
23
23
|
cursor: p.cursor || undefined,
|
|
24
24
|
});
|
|
25
25
|
export const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
26
|
-
// A header-safe idempotency key: printable ASCII, no spaces. Anything else would make fetch throw before
|
|
27
|
-
// sending, which would look like a network failure.
|
|
28
|
-
export const assertKey = (key) => {
|
|
29
|
-
if (!/^[\x21-\x7e]+$/.test(key))
|
|
30
|
-
throw new Error(`Invalid idempotency key "${key}": use printable ASCII without spaces`);
|
|
31
|
-
};
|
package/dist/nomba.d.ts
CHANGED
|
@@ -26,6 +26,10 @@ import type { IElectricityV2Service } from "./interfaces/v2/electricity/index.js
|
|
|
26
26
|
import type { ITransfersService } from "./interfaces/v2/transfers/index.js";
|
|
27
27
|
import type { TokenData } from "./lib/token-store.js";
|
|
28
28
|
import type { Environment, HttpMethod, HttpRequestOptions, NombaOptions } from "./types/common/common.types.js";
|
|
29
|
+
/**
|
|
30
|
+
* The Nomba SDK client. Exposes one property per product (`accounts`, `transfers`, `electricity`, ...),
|
|
31
|
+
* each backed by the shared, authenticated HTTP transport.
|
|
32
|
+
*/
|
|
29
33
|
export declare class Nomba {
|
|
30
34
|
#private;
|
|
31
35
|
readonly accounts: IAccountsService;
|
|
@@ -49,15 +53,44 @@ export declare class Nomba {
|
|
|
49
53
|
readonly globalCollections: IGlobalCollectionsService;
|
|
50
54
|
readonly globalPayout: IGlobalPayoutService;
|
|
51
55
|
readonly webhooks: IWebhooksService;
|
|
52
|
-
/**
|
|
56
|
+
/**
|
|
57
|
+
* @param options Client credentials and transport configuration.
|
|
58
|
+
* @param http Replace the transport, e.g. with a fake in tests. Defaults to the real `HttpClient`.
|
|
59
|
+
*/
|
|
53
60
|
constructor(options: NombaOptions, http?: IHttpClient);
|
|
61
|
+
/** `"production"` or `"sandbox"`, as passed to the constructor (defaults to `"production"`). */
|
|
54
62
|
get environment(): Environment;
|
|
55
|
-
/**
|
|
63
|
+
/**
|
|
64
|
+
* `POST /v1/auth/token/issue`. Gets a new access token. Called for you automatically on first use,
|
|
65
|
+
* so you normally never need this directly.
|
|
66
|
+
* @returns The issued token data.
|
|
67
|
+
* @throws {NombaError} The credentials were rejected.
|
|
68
|
+
*/
|
|
56
69
|
issueToken(): Promise<TokenData>;
|
|
57
|
-
/**
|
|
70
|
+
/**
|
|
71
|
+
* `POST /v1/auth/token/refresh`. Refreshes the stored token. Called for you automatically 5 minutes
|
|
72
|
+
* before expiry, so you normally never need this directly.
|
|
73
|
+
* @returns The refreshed token data.
|
|
74
|
+
* @throws {Error} No token has been issued yet.
|
|
75
|
+
* @throws {NombaError} The refresh token was rejected.
|
|
76
|
+
*/
|
|
58
77
|
refreshToken(): Promise<TokenData>;
|
|
59
|
-
/**
|
|
78
|
+
/**
|
|
79
|
+
* `POST /v1/auth/token/revoke`. Ends the session, e.g. on shutdown. A no-op if no token is held.
|
|
80
|
+
* @throws {NombaError} Nomba rejected the revoke call.
|
|
81
|
+
*/
|
|
60
82
|
revokeToken(): Promise<void>;
|
|
61
|
-
/**
|
|
83
|
+
/**
|
|
84
|
+
* Makes an authenticated call to any Nomba endpoint, including ones this SDK has no dedicated method
|
|
85
|
+
* for. Issues or refreshes the token as needed.
|
|
86
|
+
* @param method The HTTP method.
|
|
87
|
+
* @param path The endpoint path, starting with `/` (e.g. `/v1/accounts/parent`).
|
|
88
|
+
* @param options Query params, request body, idempotency key and related per-call overrides.
|
|
89
|
+
* @returns The response body's `data` field.
|
|
90
|
+
* @throws {TokenError} Getting or refreshing the access token failed; the request was never sent.
|
|
91
|
+
* @throws {NombaError} A non-2xx response, or a 2xx whose body `code` is not one of `options.okCodes`.
|
|
92
|
+
* @throws {OutcomeUnknownError} A mutating call failed in a way that does not say whether it was
|
|
93
|
+
* processed (timeout, network error, 5xx, or a 409); check its status before retrying.
|
|
94
|
+
*/
|
|
62
95
|
request<T>(method: HttpMethod, path: string, options?: HttpRequestOptions): Promise<T>;
|
|
63
96
|
}
|
package/dist/nomba.js
CHANGED
|
@@ -24,6 +24,10 @@ import { CableV2Service } from "./services/v2/cable/cable.service.js";
|
|
|
24
24
|
import { DataV2Service } from "./services/v2/data/data.service.js";
|
|
25
25
|
import { ElectricityV2Service } from "./services/v2/electricity/electricity.service.js";
|
|
26
26
|
import { TransfersService } from "./services/v2/transfers/transfers.service.js";
|
|
27
|
+
/**
|
|
28
|
+
* The Nomba SDK client. Exposes one property per product (`accounts`, `transfers`, `electricity`, ...),
|
|
29
|
+
* each backed by the shared, authenticated HTTP transport.
|
|
30
|
+
*/
|
|
27
31
|
export class Nomba {
|
|
28
32
|
accounts;
|
|
29
33
|
virtualAccounts;
|
|
@@ -48,7 +52,10 @@ export class Nomba {
|
|
|
48
52
|
globalPayout;
|
|
49
53
|
webhooks;
|
|
50
54
|
#http;
|
|
51
|
-
/**
|
|
55
|
+
/**
|
|
56
|
+
* @param options Client credentials and transport configuration.
|
|
57
|
+
* @param http Replace the transport, e.g. with a fake in tests. Defaults to the real `HttpClient`.
|
|
58
|
+
*/
|
|
52
59
|
constructor(options, http = new HttpClient(options)) {
|
|
53
60
|
this.#http = http;
|
|
54
61
|
this.accounts = new AccountsService(http);
|
|
@@ -75,22 +82,48 @@ export class Nomba {
|
|
|
75
82
|
this.globalPayout = new GlobalPayoutService(http);
|
|
76
83
|
this.webhooks = new WebhooksService(http);
|
|
77
84
|
}
|
|
85
|
+
/** `"production"` or `"sandbox"`, as passed to the constructor (defaults to `"production"`). */
|
|
78
86
|
get environment() {
|
|
79
87
|
return this.#http.environment;
|
|
80
88
|
}
|
|
81
|
-
/**
|
|
89
|
+
/**
|
|
90
|
+
* `POST /v1/auth/token/issue`. Gets a new access token. Called for you automatically on first use,
|
|
91
|
+
* so you normally never need this directly.
|
|
92
|
+
* @returns The issued token data.
|
|
93
|
+
* @throws {NombaError} The credentials were rejected.
|
|
94
|
+
*/
|
|
82
95
|
issueToken() {
|
|
83
96
|
return this.#http.issueToken();
|
|
84
97
|
}
|
|
85
|
-
/**
|
|
98
|
+
/**
|
|
99
|
+
* `POST /v1/auth/token/refresh`. Refreshes the stored token. Called for you automatically 5 minutes
|
|
100
|
+
* before expiry, so you normally never need this directly.
|
|
101
|
+
* @returns The refreshed token data.
|
|
102
|
+
* @throws {Error} No token has been issued yet.
|
|
103
|
+
* @throws {NombaError} The refresh token was rejected.
|
|
104
|
+
*/
|
|
86
105
|
refreshToken() {
|
|
87
106
|
return this.#http.refreshToken();
|
|
88
107
|
}
|
|
89
|
-
/**
|
|
108
|
+
/**
|
|
109
|
+
* `POST /v1/auth/token/revoke`. Ends the session, e.g. on shutdown. A no-op if no token is held.
|
|
110
|
+
* @throws {NombaError} Nomba rejected the revoke call.
|
|
111
|
+
*/
|
|
90
112
|
revokeToken() {
|
|
91
113
|
return this.#http.revokeToken();
|
|
92
114
|
}
|
|
93
|
-
/**
|
|
115
|
+
/**
|
|
116
|
+
* Makes an authenticated call to any Nomba endpoint, including ones this SDK has no dedicated method
|
|
117
|
+
* for. Issues or refreshes the token as needed.
|
|
118
|
+
* @param method The HTTP method.
|
|
119
|
+
* @param path The endpoint path, starting with `/` (e.g. `/v1/accounts/parent`).
|
|
120
|
+
* @param options Query params, request body, idempotency key and related per-call overrides.
|
|
121
|
+
* @returns The response body's `data` field.
|
|
122
|
+
* @throws {TokenError} Getting or refreshing the access token failed; the request was never sent.
|
|
123
|
+
* @throws {NombaError} A non-2xx response, or a 2xx whose body `code` is not one of `options.okCodes`.
|
|
124
|
+
* @throws {OutcomeUnknownError} A mutating call failed in a way that does not say whether it was
|
|
125
|
+
* processed (timeout, network error, 5xx, or a 409); check its status before retrying.
|
|
126
|
+
*/
|
|
94
127
|
request(method, path, options) {
|
|
95
128
|
return this.#http.request(method, path, options);
|
|
96
129
|
}
|
|
@@ -6,22 +6,56 @@ import type { IHttpClient } from "../../interfaces/http/IHttpClient.js";
|
|
|
6
6
|
import { type TokenData } from "../../lib/token-store.js";
|
|
7
7
|
import type { Environment, HttpMethod, HttpRequestOptions, NombaOptions } from "../../types/common/common.types.js";
|
|
8
8
|
import type { OutcomeEvent } from "../../types/common/outcome.types.js";
|
|
9
|
+
/**
|
|
10
|
+
* Authenticated HTTP transport for the Nomba API: owns the token lifecycle (issue, refresh, revoke,
|
|
11
|
+
* sharing via `TokenStore`) and the retry/idempotency rules for `request`.
|
|
12
|
+
*/
|
|
9
13
|
export declare class HttpClient implements IHttpClient {
|
|
10
14
|
#private;
|
|
11
15
|
readonly environment: Environment;
|
|
12
16
|
constructor(options: NombaOptions);
|
|
13
|
-
/**
|
|
17
|
+
/**
|
|
18
|
+
* `POST /v1/auth/token/issue`.
|
|
19
|
+
* @returns The issued token data, saved to the configured `TokenStore`.
|
|
20
|
+
* @throws {NombaError} The credentials were rejected.
|
|
21
|
+
*/
|
|
14
22
|
issueToken(): Promise<TokenData>;
|
|
15
|
-
/**
|
|
23
|
+
/**
|
|
24
|
+
* `POST /v1/auth/token/refresh`.
|
|
25
|
+
* @returns The refreshed token data, saved to the configured `TokenStore`.
|
|
26
|
+
* @throws {Error} No token is held yet; call `issueToken()` first.
|
|
27
|
+
* @throws {NombaError} The refresh token was rejected.
|
|
28
|
+
*/
|
|
16
29
|
refreshToken(): Promise<TokenData>;
|
|
17
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* `POST /v1/auth/token/revoke`. A no-op if no token is held.
|
|
32
|
+
* @throws {NombaError} Nomba rejected the revoke call.
|
|
33
|
+
*/
|
|
18
34
|
revokeToken(): Promise<void>;
|
|
19
35
|
/**
|
|
20
|
-
* Authenticated call. Issues or refreshes the token as needed
|
|
21
|
-
* `okCodes` lists the body codes that are not errors; the default is ["00"].
|
|
36
|
+
* Authenticated call. Issues or refreshes the token as needed.
|
|
22
37
|
* Every non-GET call carries an idempotency key, so failures that leave the outcome unknown are retried
|
|
23
|
-
* safely, and surface as OutcomeUnknownError once retries run out.
|
|
38
|
+
* safely, and surface as `OutcomeUnknownError` once retries run out.
|
|
39
|
+
* @param method The HTTP method.
|
|
40
|
+
* @param path The endpoint path, starting with `/`.
|
|
41
|
+
* @param options.body The JSON request body.
|
|
42
|
+
* @param options.query Query string parameters; entries with a `undefined` value are omitted.
|
|
43
|
+
* @param options.okCodes Body codes that are not errors. Defaults to `["00"]`.
|
|
44
|
+
* @param options.idempotencyKey Tracked in `OutcomeUnknownError` for non-GET calls; this generic
|
|
45
|
+
* transport does not send it to Nomba. Put the endpoint's actual idempotency field (for example
|
|
46
|
+
* `merchantTxRef`) in `options.body`. A random UUID is generated if omitted.
|
|
47
|
+
* @param options.codeField The response field holding the result code. Defaults to `"code"`.
|
|
48
|
+
* @param options.descriptionField The response field holding the error description. Defaults to
|
|
49
|
+
* `"description"`.
|
|
50
|
+
* @param options.envelope When true, resolves to `{ code, data }` instead of just `data`.
|
|
51
|
+
* @returns The response body's `data` field (or `{ code, data }` with `envelope: true`).
|
|
52
|
+
* @throws {TypeError} `path` does not start with `/`.
|
|
53
|
+
* @throws {TokenError} Getting or refreshing the access token failed; the request was never sent.
|
|
54
|
+
* @throws {NombaError} A non-2xx response, or a 2xx whose body code is not in `okCodes`.
|
|
55
|
+
* @throws {OutcomeUnknownError} A non-GET call failed in a way that does not say whether it was
|
|
56
|
+
* processed (timeout, network error, 5xx, or a 409); check its status before retrying.
|
|
24
57
|
*/
|
|
25
58
|
request<T>(method: HttpMethod, path: string, { body, query, okCodes, idempotencyKey, codeField, descriptionField, envelope, }?: HttpRequestOptions): Promise<T>;
|
|
59
|
+
/** Invokes the configured `onOutcome` hook, if any, swallowing any error it throws. */
|
|
26
60
|
reportOutcome(event: OutcomeEvent): void;
|
|
27
61
|
}
|
|
@@ -6,7 +6,7 @@ import { randomUUID } from "node:crypto";
|
|
|
6
6
|
import { NombaError, OutcomeUnknownError, TokenError, } from "../../lib/errors.js";
|
|
7
7
|
import { SDK_VERSION } from "../../lib/sdk-version.js";
|
|
8
8
|
import { MemoryTokenStore, } from "../../lib/token-store.js";
|
|
9
|
-
import {
|
|
9
|
+
import { sleep } from "../../lib/util.js";
|
|
10
10
|
const BASE_URLS = {
|
|
11
11
|
production: "https://api.nomba.com",
|
|
12
12
|
sandbox: "https://sandbox.nomba.com",
|
|
@@ -15,6 +15,10 @@ const BASE_URLS = {
|
|
|
15
15
|
const REFRESH_MARGIN_MS = 5 * 60_000;
|
|
16
16
|
// A failed store.set() is retried this many times (100ms, 200ms apart) before giving up on the shared copy.
|
|
17
17
|
const SET_RETRIES = 2;
|
|
18
|
+
/**
|
|
19
|
+
* Authenticated HTTP transport for the Nomba API: owns the token lifecycle (issue, refresh, revoke,
|
|
20
|
+
* sharing via `TokenStore`) and the retry/idempotency rules for `request`.
|
|
21
|
+
*/
|
|
18
22
|
export class HttpClient {
|
|
19
23
|
environment;
|
|
20
24
|
#options;
|
|
@@ -34,11 +38,20 @@ export class HttpClient {
|
|
|
34
38
|
this.environment = env;
|
|
35
39
|
this.#baseUrl = BASE_URLS[env];
|
|
36
40
|
}
|
|
37
|
-
/**
|
|
41
|
+
/**
|
|
42
|
+
* `POST /v1/auth/token/issue`.
|
|
43
|
+
* @returns The issued token data, saved to the configured `TokenStore`.
|
|
44
|
+
* @throws {NombaError} The credentials were rejected.
|
|
45
|
+
*/
|
|
38
46
|
issueToken() {
|
|
39
47
|
return this.#locked(() => this.#issue());
|
|
40
48
|
}
|
|
41
|
-
/**
|
|
49
|
+
/**
|
|
50
|
+
* `POST /v1/auth/token/refresh`.
|
|
51
|
+
* @returns The refreshed token data, saved to the configured `TokenStore`.
|
|
52
|
+
* @throws {Error} No token is held yet; call `issueToken()` first.
|
|
53
|
+
* @throws {NombaError} The refresh token was rejected.
|
|
54
|
+
*/
|
|
42
55
|
refreshToken() {
|
|
43
56
|
return this.#locked(async () => {
|
|
44
57
|
const t = await this.#read();
|
|
@@ -47,7 +60,10 @@ export class HttpClient {
|
|
|
47
60
|
return this.#refresh(t);
|
|
48
61
|
});
|
|
49
62
|
}
|
|
50
|
-
/**
|
|
63
|
+
/**
|
|
64
|
+
* `POST /v1/auth/token/revoke`. A no-op if no token is held.
|
|
65
|
+
* @throws {NombaError} Nomba rejected the revoke call.
|
|
66
|
+
*/
|
|
51
67
|
revokeToken() {
|
|
52
68
|
return this.#locked(async () => {
|
|
53
69
|
const t = await this.#read();
|
|
@@ -70,10 +86,27 @@ export class HttpClient {
|
|
|
70
86
|
});
|
|
71
87
|
}
|
|
72
88
|
/**
|
|
73
|
-
* Authenticated call. Issues or refreshes the token as needed
|
|
74
|
-
* `okCodes` lists the body codes that are not errors; the default is ["00"].
|
|
89
|
+
* Authenticated call. Issues or refreshes the token as needed.
|
|
75
90
|
* Every non-GET call carries an idempotency key, so failures that leave the outcome unknown are retried
|
|
76
|
-
* safely, and surface as OutcomeUnknownError once retries run out.
|
|
91
|
+
* safely, and surface as `OutcomeUnknownError` once retries run out.
|
|
92
|
+
* @param method The HTTP method.
|
|
93
|
+
* @param path The endpoint path, starting with `/`.
|
|
94
|
+
* @param options.body The JSON request body.
|
|
95
|
+
* @param options.query Query string parameters; entries with a `undefined` value are omitted.
|
|
96
|
+
* @param options.okCodes Body codes that are not errors. Defaults to `["00"]`.
|
|
97
|
+
* @param options.idempotencyKey Tracked in `OutcomeUnknownError` for non-GET calls; this generic
|
|
98
|
+
* transport does not send it to Nomba. Put the endpoint's actual idempotency field (for example
|
|
99
|
+
* `merchantTxRef`) in `options.body`. A random UUID is generated if omitted.
|
|
100
|
+
* @param options.codeField The response field holding the result code. Defaults to `"code"`.
|
|
101
|
+
* @param options.descriptionField The response field holding the error description. Defaults to
|
|
102
|
+
* `"description"`.
|
|
103
|
+
* @param options.envelope When true, resolves to `{ code, data }` instead of just `data`.
|
|
104
|
+
* @returns The response body's `data` field (or `{ code, data }` with `envelope: true`).
|
|
105
|
+
* @throws {TypeError} `path` does not start with `/`.
|
|
106
|
+
* @throws {TokenError} Getting or refreshing the access token failed; the request was never sent.
|
|
107
|
+
* @throws {NombaError} A non-2xx response, or a 2xx whose body code is not in `okCodes`.
|
|
108
|
+
* @throws {OutcomeUnknownError} A non-GET call failed in a way that does not say whether it was
|
|
109
|
+
* processed (timeout, network error, 5xx, or a 409); check its status before retrying.
|
|
77
110
|
*/
|
|
78
111
|
async request(method, path, { body, query, okCodes, idempotencyKey, codeField, descriptionField, envelope, } = {}) {
|
|
79
112
|
// The URL is base + path, so a path not starting with "/" (".evil.com", "@evil.com") would change the host
|
|
@@ -83,9 +116,10 @@ export class HttpClient {
|
|
|
83
116
|
const qs = new URLSearchParams(Object.entries(query ?? {}).flatMap(([k, v]) => v === undefined ? [] : [[k, String(v)]])).toString();
|
|
84
117
|
const url = qs ? `${path}?${qs}` : path;
|
|
85
118
|
// One key for every attempt of this call, including the retry after a 401 token renewal.
|
|
119
|
+
// The key is only tracked internally for error reporting (OutcomeUnknownError) and hooks (onOutcome);
|
|
120
|
+
// it is never sent in headers. User-provided values (orderReference, tokenKey, etc.) can contain
|
|
121
|
+
// any characters.
|
|
86
122
|
const key = method === "GET" ? undefined : idempotencyKey || randomUUID();
|
|
87
|
-
if (key !== undefined)
|
|
88
|
-
assertKey(key);
|
|
89
123
|
const token = await this.#tokenForRequest();
|
|
90
124
|
try {
|
|
91
125
|
return await this.#send(method, url, {
|
|
@@ -113,6 +147,7 @@ export class HttpClient {
|
|
|
113
147
|
});
|
|
114
148
|
}
|
|
115
149
|
}
|
|
150
|
+
/** Invokes the configured `onOutcome` hook, if any, swallowing any error it throws. */
|
|
116
151
|
reportOutcome(event) {
|
|
117
152
|
this.#hook(() => this.#options.onOutcome?.(event));
|
|
118
153
|
}
|