UNPKG

@sapphire/plugin-api

Version:

Plugin for @sapphire/framework to expose a REST API

179 lines (177 loc) 5.19 kB
import { __name, __publicField } from '../../../chunk-S573YWRP.mjs'; import { isNullish } from '@sapphire/utilities'; import { RouterNode } from './RouterNode.mjs'; var _RouterBranch = class _RouterBranch { constructor(name, dynamic, parent) { /** * The name of the branch. */ __publicField(this, "name"); /** * Whether or not the branch is dynamic. */ __publicField(this, "dynamic"); /** * The parent branch, if any. */ __publicField(this, "parent"); /** * The node this branch is associated with. */ __publicField(this, "node", new RouterNode(this)); /** * The methods supported by the branch's node or any of its children. */ __publicField(this, "supportedMethods", []); __publicField(this, "_staticChildren", []); __publicField(this, "_dynamicChild", null); this.name = name; this.dynamic = dynamic; this.parent = parent; } /** * The path representing this branch * @version 7.0.0 */ get path() { return this.parent ? `${this.parent}/${this}` : `${this}`; } /** * The branches the branch is associated with * @version 7.0.0 */ get children() { return this._staticChildren.concat(this._dynamicChild ?? []); } /** * Whether or not the branch is empty * @version 7.0.0 */ get empty() { return this._staticChildren.length === 0 && this._dynamicChild === null; } /** * Tries to find a branch given a path * @version 7.0.0 * * @param parts The parts of a path to find a node from * @returns The branch found, or null if not found */ find(parts) { return this._find(parts, 0); } /** * Checks if the given name matches the branch * @version 7.0.0 * * @param name The name to match * @returns Whether or not the branch matches the name */ matches(name) { return this.dynamic || this.name === name; } /** * Returns the string representation of the branch * @version 7.0.0 * * @returns The string representation of the branch */ toString() { return this.dynamic ? `[${this.name}]` : this.name; } *nodes() { yield this.node; for (const child of this._staticChildren) { yield* child.nodes(); } if (this._dynamicChild) { yield* this._dynamicChild.nodes(); } } _add(parts, index, route) { const result = this._performAdd(parts, index, route); this._updateSupportedChildrenMethods(); return result; } _remove(parts, index, route) { const result = this._performRemove(parts, index, route); if (result) this._updateSupportedChildrenMethods(); return result; } _performAdd(parts, index, route) { if (index >= parts.length) { for (const method of route.methods) { this.node.set(method, route); } return this.node; } const part = parts[index]; const child = this._staticChildren.find((branch2) => branch2.matches(part)); if (child) { return child._add(parts, index + 1, route); } if (this._dynamicChild) { return this._dynamicChild._add(parts, index + 1, route); } const dynamic = part.startsWith("[") && part.endsWith("]"); let branch; if (dynamic) { branch = new _RouterBranch(part.slice(1, -1), true, this); this._dynamicChild = branch; } else { branch = new _RouterBranch(part, false, this); this._staticChildren.push(branch); } return branch._add(parts, index + 1, route); } _performRemove(parts, index, route) { if (index >= parts.length) { let success = false; for (const method of route.methods) { if (this.node.delete(method, route)) { success = true; } } return success; } const part = parts[index]; const staticChildIndex = this._staticChildren.findIndex((branch) => branch.matches(part)); if (staticChildIndex === -1) { const child = this._staticChildren[index]; const removed = child._remove(parts, index + 1, route); if (removed && child.empty) { this._staticChildren = this._staticChildren.filter((branch) => branch !== child); } return removed; } if (this._dynamicChild) { const removed = this._dynamicChild._remove(parts, index + 1, route); if (removed && this._dynamicChild.empty) { this._dynamicChild = null; } return removed; } return false; } _find(parts, index) { if (index >= parts.length) return this; const part = parts[index]; const child = this._staticChildren.find((branch) => branch.matches(part)) ?? this._dynamicChild; if (isNullish(child)) return null; return child._find(parts, index + 1); } _updateSupportedChildrenMethods() { const methods = new Set(this.node.methods()); for (const child of this.children) { for (const method of child.node.methods()) { methods.add(method); } } this.supportedMethods = [...methods]; } }; __name(_RouterBranch, "RouterBranch"); var RouterBranch = _RouterBranch; export { RouterBranch }; //# sourceMappingURL=RouterBranch.mjs.map //# sourceMappingURL=RouterBranch.mjs.map