@squarecloud/blob
Version:
Official Square Cloud Blob SDK for NodeJS
88 lines (85 loc) • 2.67 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/validation/assertions/handlers.ts
var handlers_exports = {};
__export(handlers_exports, {
handleAPIObjectAssertion: () => handleAPIObjectAssertion,
handleLiteralAssertion: () => handleLiteralAssertion
});
module.exports = __toCommonJS(handlers_exports);
// src/structures/error.ts
var SquareCloudBlobError = class _SquareCloudBlobError extends Error {
constructor(code, message, cause) {
super(message, { cause });
this.name = _SquareCloudBlobError.name;
this.message = this.getMessage(code);
}
getMessage(rawCode) {
const code = rawCode.replaceAll("_", " ").toLowerCase().replace(/(^|\s)\S/g, (L) => L.toUpperCase());
const message = this.message ? `: ${this.message}` : "";
return `${code}${message}`;
}
};
var SquareCloudValidationError = class _SquareCloudValidationError extends SquareCloudBlobError {
constructor(...args) {
super(...args);
this.name = _SquareCloudValidationError.name;
}
};
// src/validation/assertions/handlers.ts
function handleLiteralAssertion({
schema,
value,
expect,
code
}) {
try {
schema.parse(value);
} catch {
throw new SquareCloudValidationError(
code ? `INVALID_${code}` : "VALIDATION_ERROR",
`Expect ${expect}, got ${typeof value}`
);
}
}
function handleAPIObjectAssertion({
schema,
value,
code
}) {
const name = code.toLowerCase().replaceAll("_", " ");
try {
return schema.parse(value);
} catch (err) {
const cause = err.errors?.map((err2) => ({
...err2,
path: err2.path.join(" > ")
}));
throw new SquareCloudBlobError(
`INVALID_API_${code}`,
`Invalid ${name} object received from API`,
{ cause }
);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
handleAPIObjectAssertion,
handleLiteralAssertion
});
//# sourceMappingURL=handlers.js.map