Skip to content

Commit

Permalink
feat: renamed queryParams to params and hashParam to hash
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNaubit committed Mar 6, 2024
1 parent fd3cd57 commit 1c663c8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 38 deletions.
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ npm install --save @nauverse/make-url
import { makeURL } from "@nauverse/make-url";

makeURL("https://api.example.com/", "/:id/:param2/:id///", {
queryParams: {
params: {
id: 1,
param2: "678"
}
Expand Down Expand Up @@ -171,7 +171,7 @@ const SOME_SLUG = 'hey';

function getUserPosts(id, limit, offset) {
const requestUrl = makeURL('api.example.com', SOME_SLUG, 'users/:id/posts', {
queryParams: {
params: {
id,
limit,
offset
Expand Down Expand Up @@ -213,26 +213,27 @@ You can call with with any amount of string values and optionally at the end wit
A config object has the following interface:
~~~ts
export interface IParams {
queryParams: Record<string, unknown>;
hashParam: string;
params: Record<string, unknown>;
hash: string;
config: {
forceProtocol: "http" | "https" | "none" | "auto" | "auto-insecure";
trailingSlash: "add" | "remove";
strict: boolean;
allowEmptyPathSegments: boolean;
arraySerializer: "stringify" | "repeat" | "comma";
};
}
~~~
Every field in the object is optional.

`queryParams` is an optional object that contains key-value pairs. If in the generated URL is there any match of `:<key>`, being `<key>` any of the keys in that object, the matches will be replaced with the value of their respective keys.
`params` is an optional object that contains key-value pairs. If in the generated URL is there any match of `:<key>`, being `<key>` any of the keys in that object, the matches will be replaced with the value of their respective keys.
If they are not replaced in the URL, they will be added to the URL as query parameters.

Let's see some examples:
### makeURL with only query parameters
~~~ts
makeURL("https://example.com", {
queryParams: {
params: {
id: 12,
name: "test"
}
Expand All @@ -243,26 +244,26 @@ makeURL("https://example.com", {
### makeURL with query parameters and also URL params
~~~ts
makeURL("https://example.com", ":id/", {
queryParams: {
params: {
id: 12,
name: "test"
}
});
// https://example.com/12?name=test
~~~

> Notice that the `queryParams` values can be of any type. I used in the examples `string` and `number` types, but you can use anything (including objects) and it will be always safely casted. Don't worry if the string contains invalid characters, everything is safely encoded in this library!
> Notice that the `params` values can be of any type. I used in the examples `string` and `number` types, but you can use anything (including objects) and it will be always safely casted. Don't worry if the string contains invalid characters, everything is safely encoded in this library!
---

`hashParam` is another optional field containg a string.
`hash` is another optional field containg a string.
> Don't worry if the string contains invalid characters, everything is safely encoded in this library!
Let's see one example:
### makeURL with a hash parameter
~~~ts
makeURL("https://example.com", {
hashParam: "test"
hash: "test"
});
// https://example.com#test
~~~
Expand Down Expand Up @@ -309,7 +310,7 @@ There are three possible values: `repeat`, `comma` and `stringify`. Each of them
#### makeURL with `arraySerializer: 'repeat'`
~~~ts
makeURL("https://example.com", "/test", {
queryParams: {
params: {
arr: ['a', 'b', 'c']
},
config: {
Expand All @@ -322,7 +323,7 @@ makeURL("https://example.com", "/test", {
#### makeURL with `arraySerializer: 'stringify'`
~~~ts
makeURL("https://example.com", "/test", {
queryParams: {
params: {
arr: ['a', 'b', 'c']
},
config: {
Expand All @@ -335,7 +336,7 @@ makeURL("https://example.com", "/test", {
#### makeURL with `arraySerializer: 'comma'`
~~~ts
makeURL("https://example.com", "/test", {
queryParams: {
params: {
arr: ['a', 'b', 'c']
},
config: {
Expand All @@ -349,7 +350,7 @@ makeURL("https://example.com", "/test", {
#### makeURL with `arraySerializer: 'comma'` with array as URL variable
~~~ts
makeURL("https://example.com", "/test/:arr", {
queryParams: {
params: {
arr: ['a', 'b', 'c']
},
config: {
Expand Down Expand Up @@ -818,11 +819,11 @@ To finish with this "guide", I want to provide some examples combining several o
#### Example 1
~~~ts
makeURL("example.com/", "/test/:id///edit/", {
queryParams: {
params: {
id: 1,
name: "John"
},
hashParam: "test",
hash: "test",
config: {
forceProtocol: "auto",
trailingSlash: "remove",
Expand All @@ -845,7 +846,7 @@ setMakeURLDefaultConfig({
//

makeURL("https://api.example.com/", "/:id/:param2/:id///", {
queryParams: {
params: {
id: 1,
param2: "678"
}
Expand Down
30 changes: 15 additions & 15 deletions src/makeURL.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("makeURL", () => {
});

const url = makeURL("https://example.com", {
queryParams: { key: "value" }
params: { key: "value" }
});
expect(url).toBe("https://example.com/?key=value");
});
Expand All @@ -59,7 +59,7 @@ describe("makeURL", () => {
});

const url = makeURL("https://example.com", {
queryParams: { key: "value" }
params: { key: "value" }
});
expect(url).toBe("https://example.com?key=value");
});
Expand All @@ -74,7 +74,7 @@ describe("makeURL", () => {
});

const url = makeURL("https://example.com", {
hashParam: "about"
hash: "about"
});
expect(url).toBe("https://example.com/#about");
});
Expand All @@ -89,7 +89,7 @@ describe("makeURL", () => {
});

const url = makeURL("https://example.com", {
hashParam: "about"
hash: "about"
});
expect(url).toBe("https://example.com#about");
});
Expand All @@ -103,7 +103,7 @@ describe("makeURL", () => {
it("should throw an error if no string params are provided", () => {
expect(() => {
makeURL({
queryParams: {
params: {
key: "value"
}
});
Expand All @@ -114,7 +114,7 @@ describe("makeURL", () => {
expect(() => {
makeURL(
{
queryParams: {
params: {
key: "value"
}
},
Expand All @@ -128,7 +128,7 @@ describe("makeURL", () => {
makeURL(
"example.com",
{
queryParams: {
params: {
key: "value"
}
},
Expand Down Expand Up @@ -490,11 +490,11 @@ describe("makeURL", () => {
"about me",
"/:id/",
{
queryParams: {
params: {
id: "*Æ",
"testQÉr/y": "%ª`B"
},
hashParam: "ܺ&%·3"
hash: "ܺ&%·3"
}
);
expect(url).toBe(
Expand All @@ -513,7 +513,7 @@ describe("makeURL", () => {
});

const url = makeURL("https://example.com", {
queryParams: {
params: {
key: "value",
arr: ["item1", "item2"]
}
Expand All @@ -534,7 +534,7 @@ describe("makeURL", () => {
});

const url = makeURL("https://example.com", {
queryParams: {
params: {
key: "value",
arr: []
}
Expand All @@ -554,7 +554,7 @@ describe("makeURL", () => {
});

const url = makeURL("https://example.com", {
queryParams: {
params: {
key: "value",
arr: ["item1", "item2"]
}
Expand All @@ -573,7 +573,7 @@ describe("makeURL", () => {
});

const url = makeURL("https://example.com", {
queryParams: {
params: {
key: "value",
arr: []
}
Expand All @@ -592,7 +592,7 @@ describe("makeURL", () => {
});

const url = makeURL("https://example.com", {
queryParams: {
params: {
key: "value",
arr: ["item1", "item2"]
}
Expand All @@ -611,7 +611,7 @@ describe("makeURL", () => {
});

const url = makeURL("https://example.com", {
queryParams: {
params: {
key: "value",
arr: []
}
Expand Down
8 changes: 4 additions & 4 deletions src/makeURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export const BASE_DEFAULT_MAKE_URL_CONFIG: IConfig = {
};

const DEFAULT_PARAMS: IParams<IConfig> = {
queryParams: {},
hashParam: "",
params: {},
hash: "",
config: BASE_DEFAULT_MAKE_URL_CONFIG
};

Expand Down Expand Up @@ -206,7 +206,7 @@ export default function makeURL(
}
> = {};

Object.entries(safeParams.queryParams).forEach(([key, value]) => {
Object.entries(safeParams.params).forEach(([key, value]) => {
// We need to safe cast the value to a string, it could be anything: a string, a number, an object, etc... so sometimes the .toString() method will not work as expected
let safeValue: string | Array<string> = "";
let isValueArray = false;
Expand Down Expand Up @@ -288,7 +288,7 @@ export default function makeURL(

// Now we have to add the hash params to the URL
// Add the hash params to the URL if there are any
const safeHashParamValue: string = safeParams.hashParam.trim();
const safeHashParamValue: string = safeParams.hash.trim();
if (safeHashParamValue !== "") {
url += `#${encodeURIComponent(safeHashParamValue)}`;
}
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface IConfig {
}

export interface IParams<T> {
queryParams: Record<string, unknown>;
hashParam: string;
params: Record<string, unknown>;
hash: string;
config: T;
}

0 comments on commit 1c663c8

Please sign in to comment.