UNPKG

@lokalise/api-contracts

Version:

Key idea behind API contracts: backend owns entire definition for the route, including its path, HTTP method used and response structure expectations, and exposes it as a part of its API schemas. Then frontend consumes that definition instead of forming f

30 lines 1.2 kB
/** * Builds a request path by combining a base path with an optional path prefix. * Ensures proper slash handling to avoid double slashes or missing slashes. * * @param path - The base path for the request * @param pathPrefix - Optional prefix to prepend to the path * @returns The combined path with proper slash formatting * * @example * ```typescript * buildRequestPath('/api/users') // '/api/users' * buildRequestPath('api/users') // '/api/users' * buildRequestPath('/api/users', 'v1') // '/v1/api/users' * buildRequestPath('/api/users', '/v1/') // '/v1/api/users' * buildRequestPath('api/users', 'v1') // '/v1/api/users' * ``` */ export function buildRequestPath(path, pathPrefix) { if (!pathPrefix) { return ensureStartsWithSlash(path); } // Remove trailing slash from pathPrefix and leading slash from path const cleanPrefix = pathPrefix.endsWith('/') ? pathPrefix.slice(0, -1) : pathPrefix; const cleanPath = path.startsWith('/') ? path.slice(1) : path; return ensureStartsWithSlash(`${cleanPrefix}/${cleanPath}`); } function ensureStartsWithSlash(str) { return str.startsWith('/') ? str : `/${str}`; } //# sourceMappingURL=pathUtils.js.map