@allgemein/schema-api
Version:
Library for schema api
97 lines • 3.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonSchema = void 0;
const lodash_1 = require("lodash");
const JsonSchema7Serializer_1 = require("./JsonSchema7Serializer");
const Constants_1 = require("./Constants");
const base_1 = require("@allgemein/base");
const JsonSchema7Unserializer_1 = require("./JsonSchema7Unserializer");
class JsonSchema {
static async request(addr, opts = {}) {
try {
if (addr.startsWith('file:///')) {
const buffer = await base_1.PlatformUtils.readFile(addr.replace('file://', ''));
const strValue = buffer.toString((0, lodash_1.get)(opts, 'encoding', 'utf8'));
return JSON.parse(strValue);
}
else if (addr.startsWith('.')) {
const cwd = opts.cwd ? opts.cwd : process.cwd();
const path = base_1.PlatformUtils.join(cwd, addr);
const buffer = await base_1.PlatformUtils.readFile(path);
const strValue = buffer.toString((0, lodash_1.get)(opts, 'encoding', 'utf8'));
return JSON.parse(strValue);
}
else if (base_1.PlatformUtils.isAbsolute(addr)) {
const buffer = await base_1.PlatformUtils.readFile(addr);
const strValue = buffer.toString((0, lodash_1.get)(opts, 'encoding', 'utf8'));
return JSON.parse(strValue);
}
else {
const got = base_1.PlatformUtils.load('got');
const response = await got(addr, opts);
return JSON.parse(response.body);
}
}
catch (e) {
console.error(e);
return {};
}
}
static getSerializer(options = { version: Constants_1.DRAFT_07 }) {
options = options || {};
(0, lodash_1.defaults)(options, { version: Constants_1.DRAFT_07 });
switch (options.version) {
case Constants_1.DRAFT_07:
return new JsonSchema7Serializer_1.JsonSchema7Serializer(options);
}
return null;
}
static getUnserializer(options = { version: Constants_1.DRAFT_07 }) {
options = options || {};
(0, lodash_1.defaults)(options, { version: Constants_1.DRAFT_07 });
switch (options.version) {
case Constants_1.DRAFT_07:
return new JsonSchema7Unserializer_1.JsonSchema7Unserializer(options);
}
return null;
}
static serialize(klass, options) {
const serializer = JsonSchema.getSerializer(options);
return serializer.serialize(klass);
}
static unserialize(data, options) {
options = options ? options : {};
const schema = this.detectSchemaVersion(data);
options.version = schema;
const serializer = this.getUnserializer(options);
return serializer.unserialize(data);
}
static detectSchemaVersion(schema, fallback = Constants_1.DRAFT_07) {
if (schema['$schema']) {
if ((0, lodash_1.isString)(schema['$schema'])) {
const match = schema['$schema'].match(/json-schema.org\/(.*)\/schema/);
if (match && match[1]) {
return match[1];
}
}
}
return fallback;
}
getName() {
return Constants_1.JSON_SCHEMA_SERIALIZER;
}
getSerializer(options) {
return JsonSchema.getSerializer(options);
}
getUnserializer(options) {
return JsonSchema.getUnserializer(options);
}
serialize(klass, options) {
return JsonSchema.serialize(klass, options);
}
unserialize(data, options) {
return JsonSchema.unserialize(data, options);
}
}
exports.JsonSchema = JsonSchema;
//# sourceMappingURL=JsonSchema.js.map