An unofficial TypeScript SDK for the 'Have I been pwned?' service.
翻譯準備中
正在載入完整 README。
檢視完整 README ↗An unofficial TypeScript SDK for Troy Hunt's Have I been pwned? service.
In Node.js:
npm install hibp
In Deno:
// Replace x.y.z with the desired hibp version
import * as hibp from "npm:[email protected]";
See the browser section below for information on how to use it in the browser.
AbortSignal to cancel in-flight requestsAPI features are grouped by the HIBP plans whose API key unlocks them.
// import individual modules as needed
import { dataClasses, search } from "hibp";
// or, import all modules into a local namespace
import * as hibp from "hibp";
The following modules are available:
Please see the API reference for more detailed usage information and examples.
import { search } from "hibp";
async function main() {
try {
const data = await search("someAccountOrEmail", { apiKey: "my-api-key" });
if (data.breaches || data.pastes) {
// Bummer...
console.log(data);
} else {
// Phew! We're clear.
console.log("Good news — no pwnage found!");
}
} catch (err) {
// Something went wrong.
console.log(err.message);
}
}
void main();
The haveibeenpwned.com API rate limits requests to prevent abuse. In
the event you get rate limited, the module will throw a custom RateLimitError which will include a
retryAfterSeconds property so you know when you can try the call again (as a number, unless the
remote API did not provide one, in which case it will be undefined - but that should never
happen).
You have a couple of options for using this library in a browser environment:
Bundled
The most efficient and recommended method is to bundle it with client-side code using a module bundler, most likely dictated by your web application framework of choice.
ESM for Browsers
Alternatively, you can also import the library directly in your HTML via <script type="module">
tags in modern browsers. The pre-bundled module is available through the
unpkg CDN, but you must specify the full path (including the file extension). It's also
strongly recommended to include the exact version number as well, otherwise the latest tag will
be used, which could be dangerous if/when there are breaking changes made to the API. See
unpkg for details and advanced version specification, but you will probably want to do
the following (replacing x.y.z with the version you want):
<script type="module">
// Replace x.y.z with the desired hibp version ↓ ↓ ↓
import { dataClasses } from "https://unpkg.com/[email protected]/dist/browser/hibp.module.js";
const logDataClasses = async () => {
console.table(await dataClasses());
};
logDataClasses();
</script>
For more information on ESM in the browser, check out Using JS modules in the browser.
Test hibp in your browser with StackBlitz.
以上為 README 的開頭部分。其餘內容請閱讀原文。 檢視完整 README ↗