@prismicio/types-internal
Version:
Prismic types for Custom Types and Prismic Data
34 lines (33 loc) • 1.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.withKey = exports.hasContentType = exports.hasFieldContentType = void 0;
const tslib_1 = require("tslib");
const Either_1 = require("fp-ts/lib/Either");
const t = (0, tslib_1.__importStar)(require("io-ts"));
const uuid_1 = require("uuid");
const fields_1 = require("./fields");
function hasFieldContentType(obj) {
var _a;
return (hasContentType(obj) &&
obj.__TYPE__ === fields_1.FieldContentType &&
((_a = obj) === null || _a === void 0 ? void 0 : _a.type) !== undefined &&
typeof obj.type === "string" &&
(0, Either_1.isRight)(fields_1.FieldType.decode(obj.type)));
}
exports.hasFieldContentType = hasFieldContentType;
function hasContentType(obj) {
return typeof obj === "object" && obj !== null && "__TYPE__" in obj;
}
exports.hasContentType = hasContentType;
const isUuid = (input) => typeof input === "string" && (0, uuid_1.validate)(input);
const uuidWithFallback = new t.Type("UUID", isUuid, (u, c) => {
if (typeof u === "undefined")
return t.success((0, uuid_1.v4)());
if (isUuid(u))
return t.success(u);
return t.failure(u, c);
}, t.identity);
function withKey(codec) {
return t.intersection([t.strict({ key: uuidWithFallback }), codec]);
}
exports.withKey = withKey;