@centure/node-sdk
Version:
A Typescript SDK for interacting with Centure's API
119 lines • 5.46 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CentureClient = void 0;
const errors_1 = require("./errors");
const types_1 = require("./types");
/**
* Client for interacting with the Centure API
*/
class CentureClient {
/**
* Creates a new Centure API client
* @param options - Configuration options
* @throws {MissingApiKeyError} If no API key is provided
*/
constructor(options) {
this.baseUrl = (options === null || options === void 0 ? void 0 : options.baseUrl) || "https://api.centure.ai";
// Get API key from options or environment variable
const apiKey = (options === null || options === void 0 ? void 0 : options.apiKey) || process.env.CENTURE_API_KEY;
if (!apiKey) {
throw new errors_1.MissingApiKeyError();
}
this.apiKey = apiKey;
// Use custom fetch or global fetch
this.fetchFn = (options === null || options === void 0 ? void 0 : options.fetch) || fetch;
this.fetchOptions = (options === null || options === void 0 ? void 0 : options.fetchOptions) || {};
}
/**
* Makes an API request with proper error handling
*/
request(endpoint, init) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${this.baseUrl}${endpoint}`;
const headers = Object.assign(Object.assign({ Authorization: `Bearer ${this.apiKey}` }, this.fetchOptions.headers), init === null || init === void 0 ? void 0 : init.headers);
const response = yield this.fetchFn(url, Object.assign(Object.assign(Object.assign({}, this.fetchOptions), init), { headers }));
// Handle successful response
if (response.ok) {
return response.json();
}
// Handle error responses
let errorBody;
try {
errorBody = yield response.json();
}
catch (_a) {
// If we can't parse the error body, continue with undefined
}
const errorMessage = (errorBody === null || errorBody === void 0 ? void 0 : errorBody.error) ||
(errorBody === null || errorBody === void 0 ? void 0 : errorBody.message) ||
(errorBody === null || errorBody === void 0 ? void 0 : errorBody.detail) ||
response.statusText;
// Throw appropriate error based on status code
switch (response.status) {
case types_1.HttpStatus.BAD_REQUEST:
throw new errors_1.BadRequestError(errorMessage, errorBody);
case types_1.HttpStatus.UNAUTHORIZED:
throw new errors_1.UnauthorizedError(errorMessage, errorBody);
case types_1.HttpStatus.PAYLOAD_TOO_LARGE:
throw new errors_1.PayloadTooLargeError(errorMessage, errorBody);
case types_1.HttpStatus.INTERNAL_SERVER_ERROR:
throw new errors_1.InternalServerError(errorMessage, errorBody);
default:
throw new errors_1.CentureApiError(errorMessage, response.status, errorBody);
}
});
}
/**
* Scans text content for prompt injection attacks
* @param content - The text content to scan
* @returns Scan results with safety assessment and detected categories
*/
scanText(content) {
return __awaiter(this, void 0, void 0, function* () {
const body = { content };
return this.request("/v1/prompt-injection/text", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
});
}
/**
* Scans an image for prompt injection attacks
* @param image - Base64-encoded image string OR Buffer (will be converted to base64)
* @returns Scan results with safety assessment and detected categories
*/
scanImage(image) {
return __awaiter(this, void 0, void 0, function* () {
let base64Image;
// Convert Buffer to base64 if needed
if (Buffer.isBuffer(image)) {
base64Image = image.toString("base64");
}
else {
base64Image = image;
}
const body = { image: base64Image };
return this.request("/v1/prompt-injection/image", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
});
}
}
exports.CentureClient = CentureClient;
//# sourceMappingURL=client.js.map