|
1 | | -import urlJoin from "url-join"; |
2 | 1 | import { parseTemplate } from "url-template"; |
3 | 2 | import type { KeycloakAdminClient } from "../client.js"; |
4 | 3 | import { |
5 | 4 | fetchWithError, |
6 | 5 | NetworkError, |
7 | 6 | parseResponse, |
8 | 7 | } from "../utils/fetchWithError.js"; |
| 8 | +import { joinPath } from "../utils/joinPath.js"; |
9 | 9 | import { stringifyQueryParams } from "../utils/stringifyQueryParams.js"; |
10 | 10 |
|
11 | 11 | // constants |
@@ -53,7 +53,7 @@ export class Agent { |
53 | 53 | #client: KeycloakAdminClient; |
54 | 54 | #basePath: string; |
55 | 55 | #getBaseParams?: () => Record<string, any>; |
56 | | - #getBaseUrl?: () => string; |
| 56 | + #getBaseUrl: () => string; |
57 | 57 |
|
58 | 58 | constructor({ |
59 | 59 | client, |
@@ -199,12 +199,6 @@ export class Agent { |
199 | 199 | returnResourceIdInLocationHeader?: { field: string }; |
200 | 200 | headers?: [string, string][] | Record<string, string> | Headers; |
201 | 201 | }) { |
202 | | - const newPath = urlJoin(this.#basePath, path); |
203 | | - |
204 | | - // Parse template and replace with values from urlParams |
205 | | - const pathTemplate = parseTemplate(newPath); |
206 | | - const parsedPath = pathTemplate.expand(urlParams); |
207 | | - const url = new URL(`${this.#getBaseUrl?.() ?? ""}${parsedPath}`); |
208 | 202 | const requestOptions = { ...this.#client.getRequestOptions() }; |
209 | 203 | const requestHeaders = new Headers([ |
210 | 204 | ...new Headers(requestOptions.headers).entries(), |
@@ -243,6 +237,10 @@ export class Agent { |
243 | 237 | Object.assign(searchParams, queryParams); |
244 | 238 | } |
245 | 239 |
|
| 240 | + const url = new URL(this.#getBaseUrl()); |
| 241 | + const pathTemplate = parseTemplate(joinPath(this.#basePath, path)); |
| 242 | + |
| 243 | + url.pathname = joinPath(url.pathname, pathTemplate.expand(urlParams)); |
246 | 244 | url.search = stringifyQueryParams(searchParams); |
247 | 245 |
|
248 | 246 | try { |
|
0 commit comments