diff --git a/package.json b/package.json index 29db34b..767f9bd 100644 --- a/package.json +++ b/package.json @@ -75,7 +75,8 @@ "axios": "^0.27.0", "defu": "^5.0.0", "js-cookie": "^3.0.0", - "qs": "^6.10.1" + "qs": "^6.10.1", + "ufo": "^1.2.0" }, "devDependencies": { "@commitlint/cli": "17.6.7", diff --git a/src/lib/strapi.ts b/src/lib/strapi.ts index a08656d..7f7b223 100644 --- a/src/lib/strapi.ts +++ b/src/lib/strapi.ts @@ -9,6 +9,7 @@ import axios, { import defu from "defu"; import qs from "qs"; import Cookies from "js-cookie"; +import { cleanDoubleSlashes, joinURL } from "ufo"; // Load custom types import type { @@ -60,11 +61,18 @@ export class Strapi { */ constructor(options?: StrapiOptions) { // merge given options with default values - this.options = defu((options as StrapiDefaultOptions) || {}, defaults); + const _options = defu(options || {}, defaults); + + // clean url & prefix + this.options = { + ..._options, + url: cleanDoubleSlashes(_options?.url), + prefix: cleanDoubleSlashes(_options?.prefix), + }; // create axios instance this.axios = axios.create({ - baseURL: new URL(this.options.prefix, this.options.url).href, + baseURL: joinURL(this.options.url, this.options.prefix), paramsSerializer: qs.stringify, ...this.options.axiosOptions, }); @@ -253,7 +261,7 @@ export class Strapi { * @returns string */ public getProviderAuthenticationUrl(provider: StrapiAuthProvider): string { - return new URL(`/connect/${provider}`, this.options.url).href; + return joinURL(this.options.url, this.options.prefix, "connect", provider); } /** diff --git a/test/lib/strapi.test.ts b/test/lib/strapi.test.ts index bb69bc3..74de1f2 100644 --- a/test/lib/strapi.test.ts +++ b/test/lib/strapi.test.ts @@ -3,6 +3,7 @@ import sinon from "sinon"; import Cookies from "js-cookie"; import { StrapiError } from "../../src"; import { AxiosError } from "axios"; +import { joinURL } from "ufo"; interface TestContext { strapi: Strapi; @@ -273,7 +274,7 @@ describe("Strapi SDK", () => { await context.strapi.changePassword({ currentPassword: "password", password: "newPassword", - passwordConfirmation: "newPassword" + passwordConfirmation: "newPassword", }); expect( context.axiosRequest.calledWith({ @@ -282,8 +283,8 @@ describe("Strapi SDK", () => { data: { currentPassword: "password", password: "newPassword", - passwordConfirmation: "newPassword" - } + passwordConfirmation: "newPassword", + }, }) ).toBe(true); }); @@ -356,7 +357,13 @@ describe("Strapi SDK", () => { test("Get Authentication Provider", () => { const url = context.strapi.getProviderAuthenticationUrl("github"); - expect(url).toBe("http://strapi-host/connect/github"); + expect(url).toBe( + joinURL( + context.strapi.options.url, + context.strapi.options.prefix, + "connect/github" + ) + ); }); test("Authentication with third party token", async () => { diff --git a/yarn.lock b/yarn.lock index 3405a57..768d17c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7052,6 +7052,11 @@ typescript@^4.4.3: resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.4.tgz#a17d3a0263bf5c8723b9c52f43c5084edf13c2e8" integrity sha512-VgYs2A2QIRuGphtzFV7aQJduJ2gyfTljngLzjpfW9FoYZF6xuw1W0vW9ghCKLfcWrCFxK81CSGRAvS1pn4fIUg== +ufo@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/ufo/-/ufo-1.2.0.tgz#28d127a087a46729133fdc89cb1358508b3f80ba" + integrity sha512-RsPyTbqORDNDxqAdQPQBpgqhWle1VcTSou/FraClYlHf6TZnQcGslpLcAphNR+sQW4q5lLWLbOsRlh9j24baQg== + uglify-js@^3.1.4: version "3.14.5" resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.14.5.tgz#cdabb7d4954231d80cb4a927654c4655e51f4859"