@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 +1 -0
- package/index.js +1 -0
- package/package.json +1 -1
- package/url-query/index.d.ts +25 -0
- package/url-query/index.js +42 -0
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@molopos/shared","publishConfig":{"access":"public"},"version":"2.0.
|
|
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;
|