@ambitiondev/dynamic-url 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # @ambitiondev/dynamic-url
2
+
3
+ A lightweight JavaScript library for generating dynamic URLs based on templates and parameters.
4
+
5
+ ## Installation
6
+
7
+ You can install the library using npm:
8
+
9
+ ```bash
10
+ npm install @ambitiondev/dynamic-url
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Here's a simple example of how to use the library:
16
+
17
+ ```typescript
18
+ import { DynamicURL } from "@ambitiondev/dynamic-url";
19
+
20
+ // Init the url with your template
21
+ const url = new DynamicURL("https://example.com/{user}/{id}");
22
+ // Set route and query parameters
23
+ url.setRouteParams({ user: "john_doe", id: 123 });
24
+ url.setQueryParams({ ref: "newsletter", campaign: "spring_sale" });
25
+
26
+ // Resolve the final URL
27
+ console.log(url.resolve());
28
+ // Output: https://example.com/john_doe/123?ref=newsletter&campaign=spring_sale
29
+ ```
30
+
31
+ ## More docs
32
+
33
+ - [API](./docs/api.md)
@@ -0,0 +1,51 @@
1
+ import type { TParamsObject } from "../types";
2
+ export declare class DynamicURL {
3
+ private url;
4
+ constructor(url: string);
5
+ /**
6
+ * Takes a query object and appends it to the URL as a query string.
7
+ *
8
+ * @param query - An object representing the query parameters.
9
+ * @returns {DynamicURL}
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * const url = new DynamicURL("https://example.com");
14
+ * url.setQueryParams({ citizen: "robespierre", hero: "ironman" });
15
+ * console.log(url.resolve()); // "https://example.com?citizen=robespierre&hero=ironman"
16
+ * ```
17
+ */
18
+ setQueryParams(query: Record<string, any>): this;
19
+ /**
20
+ * Replaces route parameters in the URL with the provided values.
21
+ *
22
+ * @param {TParamsObject | string} replaceValue - An object or string to replace the route parameters.
23
+ *
24
+ * @see {@link TParamsObject}
25
+ * @returns {DynamicURL}
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * const url = new DynamicURL("https://example.com/{citizen}/{hero}");
30
+ * url.setRouteParams({ citizen: "robespierre", hero: "ironman" });
31
+ * console.log(url.resolve()); // "https://example.com/robespierre/ironman"
32
+ * ```
33
+ */
34
+ setRouteParams(replaceValue: TParamsObject | string): this;
35
+ /**
36
+ * Resolves and returns the final URL as a string.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * const url = new DynamicURL("https://example.com/{citizen}/{hero}");
41
+ * url.setRouteParams({ citizen: "robespierre", hero: "ironman" });
42
+ *
43
+ * // Resolve the final URL
44
+ * console.log(url.resolve()); // "https://example.com/robespierre/ironman"
45
+ * ```
46
+ *
47
+ * @returns {string}
48
+ */
49
+ resolve(): string;
50
+ }
51
+ //# sourceMappingURL=url.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"url.d.ts","sourceRoot":"","sources":["../../src/url.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAE9C,qBAAa,UAAU;IACrB,OAAO,CAAC,GAAG,CAAS;gBAER,GAAG,EAAE,MAAM;IAIvB;;;;;;;;;;;;OAYG;IAEH,cAAc,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAazC;;;;;;;;;;;;;;OAcG;IACH,cAAc,CAAC,YAAY,EAAE,aAAa,GAAG,MAAM;IAiBnD;;;;;;;;;;;;;OAaG;IACH,OAAO;CAGR"}
@@ -0,0 +1,77 @@
1
+ // Vendor
2
+ import qs from "qs";
3
+ export class DynamicURL {
4
+ constructor(url) {
5
+ this.url = url;
6
+ }
7
+ /**
8
+ * Takes a query object and appends it to the URL as a query string.
9
+ *
10
+ * @param query - An object representing the query parameters.
11
+ * @returns {DynamicURL}
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const url = new DynamicURL("https://example.com");
16
+ * url.setQueryParams({ citizen: "robespierre", hero: "ironman" });
17
+ * console.log(url.resolve()); // "https://example.com?citizen=robespierre&hero=ironman"
18
+ * ```
19
+ */
20
+ /* eslint-disable-next-line @typescript-eslint/no-explicit-any */
21
+ setQueryParams(query) {
22
+ const queryString = qs.stringify(query, {
23
+ skipNulls: true,
24
+ encode: true,
25
+ });
26
+ if (queryString) {
27
+ this.url = `${this.url}?${queryString}`;
28
+ }
29
+ return this;
30
+ }
31
+ /**
32
+ * Replaces route parameters in the URL with the provided values.
33
+ *
34
+ * @param {TParamsObject | string} replaceValue - An object or string to replace the route parameters.
35
+ *
36
+ * @see {@link TParamsObject}
37
+ * @returns {DynamicURL}
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * const url = new DynamicURL("https://example.com/{citizen}/{hero}");
42
+ * url.setRouteParams({ citizen: "robespierre", hero: "ironman" });
43
+ * console.log(url.resolve()); // "https://example.com/robespierre/ironman"
44
+ * ```
45
+ */
46
+ setRouteParams(replaceValue) {
47
+ if (typeof replaceValue === "string") {
48
+ this.url = this.url.replace(/ *\{[^)]*\} */g, replaceValue);
49
+ return this;
50
+ }
51
+ this.url = this.url.replace(/\{([^}]+)\}/g, (match, key) => {
52
+ if (Object.prototype.hasOwnProperty.call(replaceValue, key)) {
53
+ const value = replaceValue[key];
54
+ return String(value);
55
+ }
56
+ return match;
57
+ });
58
+ return this;
59
+ }
60
+ /**
61
+ * Resolves and returns the final URL as a string.
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * const url = new DynamicURL("https://example.com/{citizen}/{hero}");
66
+ * url.setRouteParams({ citizen: "robespierre", hero: "ironman" });
67
+ *
68
+ * // Resolve the final URL
69
+ * console.log(url.resolve()); // "https://example.com/robespierre/ironman"
70
+ * ```
71
+ *
72
+ * @returns {string}
73
+ */
74
+ resolve() {
75
+ return this.url;
76
+ }
77
+ }
@@ -0,0 +1,2 @@
1
+ export type TParamsObject = Record<string, string | number | boolean>;
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../types/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@ambitiondev/dynamic-url",
3
+ "description": "lightweight util for creating dynamic endpoint strings",
4
+ "version": "0.0.0",
5
+ "main": "./dist/src/url.js",
6
+ "types": "./dist/src/url.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "authors": [
11
+ {
12
+ "name": "Berrie Nachtweh - Ambition Concepts",
13
+ "email": "info@ambitiondev.io",
14
+ "url": "https://ambitiondev.io"
15
+ }
16
+ ],
17
+ "devDependencies": {
18
+ "@commitlint/config-conventional": "^20.3.1",
19
+ "@eslint/js": "^9.39.2",
20
+ "@types/qs": "^6.14.0",
21
+ "@vitest/coverage-v8": "^4.0.17",
22
+ "commitizen": "^4.3.1",
23
+ "eslint": "^9.39.2",
24
+ "husky": "^9.1.7",
25
+ "typedoc": "^0.28.16",
26
+ "typedoc-plugin-markdown": "^4.9.0",
27
+ "typescript": "^5.9.3",
28
+ "typescript-eslint": "^8.53.0",
29
+ "vitest": "^4.0.17"
30
+ },
31
+ "dependencies": {
32
+ "qs": "^6.14.1"
33
+ },
34
+ "scripts": {
35
+ "--- ๐ŸŽฃ Hooks ---": "",
36
+ "prepare": "husky",
37
+ "--- ๐Ÿงช Testing ---": "",
38
+ "test": "vitest",
39
+ "test:coverage": "vitest run --coverage",
40
+ "--- ๐ŸŽ—๏ธ Lint ---": "",
41
+ "lint": "eslint src/*.ts",
42
+ "--- ๐Ÿ“š Documentation ---": "",
43
+ "docs:generate": "./.scripts/generate-docs.sh",
44
+ "--- ๐Ÿค“ Developer Tools ---": "",
45
+ "commit": "git-cz",
46
+ "build": "tsc"
47
+ },
48
+ "packageManager": "pnpm@10.7.0+sha512.6b865ad4b62a1d9842b61d674a393903b871d9244954f652b8842c2b553c72176b278f64c463e52d40fff8aba385c235c8c9ecf5cc7de4fd78b8bb6d49633ab6"
49
+ }