hibp 15.2.1 → 16.0.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/API.md +433 -126
- package/CHANGELOG.md +44 -0
- package/MIGRATION.md +7 -5
- package/README.md +53 -27
- package/dist/browser/hibp.module.js +2 -2
- package/dist/esm/api/base-fetch.d.ts +3 -1
- package/dist/esm/api/base-fetch.js +20 -7
- package/dist/esm/api/base-fetch.js.map +1 -1
- package/dist/esm/api/haveibeenpwned/fetch-from-api.d.ts +1 -1
- package/dist/esm/api/haveibeenpwned/fetch-from-api.js +71 -24
- package/dist/esm/api/haveibeenpwned/fetch-from-api.js.map +1 -1
- package/dist/esm/api/haveibeenpwned/package-info.d.ts +1 -1
- package/dist/esm/api/haveibeenpwned/package-info.js +1 -1
- package/dist/esm/api/haveibeenpwned/post-to-api.d.ts +1 -0
- package/dist/esm/api/haveibeenpwned/post-to-api.js +31 -0
- package/dist/esm/api/haveibeenpwned/post-to-api.js.map +1 -0
- package/dist/esm/api/haveibeenpwned/responses.js +8 -7
- package/dist/esm/api/haveibeenpwned/responses.js.map +1 -1
- package/dist/esm/api/haveibeenpwned/types.d.ts +22 -2
- package/dist/esm/api/pwnedpasswords/fetch-from-api.js +4 -4
- package/dist/esm/api/pwnedpasswords/fetch-from-api.js.map +1 -1
- package/dist/esm/api/pwnedpasswords/responses.js +1 -1
- package/dist/esm/breach.d.ts +8 -1
- package/dist/esm/breach.js +8 -1
- package/dist/esm/breach.js.map +1 -1
- package/dist/esm/breached-account-range.d.ts +73 -0
- package/dist/esm/breached-account-range.js +60 -0
- package/dist/esm/breached-account-range.js.map +1 -0
- package/dist/esm/breached-account.d.ts +56 -45
- package/dist/esm/breached-account.js +4 -76
- package/dist/esm/breached-account.js.map +1 -1
- package/dist/esm/breached-domain.d.ts +2 -3
- package/dist/esm/breached-domain.js +2 -3
- package/dist/esm/breached-domain.js.map +1 -1
- package/dist/esm/breaches.d.ts +9 -2
- package/dist/esm/breaches.js +10 -5
- package/dist/esm/breaches.js.map +1 -1
- package/dist/esm/data-class-descriptions.d.ts +55 -0
- package/dist/esm/data-class-descriptions.js +39 -0
- package/dist/esm/data-class-descriptions.js.map +1 -0
- package/dist/esm/data-classes.js +2 -2
- package/dist/esm/data-classes.js.map +1 -1
- package/dist/esm/generate-dns-token.d.ts +67 -0
- package/dist/esm/generate-dns-token.js +48 -0
- package/dist/esm/generate-dns-token.js.map +1 -0
- package/dist/esm/hibp.d.ts +28 -18
- package/dist/esm/hibp.js +23 -18
- package/dist/esm/hibp.js.map +1 -1
- package/dist/esm/latest-breach.d.ts +1 -1
- package/dist/esm/latest-breach.js +2 -2
- package/dist/esm/latest-breach.js.map +1 -1
- package/dist/esm/paste-account.d.ts +8 -8
- package/dist/esm/paste-account.js +8 -8
- package/dist/esm/paste-account.js.map +1 -1
- package/dist/esm/pwned-password-range.d.ts +1 -1
- package/dist/esm/pwned-password-range.js +4 -4
- package/dist/esm/pwned-password-range.js.map +1 -1
- package/dist/esm/pwned-password.js +6 -6
- package/dist/esm/pwned-password.js.map +1 -1
- package/dist/esm/search.d.ts +57 -40
- package/dist/esm/search.js +3 -72
- package/dist/esm/search.js.map +1 -1
- package/dist/esm/send-email.d.ts +69 -0
- package/dist/esm/send-email.js +49 -0
- package/dist/esm/send-email.js.map +1 -0
- package/dist/esm/stealer-logs-by-email-domain.d.ts +8 -9
- package/dist/esm/stealer-logs-by-email-domain.js +8 -9
- package/dist/esm/stealer-logs-by-email-domain.js.map +1 -1
- package/dist/esm/stealer-logs-by-email.d.ts +4 -5
- package/dist/esm/stealer-logs-by-email.js +5 -6
- package/dist/esm/stealer-logs-by-email.js.map +1 -1
- package/dist/esm/stealer-logs-by-website-domain.d.ts +4 -5
- package/dist/esm/stealer-logs-by-website-domain.js +5 -6
- package/dist/esm/stealer-logs-by-website-domain.js.map +1 -1
- package/dist/esm/subscribed-domains.d.ts +2 -3
- package/dist/esm/subscribed-domains.js +3 -4
- package/dist/esm/subscribed-domains.js.map +1 -1
- package/dist/esm/subscription-status.d.ts +11 -6
- package/dist/esm/subscription-status.js +12 -7
- package/dist/esm/subscription-status.js.map +1 -1
- package/dist/esm/verify-dns-token.d.ts +60 -0
- package/dist/esm/verify-dns-token.js +41 -0
- package/dist/esm/verify-dns-token.js.map +1 -0
- package/package.json +55 -56
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,49 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 16.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- [#637](https://github.com/wKovacs64/hibp/pull/637) [`d65fda6`](https://github.com/wKovacs64/hibp/commit/d65fda6f9b0703aa622324ed76760b4bbf0a5a80) Thanks [@wKovacs64](https://github.com/wKovacs64)! - Drop support for Node.js 20, which is [end-of-life](https://nodejs.org/en/about/previous-releases). The minimum Node.js version is now 22.12.0, the first Node.js 22 release that can `require()` ESM without a flag. Upgrade Node.js, or keep using hibp 15.x if you can't.
|
|
8
|
+
|
|
9
|
+
- [#625](https://github.com/wKovacs64/hibp/pull/625) [`c55048e`](https://github.com/wKovacs64/hibp/commit/c55048e82227cb3850269d5602882e3d35c60a9f) Thanks [@wKovacs64](https://github.com/wKovacs64)! - Correct `breachedAccount()` and `search().breaches` types to reflect truncation. Default and `truncate: true` searches expose only `Name`; `truncate: false` exposes full `Breach` objects. Dynamic boolean options allow either result shape. Both preserve `null`.
|
|
10
|
+
|
|
11
|
+
TypeScript consumers accessing full-only fields such as `Title` or `DataClasses` must pass `truncate: false` or narrow dynamic results. Runtime requests and responses are unchanged.
|
|
12
|
+
|
|
13
|
+
- [#624](https://github.com/wKovacs64/hibp/pull/624) [`8517b19`](https://github.com/wKovacs64/hibp/commit/8517b19819a2005da8de8c7a535152ddff83a440) Thanks [@wKovacs64](https://github.com/wKovacs64)! - Correct `Paste.Title` to an optional string and `Paste.Date` to `string | null`, matching the [haveibeenpwned.com paste model](https://haveibeenpwned.com/API/v3#PasteModel).
|
|
14
|
+
|
|
15
|
+
TypeScript consumers of `Paste`, `pasteAccount()`, and `search()` must handle absent titles and null dates before using them as strings. This can introduce compile errors with strict null checks; HTTP response handling is unchanged.
|
|
16
|
+
|
|
17
|
+
### Minor Changes
|
|
18
|
+
|
|
19
|
+
- [#619](https://github.com/wKovacs64/hibp/pull/619) [`9bf2740`](https://github.com/wKovacs64/hibp/commit/9bf27402f53aba1864228005e088f09aafcb95ed) Thanks [@wKovacs64](https://github.com/wKovacs64)! - Add `dataClassDescriptions` and the `DataClassDescription` type with lowercase `name` and `description` fields matching haveibeenpwned.com's live response.
|
|
20
|
+
|
|
21
|
+
- [#626](https://github.com/wKovacs64/hibp/pull/626) [`d47a032`](https://github.com/wKovacs64/hibp/commit/d47a03208b72c0750f1a2f74e8fad262a8059f1e) Thanks [@wKovacs64](https://github.com/wKovacs64)! - Add `sendEmail` and the `EmailAlias` type for domain verification approval emails.
|
|
22
|
+
|
|
23
|
+
- [#620](https://github.com/wKovacs64/hibp/pull/620) [`87f02b6`](https://github.com/wKovacs64/hibp/commit/87f02b6616e703804145dd90ec821da8e7bd989f) Thanks [@wKovacs64](https://github.com/wKovacs64)! - Add `breachedAccountRange` for six-character SHA-1 email hash prefixes, returning `hashSuffix` and `websites` results. Requires a Pro or High RPM subscription; callers must discard unrelated results per haveibeenpwned.com's terms of use.
|
|
24
|
+
|
|
25
|
+
- [#623](https://github.com/wKovacs64/hibp/pull/623) [`a451599`](https://github.com/wKovacs64/hibp/commit/a4515993bc0c3ca4828b4c91cde0e204767f344e) Thanks [@wKovacs64](https://github.com/wKovacs64)! - Add `generateDnsToken` and `verifyDnsToken` for domain verification through DNS.
|
|
26
|
+
|
|
27
|
+
- [#618](https://github.com/wKovacs64/hibp/pull/618) [`1df8169`](https://github.com/wKovacs64/hibp/commit/1df816999c3b0af19bcbd8ca7dca4631a4ca4e1b) Thanks [@wKovacs64](https://github.com/wKovacs64)! - Add the `isSpamList` option to `breaches()`: `true` selects spam-list breaches, `false` selects non-spam-list breaches, and omission applies no spam-list filter. Can be combined with `domain`.
|
|
28
|
+
|
|
29
|
+
### Patch Changes
|
|
30
|
+
|
|
31
|
+
- [#621](https://github.com/wKovacs64/hibp/pull/621) [`cc059b1`](https://github.com/wKovacs64/hibp/commit/cc059b183c586a4762f7bb59fc757d28077a7ec4) Thanks [@wKovacs64](https://github.com/wKovacs64)! - Complete the exported `Breach` type with required `IsStealerLog: boolean`,
|
|
32
|
+
`Attribution: string | null`, and `DisclosureUrl: string | null` fields, matching
|
|
33
|
+
[haveibeenpwned.com's live responses](https://haveibeenpwned.com/api/v3/breaches).
|
|
34
|
+
|
|
35
|
+
TypeScript consumers constructing `Breach` objects, including mocks and fixtures,
|
|
36
|
+
must supply these fields. Handle null when reading attribution or disclosure URLs.
|
|
37
|
+
The SDK continues returning the upstream response without transforming it.
|
|
38
|
+
|
|
39
|
+
- [#622](https://github.com/wKovacs64/hibp/pull/622) [`70c08fb`](https://github.com/wKovacs64/hibp/commit/70c08fbe4a805e5e0e929f219dbc47450f3ddd1d) Thanks [@wKovacs64](https://github.com/wKovacs64)! - Complete `SubscriptionStatus` with the [documented haveibeenpwned.com fields](https://haveibeenpwned.com/API/v3#SubscriptionStatus): `MaxBreachedDomains` (`number | null`), `IncludesBulkDomainAdd`, `IncludesAutoSubdomainVerification`, `IncludesCustomerDomains`, and `IncludesKAnon` (`boolean`). These fields are required, so TypeScript consumers constructing subscription status objects, including mocks and fixtures, must supply them. A `null` domain limit means unlimited domains. Runtime response handling is unchanged.
|
|
40
|
+
|
|
41
|
+
- [#636](https://github.com/wKovacs64/hibp/pull/636) [`99e437b`](https://github.com/wKovacs64/hibp/commit/99e437b40ca0380cbe0c2a14abe74117eabfba4f) Thanks [@wKovacs64](https://github.com/wKovacs64)! - Send the default `hibp <version>` User-Agent in every non-browser runtime. The SDK skipped it whenever a global `navigator` existed, which Node.js 21+, Bun, Deno, and Cloudflare Workers all define. Those requests went out with the runtime's User-Agent (e.g. `node`), or none at all on Cloudflare Workers. Browser pages and workers still send their own User-Agent, because setting one triggers a CORS preflight that haveibeenpwned.com rejects.
|
|
42
|
+
|
|
43
|
+
- [#635](https://github.com/wKovacs64/hibp/pull/635) [`f165d82`](https://github.com/wKovacs64/hibp/commit/f165d82983fd1be04c73277f2f4093599ac7dcfc) Thanks [@wKovacs64](https://github.com/wKovacs64)! - Document which HIBP plans (Core, Pro, High RPM) include each key-requiring function.
|
|
44
|
+
|
|
45
|
+
- [#633](https://github.com/wKovacs64/hibp/pull/633) [`5b645a5`](https://github.com/wKovacs64/hibp/commit/5b645a58b25fbb3b89da93a7e7a6b10feec60595) Thanks [@wKovacs64](https://github.com/wKovacs64)! - GET operations reject a 401 with the JSON body's `message` as `error.message` (e.g. `Access denied due to missing hibp-api-key.`) instead of the raw JSON body, matching domain verification POST operations. Plain-text 401 bodies are still used as-is, and an empty 401 body rejects with the HTTP status (e.g. `401 Unauthorized`). A 429 without a JSON body now rejects with `RateLimitError` carrying the HTTP status as its message, instead of `SyntaxError`.
|
|
46
|
+
|
|
3
47
|
## 15.2.1
|
|
4
48
|
|
|
5
49
|
### Patch Changes
|
package/MIGRATION.md
CHANGED
|
@@ -113,15 +113,15 @@
|
|
|
113
113
|
|
|
114
114
|
```javascript
|
|
115
115
|
// 4.x
|
|
116
|
-
import hibp from
|
|
116
|
+
import hibp from "hibp";
|
|
117
117
|
hibp.breachedAccount(/* ... */).then(/* ... */);
|
|
118
118
|
|
|
119
119
|
// 5.x (upgrade option 1, one-liner quick fix)
|
|
120
|
-
import * as hibp from
|
|
120
|
+
import * as hibp from "hibp";
|
|
121
121
|
hibp.breachedAccount(/* ... */).then(/* ... */);
|
|
122
122
|
|
|
123
123
|
// 5.x (upgrade option 2, more explicit but requires more code changes)
|
|
124
|
-
import { breachedAccount } from
|
|
124
|
+
import { breachedAccount } from "hibp";
|
|
125
125
|
breachedAccount(/* ... */).then(/* ... */);
|
|
126
126
|
```
|
|
127
127
|
|
|
@@ -183,13 +183,15 @@
|
|
|
183
183
|
1.0.8 (old):
|
|
184
184
|
|
|
185
185
|
```javascript
|
|
186
|
-
hibp.breachedAccount(account,
|
|
186
|
+
hibp.breachedAccount(account, "adobe.com", true).then(/* ... */);
|
|
187
187
|
```
|
|
188
188
|
|
|
189
189
|
2.0.0 (new):
|
|
190
190
|
|
|
191
191
|
```javascript
|
|
192
|
-
hibp
|
|
192
|
+
hibp
|
|
193
|
+
.breachedAccount(account, { domain: "adobe.com", truncate: true })
|
|
194
|
+
.then(/* ... */);
|
|
193
195
|
```
|
|
194
196
|
|
|
195
197
|
This change was made to make the API more expressive and reduce ambiguity. See the API
|
package/README.md
CHANGED
|
@@ -13,7 +13,6 @@
|
|
|
13
13
|
_An unofficial TypeScript SDK for [Troy Hunt][troy]'s [Have I been pwned?][haveibeenpwned] service._
|
|
14
14
|
|
|
15
15
|
[![npm Version][npm-image]][npm-url] [![Build Status][ci-image]][ci-url]
|
|
16
|
-
[![Code Coverage][coverage-image]][coverage-url]
|
|
17
16
|
[](#contributors-)
|
|
18
17
|
|
|
19
18
|
## Installation
|
|
@@ -28,78 +27,105 @@ In [Deno][deno]:
|
|
|
28
27
|
|
|
29
28
|
```ts
|
|
30
29
|
// Replace x.y.z with the desired hibp version
|
|
31
|
-
import * as hibp from
|
|
30
|
+
import * as hibp from "npm:hibp@x.y.z";
|
|
32
31
|
```
|
|
33
32
|
|
|
34
33
|
See the [browser](#using-in-the-browser) section below for information on how to use it in the
|
|
35
34
|
browser.
|
|
36
35
|
|
|
37
|
-
## Features
|
|
36
|
+
## Features
|
|
37
|
+
|
|
38
|
+
- All queries return a Promise
|
|
39
|
+
- Provide your own `AbortSignal` to cancel in-flight requests
|
|
40
|
+
- Available server-side (e.g., Node.js) and client-side (browser)
|
|
41
|
+
- Written in TypeScript, so all modules come fully typed
|
|
42
|
+
|
|
43
|
+
API features are grouped by the [HIBP plans][hibp-plans] whose [API key][get-api-key] unlocks
|
|
44
|
+
them.
|
|
45
|
+
|
|
46
|
+
#### No API key
|
|
38
47
|
|
|
39
48
|
- Get the most recently added breach
|
|
40
49
|
- Get a single breach event
|
|
41
|
-
- Get all breaches for an account 🔑
|
|
42
|
-
- Get all breached email addresses for a domain 🔑
|
|
43
50
|
- Get all breach events in the system
|
|
44
51
|
- Get all data classes
|
|
45
|
-
- Get
|
|
52
|
+
- Get data class names and descriptions
|
|
46
53
|
- [Securely][search-by-range] check a password to see if it has been exposed in a data breach
|
|
47
54
|
- Check a SHA-1 or NTLM prefix to see if it has been exposed in a data breach
|
|
48
|
-
|
|
49
|
-
-
|
|
50
|
-
|
|
51
|
-
- Get all
|
|
52
|
-
- Get all
|
|
53
|
-
-
|
|
54
|
-
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
|
|
56
|
+
#### ![Core][core-plan] / ![Pro][pro-plan] / ![High RPM][high-rpm-plan]
|
|
57
|
+
|
|
58
|
+
- Get all breaches for an account
|
|
59
|
+
- Get all pastes for an account
|
|
60
|
+
- Search for an account in both breaches and pastes at the same time
|
|
61
|
+
- Get your subscription status, domain limits, and plan capabilities
|
|
62
|
+
|
|
63
|
+
#### ![Core][core-plan] / ![Pro][pro-plan]
|
|
64
|
+
|
|
65
|
+
- Get all breached email addresses for a domain
|
|
66
|
+
- Get all subscribed domains
|
|
67
|
+
|
|
68
|
+
#### ![Pro][pro-plan] / ![High RPM][high-rpm-plan]
|
|
69
|
+
|
|
70
|
+
- Search breaches by a six-character SHA-1 email prefix
|
|
71
|
+
|
|
72
|
+
#### ![Pro][pro-plan]
|
|
73
|
+
|
|
74
|
+
- Get all stealer log domains for an email address
|
|
75
|
+
- Get all stealer log email aliases for an email domain
|
|
76
|
+
- Get all stealer log email addresses for a website domain
|
|
77
|
+
- Generate and verify DNS domain verification tokens
|
|
78
|
+
- Request domain verification approval emails
|
|
58
79
|
|
|
59
80
|
## Usage
|
|
60
81
|
|
|
61
82
|
```typescript
|
|
62
83
|
// import individual modules as needed
|
|
63
|
-
import { dataClasses, search } from
|
|
84
|
+
import { dataClasses, search } from "hibp";
|
|
64
85
|
|
|
65
86
|
// or, import all modules into a local namespace
|
|
66
|
-
import * as hibp from
|
|
87
|
+
import * as hibp from "hibp";
|
|
67
88
|
```
|
|
68
89
|
|
|
69
90
|
The following modules are available:
|
|
70
91
|
|
|
71
92
|
- [breach](API.md#breach)
|
|
72
93
|
- [breachedAccount](API.md#breachedaccount)
|
|
94
|
+
- [breachedAccountRange](API.md#breachedaccountrange)
|
|
73
95
|
- [breachedDomain](API.md#breacheddomain)
|
|
74
96
|
- [breaches](API.md#breaches)
|
|
75
97
|
- [dataClasses](API.md#dataclasses)
|
|
98
|
+
- [dataClassDescriptions](API.md#dataclassdescriptions)
|
|
99
|
+
- [generateDnsToken](API.md#generatednstoken)
|
|
76
100
|
- [latestBreach](API.md#latestbreach)
|
|
77
101
|
- [pasteAccount](API.md#pasteaccount)
|
|
78
102
|
- [pwnedPassword](API.md#pwnedpassword)
|
|
79
103
|
- [pwnedPasswordRange](API.md#pwnedpasswordrange)
|
|
80
104
|
- [search](API.md#search)
|
|
105
|
+
- [sendEmail](API.md#sendemail)
|
|
81
106
|
- [stealerLogsByEmail](API.md#stealerlogsbyemail)
|
|
82
107
|
- [stealerLogsByEmailDomain](API.md#stealerlogsbyemaildomain)
|
|
83
108
|
- [stealerLogsByWebsiteDomain](API.md#stealerlogsbywebsitedomain)
|
|
84
109
|
- [subscribedDomains](API.md#subscribeddomains)
|
|
85
110
|
- [subscriptionStatus](API.md#subscriptionstatus)
|
|
111
|
+
- [verifyDnsToken](API.md#verifydnstoken)
|
|
86
112
|
|
|
87
113
|
Please see the [API reference](API.md) for more detailed usage information and examples.
|
|
88
114
|
|
|
89
115
|
#### Quick-Start Example
|
|
90
116
|
|
|
91
117
|
```javascript
|
|
92
|
-
import { search } from
|
|
118
|
+
import { search } from "hibp";
|
|
93
119
|
|
|
94
120
|
async function main() {
|
|
95
121
|
try {
|
|
96
|
-
const data = await search(
|
|
122
|
+
const data = await search("someAccountOrEmail", { apiKey: "my-api-key" });
|
|
97
123
|
if (data.breaches || data.pastes) {
|
|
98
124
|
// Bummer...
|
|
99
125
|
console.log(data);
|
|
100
126
|
} else {
|
|
101
127
|
// Phew! We're clear.
|
|
102
|
-
console.log(
|
|
128
|
+
console.log("Good news — no pwnage found!");
|
|
103
129
|
}
|
|
104
130
|
} catch (err) {
|
|
105
131
|
// Something went wrong.
|
|
@@ -140,7 +166,7 @@ You have a couple of options for using this library in a browser environment:
|
|
|
140
166
|
```html
|
|
141
167
|
<script type="module">
|
|
142
168
|
// Replace x.y.z with the desired hibp version ↓ ↓ ↓
|
|
143
|
-
import { dataClasses } from
|
|
169
|
+
import { dataClasses } from "https://unpkg.com/hibp@x.y.z/dist/browser/hibp.module.js";
|
|
144
170
|
|
|
145
171
|
const logDataClasses = async () => {
|
|
146
172
|
console.table(await dataClasses());
|
|
@@ -178,17 +204,17 @@ This module is distributed under the [MIT License][license].
|
|
|
178
204
|
|
|
179
205
|
[npm-image]: https://img.shields.io/npm/v/hibp.svg?style=flat-square
|
|
180
206
|
[npm-url]: https://www.npmjs.com/package/hibp
|
|
181
|
-
[ci-image]:
|
|
182
|
-
https://img.shields.io/github/actions/workflow/status/wKovacs64/hibp/ci.yml?logo=github&style=flat-square
|
|
207
|
+
[ci-image]: https://img.shields.io/github/actions/workflow/status/wKovacs64/hibp/ci.yml?logo=github&style=flat-square
|
|
183
208
|
[ci-url]: https://github.com/wKovacs64/hibp/actions?query=workflow%3Aci
|
|
184
|
-
[coverage-image]: https://img.shields.io/codecov/c/github/wKovacs64/hibp/main.svg?style=flat-square
|
|
185
|
-
[coverage-url]: https://codecov.io/gh/wKovacs64/hibp/branch/main
|
|
186
209
|
[deno]: https://deno.com/
|
|
187
210
|
[troy]: https://www.troyhunt.com
|
|
188
211
|
[haveibeenpwned]: https://haveibeenpwned.com
|
|
189
212
|
[haveibeenpwned-rate-limiting]: https://haveibeenpwned.com/API/v3#RateLimiting
|
|
190
213
|
[search-by-range]: https://haveibeenpwned.com/API/v2#SearchingPwnedPasswordsByRange
|
|
191
|
-
[
|
|
214
|
+
[hibp-plans]: https://haveibeenpwned.com/Subscription
|
|
215
|
+
[core-plan]: https://img.shields.io/badge/Core-3c9f43?style=flat-square
|
|
216
|
+
[pro-plan]: https://img.shields.io/badge/Pro-187dbd?style=flat-square
|
|
217
|
+
[high-rpm-plan]: https://img.shields.io/badge/High%20RPM-ef4444?style=flat-square
|
|
192
218
|
[get-api-key]: https://haveibeenpwned.com/API/Key
|
|
193
219
|
[unpkg]: https://unpkg.com
|
|
194
220
|
[caniuse-esm]: https://caniuse.com/#feat=es6-module
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
2
|
-
`).filter(Boolean).reduce((u,l)=>{let[
|
|
1
|
+
var B="hibp",U="16.0.0";async function b({baseUrl:e,endpoint:t,headers:s,timeoutMs:r,signal:n,userAgent:o,queryParams:i,method:c,body:p}){let m={method:c,body:p,headers:K(o,s),signal:N(r,n)},u=C(e,t,i);return fetch(u,m)}function C(e,t,s){let r=e.replace(/\/$/g,""),n=t.startsWith("/")?t:`/${t}`,o=new URL(`${r}${n}`);if(s)for(let[i,c]of Object.entries(s))o.searchParams.set(i,c);return o.toString()}function K(e,t){let s={...t};return e?s["User-Agent"]=e:k()||(s["User-Agent"]=`${B} ${U}`),s}function N(e,t){let s=[];if(e&&s.push(AbortSignal.timeout(e)),t&&s.push(t),s.length!==0)return s.length===1?s[0]:AbortSignal.any(s)}function k(){return typeof document<"u"||"importScripts"in globalThis}var P={status:400,statusText:"Bad request \u2014 the account does not comply with an acceptable format."},E={status:401,body:`Your request to the API couldn't be authorised. Check you have the right value in the "hibp-api-key" header, refer to the documentation for more: https://haveibeenpwned.com/API/v3#Authorisation`},f={status:403,statusText:"Forbidden - access denied."};var T={status:404},h={headers:new Map([["retry-after","2"]]),status:429,body:{statusCode:429,message:"Rate limit is exceeded. Try again in 2 seconds."}};var g=class extends Error{retryAfterSeconds;constructor(t,s,r){super(s,r),this.name=this.constructor.name,this.retryAfterSeconds=typeof t=="string"?Number.parseInt(t,10):void 0}};function w(e){return`Request blocked, contact haveibeenpwned.com if this continues (Ray ID: ${e})`}function A({status:e,statusText:t}){return`${e} ${t}`.trim()}function y(e){try{let t=JSON.parse(e);if(typeof t=="object"&&t!==null&&"message"in t&&typeof t.message=="string"&&t.message.trim())return t.message}catch{}}function S(e,t={},s){let{apiKey:r,baseUrl:n="https://haveibeenpwned.com/api/v3",timeoutMs:o,signal:i,userAgent:c}=t,p={};return r&&(p["HIBP-API-Key"]=r),s&&(p["Content-Type"]="application/json"),b({baseUrl:n,endpoint:e,headers:p,timeoutMs:o,signal:i,userAgent:c,method:s?"POST":void 0,body:s&&JSON.stringify(s)})}async function a(e,t={}){let s=await S(e,t);if(s.ok)return s.json();switch(s.status){case P.status:throw new Error(P.statusText);case E.status:{let r=await s.text();throw new Error(y(r)??(r.trim()?r:A(s)))}case f.status:{let r=s.headers.get("cf-ray");throw r?new Error(w(r)):new Error(f.statusText)}case T.status:return null;case h.status:{let r=y(await s.text())??A(s),n=s.headers.get("retry-after");throw new g(n,r)}default:throw new Error(s.statusText)}}function j(e,t={}){return a(`/breach/${encodeURIComponent(e)}`,t)}function x(e,t={}){let{apiKey:s,domain:r,includeUnverified:n=!0,timeoutMs:o,signal:i,truncate:c=!0,baseUrl:p,userAgent:m}=t,u=`/breachedaccount/${encodeURIComponent(e)}?`,l=[];return r&&l.push(`domain=${encodeURIComponent(r)}`),n||l.push("includeUnverified=false"),c||l.push("truncateResponse=false"),a(`${u}${l.join("&")}`,{apiKey:s,baseUrl:p,timeoutMs:o,signal:i,userAgent:m})}function F(e,t={}){let{apiKey:s,baseUrl:r,timeoutMs:n,signal:o,userAgent:i}=t;return a(`/breachedaccount/range/${encodeURIComponent(e)}`,{apiKey:s,baseUrl:r,timeoutMs:n,signal:o,userAgent:i})}function $(e,t={}){let{apiKey:s,baseUrl:r,timeoutMs:n,signal:o,userAgent:i}=t,c=`/breacheddomain/${encodeURIComponent(e)}`;return a(c,{apiKey:s,baseUrl:r,timeoutMs:n,signal:o,userAgent:i})}function L(e={}){let{domain:t,isSpamList:s,baseUrl:r,timeoutMs:n,signal:o,userAgent:i}=e,c="/breaches?",p=[];return t&&p.push(`domain=${encodeURIComponent(t)}`),s!==void 0&&p.push(`isSpamList=${s}`),a(`${c}${p.join("&")}`,{baseUrl:r,timeoutMs:n,signal:o,userAgent:i})}function _(e={}){return a("/dataclasses",e)}function H(e={}){return a("/dataclasses/descriptions",e)}async function d(e,t,s){let r=await S(e,s,t);if(r.ok)return r;let{headers:n,status:o}=r,i=await r.text(),c=n.get("cf-ray"),p=/^text\/plain\b/i.test(n.get("content-type")??""),m=y(i)??(o===f.status&&c?w(c):void 0)??(p&&i.trim()?i:void 0)??A(r);throw o===h.status?new g(n.get("retry-after"),m):new Error(m)}async function W(e,t={}){return(await d("/domainVerification/generateDnsToken",{DomainName:e},t)).json()}function q(e={}){return a("/latestbreach",e)}function D(e,t={}){return a(`/pasteaccount/${encodeURIComponent(e)}`,t)}var M={status:400,body:"The hash prefix was not in a valid format"};async function O(e,t={}){let{baseUrl:s="https://api.pwnedpasswords.com",timeoutMs:r,signal:n,userAgent:o,addPadding:i=!1,mode:c="sha1"}=t,p={};i&&(p["Add-Padding"]="true");let m=await b({baseUrl:s,endpoint:e,headers:p,timeoutMs:r,signal:n,userAgent:o,queryParams:{mode:c}});if(m.ok)return m.text();if(m.status===M.status){let u=await m.text();throw new Error(u)}throw new Error(m.statusText)}async function R(e,t={}){let{baseUrl:s,timeoutMs:r,signal:n,userAgent:o,addPadding:i=!1,mode:c="sha1"}=t;return(await O(`/range/${encodeURIComponent(e)}`,{baseUrl:s,timeoutMs:r,signal:n,userAgent:o,addPadding:i,mode:c})).split(`
|
|
2
|
+
`).filter(Boolean).reduce((u,l)=>{let[v,I]=l.split(":");return u[v]=Number.parseInt(I,10),u},{})}async function Q(e,t={}){let[s,r]=await V(e);return(await R(s,t))[r]||0}async function V(e){if(typeof crypto=="object"&&crypto.subtle){let t=new TextEncoder().encode(e),s=await crypto.subtle.digest("SHA-1",t),n=Array.from(new Uint8Array(s)).map(o=>o.toString(16).padStart(2,"0")).join("").toUpperCase();return[n.slice(0,5),n.slice(5)]}throw new Error("The Web Crypto API is not available in this environment.")}async function G(e,t={}){let{apiKey:s,domain:r,truncate:n=!0,baseUrl:o,timeoutMs:i,signal:c,userAgent:p}=t,[m,u]=await Promise.all([x(e,{apiKey:s,domain:r,truncate:n,baseUrl:o,timeoutMs:i,signal:c,userAgent:p}),/^.+@.+$/.test(e)?D(e,{apiKey:s,baseUrl:o,timeoutMs:i,signal:c,userAgent:p}):null]);return{breaches:m,pastes:u}}async function Y(e,t,s={}){await d("/domainVerification/sendEmail",{DomainName:e,EmailAlias:t},s)}function J(e,t={}){let{apiKey:s,baseUrl:r,timeoutMs:n,signal:o,userAgent:i}=t,c=`/stealerlogsbyemail/${encodeURIComponent(e)}`;return a(c,{apiKey:s,baseUrl:r,timeoutMs:n,signal:o,userAgent:i})}function Z(e,t={}){let{apiKey:s,baseUrl:r,timeoutMs:n,signal:o,userAgent:i}=t,c=`/stealerlogsbyemaildomain/${encodeURIComponent(e)}`;return a(c,{apiKey:s,baseUrl:r,timeoutMs:n,signal:o,userAgent:i})}function z(e,t={}){let{apiKey:s,baseUrl:r,timeoutMs:n,signal:o,userAgent:i}=t,c=`/stealerlogsbywebsitedomain/${encodeURIComponent(e)}`;return a(c,{apiKey:s,baseUrl:r,timeoutMs:n,signal:o,userAgent:i})}function X(e={}){let{apiKey:t,baseUrl:s,timeoutMs:r,signal:n,userAgent:o}=e;return a("/subscribeddomains",{apiKey:t,baseUrl:s,timeoutMs:r,signal:n,userAgent:o})}async function ee(e={}){return a("/subscription/status",e)}async function te(e,t={}){await d("/domainVerification/verifyDnsToken",{DomainName:e},t)}export{g as RateLimitError,j as breach,x as breachedAccount,F as breachedAccountRange,$ as breachedDomain,L as breaches,H as dataClassDescriptions,_ as dataClasses,W as generateDnsToken,q as latestBreach,D as pasteAccount,Q as pwnedPassword,R as pwnedPasswordRange,G as search,Y as sendEmail,J as stealerLogsByEmail,Z as stealerLogsByEmailDomain,z as stealerLogsByWebsiteDomain,X as subscribedDomains,ee as subscriptionStatus,te as verifyDnsToken};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare function baseFetch({ baseUrl, endpoint, headers, timeoutMs, signal, userAgent, queryParams, }: {
|
|
1
|
+
export declare function baseFetch({ baseUrl, endpoint, headers, timeoutMs, signal, userAgent, queryParams, method, body, }: {
|
|
2
2
|
baseUrl: string;
|
|
3
3
|
endpoint: string;
|
|
4
4
|
headers?: Record<string, string>;
|
|
@@ -6,6 +6,8 @@ export declare function baseFetch({ baseUrl, endpoint, headers, timeoutMs, signa
|
|
|
6
6
|
signal?: AbortSignal;
|
|
7
7
|
userAgent?: string;
|
|
8
8
|
queryParams?: Record<string, string>;
|
|
9
|
+
method?: "POST";
|
|
10
|
+
body?: string;
|
|
9
11
|
}): Promise<Response>;
|
|
10
12
|
export declare function buildUrl(baseUrl: string, endpoint: string, queryParams?: Record<string, string>): string;
|
|
11
13
|
export declare function buildHeaders(userAgent?: string, extra?: Record<string, string>): Record<string, string>;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
// @ts-ignore - package-info.js is generated
|
|
2
|
-
import { PACKAGE_NAME, PACKAGE_VERSION } from
|
|
3
|
-
export async function baseFetch({ baseUrl, endpoint, headers, timeoutMs, signal, userAgent, queryParams, }) {
|
|
2
|
+
import { PACKAGE_NAME, PACKAGE_VERSION, } from "./haveibeenpwned/package-info.js";
|
|
3
|
+
export async function baseFetch({ baseUrl, endpoint, headers, timeoutMs, signal, userAgent, queryParams, method, body, }) {
|
|
4
4
|
const requestInit = {
|
|
5
|
+
method,
|
|
6
|
+
body,
|
|
5
7
|
headers: buildHeaders(userAgent, headers),
|
|
6
8
|
signal: buildSignal(timeoutMs, signal),
|
|
7
9
|
};
|
|
@@ -9,8 +11,10 @@ export async function baseFetch({ baseUrl, endpoint, headers, timeoutMs, signal,
|
|
|
9
11
|
return fetch(url, requestInit);
|
|
10
12
|
}
|
|
11
13
|
export function buildUrl(baseUrl, endpoint, queryParams) {
|
|
12
|
-
const base = baseUrl.replace(/\/$/g,
|
|
13
|
-
const normalizedEndpoint = endpoint.startsWith(
|
|
14
|
+
const base = baseUrl.replace(/\/$/g, "");
|
|
15
|
+
const normalizedEndpoint = endpoint.startsWith("/")
|
|
16
|
+
? endpoint
|
|
17
|
+
: `/${endpoint}`;
|
|
14
18
|
const url = new URL(`${base}${normalizedEndpoint}`);
|
|
15
19
|
if (queryParams) {
|
|
16
20
|
for (const [key, value] of Object.entries(queryParams)) {
|
|
@@ -22,10 +26,10 @@ export function buildUrl(baseUrl, endpoint, queryParams) {
|
|
|
22
26
|
export function buildHeaders(userAgent, extra) {
|
|
23
27
|
const headers = { ...extra };
|
|
24
28
|
if (userAgent) {
|
|
25
|
-
headers[
|
|
29
|
+
headers["User-Agent"] = userAgent;
|
|
26
30
|
}
|
|
27
|
-
else if (
|
|
28
|
-
headers[
|
|
31
|
+
else if (!isBrowser()) {
|
|
32
|
+
headers["User-Agent"] = `${PACKAGE_NAME} ${PACKAGE_VERSION}`;
|
|
29
33
|
}
|
|
30
34
|
return headers;
|
|
31
35
|
}
|
|
@@ -41,4 +45,13 @@ function buildSignal(timeoutMs, signal) {
|
|
|
41
45
|
return signals[0];
|
|
42
46
|
return AbortSignal.any(signals);
|
|
43
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Browser pages have a `document` and browser workers have `importScripts`.
|
|
50
|
+
* Browsers send their own User-Agent, and setting one would force a CORS
|
|
51
|
+
* preflight, which haveibeenpwned.com rejects. Server runtimes have neither
|
|
52
|
+
* global, and some (e.g. Cloudflare Workers) send no User-Agent of their own.
|
|
53
|
+
*/
|
|
54
|
+
function isBrowser() {
|
|
55
|
+
return typeof document !== "undefined" || "importScripts" in globalThis;
|
|
56
|
+
}
|
|
44
57
|
//# sourceMappingURL=base-fetch.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base-fetch.js","sourceRoot":"","sources":["../../../src/api/base-fetch.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,OAAO,
|
|
1
|
+
{"version":3,"file":"base-fetch.js","sourceRoot":"","sources":["../../../src/api/base-fetch.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,OAAO,EACL,YAAY,EACZ,eAAe,GAChB,MAAM,kCAAkC,CAAC;AAE1C,MAAM,CAAC,KAAK,UAAU,SAAS,CAAC,EAC9B,OAAO,EACP,QAAQ,EACR,OAAO,EACP,SAAS,EACT,MAAM,EACN,SAAS,EACT,WAAW,EACX,MAAM,EACN,IAAI,GAWL;IACC,MAAM,WAAW,GAAgB;QAC/B,MAAM;QACN,IAAI;QACJ,OAAO,EAAE,YAAY,CAAC,SAAS,EAAE,OAAO,CAAC;QACzC,MAAM,EAAE,WAAW,CAAC,SAAS,EAAE,MAAM,CAAC;KACvC,CAAC;IAEF,MAAM,GAAG,GAAG,QAAQ,CAAC,OAAO,EAAE,QAAQ,EAAE,WAAW,CAAC,CAAC;IAErD,OAAO,KAAK,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC;AACjC,CAAC;AAED,MAAM,UAAU,QAAQ,CACtB,OAAe,EACf,QAAgB,EAChB,WAAoC;IAEpC,MAAM,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IACzC,MAAM,kBAAkB,GAAG,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC;QACjD,CAAC,CAAC,QAAQ;QACV,CAAC,CAAC,IAAI,QAAQ,EAAE,CAAC;IACnB,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,GAAG,IAAI,GAAG,kBAAkB,EAAE,CAAC,CAAC;IAEpD,IAAI,WAAW,EAAE,CAAC;QAChB,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IA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|
retryAfterSeconds: number | undefined;
|
|
15
|
-
constructor(retryAfter: ReturnType<Headers[
|
|
15
|
+
constructor(retryAfter: ReturnType<Headers["get"]>, message: string | undefined, options?: ErrorOptions);
|
|
16
16
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { baseFetch } from
|
|
2
|
-
import { BAD_REQUEST, UNAUTHORIZED, FORBIDDEN, NOT_FOUND, TOO_MANY_REQUESTS } from
|
|
1
|
+
import { baseFetch } from "../base-fetch.js";
|
|
2
|
+
import { BAD_REQUEST, UNAUTHORIZED, FORBIDDEN, NOT_FOUND, TOO_MANY_REQUESTS, } from "./responses.js";
|
|
3
3
|
/**
|
|
4
4
|
* Custom error thrown when the haveibeenpwned.com API responds with 429 Too
|
|
5
5
|
* Many Requests. See the `retryAfterSeconds` property for the number of seconds
|
|
@@ -18,21 +18,79 @@ export class RateLimitError extends Error {
|
|
|
18
18
|
super(message, options);
|
|
19
19
|
this.name = this.constructor.name;
|
|
20
20
|
this.retryAfterSeconds =
|
|
21
|
-
typeof retryAfter ===
|
|
21
|
+
typeof retryAfter === "string"
|
|
22
|
+
? Number.parseInt(retryAfter, 10)
|
|
23
|
+
: undefined;
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
|
-
|
|
26
|
+
/** @internal */
|
|
27
|
+
export function blockedWithRayId(rayId) {
|
|
25
28
|
return `Request blocked, contact haveibeenpwned.com if this continues (Ray ID: ${rayId})`;
|
|
26
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Formats the HTTP status of a response, e.g. `429 Too Many Requests`.
|
|
32
|
+
*
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
export function statusLine({ status, statusText }) {
|
|
36
|
+
return `${status} ${statusText}`.trim();
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Reads the `message` of a JSON error body, or `undefined` if absent or blank.
|
|
40
|
+
*
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
export function jsonMessage(text) {
|
|
44
|
+
try {
|
|
45
|
+
const error = JSON.parse(text);
|
|
46
|
+
if (typeof error === "object" &&
|
|
47
|
+
error !== null &&
|
|
48
|
+
"message" in error &&
|
|
49
|
+
typeof error.message === "string" &&
|
|
50
|
+
error.message.trim()) {
|
|
51
|
+
return error.message;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
// Not JSON.
|
|
56
|
+
}
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Sends a request to the haveibeenpwned.com API, as a JSON POST when `body` is
|
|
61
|
+
* supplied.
|
|
62
|
+
*
|
|
63
|
+
* @internal
|
|
64
|
+
*/
|
|
65
|
+
export function requestApi(endpoint, options = {}, body) {
|
|
66
|
+
const { apiKey, baseUrl = "https://haveibeenpwned.com/api/v3", timeoutMs, signal, userAgent, } = options;
|
|
67
|
+
const headers = {};
|
|
68
|
+
if (apiKey)
|
|
69
|
+
headers["HIBP-API-Key"] = apiKey;
|
|
70
|
+
if (body)
|
|
71
|
+
headers["Content-Type"] = "application/json";
|
|
72
|
+
return baseFetch({
|
|
73
|
+
baseUrl,
|
|
74
|
+
endpoint,
|
|
75
|
+
headers,
|
|
76
|
+
timeoutMs,
|
|
77
|
+
signal,
|
|
78
|
+
userAgent,
|
|
79
|
+
method: body ? "POST" : undefined,
|
|
80
|
+
body: body && JSON.stringify(body),
|
|
81
|
+
});
|
|
82
|
+
}
|
|
27
83
|
/**
|
|
28
84
|
* Fetches data from the supplied API endpoint.
|
|
29
85
|
*
|
|
30
86
|
* HTTP status code 200 returns an Object (data found).
|
|
31
87
|
* HTTP status code 404 returns null (no data found).
|
|
32
88
|
* HTTP status code 400 throws an Error (bad request).
|
|
33
|
-
* HTTP status code 401 throws an Error (unauthorized)
|
|
89
|
+
* HTTP status code 401 throws an Error (unauthorized) with the body's JSON
|
|
90
|
+
* `message`, the body text, or the HTTP status.
|
|
34
91
|
* HTTP status code 403 throws an Error (forbidden).
|
|
35
|
-
* HTTP status code 429 throws
|
|
92
|
+
* HTTP status code 429 throws a RateLimitError (too many requests) with the
|
|
93
|
+
* body's JSON `message`, or the HTTP status.
|
|
36
94
|
*
|
|
37
95
|
* @internal
|
|
38
96
|
* @private
|
|
@@ -53,18 +111,7 @@ function blockedWithRayId(rayId) {
|
|
|
53
111
|
* Error
|
|
54
112
|
*/
|
|
55
113
|
export async function fetchFromApi(endpoint, options = {}) {
|
|
56
|
-
const
|
|
57
|
-
const headers = {};
|
|
58
|
-
if (apiKey)
|
|
59
|
-
headers['HIBP-API-Key'] = apiKey;
|
|
60
|
-
const response = await baseFetch({
|
|
61
|
-
baseUrl,
|
|
62
|
-
endpoint,
|
|
63
|
-
headers,
|
|
64
|
-
timeoutMs,
|
|
65
|
-
signal,
|
|
66
|
-
userAgent,
|
|
67
|
-
});
|
|
114
|
+
const response = await requestApi(endpoint, options);
|
|
68
115
|
if (response.ok)
|
|
69
116
|
return response.json();
|
|
70
117
|
switch (response.status) {
|
|
@@ -72,11 +119,11 @@ export async function fetchFromApi(endpoint, options = {}) {
|
|
|
72
119
|
throw new Error(BAD_REQUEST.statusText);
|
|
73
120
|
}
|
|
74
121
|
case UNAUTHORIZED.status: {
|
|
75
|
-
const
|
|
76
|
-
throw new Error(
|
|
122
|
+
const text = await response.text();
|
|
123
|
+
throw new Error(jsonMessage(text) ?? (text.trim() ? text : statusLine(response)));
|
|
77
124
|
}
|
|
78
125
|
case FORBIDDEN.status: {
|
|
79
|
-
const rayId = response.headers.get(
|
|
126
|
+
const rayId = response.headers.get("cf-ray");
|
|
80
127
|
if (rayId)
|
|
81
128
|
throw new Error(blockedWithRayId(rayId));
|
|
82
129
|
throw new Error(FORBIDDEN.statusText);
|
|
@@ -85,9 +132,9 @@ export async function fetchFromApi(endpoint, options = {}) {
|
|
|
85
132
|
return null;
|
|
86
133
|
}
|
|
87
134
|
case TOO_MANY_REQUESTS.status: {
|
|
88
|
-
const
|
|
89
|
-
const retryAfter = response.headers.get(
|
|
90
|
-
throw new RateLimitError(retryAfter,
|
|
135
|
+
const message = jsonMessage(await response.text()) ?? statusLine(response);
|
|
136
|
+
const retryAfter = response.headers.get("retry-after");
|
|
137
|
+
throw new RateLimitError(retryAfter, message);
|
|
91
138
|
}
|
|
92
139
|
default: {
|
|
93
140
|
throw new Error(response.statusText);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch-from-api.js","sourceRoot":"","sources":["../../../../src/api/haveibeenpwned/fetch-from-api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,
|
|
1
|
+
{"version":3,"file":"fetch-from-api.js","sourceRoot":"","sources":["../../../../src/api/haveibeenpwned/fetch-from-api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EACL,WAAW,EACX,YAAY,EACZ,SAAS,EACT,SAAS,EACT,iBAAiB,GAClB,MAAM,gBAAgB,CAAC;AAGxB;;;;;;GAMG;AACH,MAAM,OAAO,cAAe,SAAQ,KAAK;IACvC;;;;OAIG;IACI,iBAAiB,CAAqB;IAE7C,YACE,UAAsC,EACtC,OAA2B,EAC3B,OAAsB;QAEtB,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;QAClC,IAAI,CAAC,iBAAiB;YACpB,OAAO,UAAU,KAAK,QAAQ;gBAC5B,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,UAAU,EAAE,EAAE,CAAC;gBACjC,CAAC,CAAC,SAAS,CAAC;IAClB,CAAC;CACF;AAED,gBAAgB;AAChB,MAAM,UAAU,gBAAgB,CAAC,KAAa;IAC5C,OAAO,0EAA0E,KAAK,GAAG,CAAC;AAC5F,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,EA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,GAAG,CAAC,aAAa,CAAC,CAAC;YACvD,MAAM,IAAI,cAAc,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;QAChD,CAAC;QACD,SAAS,CAAC;YACR,MAAM,IAAI,KAAK,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;QACvC,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export declare const PACKAGE_NAME = "hibp";
|
|
2
|
-
export declare const PACKAGE_VERSION = "
|
|
2
|
+
export declare const PACKAGE_VERSION = "16.0.0";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { blockedWithRayId, jsonMessage, RateLimitError, requestApi, statusLine, } from "./fetch-from-api.js";
|
|
2
|
+
import { FORBIDDEN, TOO_MANY_REQUESTS } from "./responses.js";
|
|
3
|
+
/**
|
|
4
|
+
* Posts a JSON body to the supplied API endpoint.
|
|
5
|
+
*
|
|
6
|
+
* Failures reject with the first available reason: the JSON body's `message`,
|
|
7
|
+
* the Cloudflare Ray ID of a blocked (403) request, a plain-text body, or the
|
|
8
|
+
* HTTP status.
|
|
9
|
+
*
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
export async function postToApi(endpoint, body, options) {
|
|
13
|
+
const response = await requestApi(endpoint, options, body);
|
|
14
|
+
if (response.ok)
|
|
15
|
+
return response;
|
|
16
|
+
const { headers, status } = response;
|
|
17
|
+
const text = await response.text();
|
|
18
|
+
const rayId = headers.get("cf-ray");
|
|
19
|
+
const isPlainText = /^text\/plain\b/i.test(headers.get("content-type") ?? "");
|
|
20
|
+
const message = jsonMessage(text) ??
|
|
21
|
+
(status === FORBIDDEN.status && rayId
|
|
22
|
+
? blockedWithRayId(rayId)
|
|
23
|
+
: undefined) ??
|
|
24
|
+
(isPlainText && text.trim() ? text : undefined) ??
|
|
25
|
+
statusLine(response);
|
|
26
|
+
if (status === TOO_MANY_REQUESTS.status) {
|
|
27
|
+
throw new RateLimitError(headers.get("retry-after"), message);
|
|
28
|
+
}
|
|
29
|
+
throw new Error(message);
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=post-to-api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"post-to-api.js","sourceRoot":"","sources":["../../../../src/api/haveibeenpwned/post-to-api.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,WAAW,EACX,cAAc,EACd,UAAU,EACV,UAAU,GAEX,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAE9D;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAC7B,QAAgB,EAChB,IAA4B,EAC5B,OAAoB;IAEpB,MAAM,QAAQ,GAAG,MAAM,UAAU,CAAC,QAAQ,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;IAC3D,IAAI,QAAQ,CAAC,EAAE;QAAE,OAAO,QAAQ,CAAC;IAEjC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,QAAQ,CAAC;IACrC,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;IACnC,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACpC,MAAM,WAAW,GAAG,iBAAiB,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,IAAI,EAAE,CAAC,CAAC;IAC9E,MAAM,OAAO,GACX,WAAW,CAAC,IAAI,CAAC;QACjB,CAAC,MAAM,KAAK,SAAS,CAAC,MAAM,IAAI,KAAK;YACnC,CAAC,CAAC,gBAAgB,CAAC,KAAK,CAAC;YACzB,CAAC,CAAC,SAAS,CAAC;QACd,CAAC,WAAW,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,S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