UNPKG

graphqldoc

Version:
156 lines (155 loc) 5.73 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const url = require("url"); const introspection_1 = require("./introspection"); /** * Plugin Base implementation */ // @ts-ignore class Plugin { // getNavigations?: (buildForType?: string) => NavigationSectionInterface[] | PromiseLike<NavigationSectionInterface[]>; // getDocuments?: (buildForType?: string) => DocumentSectionInterface[] | PromiseLike<DocumentSectionInterface[]>; // getHeaders?: (buildForType?: string) => string[] | PromiseLike<string[]>; // getAssets?: () => string[] | PromiseLike<string[]>; constructor(document, projectPackage, graphqldocPackage) { this.document = document; this.projectPackage = projectPackage; this.graphqldocPackage = graphqldocPackage; this.queryType = null; this.mutationType = null; this.subscriptionType = null; this.typeMap = {}; this.directiveMap = {}; this.document.types = this.document.types ? this.document.types.sort(sortTypes) : []; this.document.directives = this.document.directives ? this.document.directives.sort((a, b) => a.name.localeCompare(b.name)) : []; this.document.types.forEach((type) => { this.typeMap[type.name] = type; }); this.document.directives.forEach((directive) => { this.directiveMap[directive.name] = directive; }); if (document.queryType) { this.queryType = this.typeMap[document.queryType.name]; } if (document.mutationType) { this.mutationType = this.typeMap[document.mutationType.name]; } if (document.subscriptionType) { this.subscriptionType = this.typeMap[document.subscriptionType.name]; } } static collect(collection) { let result = []; collection .forEach(item => { if (Array.isArray(item)) result = result.concat(item); }); return result; } static collectNavigations(plugins, buildForType) { return __awaiter(this, void 0, void 0, function* () { const navigationCollection = yield Promise .all(plugins.map(plugin => { return plugin.getNavigations ? plugin.getNavigations(buildForType) : null; })); return Plugin.collect(navigationCollection); }); } static collectDocuments(plugins, buildForType) { return __awaiter(this, void 0, void 0, function* () { const navigationCollection = yield Promise .all(plugins.map(plugin => { return plugin.getDocuments ? plugin.getDocuments(buildForType) : null; })); return Plugin.collect(navigationCollection); }); } static collectHeaders(plugins, buildForType) { return __awaiter(this, void 0, void 0, function* () { const headerCollection = yield Promise .all(plugins.map(plugin => { return plugin.getHeaders ? plugin.getHeaders(buildForType) : null; })); return Plugin.collect(headerCollection); }); } static collectAssets(plugins) { return __awaiter(this, void 0, void 0, function* () { const assetCollection = yield Promise .all(plugins.map(plugin => { return plugin.getAssets ? plugin.getAssets() : null; })); return Plugin.collect(assetCollection); }); } url(type) { return url.resolve(this.projectPackage.graphqldoc.baseUrl, introspection_1.getFilenameOf(type)); } } exports.Plugin = Plugin; /** * NavigationSectionInterface short implementation */ class NavigationSection { constructor(title, items = []) { this.title = title; this.items = items; } } exports.NavigationSection = NavigationSection; /** * NavigationItemInterface short implementation */ class NavigationItem { constructor(text, href, isActive) { this.text = text; this.href = href; this.isActive = isActive; } } exports.NavigationItem = NavigationItem; /** * DocumentSectionInterface short implementation */ class DocumentSection { constructor(title, description) { this.title = title; this.description = description; } } exports.DocumentSection = DocumentSection; function priorityType(type) { return (0 /* initial priority */ | (type.name[0] === '_' ? 1 : 0) /* protected type priority */ | (type.name[0] === '_' && type.name[1] === '_' ? 2 : 0) /* spec type priority */); } function sortTypes(a, b) { const priorityA = priorityType(a); const priorityB = priorityType(b); if (priorityA === priorityB) { return a.name.localeCompare(b.name); } else { return priorityA - priorityB; } } exports.sortTypes = sortTypes;