screenshotgenie
Version:
Node.js client library for Screenshot Genie
105 lines (103 loc) • 4.03 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);
// dist/index.js
var index_exports = {};
__export(index_exports, {
ScreenshotGenie: () => ScreenshotGenie,
isImageKeyExpired: () => isImageKeyExpired
});
module.exports = __toCommonJS(index_exports);
var import_promises = require("fs/promises");
var ScreenshotGenie = class {
constructor(config = {}) {
const { apiKey = process.env.SCREENSHOT_GENIE_API_KEY, host = "https://screenshotgenie.com" } = config;
const providedApiKey = apiKey || process.env.SCREENSHOT_GENIE_API_KEY;
if (!providedApiKey) {
throw new Error("ScreenshotGenie requires an API key. Either provide it in the constructor options or set the SCREENSHOT_GENIE_API_KEY environment variable.");
}
this.apiKey = providedApiKey;
this.host = host;
}
async createImageKey(imageGenerationInputs) {
if (!imageGenerationInputs.browserZoom) {
imageGenerationInputs.browserZoom = 1;
}
const response = await fetch(`${this.host}/api/create-image-key`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`
},
body: JSON.stringify({ imageGenerationInputs })
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to create image key. Status: ${response.status}, Body: ${errorText}`);
}
const json = await response.json();
if (json.ok === false) {
throw new Error(json.body);
}
console.log("json", json);
return json.imageKey;
}
getImageUrl(imageKey, width) {
if (!imageKey) {
throw new Error("`imageKey` is required to generate the image URL.");
}
if (!width) {
throw new Error("`width` is required to generate the image URL.");
}
const imageId = imageKey.split("_expires_")[0];
const imageFileName = `${imageId}_${width}px.png`;
return `${this.host}/images/${imageFileName}`;
}
// NodeJS only
async downloadImage(url, filePath) {
const response = await fetch(url);
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to download image. Status: ${response.status}, Body: ${errorText}`);
}
const buffer = Buffer.from(await response.arrayBuffer());
await (0, import_promises.writeFile)(filePath, buffer);
}
};
var isImageKeyExpired = (imageKey) => {
const parts = imageKey.split("_expires_");
if (parts.length !== 2) {
throw new Error("Invalid image key format. Expected format: {imageHash}_expires_{expirationDate}");
}
const expirationDateStr = parts[1];
if (!/^\d{8}$/.test(expirationDateStr)) {
throw new Error("Invalid expiration date format in image key. Expected format: YYYYMMDD");
}
const year = parseInt(expirationDateStr.substring(0, 4), 10);
const month = parseInt(expirationDateStr.substring(4, 6), 10) - 1;
const day = parseInt(expirationDateStr.substring(6, 8), 10);
const expirationDate = new Date(year, month, day);
expirationDate.setHours(23, 59, 59, 999);
const currentDate = /* @__PURE__ */ new Date();
return currentDate > expirationDate;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ScreenshotGenie,
isImageKeyExpired
});