@rushdb/javascript-sdk 0.4.0 → 0.5.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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @rushdb/javascript-sdk
2
2
 
3
+ ## 0.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 3112626: Implemented logger feature for sdk
8
+
9
+ ## 0.4.1
10
+
11
+ ### Patch Changes
12
+
13
+ - 070d094: Minor cloud update
14
+
3
15
  ## 0.4.0
4
16
 
5
17
  ### Minor Changes
@@ -9,6 +21,7 @@
9
21
  ### Patch Changes
10
22
 
11
23
  - 4f83de0: Minor fixes and updates
24
+ - defeaf0: Minor fixes and updates
12
25
 
13
26
  ## 0.3.1
14
27
 
package/cjs/api/api.js CHANGED
@@ -67,7 +67,7 @@ class RestAPI {
67
67
  url
68
68
  });
69
69
  }
70
- this.api = (0, create_api_js_1.createApi)(this.fetcher);
70
+ this.api = (0, create_api_js_1.createApi)(this.fetcher, config === null || config === void 0 ? void 0 : config.logger);
71
71
  this.records = {
72
72
  attach: async (sourceId, target, options, transaction) => {
73
73
  var _a, _b;
@@ -6,39 +6,51 @@ const errors_js_1 = require("../sdk/errors.js");
6
6
  const record_js_1 = require("../sdk/record.js");
7
7
  const utils_js_2 = require("./utils.js");
8
8
  // POST /api/v1/records/:id @TODO
9
- const createApi = (fetcher) => ({
9
+ const createApi = (fetcher, logger) => ({
10
10
  labels: {
11
11
  async find(searchParams, transaction) {
12
12
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
13
- return fetcher(`/labels`, {
13
+ const path = '/labels';
14
+ const payload = {
14
15
  headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
15
16
  method: 'POST',
16
17
  requestData: searchParams
17
- });
18
+ };
19
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
20
+ return fetcher(path, payload);
18
21
  }
19
22
  },
20
23
  properties: {
21
24
  delete: (id, transaction) => {
22
25
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
23
- return fetcher(`/properties/${id}`, {
26
+ const path = `/properties/${id}`;
27
+ const payload = {
24
28
  headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
25
29
  method: 'DELETE'
26
- });
30
+ };
31
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
32
+ return fetcher(path, payload);
27
33
  },
28
34
  find: (searchParams, transaction) => {
29
35
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
30
- return fetcher(`/properties`, {
36
+ const path = `/properties`;
37
+ const payload = {
31
38
  headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
32
39
  method: 'POST',
33
40
  requestData: searchParams
34
- });
41
+ };
42
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
43
+ return fetcher(path, payload);
35
44
  },
36
45
  findById: (id, transaction) => {
37
46
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
38
- return fetcher(`/properties/${id}`, {
47
+ const path = `/properties/${id}`;
48
+ const payload = {
39
49
  headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
40
50
  method: 'GET'
41
- });
51
+ };
52
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
53
+ return fetcher(path, payload);
42
54
  },
43
55
  // update: () => {
44
56
  // // @TODO
@@ -48,125 +60,141 @@ const createApi = (fetcher) => ({
48
60
  // },
49
61
  values: (id, transaction) => {
50
62
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
51
- return fetcher(`/properties/${id}/values`, {
63
+ const path = `/properties/${id}/values`;
64
+ const payload = {
52
65
  headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
53
66
  method: 'GET'
54
- });
67
+ };
68
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
69
+ return fetcher(path, payload);
55
70
  }
56
71
  },
57
72
  records: {
58
- attach: async (source, idOrIds, options, transaction) => {
73
+ async attach(source, idOrIds, options, transaction) {
59
74
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
60
75
  const recordId = (0, utils_js_2.pickRecordId)(source);
61
- return fetcher(`/records/${recordId}/relations`, {
76
+ const path = `/records/${recordId}/relations`;
77
+ const payload = {
62
78
  headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
63
79
  method: 'POST',
64
- requestData: Object.assign(Object.assign({ targetIds: idOrIds }, ((options === null || options === void 0 ? void 0 : options.type) && { type: options === null || options === void 0 ? void 0 : options.type })), ((options === null || options === void 0 ? void 0 : options.direction) && { direction: options === null || options === void 0 ? void 0 : options.direction }))
65
- });
80
+ requestData: Object.assign(Object.assign({ targetIds: idOrIds }, ((options === null || options === void 0 ? void 0 : options.type) && { type: options.type })), ((options === null || options === void 0 ? void 0 : options.direction) && { direction: options.direction }))
81
+ };
82
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
83
+ return fetcher(path, payload);
66
84
  },
67
85
  async create(data, transaction) {
68
86
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
69
- if (data instanceof record_js_1.DBRecordDraft) {
70
- return fetcher(`/records`, {
71
- headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
72
- method: 'POST',
73
- requestData: data.toJson()
74
- });
75
- }
76
- return { data: undefined, success: false };
87
+ const path = `/records`;
88
+ const payload = {
89
+ headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
90
+ method: 'POST',
91
+ requestData: data instanceof record_js_1.DBRecordDraft ? data.toJson() : data
92
+ };
93
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
94
+ return fetcher(path, payload);
77
95
  },
78
96
  async createMany(data, transaction) {
79
97
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
80
- if (data instanceof record_js_1.DBRecordsBatchDraft) {
81
- return fetcher(`/records/import/json`, {
82
- headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
83
- method: 'POST',
84
- requestData: data.toJson()
85
- });
86
- }
87
- return { data: [], success: false };
98
+ const path = `/records/import/json`;
99
+ const payload = {
100
+ headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
101
+ method: 'POST',
102
+ requestData: data instanceof record_js_1.DBRecordsBatchDraft ? data.toJson() : data
103
+ };
104
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
105
+ return fetcher(path, payload);
88
106
  },
89
107
  delete(searchParams, transaction) {
90
108
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
91
- return fetcher(`/records/delete`, {
109
+ const path = `/records/delete`;
110
+ const payload = {
92
111
  headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
93
112
  method: 'PUT',
94
113
  requestData: searchParams
95
- });
114
+ };
115
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
116
+ return fetcher(path, payload);
96
117
  },
97
118
  deleteById(idOrIds, transaction) {
98
119
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
99
- if ((0, utils_js_1.isArray)(idOrIds)) {
100
- return fetcher(`/records/delete`, {
101
- headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
102
- method: 'PUT',
103
- requestData: { limit: 1000, where: { $id: { $in: idOrIds } } }
104
- });
105
- }
106
- else {
107
- return fetcher(`/records/${idOrIds}`, {
108
- headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
109
- method: 'DELETE'
110
- });
111
- }
120
+ const multipleTargets = (0, utils_js_1.isArray)(idOrIds);
121
+ const path = multipleTargets ? `/records/delete` : `/records/${idOrIds}`;
122
+ const payload = {
123
+ headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
124
+ method: multipleTargets ? 'PUT' : 'DELETE',
125
+ requestData: multipleTargets ? { limit: 1000, where: { $id: { $in: idOrIds } } } : undefined
126
+ };
127
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
128
+ return fetcher(path, payload);
112
129
  },
113
- detach: async (source, idOrIds, options, transaction) => {
130
+ async detach(source, idOrIds, options, transaction) {
114
131
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
115
132
  const recordId = (0, utils_js_2.pickRecordId)(source);
116
- return fetcher(`/records/${recordId}/relations`, {
133
+ const path = `/records/${recordId}/relations`;
134
+ const payload = {
117
135
  headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
118
136
  method: 'PUT',
119
- requestData: Object.assign(Object.assign({ targetIds: idOrIds }, ((options === null || options === void 0 ? void 0 : options.typeOrTypes) && { typeOrTypes: options === null || options === void 0 ? void 0 : options.typeOrTypes })), ((options === null || options === void 0 ? void 0 : options.direction) && { direction: options === null || options === void 0 ? void 0 : options.direction }))
120
- });
137
+ requestData: Object.assign(Object.assign({ targetIds: idOrIds }, ((options === null || options === void 0 ? void 0 : options.typeOrTypes) && { typeOrTypes: options.typeOrTypes })), ((options === null || options === void 0 ? void 0 : options.direction) && { direction: options.direction }))
138
+ };
139
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
140
+ return fetcher(path, payload);
121
141
  },
122
142
  export(searchParams, transaction) {
123
143
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
124
- return fetcher(`/records/export/csv`, {
144
+ const path = `/records/export/csv`;
145
+ const payload = {
125
146
  headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
126
147
  method: 'POST',
127
148
  requestData: searchParams
128
- });
149
+ };
150
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
151
+ return fetcher(path, payload);
129
152
  },
130
153
  find(params, transaction) {
131
154
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
132
- const url = (params === null || params === void 0 ? void 0 : params.id) ? `/records/${params === null || params === void 0 ? void 0 : params.id}/search` : `/records/search`;
133
- return fetcher(url, {
155
+ const path = (params === null || params === void 0 ? void 0 : params.id) ? `/records/${params.id}/search` : `/records/search`;
156
+ const payload = {
134
157
  headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
135
158
  method: 'POST',
136
159
  requestData: params === null || params === void 0 ? void 0 : params.searchParams
137
- });
160
+ };
161
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
162
+ return fetcher(path, payload);
138
163
  },
139
164
  findById(idOrIds, transaction) {
140
165
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
141
- if ((0, utils_js_1.isArray)(idOrIds)) {
142
- return fetcher(`/records`, {
143
- headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
144
- method: 'POST',
145
- requestData: { ids: idOrIds }
146
- });
147
- }
148
- return fetcher(`/records/${idOrIds}`, {
166
+ const path = (0, utils_js_1.isArray)(idOrIds) ? `/records` : `/records/${idOrIds}`;
167
+ const payload = {
149
168
  headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
150
- method: 'GET'
151
- });
169
+ method: (0, utils_js_1.isArray)(idOrIds) ? 'POST' : 'GET',
170
+ requestData: (0, utils_js_1.isArray)(idOrIds) ? { ids: idOrIds } : undefined
171
+ };
172
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
173
+ return fetcher(path, payload);
152
174
  },
153
175
  async findOne(searchParams, transaction) {
154
176
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
155
- const response = await fetcher(`/records/search`, {
177
+ const path = `/records/search`;
178
+ const payload = {
156
179
  headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
157
180
  method: 'POST',
158
181
  requestData: Object.assign(Object.assign({}, searchParams), { limit: 1, skip: 0 })
159
- });
182
+ };
183
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
184
+ const response = await fetcher(path, payload);
160
185
  const [record] = response.data;
161
186
  return Object.assign(Object.assign({}, response), { data: record });
162
187
  },
163
188
  async findUniq(searchParams, transaction) {
164
189
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
165
- const response = await fetcher(`/records/search`, {
190
+ const path = `/records/search`;
191
+ const payload = {
166
192
  headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
167
193
  method: 'POST',
168
194
  requestData: Object.assign(Object.assign({}, searchParams), { limit: 1, skip: 0 })
169
- });
195
+ };
196
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
197
+ const response = await fetcher(path, payload);
170
198
  if (typeof response.total !== 'undefined' && response.total > 1) {
171
199
  throw new errors_js_1.NonUniqueResultError(response.total, searchParams);
172
200
  }
@@ -176,18 +204,24 @@ const createApi = (fetcher) => ({
176
204
  properties(target, transaction) {
177
205
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
178
206
  const recordId = (0, utils_js_2.pickRecordId)(target);
179
- return fetcher(`/records/${recordId}/properties`, {
207
+ const path = `/records/${recordId}/properties`;
208
+ const payload = {
180
209
  headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
181
210
  method: 'GET'
182
- });
211
+ };
212
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
213
+ return fetcher(path, payload);
183
214
  },
184
- relations: async (target, transaction) => {
215
+ async relations(target, transaction) {
185
216
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
186
217
  const recordId = (0, utils_js_2.pickRecordId)(target);
187
- return fetcher(`/records/${recordId}/relations`, {
218
+ const path = `/records/${recordId}/relations`;
219
+ const payload = {
188
220
  headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
189
221
  method: 'GET'
190
- });
222
+ };
223
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
224
+ return fetcher(path, payload);
191
225
  },
192
226
  // upsert() {
193
227
  // // @TODO
@@ -195,63 +229,84 @@ const createApi = (fetcher) => ({
195
229
  set(target, data, transaction) {
196
230
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
197
231
  const recordId = (0, utils_js_2.pickRecordId)(target);
198
- return fetcher(`/records/${recordId}`, {
232
+ const path = `/records/${recordId}`;
233
+ const payload = {
199
234
  headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
200
235
  method: 'PUT',
201
236
  requestData: data instanceof record_js_1.DBRecordDraft ? data.toJson() : data
202
- });
237
+ };
238
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
239
+ return fetcher(path, payload);
203
240
  },
204
241
  update(target, data, transaction) {
205
242
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
206
243
  const recordId = (0, utils_js_2.pickRecordId)(target);
207
- return fetcher(`/records/${recordId}`, {
244
+ const path = `/records/${recordId}`;
245
+ const payload = {
208
246
  headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
209
247
  method: 'PATCH',
210
248
  requestData: data instanceof record_js_1.DBRecordDraft ? data.toJson() : data
211
- });
249
+ };
250
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
251
+ return fetcher(path, payload);
212
252
  }
213
253
  },
214
254
  relations: {
215
- find: async (searchParams, pagination, transaction) => {
255
+ async find(searchParams, pagination, transaction) {
216
256
  const txId = (0, utils_js_2.pickTransactionId)(transaction);
217
257
  const queryParams = new URLSearchParams();
218
- if (typeof (pagination === null || pagination === void 0 ? void 0 : pagination.limit) !== 'undefined') {
258
+ if ((pagination === null || pagination === void 0 ? void 0 : pagination.limit) !== undefined) {
219
259
  queryParams.append('limit', pagination.limit.toString());
220
260
  }
221
- if (typeof (pagination === null || pagination === void 0 ? void 0 : pagination.skip) !== 'undefined') {
222
- queryParams.append('skip', pagination === null || pagination === void 0 ? void 0 : pagination.skip.toString());
261
+ if ((pagination === null || pagination === void 0 ? void 0 : pagination.skip) !== undefined) {
262
+ queryParams.append('skip', pagination.skip.toString());
223
263
  }
224
264
  const queryString = queryParams.toString() ? '?' + queryParams.toString() : '';
225
- return fetcher(`/records/relations/search${queryString}`, {
265
+ const path = `/records/relations/search${queryString}`;
266
+ const payload = {
226
267
  headers: Object.assign({}, (0, utils_js_2.buildTransactionHeader)(txId)),
227
268
  method: 'POST',
228
269
  requestData: searchParams
229
- });
270
+ };
271
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
272
+ return fetcher(path, payload);
230
273
  }
231
274
  },
232
275
  tx: {
233
276
  begin(config = {}) {
234
- return fetcher(`/tx`, {
277
+ const path = `/tx`;
278
+ const payload = {
235
279
  method: 'POST',
236
280
  requestData: config
237
- });
281
+ };
282
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
283
+ return fetcher(path, payload);
238
284
  },
239
285
  commit(id) {
240
- return fetcher(`/tx/${id}/commit`, {
286
+ const path = `/tx/${id}/commit`;
287
+ const payload = {
241
288
  method: 'POST',
242
289
  requestData: {}
243
- });
290
+ };
291
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
292
+ return fetcher(path, payload);
244
293
  },
245
294
  get(id) {
246
- return fetcher(`/tx/${id}`, {
295
+ const path = `/tx/${id}`;
296
+ const payload = {
247
297
  method: 'GET'
248
- });
298
+ };
299
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
300
+ return fetcher(path, payload);
249
301
  },
250
302
  rollback(id) {
251
- return fetcher(`/tx/${id}/rollback`, {
303
+ const path = `/tx/${id}/rollback`;
304
+ const payload = {
252
305
  method: 'POST',
253
306
  requestData: {}
254
- });
307
+ };
308
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
309
+ return fetcher(path, payload);
255
310
  }
256
311
  }
257
312
  });
@@ -8,5 +8,6 @@ exports.ALLOWED_CONFIG_PROPERTIES = [
8
8
  'port',
9
9
  'protocol',
10
10
  'url',
11
- 'validator'
11
+ 'validator',
12
+ 'logger'
12
13
  ];
package/esm/api/api.js CHANGED
@@ -64,7 +64,7 @@ export class RestAPI {
64
64
  url
65
65
  });
66
66
  }
67
- this.api = createApi(this.fetcher);
67
+ this.api = createApi(this.fetcher, config === null || config === void 0 ? void 0 : config.logger);
68
68
  this.records = {
69
69
  attach: async (sourceId, target, options, transaction) => {
70
70
  var _a, _b;
@@ -3,39 +3,51 @@ import { NonUniqueResultError } from '../sdk/errors.js';
3
3
  import { DBRecordsBatchDraft, DBRecordDraft } from '../sdk/record.js';
4
4
  import { buildTransactionHeader, pickRecordId, pickTransactionId } from './utils.js';
5
5
  // POST /api/v1/records/:id @TODO
6
- export const createApi = (fetcher) => ({
6
+ export const createApi = (fetcher, logger) => ({
7
7
  labels: {
8
8
  async find(searchParams, transaction) {
9
9
  const txId = pickTransactionId(transaction);
10
- return fetcher(`/labels`, {
10
+ const path = '/labels';
11
+ const payload = {
11
12
  headers: Object.assign({}, buildTransactionHeader(txId)),
12
13
  method: 'POST',
13
14
  requestData: searchParams
14
- });
15
+ };
16
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
17
+ return fetcher(path, payload);
15
18
  }
16
19
  },
17
20
  properties: {
18
21
  delete: (id, transaction) => {
19
22
  const txId = pickTransactionId(transaction);
20
- return fetcher(`/properties/${id}`, {
23
+ const path = `/properties/${id}`;
24
+ const payload = {
21
25
  headers: Object.assign({}, buildTransactionHeader(txId)),
22
26
  method: 'DELETE'
23
- });
27
+ };
28
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
29
+ return fetcher(path, payload);
24
30
  },
25
31
  find: (searchParams, transaction) => {
26
32
  const txId = pickTransactionId(transaction);
27
- return fetcher(`/properties`, {
33
+ const path = `/properties`;
34
+ const payload = {
28
35
  headers: Object.assign({}, buildTransactionHeader(txId)),
29
36
  method: 'POST',
30
37
  requestData: searchParams
31
- });
38
+ };
39
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
40
+ return fetcher(path, payload);
32
41
  },
33
42
  findById: (id, transaction) => {
34
43
  const txId = pickTransactionId(transaction);
35
- return fetcher(`/properties/${id}`, {
44
+ const path = `/properties/${id}`;
45
+ const payload = {
36
46
  headers: Object.assign({}, buildTransactionHeader(txId)),
37
47
  method: 'GET'
38
- });
48
+ };
49
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
50
+ return fetcher(path, payload);
39
51
  },
40
52
  // update: () => {
41
53
  // // @TODO
@@ -45,125 +57,141 @@ export const createApi = (fetcher) => ({
45
57
  // },
46
58
  values: (id, transaction) => {
47
59
  const txId = pickTransactionId(transaction);
48
- return fetcher(`/properties/${id}/values`, {
60
+ const path = `/properties/${id}/values`;
61
+ const payload = {
49
62
  headers: Object.assign({}, buildTransactionHeader(txId)),
50
63
  method: 'GET'
51
- });
64
+ };
65
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
66
+ return fetcher(path, payload);
52
67
  }
53
68
  },
54
69
  records: {
55
- attach: async (source, idOrIds, options, transaction) => {
70
+ async attach(source, idOrIds, options, transaction) {
56
71
  const txId = pickTransactionId(transaction);
57
72
  const recordId = pickRecordId(source);
58
- return fetcher(`/records/${recordId}/relations`, {
73
+ const path = `/records/${recordId}/relations`;
74
+ const payload = {
59
75
  headers: Object.assign({}, buildTransactionHeader(txId)),
60
76
  method: 'POST',
61
- requestData: Object.assign(Object.assign({ targetIds: idOrIds }, ((options === null || options === void 0 ? void 0 : options.type) && { type: options === null || options === void 0 ? void 0 : options.type })), ((options === null || options === void 0 ? void 0 : options.direction) && { direction: options === null || options === void 0 ? void 0 : options.direction }))
62
- });
77
+ requestData: Object.assign(Object.assign({ targetIds: idOrIds }, ((options === null || options === void 0 ? void 0 : options.type) && { type: options.type })), ((options === null || options === void 0 ? void 0 : options.direction) && { direction: options.direction }))
78
+ };
79
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
80
+ return fetcher(path, payload);
63
81
  },
64
82
  async create(data, transaction) {
65
83
  const txId = pickTransactionId(transaction);
66
- if (data instanceof DBRecordDraft) {
67
- return fetcher(`/records`, {
68
- headers: Object.assign({}, buildTransactionHeader(txId)),
69
- method: 'POST',
70
- requestData: data.toJson()
71
- });
72
- }
73
- return { data: undefined, success: false };
84
+ const path = `/records`;
85
+ const payload = {
86
+ headers: Object.assign({}, buildTransactionHeader(txId)),
87
+ method: 'POST',
88
+ requestData: data instanceof DBRecordDraft ? data.toJson() : data
89
+ };
90
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
91
+ return fetcher(path, payload);
74
92
  },
75
93
  async createMany(data, transaction) {
76
94
  const txId = pickTransactionId(transaction);
77
- if (data instanceof DBRecordsBatchDraft) {
78
- return fetcher(`/records/import/json`, {
79
- headers: Object.assign({}, buildTransactionHeader(txId)),
80
- method: 'POST',
81
- requestData: data.toJson()
82
- });
83
- }
84
- return { data: [], success: false };
95
+ const path = `/records/import/json`;
96
+ const payload = {
97
+ headers: Object.assign({}, buildTransactionHeader(txId)),
98
+ method: 'POST',
99
+ requestData: data instanceof DBRecordsBatchDraft ? data.toJson() : data
100
+ };
101
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
102
+ return fetcher(path, payload);
85
103
  },
86
104
  delete(searchParams, transaction) {
87
105
  const txId = pickTransactionId(transaction);
88
- return fetcher(`/records/delete`, {
106
+ const path = `/records/delete`;
107
+ const payload = {
89
108
  headers: Object.assign({}, buildTransactionHeader(txId)),
90
109
  method: 'PUT',
91
110
  requestData: searchParams
92
- });
111
+ };
112
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
113
+ return fetcher(path, payload);
93
114
  },
94
115
  deleteById(idOrIds, transaction) {
95
116
  const txId = pickTransactionId(transaction);
96
- if (isArray(idOrIds)) {
97
- return fetcher(`/records/delete`, {
98
- headers: Object.assign({}, buildTransactionHeader(txId)),
99
- method: 'PUT',
100
- requestData: { limit: 1000, where: { $id: { $in: idOrIds } } }
101
- });
102
- }
103
- else {
104
- return fetcher(`/records/${idOrIds}`, {
105
- headers: Object.assign({}, buildTransactionHeader(txId)),
106
- method: 'DELETE'
107
- });
108
- }
117
+ const multipleTargets = isArray(idOrIds);
118
+ const path = multipleTargets ? `/records/delete` : `/records/${idOrIds}`;
119
+ const payload = {
120
+ headers: Object.assign({}, buildTransactionHeader(txId)),
121
+ method: multipleTargets ? 'PUT' : 'DELETE',
122
+ requestData: multipleTargets ? { limit: 1000, where: { $id: { $in: idOrIds } } } : undefined
123
+ };
124
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
125
+ return fetcher(path, payload);
109
126
  },
110
- detach: async (source, idOrIds, options, transaction) => {
127
+ async detach(source, idOrIds, options, transaction) {
111
128
  const txId = pickTransactionId(transaction);
112
129
  const recordId = pickRecordId(source);
113
- return fetcher(`/records/${recordId}/relations`, {
130
+ const path = `/records/${recordId}/relations`;
131
+ const payload = {
114
132
  headers: Object.assign({}, buildTransactionHeader(txId)),
115
133
  method: 'PUT',
116
- requestData: Object.assign(Object.assign({ targetIds: idOrIds }, ((options === null || options === void 0 ? void 0 : options.typeOrTypes) && { typeOrTypes: options === null || options === void 0 ? void 0 : options.typeOrTypes })), ((options === null || options === void 0 ? void 0 : options.direction) && { direction: options === null || options === void 0 ? void 0 : options.direction }))
117
- });
134
+ requestData: Object.assign(Object.assign({ targetIds: idOrIds }, ((options === null || options === void 0 ? void 0 : options.typeOrTypes) && { typeOrTypes: options.typeOrTypes })), ((options === null || options === void 0 ? void 0 : options.direction) && { direction: options.direction }))
135
+ };
136
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
137
+ return fetcher(path, payload);
118
138
  },
119
139
  export(searchParams, transaction) {
120
140
  const txId = pickTransactionId(transaction);
121
- return fetcher(`/records/export/csv`, {
141
+ const path = `/records/export/csv`;
142
+ const payload = {
122
143
  headers: Object.assign({}, buildTransactionHeader(txId)),
123
144
  method: 'POST',
124
145
  requestData: searchParams
125
- });
146
+ };
147
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
148
+ return fetcher(path, payload);
126
149
  },
127
150
  find(params, transaction) {
128
151
  const txId = pickTransactionId(transaction);
129
- const url = (params === null || params === void 0 ? void 0 : params.id) ? `/records/${params === null || params === void 0 ? void 0 : params.id}/search` : `/records/search`;
130
- return fetcher(url, {
152
+ const path = (params === null || params === void 0 ? void 0 : params.id) ? `/records/${params.id}/search` : `/records/search`;
153
+ const payload = {
131
154
  headers: Object.assign({}, buildTransactionHeader(txId)),
132
155
  method: 'POST',
133
156
  requestData: params === null || params === void 0 ? void 0 : params.searchParams
134
- });
157
+ };
158
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
159
+ return fetcher(path, payload);
135
160
  },
136
161
  findById(idOrIds, transaction) {
137
162
  const txId = pickTransactionId(transaction);
138
- if (isArray(idOrIds)) {
139
- return fetcher(`/records`, {
140
- headers: Object.assign({}, buildTransactionHeader(txId)),
141
- method: 'POST',
142
- requestData: { ids: idOrIds }
143
- });
144
- }
145
- return fetcher(`/records/${idOrIds}`, {
163
+ const path = isArray(idOrIds) ? `/records` : `/records/${idOrIds}`;
164
+ const payload = {
146
165
  headers: Object.assign({}, buildTransactionHeader(txId)),
147
- method: 'GET'
148
- });
166
+ method: isArray(idOrIds) ? 'POST' : 'GET',
167
+ requestData: isArray(idOrIds) ? { ids: idOrIds } : undefined
168
+ };
169
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
170
+ return fetcher(path, payload);
149
171
  },
150
172
  async findOne(searchParams, transaction) {
151
173
  const txId = pickTransactionId(transaction);
152
- const response = await fetcher(`/records/search`, {
174
+ const path = `/records/search`;
175
+ const payload = {
153
176
  headers: Object.assign({}, buildTransactionHeader(txId)),
154
177
  method: 'POST',
155
178
  requestData: Object.assign(Object.assign({}, searchParams), { limit: 1, skip: 0 })
156
- });
179
+ };
180
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
181
+ const response = await fetcher(path, payload);
157
182
  const [record] = response.data;
158
183
  return Object.assign(Object.assign({}, response), { data: record });
159
184
  },
160
185
  async findUniq(searchParams, transaction) {
161
186
  const txId = pickTransactionId(transaction);
162
- const response = await fetcher(`/records/search`, {
187
+ const path = `/records/search`;
188
+ const payload = {
163
189
  headers: Object.assign({}, buildTransactionHeader(txId)),
164
190
  method: 'POST',
165
191
  requestData: Object.assign(Object.assign({}, searchParams), { limit: 1, skip: 0 })
166
- });
192
+ };
193
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
194
+ const response = await fetcher(path, payload);
167
195
  if (typeof response.total !== 'undefined' && response.total > 1) {
168
196
  throw new NonUniqueResultError(response.total, searchParams);
169
197
  }
@@ -173,18 +201,24 @@ export const createApi = (fetcher) => ({
173
201
  properties(target, transaction) {
174
202
  const txId = pickTransactionId(transaction);
175
203
  const recordId = pickRecordId(target);
176
- return fetcher(`/records/${recordId}/properties`, {
204
+ const path = `/records/${recordId}/properties`;
205
+ const payload = {
177
206
  headers: Object.assign({}, buildTransactionHeader(txId)),
178
207
  method: 'GET'
179
- });
208
+ };
209
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
210
+ return fetcher(path, payload);
180
211
  },
181
- relations: async (target, transaction) => {
212
+ async relations(target, transaction) {
182
213
  const txId = pickTransactionId(transaction);
183
214
  const recordId = pickRecordId(target);
184
- return fetcher(`/records/${recordId}/relations`, {
215
+ const path = `/records/${recordId}/relations`;
216
+ const payload = {
185
217
  headers: Object.assign({}, buildTransactionHeader(txId)),
186
218
  method: 'GET'
187
- });
219
+ };
220
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
221
+ return fetcher(path, payload);
188
222
  },
189
223
  // upsert() {
190
224
  // // @TODO
@@ -192,63 +226,84 @@ export const createApi = (fetcher) => ({
192
226
  set(target, data, transaction) {
193
227
  const txId = pickTransactionId(transaction);
194
228
  const recordId = pickRecordId(target);
195
- return fetcher(`/records/${recordId}`, {
229
+ const path = `/records/${recordId}`;
230
+ const payload = {
196
231
  headers: Object.assign({}, buildTransactionHeader(txId)),
197
232
  method: 'PUT',
198
233
  requestData: data instanceof DBRecordDraft ? data.toJson() : data
199
- });
234
+ };
235
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
236
+ return fetcher(path, payload);
200
237
  },
201
238
  update(target, data, transaction) {
202
239
  const txId = pickTransactionId(transaction);
203
240
  const recordId = pickRecordId(target);
204
- return fetcher(`/records/${recordId}`, {
241
+ const path = `/records/${recordId}`;
242
+ const payload = {
205
243
  headers: Object.assign({}, buildTransactionHeader(txId)),
206
244
  method: 'PATCH',
207
245
  requestData: data instanceof DBRecordDraft ? data.toJson() : data
208
- });
246
+ };
247
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
248
+ return fetcher(path, payload);
209
249
  }
210
250
  },
211
251
  relations: {
212
- find: async (searchParams, pagination, transaction) => {
252
+ async find(searchParams, pagination, transaction) {
213
253
  const txId = pickTransactionId(transaction);
214
254
  const queryParams = new URLSearchParams();
215
- if (typeof (pagination === null || pagination === void 0 ? void 0 : pagination.limit) !== 'undefined') {
255
+ if ((pagination === null || pagination === void 0 ? void 0 : pagination.limit) !== undefined) {
216
256
  queryParams.append('limit', pagination.limit.toString());
217
257
  }
218
- if (typeof (pagination === null || pagination === void 0 ? void 0 : pagination.skip) !== 'undefined') {
219
- queryParams.append('skip', pagination === null || pagination === void 0 ? void 0 : pagination.skip.toString());
258
+ if ((pagination === null || pagination === void 0 ? void 0 : pagination.skip) !== undefined) {
259
+ queryParams.append('skip', pagination.skip.toString());
220
260
  }
221
261
  const queryString = queryParams.toString() ? '?' + queryParams.toString() : '';
222
- return fetcher(`/records/relations/search${queryString}`, {
262
+ const path = `/records/relations/search${queryString}`;
263
+ const payload = {
223
264
  headers: Object.assign({}, buildTransactionHeader(txId)),
224
265
  method: 'POST',
225
266
  requestData: searchParams
226
- });
267
+ };
268
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
269
+ return fetcher(path, payload);
227
270
  }
228
271
  },
229
272
  tx: {
230
273
  begin(config = {}) {
231
- return fetcher(`/tx`, {
274
+ const path = `/tx`;
275
+ const payload = {
232
276
  method: 'POST',
233
277
  requestData: config
234
- });
278
+ };
279
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
280
+ return fetcher(path, payload);
235
281
  },
236
282
  commit(id) {
237
- return fetcher(`/tx/${id}/commit`, {
283
+ const path = `/tx/${id}/commit`;
284
+ const payload = {
238
285
  method: 'POST',
239
286
  requestData: {}
240
- });
287
+ };
288
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
289
+ return fetcher(path, payload);
241
290
  },
242
291
  get(id) {
243
- return fetcher(`/tx/${id}`, {
292
+ const path = `/tx/${id}`;
293
+ const payload = {
244
294
  method: 'GET'
245
- });
295
+ };
296
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
297
+ return fetcher(path, payload);
246
298
  },
247
299
  rollback(id) {
248
- return fetcher(`/tx/${id}/rollback`, {
300
+ const path = `/tx/${id}/rollback`;
301
+ const payload = {
249
302
  method: 'POST',
250
303
  requestData: {}
251
- });
304
+ };
305
+ logger === null || logger === void 0 ? void 0 : logger(Object.assign({ path }, payload));
306
+ return fetcher(path, payload);
252
307
  }
253
308
  }
254
309
  });
@@ -5,5 +5,6 @@ export const ALLOWED_CONFIG_PROPERTIES = [
5
5
  'port',
6
6
  'protocol',
7
7
  'url',
8
- 'validator'
8
+ 'validator',
9
+ 'logger'
9
10
  ];
package/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
  "rushdb"
13
13
  ],
14
14
  "license": "Apache-2.0",
15
- "version": "0.4.0",
15
+ "version": "0.5.0",
16
16
  "type": "module",
17
17
  "main": "cjs/index.cjs.node.js",
18
18
  "types": "types/index.d.ts",
@@ -4,7 +4,8 @@ import type { Transaction } from '../sdk/transaction.js';
4
4
  import type { Property, PropertyValuesData, SearchQuery, Schema, MaybeArray } from '../types/index.js';
5
5
  import type { ApiResponse } from './types.js';
6
6
  import { DBRecordsBatchDraft, DBRecordDraft } from '../sdk/record.js';
7
- export declare const createApi: (fetcher: ReturnType<typeof createFetcher>) => {
7
+ import type { Logger } from '../sdk/types.js';
8
+ export declare const createApi: (fetcher: ReturnType<typeof createFetcher>, logger?: Logger) => {
8
9
  labels: {
9
10
  find<S extends Schema = any>(searchParams: SearchQuery<S>, transaction?: Transaction | string): Promise<ApiResponse<Record<string, number>>>;
10
11
  };
@@ -15,7 +16,7 @@ export declare const createApi: (fetcher: ReturnType<typeof createFetcher>) => {
15
16
  values: (id: string, transaction?: Transaction | string) => Promise<ApiResponse<Property & PropertyValuesData>>;
16
17
  };
17
18
  records: {
18
- attach: (source: DBRecordTarget, idOrIds: MaybeArray<string>, options?: RelationOptions, transaction?: Transaction | string) => Promise<ApiResponse<{
19
+ attach(source: DBRecordTarget, idOrIds: MaybeArray<string>, options?: RelationOptions, transaction?: Transaction | string): Promise<ApiResponse<{
19
20
  message: string;
20
21
  }>>;
21
22
  create<S extends Schema = any>(data: DBRecordDraft | S, transaction?: Transaction | string): Promise<ApiResponse<DBRecord<S> | undefined>>;
@@ -26,7 +27,7 @@ export declare const createApi: (fetcher: ReturnType<typeof createFetcher>) => {
26
27
  deleteById(idOrIds: MaybeArray<string>, transaction?: Transaction | string): Promise<ApiResponse<{
27
28
  message: string;
28
29
  }>>;
29
- detach: (source: DBRecordTarget, idOrIds: MaybeArray<string>, options?: RelationDetachOptions, transaction?: Transaction | string) => Promise<ApiResponse<{
30
+ detach(source: DBRecordTarget, idOrIds: MaybeArray<string>, options?: RelationDetachOptions, transaction?: Transaction | string): Promise<ApiResponse<{
30
31
  message: string;
31
32
  }>>;
32
33
  export<S extends Schema = any>(searchParams: SearchQuery<S>, transaction?: Transaction | string): Promise<ApiResponse<{
@@ -37,16 +38,16 @@ export declare const createApi: (fetcher: ReturnType<typeof createFetcher>) => {
37
38
  id?: string;
38
39
  searchParams: SearchQuery<S>;
39
40
  }, transaction?: Transaction | string): Promise<ApiResponse<DBRecord<S>[]>>;
40
- findById<S extends Schema = any>(idOrIds: MaybeArray<string>, transaction?: Transaction | string): Promise<ApiResponse<DBRecord<S>[]>> | Promise<ApiResponse<DBRecord<S>>>;
41
+ findById<S extends Schema = any>(idOrIds: MaybeArray<string>, transaction?: Transaction | string): Promise<ApiResponse<DBRecord<S> | DBRecord<S>[]>>;
41
42
  findOne<S extends Schema = any>(searchParams: SearchQuery<S>, transaction?: Transaction | string): Promise<ApiResponse<DBRecord<S>>>;
42
43
  findUniq<S extends Schema = any>(searchParams: SearchQuery<S>, transaction?: Transaction | string): Promise<ApiResponse<DBRecord<S>>>;
43
44
  properties(target: string, transaction?: Transaction | string): Promise<ApiResponse<Property[]>>;
44
- relations: (target: DBRecordTarget, transaction?: Transaction | string) => Promise<ApiResponse<Relation[]>>;
45
+ relations(target: DBRecordTarget, transaction?: Transaction | string): Promise<ApiResponse<Relation[]>>;
45
46
  set<S extends Schema = any>(target: DBRecordTarget, data: DBRecordDraft | S, transaction?: Transaction | string): Promise<ApiResponse<DBRecord<S>>>;
46
47
  update<S extends Schema = any>(target: DBRecordTarget, data: DBRecordDraft | S, transaction?: Transaction | string): Promise<ApiResponse<DBRecord<S>>>;
47
48
  };
48
49
  relations: {
49
- find: <S extends Schema = any>(searchParams: SearchQuery<S>, pagination?: Pick<SearchQuery, "limit" | "skip">, transaction?: Transaction | string) => Promise<ApiResponse<Relation[]>>;
50
+ find<S extends Schema = any>(searchParams: SearchQuery<S>, pagination?: Pick<SearchQuery, "limit" | "skip">, transaction?: Transaction | string): Promise<ApiResponse<Relation[]>>;
50
51
  };
51
52
  tx: {
52
53
  begin(config?: Partial<{
@@ -11,9 +11,11 @@ export type State = {
11
11
  timeout: number;
12
12
  token?: string;
13
13
  } & Partial<ApiConnectionConfig>;
14
+ export type Logger = (payload: any) => void;
14
15
  type CommonUserProvidedConfig = {
15
16
  httpClient?: HttpClientInterface;
16
17
  timeout?: number;
18
+ logger?: Logger;
17
19
  } & ApiConnectionConfig;
18
20
  export type UserProvidedConfig = CommonUserProvidedConfig;
19
21
  export {};