@molopos/shared 2.0.15 → 2.0.17

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/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./date";
2
2
  export * from "./enum";
3
3
  export * from "./lib/utils";
4
+ export * from "./url-query";
4
5
  export type DateLike = Date | string;
5
6
  export type Numberlike = number | `${number}` | `${number}.${number}`;
package/index.js CHANGED
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./date"), exports);
18
18
  __exportStar(require("./enum"), exports);
19
19
  __exportStar(require("./lib/utils"), exports);
20
+ __exportStar(require("./url-query"), exports);
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@molopos/shared","publishConfig":{"access":"public"},"version":"2.0.15","description":"Shared between backend and frontend repos","license":"ISC","repository":{"type":"git","url":"https://github.com/unicubate/molopos-shared.git"},"dependencies":{"date-fns":"^4.1.0","luxon":"^3.7.2"}}
1
+ {"name":"@molopos/shared","publishConfig":{"access":"public"},"version":"2.0.17","description":"Shared between backend and frontend repos","license":"ISC","repository":{"type":"git","url":"https://github.com/unicubate/molopos-shared.git"},"dependencies":{"date-fns":"^4.1.0","luxon":"^3.7.2"}}
@@ -0,0 +1,25 @@
1
+ export type EndpointLike<T extends string> = `/${T}`;
2
+ /**
3
+ * Create a URL with query parameters
4
+ * @param baseUrl - The base URL
5
+ * @param endpoint - The endpoint
6
+ * @param urlParams - The URL parameters
7
+ * @param queryParams - The query parameters
8
+ * @returns
9
+ *
10
+ * @example
11
+ * const url = URLEndpoint({
12
+ * baseUrl: "https://api.example.com",
13
+ * endpoint: "/users",
14
+ * urlParams: { id: "123" },
15
+ * queryParams: { limit: 10, offset: 0 },
16
+ * });
17
+ * // https://api.example.com/users/123?limit=10&offset=0
18
+ *
19
+ */
20
+ export declare const URLEndpoint: ({ query, params, baseUrl, endpoint, }: {
21
+ query?: Object;
22
+ baseUrl: string;
23
+ params?: Object;
24
+ endpoint: EndpointLike<string>;
25
+ }) => string;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.URLEndpoint = void 0;
4
+ /**
5
+ * Create a URL with query parameters
6
+ * @param baseUrl - The base URL
7
+ * @param endpoint - The endpoint
8
+ * @param urlParams - The URL parameters
9
+ * @param queryParams - The query parameters
10
+ * @returns
11
+ *
12
+ * @example
13
+ * const url = URLEndpoint({
14
+ * baseUrl: "https://api.example.com",
15
+ * endpoint: "/users",
16
+ * urlParams: { id: "123" },
17
+ * queryParams: { limit: 10, offset: 0 },
18
+ * });
19
+ * // https://api.example.com/users/123?limit=10&offset=0
20
+ *
21
+ */
22
+ const URLEndpoint = ({ query, params, baseUrl, endpoint, }) => {
23
+ //replace params in url
24
+ let url = baseUrl + endpoint;
25
+ if (params) {
26
+ Object.keys(params).forEach((key) => {
27
+ url = url.replace(`:${key}`, params[key]);
28
+ });
29
+ }
30
+ //add query params
31
+ if (query) {
32
+ url += "?";
33
+ Object.keys(query).forEach((key) => {
34
+ if (query[key]) {
35
+ url += `${key}=${query[key]}&`;
36
+ }
37
+ });
38
+ url = url.slice(0, -1);
39
+ }
40
+ return url;
41
+ };
42
+ exports.URLEndpoint = URLEndpoint;