Skip to content

Commit 8ded29b

Browse files
authored
Remove invariant dependency (#200)
1 parent 587ec50 commit 8ded29b

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

.changeset/empty-keys-hammer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@dgac/nmb2b-client': patch
3+
---
4+
5+
Remove `invariant` dependency

src/config.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Security } from './security.js';
22
import { isValidSecurity } from './security.js';
33
import type { B2BFlavour } from './constants.js';
44
import { B2B_VERSION, B2BFlavours } from './constants.js';
5-
import invariant from 'invariant';
5+
import { assert } from './utils/assert.js';
66
import { URL } from 'url';
77
import type { Client as SoapClient } from 'soap';
88

@@ -17,30 +17,30 @@ export interface Config {
1717
}
1818

1919
export function isConfigValid(args: unknown): args is Config {
20-
invariant(!!args && typeof args === 'object', 'Invalid config');
20+
assert(!!args && typeof args === 'object', 'Invalid config');
2121

22-
invariant(
22+
assert(
2323
'security' in args && isValidSecurity(args.security),
2424
'Please provide a valid security option',
2525
);
2626

27-
invariant(
27+
assert(
2828
'flavour' in args && typeof args.flavour === 'string',
2929
`Invalid config.flavour. Supported flavours: ${B2BFlavours.join(', ')}`,
3030
);
3131

32-
invariant(
32+
assert(
3333
B2BFlavours.includes(args.flavour),
3434
`Invalid config.flavour. Supported flavours: ${B2BFlavours.join(', ')}`,
3535
);
3636

3737
if ('apiKeyId' in args.security) {
38-
invariant(
38+
assert(
3939
'endpoint' in args && !!args.endpoint,
4040
`When using an config.security.apiKeyId, config.endpoint must be defined`,
4141
);
4242

43-
invariant(
43+
assert(
4444
'xsdEndpoint' in args && !!args.xsdEndpoint,
4545
`When using an config.security.apiKeyId, config.xsdEndpoint must be defined`,
4646
);

src/security.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import invariant from 'invariant';
1+
import { assert } from './utils/assert.js';
22
import d from './utils/debug.js';
33
const debug = d('security');
44
import type { Config } from './config.js';
@@ -29,17 +29,17 @@ interface ApiGwSecurity {
2929
export type Security = PfxSecurity | PemSecurity | ApiGwSecurity;
3030

3131
export function isValidSecurity(obj: unknown): obj is Security {
32-
invariant(!!obj && typeof obj === 'object', 'Must be an object');
32+
assert(!!obj && typeof obj === 'object', 'Must be an object');
3333

3434
if ('apiKeyId' in obj) {
35-
invariant(
35+
assert(
3636
!!obj.apiKeyId &&
3737
typeof obj.apiKeyId === 'string' &&
3838
obj.apiKeyId.length > 0,
3939
'security.apiKeyId must be a string with a length > 0',
4040
);
4141

42-
invariant(
42+
assert(
4343
'apiSecretKey' in obj &&
4444
typeof obj.apiSecretKey === 'string' &&
4545
obj.apiSecretKey.length > 0,
@@ -49,14 +49,14 @@ export function isValidSecurity(obj: unknown): obj is Security {
4949
return true;
5050
}
5151

52-
invariant(
52+
assert(
5353
('pfx' in obj && Buffer.isBuffer(obj.pfx)) ||
5454
('cert' in obj && Buffer.isBuffer(obj.cert)),
5555
'security.pfx or security.cert must be buffers',
5656
);
5757

5858
if ('cert' in obj && obj.cert) {
59-
invariant(
59+
assert(
6060
'key' in obj && obj.key && Buffer.isBuffer(obj.key),
6161
'security.key must be a buffer if security.pem is defined',
6262
);

src/utils/assert.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function assert(
2+
condition: unknown,
3+
message?: string,
4+
): asserts condition {
5+
if (!condition) {
6+
const errorMessage =
7+
message !== undefined
8+
? `Assertion failed: ${message}`
9+
: `Assertion failed.`;
10+
throw new AssertionError(errorMessage);
11+
}
12+
}
13+
14+
class AssertionError extends Error {}

0 commit comments

Comments
 (0)