@unkey/hono
Version:
<div align="center"> <h1 align="center">@unkey/hono</h1> <h5>Hono.js middleware for authenticating API keys</h5> </div>
71 lines (68 loc) • 2.34 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
unkey: () => unkey
});
module.exports = __toCommonJS(src_exports);
var import_api = require("@unkey/api");
var import_http_exception = require("hono/http-exception");
// package.json
var version = "1.5.0";
// src/index.ts
function unkey(config) {
return async (c, next) => {
const key = config.getKey ? config.getKey(c) : c.req.header("Authorization")?.replace("Bearer ", "") ?? null;
if (!key) {
return c.json({ error: "unauthorized" }, { status: 401 });
}
if (typeof key !== "string") {
return key;
}
const unkeyInstance = new import_api.Unkey({
rootKey: "public",
disableTelemetry: config.disableTelemetry,
wrapperSdkVersion: `@unkey/hono@${version}`
});
const res = await unkeyInstance.keys.verify({
key,
apiId: config.apiId,
tags: config.tags
});
if (res.error) {
if (config.onError) {
return config.onError(c, res.error);
}
throw new import_http_exception.HTTPException(500, {
message: `unkey error: [CODE: ${res.error.code}] - [REQUEST_ID: ${res.error.requestId}] - ${res.error.message} - read more at ${res.error.docs}`
});
}
if (!res.result.valid && config.handleInvalidKey) {
return config.handleInvalidKey(c, res.result);
}
c.set("unkey", res.result);
await next();
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
unkey
});
//# sourceMappingURL=index.js.map