Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: url usages #177

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 11 additions & 3 deletions src/lib/strapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
});
Expand Down Expand Up @@ -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);
}

/**
Expand Down
15 changes: 11 additions & 4 deletions test/lib/strapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -273,7 +274,7 @@ describe("Strapi SDK", () => {
await context.strapi.changePassword({
currentPassword: "password",
password: "newPassword",
passwordConfirmation: "newPassword"
passwordConfirmation: "newPassword",
});
expect(
context.axiosRequest.calledWith({
Expand All @@ -282,8 +283,8 @@ describe("Strapi SDK", () => {
data: {
currentPassword: "password",
password: "newPassword",
passwordConfirmation: "newPassword"
}
passwordConfirmation: "newPassword",
},
})
).toBe(true);
});
Expand Down Expand Up @@ -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 () => {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down