UNPKG

@metis-w/api-client

Version:

Modern TypeScript HTTP API client with dynamic routes, parameterized endpoints, interceptors, and advanced features

109 lines 2.83 kB
import { camelToKebab } from "."; export class MethodResolver { static determineMethod(actionName, options = {}, explicitMethod) { if (explicitMethod) { return explicitMethod; } const actionLower = actionName.toLowerCase(); if (this.DIRECT_METHODS.some((method) => method.toLowerCase() === actionLower)) { return actionLower.toUpperCase(); } if (options.methodRules) { const actionKebab = camelToKebab(actionName).toLowerCase(); for (const [pattern, method] of Object.entries(options.methodRules)) { const patternLower = pattern.toLowerCase(); // Перевіряємо і camelCase, і kebab-case варіанти if (this.matchesPattern(actionLower, patternLower) || this.matchesPattern(actionKebab, patternLower)) { return method; } } } for (const { patterns, method } of this.SEMANTIC_PATTERNS) { if (patterns.some((pattern) => actionLower.startsWith(pattern))) { return method; } } return options.defaultMethod || "POST"; } static matchesPattern(actionName, pattern) { if (pattern.endsWith("*")) { return actionName.startsWith(pattern.slice(0, -1)); } if (pattern.startsWith("*")) { return actionName.endsWith(pattern.slice(1)); } return actionName === pattern; } } MethodResolver.DIRECT_METHODS = [ "GET", "POST", "PUT", "DELETE", "PATCH", ]; MethodResolver.SEMANTIC_PATTERNS = [ { patterns: [ "get", "fetch", "load", "find", "retrieve", "read", "show", "view", ], method: "GET", }, { patterns: [ "create", "add", "save", "store", "insert", "new", "register", "submit", ], method: "POST", }, { patterns: [ "update", "edit", "modify", "change", "replace", "set", "put", ], method: "PUT", }, { patterns: [ "delete", "remove", "destroy", "clear", "drop", "cancel", ], method: "DELETE", }, { patterns: [ "patch", "partial", "toggle", "enable", "disable", "activate", "deactivate", ], method: "PATCH", }, ]; //# sourceMappingURL=method-resolver.js.map