json-schema-typescript-generator
Version:
Generate typescript types from json schemas
88 lines • 2.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseSchemaRef = exports.parseSchemaId = void 0;
const parseAuthority = (url) => {
const protocolMatch = url.match(/^([^:/]+:\/\/)/);
if (!protocolMatch) {
return;
}
const protocol = protocolMatch[1];
const withoutProtocol = url.substring(protocol.length);
const endpointStartMatch = withoutProtocol.match(/^[^/#]+([/#].*)/);
const endpoint = endpointStartMatch ? endpointStartMatch[1] : undefined;
const authority = endpoint
? url.substring(0, url.length - endpoint.length)
: url;
return authority;
};
const parsePath = (endpoint) => {
const pathMatch = endpoint.match(/^\/([^#]+)/);
if (!pathMatch) {
return;
}
const match = pathMatch[1];
const lastSlashIndex = match.lastIndexOf('/');
const folder = lastSlashIndex <= 0
? '.'
: match.substring(0, lastSlashIndex);
const name = lastSlashIndex === -1
? match
: match.substring(lastSlashIndex + 1);
return {
folder,
name,
};
};
const parseFragment = (url) => {
const pathMatch = url.match(/^[^#]*#\/?(?:(?:\$defs|definitions)\/)?(.*)$/);
if (!pathMatch) {
return;
}
return pathMatch[1];
};
const parseSchemaId = (id) => {
if (!id) {
return;
}
const authority = parseAuthority(id);
if (authority === undefined && !id.startsWith('/')) {
return;
}
const endpoint = authority === undefined
? id
: id.substring(authority.length);
const path = parsePath(endpoint);
if (!path) {
return;
}
return {
authority,
folder: path.folder,
name: path.name,
};
};
exports.parseSchemaId = parseSchemaId;
const parseSchemaRef = (ref) => {
if (!ref) {
return;
}
const authority = parseAuthority(ref);
const endpoint = authority === undefined
? ref
: ref.substring(authority.length);
const path = parsePath(endpoint);
const fragment = parseFragment(ref);
if (!path) {
return fragment
? { fragment }
: undefined;
}
return {
authority,
folder: path.folder,
name: path.name,
fragment,
};
};
exports.parseSchemaRef = parseSchemaRef;
//# sourceMappingURL=parse.js.map