@veritree/services 2.31.1-2 → 2.32.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veritree/services",
3
- "version": "2.31.1-2",
3
+ "version": "2.32.0-0",
4
4
  "description": "A collection of javascript functions/services to talk to veritree API",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -24,12 +24,12 @@ class OrgsApi extends Api {
24
24
  }
25
25
 
26
26
  async stats() {
27
- const url = `${this._geStatstUrl()}`;
27
+ const url = `${this._getStatstUrl()}`;
28
28
  return await this.get(url);
29
29
  }
30
30
 
31
- async publicStats() {
32
- const url = `${this._geStatstUrl(true)}`;
31
+ async publicStats(args) {
32
+ const url = `${this._getStatstUrl(true, args)}`;
33
33
  return await this.get(url);
34
34
  }
35
35
 
@@ -56,9 +56,9 @@ class OrgsApi extends Api {
56
56
  };
57
57
  }
58
58
 
59
- _geStatstUrl(isPublic) {
59
+ _getStatstUrl(isPublic, args) {
60
60
  const endpoint = isPublic ? 'pstats' : 'stats';
61
- return `${this.getUrl()}/${endpoint}${this.getUrlParams()}`;
61
+ return `${this.getUrl()}/${endpoint}${this.getUrlParams(args)}`;
62
62
  }
63
63
  }
64
64
 
@@ -17,14 +17,14 @@ class SponsorsApi extends Api {
17
17
  return await this.get(url);
18
18
  }
19
19
 
20
- async profile() {
21
- this.setOrg();
20
+ async profile(args) {
21
+ const params = this.getUrlParams(args);
22
22
 
23
23
  if (!this.orgId) {
24
24
  throw new Error('No org id provided');
25
25
  }
26
26
 
27
- const url = `${this.getUrl()}/${this.orgId}/profile`;
27
+ const url = `${this.getUrl()}/${this.orgId}/profile${params}`;
28
28
  return await this.get(url);
29
29
  }
30
30
 
@@ -49,7 +49,9 @@ class SponsorsApi extends Api {
49
49
  };
50
50
 
51
51
  const imageUploadUrl = async () => {
52
- const url = `${this.getUrl()}/${this.orgId}/cms/image-upload-url${urlParams}`;
52
+ const url = `${this.getUrl()}/${
53
+ this.orgId
54
+ }/cms/image-upload-url${urlParams}`;
53
55
  return await this.get(url);
54
56
  };
55
57
 
@@ -67,19 +69,21 @@ class SponsorsApi extends Api {
67
69
 
68
70
  tools(args) {
69
71
  const get = async (sponsorId) => {
70
- const url = `${this.getUrl()}/${sponsorId}/tools${this.getUrlParams(args)}`;
72
+ const url = `${this.getUrl()}/${sponsorId}/tools${this.getUrlParams(
73
+ args
74
+ )}`;
71
75
  return await this.get(url);
72
- }
76
+ };
73
77
 
74
78
  const create = async (sponsorId, data) => {
75
79
  const url = `${this.getUrl()}/${sponsorId}/tools`;
76
80
  return await this.post(url, data);
77
- }
81
+ };
78
82
 
79
83
  return {
80
84
  get,
81
- create
82
- }
85
+ create,
86
+ };
83
87
  }
84
88
  }
85
89
 
@@ -5,13 +5,12 @@ class StatsApi extends Api {
5
5
  super(resource);
6
6
  }
7
7
 
8
- async crumb(id) {
8
+ async crumb(id, args) {
9
9
  this.resource = 'pstats';
10
- const url = `${this.getUrl()}/${id}${this.getUrlParams()}`;
10
+ const url = `${this.getUrl()}/${id}${this.getUrlParams(args)}`;
11
11
 
12
12
  return await this.get(url);
13
13
  }
14
14
  }
15
15
 
16
16
  export const Stats = new StatsApi();
17
-
@@ -1,34 +1,33 @@
1
- import { getCookie } from "./cookies";
2
- import { getSession } from "./session";
3
- import { createParamsStringFromArgs } from "../utils/args";
1
+ import { getCookie } from './cookies';
2
+ import { getSession } from './session';
3
+ import { createParamsStringFromArgs } from '../utils/args';
4
4
 
5
5
  function addVersionParam(url) {
6
6
  // If URL is invalid or already has the result parameter, return it as is
7
- if (!url || url.includes("_result=1")) {
7
+ if (!url || url.includes('_result=1')) {
8
8
  return url;
9
9
  }
10
10
 
11
11
  // Check if URL already has the version parameter
12
- if (url.includes("_v=")) {
12
+ if (url.includes('_v=')) {
13
13
  return url;
14
14
  }
15
15
 
16
16
  const version = '10.0.0';
17
17
 
18
18
  // Append version parameter to URL
19
- const urlVersionParam = url.includes("?")
19
+ const urlVersionParam = url.includes('?')
20
20
  ? `&_v=${version}`
21
21
  : `?_v=${version}`;
22
22
 
23
23
  return `${url}${urlVersionParam}`;
24
24
  }
25
25
 
26
-
27
26
  function getConfig(method, data, as) {
28
- const isGet = method === "get";
27
+ const isGet = method === 'get';
29
28
  const isSpoofing = as;
30
29
  const isFormData = data instanceof FormData;
31
- const accessToken = `Bearer ${getCookie("access_token")}`;
30
+ const accessToken = `Bearer ${getCookie('access_token')}`;
32
31
 
33
32
  const config = {
34
33
  method,
@@ -38,7 +37,7 @@ function getConfig(method, data, as) {
38
37
  };
39
38
 
40
39
  if (!isFormData) {
41
- config.headers["Content-Type"] = "application/json";
40
+ config.headers['Content-Type'] = 'application/json';
42
41
  }
43
42
 
44
43
  // TODO: improve this ifs and elses
@@ -46,7 +45,7 @@ function getConfig(method, data, as) {
46
45
  if (!data) data = {};
47
46
 
48
47
  if (isFormData) {
49
- if (isSpoofing) data.set("_method", as.toUpperCase());
48
+ if (isSpoofing) data.set('_method', as.toUpperCase());
50
49
  config.body = data;
51
50
  } else {
52
51
  if (isSpoofing) data._method = as.toUpperCase();
@@ -60,7 +59,7 @@ function getConfig(method, data, as) {
60
59
  export default class Api {
61
60
  constructor(resource) {
62
61
  this.baseUrl = null;
63
- this.resource = resource ? resource : "";
62
+ this.resource = resource ? resource : '';
64
63
  this.orgId = null;
65
64
  this.orgType = null;
66
65
  }
@@ -75,7 +74,8 @@ export default class Api {
75
74
  }
76
75
 
77
76
  async single(id, args) {
78
- const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
77
+ const params = this.args ? this.getUrlParams(args) : '';
78
+ const url = `${this.getUrl(id)}${params}`;
79
79
  return await this.get(url);
80
80
  }
81
81
 
@@ -83,45 +83,43 @@ export default class Api {
83
83
  return await this.post(null, data, null, args);
84
84
  }
85
85
 
86
- async update(id, data, as = "put", args) {
86
+ async update(id, data, as = 'put', args) {
87
87
  const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
88
88
  return await this.post(url, data, as);
89
89
  }
90
90
 
91
91
  async delete(id, args) {
92
92
  const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
93
- return await this.post(url, null, "delete");
93
+ return await this.post(url, null, 'delete');
94
94
  }
95
95
 
96
96
  async get(url) {
97
- console.log(url);
98
97
  return await this.unWrap(url);
99
98
  }
100
99
 
101
100
  async post(url, data, as, args) {
102
101
  if (!url) url = `${this.getUrl()}${this.getUrlParams(args)}`;
103
- return await this.unWrap(url, "post", data, as);
102
+ return await this.unWrap(url, 'post', data, as);
104
103
  }
105
104
 
106
105
  getUrl(id) {
107
- id = id ? `/${id}` : "";
106
+ id = id ? `/${id}` : '';
108
107
  return `${this.baseUrl}/${this.resource}${id}`;
109
108
  }
110
109
 
111
110
  getUrlParams(args) {
112
- this.setOrg();
113
111
  let isOrgLess = false;
114
112
  let isOrgIdAs = false;
115
113
  let isOrgTypeAs = false;
116
- let orgIdParam = "";
117
- let orgTypeParam = "";
118
- let argsClone = structuredClone(args); // avoids mutating object
114
+ let orgIdParam = '';
115
+ let orgTypeParam = '';
116
+ let argsClone = { ...args }; // avoids mutating object
119
117
 
120
118
  // while most of endpoints require an org id and type, some endpoints do not
121
119
  if (argsClone) {
122
- isOrgLess = Object.hasOwn(argsClone, "orgless");
123
- isOrgIdAs = Object.hasOwn(argsClone, "org_id_as");
124
- isOrgTypeAs = Object.hasOwn(argsClone, "org_type_as");
120
+ isOrgLess = Object.hasOwn(argsClone, 'orgless');
121
+ isOrgIdAs = Object.hasOwn(argsClone, 'org_id_as');
122
+ isOrgTypeAs = Object.hasOwn(argsClone, 'org_type_as');
125
123
  }
126
124
 
127
125
  // a super admin user can create/edit data from other orgs
@@ -147,12 +145,18 @@ export default class Api {
147
145
  }
148
146
  }
149
147
 
148
+ if (!this.orgId && !this.orgType) {
149
+ const session = getSession();
150
+ this.orgId = session.orgId;
151
+ this.orgType = session.orgType;
152
+ }
153
+
150
154
  return `?${orgIdParam}${orgTypeParam}${createParamsStringFromArgs(
151
155
  argsClone
152
156
  )}`;
153
157
  }
154
158
 
155
- async unWrap(url, method = "get", data, as) {
159
+ async unWrap(url, method = 'get', data, as) {
156
160
  url = addVersionParam(url);
157
161
  const config = getConfig(method, data, as);
158
162
  const response = await fetch(url, config);
@@ -168,12 +172,4 @@ export default class Api {
168
172
  });
169
173
  });
170
174
  }
171
-
172
- setOrg() {
173
- const session = getSession();
174
- if (!session) return;
175
- const { orgId, orgType } = session;
176
- this.orgId = orgId;
177
- this.orgType = orgType;
178
- }
179
175
  }