hibp
Version:
An unofficial TypeScript SDK for the 'Have I been pwned?' service.
93 lines • 3.02 kB
JavaScript
import { fetchFromApi } from './api/haveibeenpwned/fetch-from-api.js';
/**
* Fetches breach data for a specific account.
*
* 🔑 `haveibeenpwned.com` requires an API key from
* https://haveibeenpwned.com/API/Key for the `breachedaccount` endpoint. The
* `apiKey` option here is not explicitly required, but direct requests made
* without it will fail (unless you specify a `baseUrl` to a proxy that inserts
* a valid API key on your behalf).
*
* @param {string} account a username or email address
* @param {object} [options] a configuration object
* @param {string} [options.apiKey] an API key from
* https://haveibeenpwned.com/API/Key (default: undefined)
* @param {string} [options.domain] a domain by which to filter the results
* (default: all domains)
* @param {boolean} [options.includeUnverified] include "unverified" breaches in
* the results (default: true)
* @param {number} [options.timeoutMs] timeout for the request in milliseconds
* (default: none)
* @param {boolean} [options.truncate] truncate the results to only include
* the name of each breach (default: true)
* @param {string} [options.baseUrl] a custom base URL for the
* haveibeenpwned.com API endpoints (default:
* `https://haveibeenpwned.com/api/v3`)
* @param {string} [options.userAgent] a custom string to send as the User-Agent
* field in the request headers (default: `hibp <version>`)
* @returns {(Promise<Breach[]> | Promise<null>)} a Promise which resolves to an
* array of breach objects (or null if no breaches were found), or rejects with
* an Error
* @example
* try {
* const data = await breachedAccount("foo", { apiKey: "my-api-key" });
* if (data) {
* // ...
* } else {
* // ...
* }
* } catch (err) {
* // ...
* }
* @example
* try {
* const data = await breachedAccount("bar", {
* includeUnverified: false,
* baseUrl: "https://my-hibp-proxy:8080",
* });
* if (data) {
* // ...
* } else {
* // ...
* }
* } catch (err) {
* // ...
* }
* @example
* try {
* const data = await breachedAccount("baz", {
* apiKey: "my-api-key",
* domain: "adobe.com",
* truncate: false,
* userAgent: "my-app 1.0",
* });
* if (data) {
* // ...
* } else {
* // ...
* }
* } catch (err) {
* // ...
* }
*/
export function breachedAccount(account, options = {}) {
const { apiKey, domain, includeUnverified = true, timeoutMs, truncate = true, baseUrl, userAgent, } = options;
const endpoint = `/breachedaccount/${encodeURIComponent(account)}?`;
const params = [];
if (domain) {
params.push(`domain=${encodeURIComponent(domain)}`);
}
if (!includeUnverified) {
params.push('includeUnverified=false');
}
if (!truncate) {
params.push('truncateResponse=false');
}
return fetchFromApi(`${endpoint}${params.join('&')}`, {
apiKey,
baseUrl,
timeoutMs,
userAgent,
});
}
//# sourceMappingURL=breached-account.js.map