UNPKG

@apollo/core-schema

Version:
125 lines 5.03 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LinkUrl = void 0; const recall_1 = __importStar(require("@protoplasm/recall")); const url_1 = require("url"); const version_1 = __importDefault(require("./version")); class LinkUrl { constructor(href, name, version) { this.href = href; this.name = name; this.version = version; } static from(input) { if (typeof input === 'string') return this.parse(input); return input !== null && input !== void 0 ? input : undefined; } static parse(input) { var _a, _b, _c; const url = new url_1.URL(input); const path = (_a = url.pathname) !== null && _a !== void 0 ? _a : ''; const parts = rsplit(path, '/'); const nameVerPart = (_b = parts.next().value) !== null && _b !== void 0 ? _b : undefined; const namePart = (_c = parts.next().value) !== null && _c !== void 0 ? _c : undefined; const version = version_1.default.parse(nameVerPart); const name = version ? parseName(namePart) : parseName(nameVerPart); url.search = ''; url.password = ''; url.username = ''; url.hash = ''; return this.canon(url.href, name !== null && name !== void 0 ? name : undefined, version !== null && version !== void 0 ? version : undefined); } static get GRAPHQL_SPEC() { return LinkUrl.from('https://specs.graphql.org'); } *suggestNames() { if (this.name) yield this.name; if (this.name && this.version) { yield this.name + '_' + this.version.major; yield this.name + '_' + this.version.major + '_' + this.version.minor; } const url = new url_1.URL(this.href); if (url.hostname) { const parts = url.hostname.split('.'); const [longest] = [...parts].sort((a, b) => b.length - a.length); if (this.name) { yield longest + '_' + this.name; yield parts.join('_') + '_' + this.name; } else { yield longest; yield parts.join('_'); } } const baseName = this.name || 'linked_schema'; for (let i = 1;; ++i) { yield baseName + '_' + i; } } static canon(href, name, version) { return new this(href, name, version); } toString() { return this.href; } } __decorate([ (0, recall_1.use)(recall_1.default) ], LinkUrl, "canon", null); exports.LinkUrl = LinkUrl; exports.default = LinkUrl; function* rsplit(haystack, sep) { let index = haystack.lastIndexOf(sep); const len = haystack.length; const sepLen = sep.length; let lastIndex = len; while (index !== -1 && lastIndex > 0) { yield haystack.substring(index + sepLen, lastIndex); lastIndex = index; index = haystack.lastIndexOf(sep, index - 1); } yield haystack.substring(0, lastIndex); } const NAME_RE = /^[a-zA-Z0-9\-]+$/; function parseName(name) { if (!name) return null; if (!NAME_RE.test(name)) return null; if (name.startsWith('-') || name.endsWith('-')) return null; return name; } //# sourceMappingURL=link-url.js.map