@veritree/services 2.65.0 → 2.67.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veritree/services",
3
- "version": "2.65.0",
3
+ "version": "2.67.0",
4
4
  "description": "A collection of javascript functions/services to talk to veritree API",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -1,31 +1,81 @@
1
+ /**
2
+ * Creates a Relations object to handle API relations.
3
+ * @param {object} api - The API object used for making API calls.
4
+ * @param {string} relationPrefix - The prefix for relation endpoints.
5
+ * @param {string} relationId - The identifier for the relation.
6
+ * @returns {object} The Relations object.
7
+ */
1
8
  const Relations = (api, relationPrefix, relationId) => {
2
9
  let url = `${api.baseUrl}/${relationPrefix}/${relationId}/${api.resource}`;
3
10
 
4
- const all = async (args) => {
5
- url = `${url}${api.getUrlParams(args)}`;
11
+ /**
12
+ * Retrieves all tags.
13
+ * @param {object} params - Additional parameters for the request.
14
+ * @returns {Promise<object>} A promise that resolves with the response data.
15
+ */
16
+ const all = async (params) => {
17
+ url = `${url}${api.getUrlParams(params)}`;
6
18
  return await api.get(url);
7
19
  };
8
20
 
9
- const single = async (id, args) => {
10
- url = `${url}/${id}${api.getUrlParams(args)}`;
21
+ /**
22
+ * Retrieves a single tag by ID.
23
+ * @param {string} tagId - The ID of the tag to retrieve.
24
+ * @param {object} params - Additional parameters for the request.
25
+ * @returns {Promise<object>} A promise that resolves with the response data.
26
+ */
27
+ const single = async (tagId, params) => {
28
+ url = `${url}/${tagId}${api.getUrlParams(params)}`;
11
29
  return await api.get(url);
12
30
  };
13
31
 
14
- const attach = async (id, data, args) => {
15
- url = `${url}/${id}/attach${api.getUrlParams(args)}`;
32
+ /**
33
+ * Attaches a tag to a resource.
34
+ * @param {string} tagId - The ID of the tag to attach.
35
+ * @param {object} data - The data to send with the request.
36
+ * @param {object} params - Additional parameters for the request.
37
+ * @returns {Promise<object>} A promise that resolves with the response data.
38
+ */
39
+ const attach = async (tagId, data, params) => {
40
+ url = `${url}/${tagId}/attach${api.getUrlParams(params)}`;
16
41
  return await api.post(url, data);
17
42
  };
18
43
 
19
- const detach = async (id, args) => {
20
- url = `${url}/${id}/detach${api.getUrlParams(args)}`;
44
+ /**
45
+ * Detaches a tag from a resource.
46
+ * @param {string} tagId - The ID of the tag to detach.
47
+ * @param {object} params - Additional parameters for the request.
48
+ * @returns {Promise<object>} A promise that resolves with the response data.
49
+ */
50
+ const detach = async (tagId, params) => {
51
+ url = `${url}/${tagId}/detach${api.getUrlParams(params)}`;
21
52
  return await api.post(url, null, "delete");
22
53
  };
23
54
 
55
+ /**
56
+ * Retrieves usable tags.
57
+ * @param {object} params - Additional parameters for the request.
58
+ * @returns {Promise<object>} A promise that resolves with the response data.
59
+ */
60
+ const usable = async (params) => {
61
+ url = `${
62
+ api.baseUrl
63
+ }/${relationPrefix}/${relationId}/usable-tags${api.getUrlParams(params)}`;
64
+ return await api.get(url);
65
+ };
66
+
67
+ const attached = async (params) => {
68
+ url = `${url}/${api.getUrlParams(params)}`;
69
+ return await api.get(url);
70
+ };
71
+
24
72
  return {
25
73
  all,
74
+ attached,
26
75
  single,
27
76
  attach,
28
77
  detach,
78
+ usable,
29
79
  };
30
80
  };
31
81
 
package/src/utils/args.js CHANGED
@@ -11,12 +11,18 @@ export const createParamsStringFromArgs = (args) => {
11
11
 
12
12
  if (Array.isArray(value)) {
13
13
  arr = value
14
+ .filter((item) => item !== undefined && item !== null && item !== "")
14
15
  .map((item, index) => {
15
16
  const param = `${key}[]=${encodeURIComponent(item)}`;
17
+
16
18
  return index === 0 ? `${param}` : `&${param}`;
17
19
  })
18
20
  .join("");
19
21
 
22
+ if (!arr.length) {
23
+ return;
24
+ }
25
+
20
26
  paramsString.push(arr);
21
27
  } else {
22
28
  if (value !== undefined) {
@@ -27,17 +33,19 @@ export const createParamsStringFromArgs = (args) => {
27
33
  * solution, we have this -x- to allow
28
34
  * same keys to be passed. The args
29
35
  * would look like:
30
- *
31
- * {
36
+ *
37
+ * {
32
38
  * 'box-x-1': 1
33
39
  * 'box-x-2': 2
34
40
  * }
35
- *
41
+ *
36
42
  * and would be translated as expected:
37
- *
43
+ *
38
44
  * box=1&box=2
39
45
  */
40
- paramsString.push(`${key.split('-x-')[0]}=${encodeURIComponent(value)}`);
46
+ paramsString.push(
47
+ `${key.split("-x-")[0]}=${encodeURIComponent(value)}`
48
+ );
41
49
  }
42
50
  }
43
51
  });