Skip to content

Commit a512452

Browse files
authored
fix: merge consumer-provided options with defaults (#424)
1 parent b74b940 commit a512452

File tree

13 files changed

+99
-59
lines changed

13 files changed

+99
-59
lines changed

.bundlewatch.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"//": "Pre-bundled for Browser (UMD)",
55
"path": "dist/browser/hibp.umd.js",
6-
"maxSize": "8.9 kB"
6+
"maxSize": "9 kB"
77
},
88
{
99
"//": "Pre-bundled for Browser (ESM)",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'hibp': patch
3+
---
4+
5+
Properly merge consumer-provided options with internal defaults.

API.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ response and returns a more structured format.</p>
4242
breach (0 indicating no exposure). The password is given in plain text, but
4343
only the first 5 characters of its SHA-1 hash will be submitted to the API.</p>
4444
</dd>
45-
<dt><a href="#search">search(account, [breachOptions])</a> ⇒ <code><a href="#SearchResults">Promise.&lt;SearchResults&gt;</a></code></dt>
45+
<dt><a href="#search">search(account, [options])</a> ⇒ <code><a href="#SearchResults">Promise.&lt;SearchResults&gt;</a></code></dt>
4646
<dd><p>Fetches all breaches and all pastes associated with the provided account
4747
(email address or username). Note that the remote API does not support
4848
querying pastes by username (only email addresses), so in the event the
@@ -381,7 +381,7 @@ try {
381381
```
382382
<a name="search"></a>
383383

384-
## search(account, [breachOptions]) ⇒ [<code>Promise.&lt;SearchResults&gt;</code>](#SearchResults)
384+
## search(account, [options]) ⇒ [<code>Promise.&lt;SearchResults&gt;</code>](#SearchResults)
385385
Fetches all breaches and all pastes associated with the provided account
386386
(email address or username). Note that the remote API does not support
387387
querying pastes by username (only email addresses), so in the event the
@@ -408,12 +408,12 @@ rejects with an Error
408408
| Param | Type | Description |
409409
| --- | --- | --- |
410410
| account | <code>string</code> | an email address or username |
411-
| [breachOptions] | <code>object</code> | a configuration object pertaining to breach queries |
412-
| [breachOptions.apiKey] | <code>string</code> | an API key from https://haveibeenpwned.com/API/Key |
413-
| [breachOptions.domain] | <code>string</code> | a domain by which to filter the results (default: all domains) |
414-
| [breachOptions.truncate] | <code>boolean</code> | truncate the results to only include the name of each breach (default: true) |
415-
| [breachOptions.baseUrl] | <code>string</code> | a custom base URL for the haveibeenpwned.com API endpoints (default: `https://haveibeenpwned.com/api/v3`) |
416-
| [breachOptions.userAgent] | <code>string</code> | a custom string to send as the User-Agent field in the request headers (default: `hibp <version>`) |
411+
| [options] | <code>object</code> | a configuration object |
412+
| [options.apiKey] | <code>string</code> | an API key from https://haveibeenpwned.com/API/Key |
413+
| [options.domain] | <code>string</code> | a domain by which to filter the breach results (default: all domains) |
414+
| [options.truncate] | <code>boolean</code> | truncate the breach results to only include the name of each breach (default: true) |
415+
| [options.baseUrl] | <code>string</code> | a custom base URL for the haveibeenpwned.com API endpoints (default: `https://haveibeenpwned.com/api/v3`) |
416+
| [options.userAgent] | <code>string</code> | a custom string to send as the User-Agent field in the request headers (default: `hibp <version>`) |
417417

418418
**Example**
419419
```js

src/api/haveibeenpwned/fetch-from-api.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,17 @@ function blockedWithRayId(rayId: string) {
6969
*/
7070
export async function fetchFromApi(
7171
endpoint: string,
72-
{
72+
options: {
73+
apiKey?: string;
74+
baseUrl?: string;
75+
userAgent?: string;
76+
} = {},
77+
): Promise<ApiData> {
78+
const {
7379
apiKey,
7480
baseUrl = 'https://haveibeenpwned.com/api/v3',
7581
userAgent,
76-
}: { apiKey?: string; baseUrl?: string; userAgent?: string } = {},
77-
): Promise<ApiData> {
82+
} = options;
7883
const headers: Record<string, string> = {};
7984

8085
if (apiKey) {

src/api/pwnedpasswords/fetch-from-api.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,20 @@ import { BAD_REQUEST } from './responses.js';
2424
*/
2525
export async function fetchFromApi(
2626
endpoint: string,
27-
{
28-
baseUrl = 'https://api.pwnedpasswords.com',
29-
userAgent,
30-
addPadding = false,
31-
mode = 'sha1',
32-
}: {
27+
options: {
3328
baseUrl?: string;
3429
userAgent?: string;
3530
addPadding?: boolean;
3631
mode?: 'sha1' | 'ntlm';
3732
} = {},
3833
): Promise<string> {
34+
const {
35+
baseUrl = 'https://api.pwnedpasswords.com',
36+
userAgent,
37+
addPadding = false,
38+
mode = 'sha1',
39+
} = options;
40+
3941
const config = Object.assign(
4042
{},
4143
userAgent ? { headers: { 'User-Agent': userAgent } } : {},

src/breach.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ import { fetchFromApi } from './api/haveibeenpwned/fetch-from-api.js';
5151
*/
5252
export function breach(
5353
breachName: string,
54-
options: { baseUrl?: string; userAgent?: string } = {},
54+
options: {
55+
baseUrl?: string;
56+
userAgent?: string;
57+
} = {},
5558
): Promise<Breach | null> {
5659
return fetchFromApi(
5760
`/breach/${encodeURIComponent(breachName)}`,

src/breached-account.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,25 +81,34 @@ export function breachedAccount(
8181
truncate?: boolean;
8282
baseUrl?: string;
8383
userAgent?: string;
84-
} = {
85-
includeUnverified: true,
86-
truncate: true,
87-
},
84+
} = {},
8885
): Promise<Breach[] | null> {
86+
const {
87+
apiKey,
88+
domain,
89+
includeUnverified = true,
90+
truncate = true,
91+
baseUrl,
92+
userAgent,
93+
} = options;
8994
const endpoint = `/breachedaccount/${encodeURIComponent(account)}?`;
9095
const params: string[] = [];
91-
if (options.domain) {
92-
params.push(`domain=${encodeURIComponent(options.domain)}`);
96+
97+
if (domain) {
98+
params.push(`domain=${encodeURIComponent(domain)}`);
9399
}
94-
if (options.includeUnverified === false) {
100+
101+
if (!includeUnverified) {
95102
params.push('includeUnverified=false');
96103
}
97-
if (options.truncate === false) {
104+
105+
if (!truncate) {
98106
params.push('truncateResponse=false');
99107
}
108+
100109
return fetchFromApi(`${endpoint}${params.join('&')}`, {
101-
apiKey: options.apiKey,
102-
baseUrl: options.baseUrl,
103-
userAgent: options.userAgent,
110+
apiKey,
111+
baseUrl,
112+
userAgent,
104113
}) as Promise<Breach[] | null>;
105114
}

src/breaches.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ export function breaches(
4444
userAgent?: string;
4545
} = {},
4646
): Promise<Breach[]> {
47+
const { domain, baseUrl, userAgent } = options;
4748
const endpoint = '/breaches?';
4849
const params: string[] = [];
49-
if (options.domain) {
50-
params.push(`domain=${encodeURIComponent(options.domain)}`);
50+
51+
if (domain) {
52+
params.push(`domain=${encodeURIComponent(domain)}`);
5153
}
54+
5255
return fetchFromApi(`${endpoint}${params.join('&')}`, {
53-
baseUrl: options.baseUrl,
54-
userAgent: options.userAgent,
56+
baseUrl,
57+
userAgent,
5558
}) as Promise<Breach[]>;
5659
}

src/data-classes.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ import { fetchFromApi } from './api/haveibeenpwned/fetch-from-api.js';
2525
* }
2626
*/
2727
export function dataClasses(
28-
options: { baseUrl?: string; userAgent?: string } = {},
28+
options: {
29+
baseUrl?: string;
30+
userAgent?: string;
31+
} = {},
2932
): Promise<string[] | null> {
3033
return fetchFromApi('/dataclasses', options) as Promise<string[] | null>;
3134
}

src/paste-account.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ import { fetchFromApi } from './api/haveibeenpwned/fetch-from-api.js';
4949
*/
5050
export function pasteAccount(
5151
email: string,
52-
options: { apiKey?: string; baseUrl?: string; userAgent?: string } = {},
52+
options: {
53+
apiKey?: string;
54+
baseUrl?: string;
55+
userAgent?: string;
56+
} = {},
5357
): Promise<Paste[] | null> {
54-
return fetchFromApi(`/pasteaccount/${encodeURIComponent(email)}`, {
55-
apiKey: options.apiKey,
56-
baseUrl: options.baseUrl,
57-
userAgent: options.userAgent,
58-
}) as Promise<Paste[] | null>;
58+
return fetchFromApi(
59+
`/pasteaccount/${encodeURIComponent(email)}`,
60+
options,
61+
) as Promise<Paste[] | null>;
5962
}

0 commit comments

Comments
 (0)