@zthun/webigail-rest 4.0.9 → 5.0.1
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/dist/index.cjs +87 -382
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +87 -382
- package/dist/index.js.map +1 -1
- package/package.json +9 -9
package/dist/index.cjs
CHANGED
|
@@ -6,24 +6,6 @@ const webigailHttp = require('@zthun/webigail-http');
|
|
|
6
6
|
const webigailUrl = require('@zthun/webigail-url');
|
|
7
7
|
const helpfulQuery = require('@zthun/helpful-query');
|
|
8
8
|
|
|
9
|
-
function _class_call_check$1(instance, Constructor) {
|
|
10
|
-
if (!(instance instanceof Constructor)) {
|
|
11
|
-
throw new TypeError("Cannot call a class as a function");
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
function _defineProperties$1(target, props) {
|
|
15
|
-
for(var i = 0; i < props.length; i++){
|
|
16
|
-
var descriptor = props[i];
|
|
17
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
|
18
|
-
descriptor.configurable = true;
|
|
19
|
-
if ("value" in descriptor) descriptor.writable = true;
|
|
20
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
function _create_class$1(Constructor, protoProps, staticProps) {
|
|
24
|
-
if (protoProps) _defineProperties$1(Constructor.prototype, protoProps);
|
|
25
|
-
return Constructor;
|
|
26
|
-
}
|
|
27
9
|
function _define_property$1(obj, key, value) {
|
|
28
10
|
if (key in obj) {
|
|
29
11
|
Object.defineProperty(obj, key, {
|
|
@@ -40,16 +22,8 @@ function _define_property$1(obj, key, value) {
|
|
|
40
22
|
/**
|
|
41
23
|
* Represents a decorator that can build a url for a restful endpoint
|
|
42
24
|
* given an existing base url.
|
|
43
|
-
*/
|
|
44
|
-
|
|
45
|
-
_class_call_check$1(this, ZRestfulUrlBuilder);
|
|
46
|
-
_define_property$1(this, "_url", void 0);
|
|
47
|
-
this._url = new webigailUrl.ZUrlBuilder().parse(baseUrl);
|
|
48
|
-
}
|
|
49
|
-
_create_class$1(ZRestfulUrlBuilder, [
|
|
50
|
-
{
|
|
51
|
-
key: "from",
|
|
52
|
-
value: /**
|
|
25
|
+
*/ class ZRestfulUrlBuilder {
|
|
26
|
+
/**
|
|
53
27
|
* Adds the request parameters to the url.
|
|
54
28
|
*
|
|
55
29
|
* This will add the page, size, search, filter, and sort parameters to the url if they are defined.
|
|
@@ -58,87 +32,37 @@ function _define_property$1(obj, key, value) {
|
|
|
58
32
|
* The request to add to the url.
|
|
59
33
|
* @returns
|
|
60
34
|
* This object.
|
|
61
|
-
*/
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
{
|
|
67
|
-
key: "count",
|
|
68
|
-
value: /**
|
|
35
|
+
*/ from(request) {
|
|
36
|
+
this._url = this._url.page(request.page).size(request.size).search(request.search).filter(new helpfulQuery.ZFilterSerialize().serialize(request.filter)).sort(new helpfulQuery.ZSortSerialize().serialize(request.sort));
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
69
40
|
* Updates the url to optimize to retrieve a count.
|
|
70
41
|
*
|
|
71
42
|
* This will add the size and page parameters to the url and remove the sort parameter.
|
|
72
43
|
*
|
|
73
44
|
* @returns
|
|
74
45
|
* This object.
|
|
75
|
-
*/
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
{
|
|
81
|
-
key: "build",
|
|
82
|
-
value: /**
|
|
46
|
+
*/ count() {
|
|
47
|
+
this._url = this._url.size(1).page(1).sort(null);
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
83
51
|
* Returns the built url.
|
|
84
52
|
*
|
|
85
53
|
* @returns
|
|
86
54
|
* The built url.
|
|
87
|
-
*/
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
]);
|
|
92
|
-
return ZRestfulUrlBuilder;
|
|
93
|
-
}();
|
|
94
|
-
|
|
95
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
96
|
-
try {
|
|
97
|
-
var info = gen[key](arg);
|
|
98
|
-
var value = info.value;
|
|
99
|
-
} catch (error) {
|
|
100
|
-
reject(error);
|
|
101
|
-
return;
|
|
55
|
+
*/ build() {
|
|
56
|
+
return this._url.build();
|
|
102
57
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
function _async_to_generator(fn) {
|
|
110
|
-
return function() {
|
|
111
|
-
var self = this, args = arguments;
|
|
112
|
-
return new Promise(function(resolve, reject) {
|
|
113
|
-
var gen = fn.apply(self, args);
|
|
114
|
-
function _next(value) {
|
|
115
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
116
|
-
}
|
|
117
|
-
function _throw(err) {
|
|
118
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
119
|
-
}
|
|
120
|
-
_next(undefined);
|
|
121
|
-
});
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
function _class_call_check(instance, Constructor) {
|
|
125
|
-
if (!(instance instanceof Constructor)) {
|
|
126
|
-
throw new TypeError("Cannot call a class as a function");
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
function _defineProperties(target, props) {
|
|
130
|
-
for(var i = 0; i < props.length; i++){
|
|
131
|
-
var descriptor = props[i];
|
|
132
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
|
133
|
-
descriptor.configurable = true;
|
|
134
|
-
if ("value" in descriptor) descriptor.writable = true;
|
|
135
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
|
58
|
+
/**
|
|
59
|
+
* Initializes a new instance of this object.
|
|
60
|
+
*/ constructor(baseUrl){
|
|
61
|
+
_define_property$1(this, "_url", void 0);
|
|
62
|
+
this._url = new webigailUrl.ZUrlBuilder().parse(baseUrl);
|
|
136
63
|
}
|
|
137
64
|
}
|
|
138
|
-
|
|
139
|
-
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
140
|
-
return Constructor;
|
|
141
|
-
}
|
|
65
|
+
|
|
142
66
|
function _define_property(obj, key, value) {
|
|
143
67
|
if (key in obj) {
|
|
144
68
|
Object.defineProperty(obj, key, {
|
|
@@ -152,97 +76,6 @@ function _define_property(obj, key, value) {
|
|
|
152
76
|
}
|
|
153
77
|
return obj;
|
|
154
78
|
}
|
|
155
|
-
function _ts_generator(thisArg, body) {
|
|
156
|
-
var f, y, t, _ = {
|
|
157
|
-
label: 0,
|
|
158
|
-
sent: function() {
|
|
159
|
-
if (t[0] & 1) throw t[1];
|
|
160
|
-
return t[1];
|
|
161
|
-
},
|
|
162
|
-
trys: [],
|
|
163
|
-
ops: []
|
|
164
|
-
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
165
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
166
|
-
return this;
|
|
167
|
-
}), g;
|
|
168
|
-
function verb(n) {
|
|
169
|
-
return function(v) {
|
|
170
|
-
return step([
|
|
171
|
-
n,
|
|
172
|
-
v
|
|
173
|
-
]);
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
function step(op) {
|
|
177
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
178
|
-
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
179
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
180
|
-
if (y = 0, t) op = [
|
|
181
|
-
op[0] & 2,
|
|
182
|
-
t.value
|
|
183
|
-
];
|
|
184
|
-
switch(op[0]){
|
|
185
|
-
case 0:
|
|
186
|
-
case 1:
|
|
187
|
-
t = op;
|
|
188
|
-
break;
|
|
189
|
-
case 4:
|
|
190
|
-
_.label++;
|
|
191
|
-
return {
|
|
192
|
-
value: op[1],
|
|
193
|
-
done: false
|
|
194
|
-
};
|
|
195
|
-
case 5:
|
|
196
|
-
_.label++;
|
|
197
|
-
y = op[1];
|
|
198
|
-
op = [
|
|
199
|
-
0
|
|
200
|
-
];
|
|
201
|
-
continue;
|
|
202
|
-
case 7:
|
|
203
|
-
op = _.ops.pop();
|
|
204
|
-
_.trys.pop();
|
|
205
|
-
continue;
|
|
206
|
-
default:
|
|
207
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
208
|
-
_ = 0;
|
|
209
|
-
continue;
|
|
210
|
-
}
|
|
211
|
-
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
212
|
-
_.label = op[1];
|
|
213
|
-
break;
|
|
214
|
-
}
|
|
215
|
-
if (op[0] === 6 && _.label < t[1]) {
|
|
216
|
-
_.label = t[1];
|
|
217
|
-
t = op;
|
|
218
|
-
break;
|
|
219
|
-
}
|
|
220
|
-
if (t && _.label < t[2]) {
|
|
221
|
-
_.label = t[2];
|
|
222
|
-
_.ops.push(op);
|
|
223
|
-
break;
|
|
224
|
-
}
|
|
225
|
-
if (t[2]) _.ops.pop();
|
|
226
|
-
_.trys.pop();
|
|
227
|
-
continue;
|
|
228
|
-
}
|
|
229
|
-
op = body.call(thisArg, _);
|
|
230
|
-
} catch (e) {
|
|
231
|
-
op = [
|
|
232
|
-
6,
|
|
233
|
-
e
|
|
234
|
-
];
|
|
235
|
-
y = 0;
|
|
236
|
-
} finally{
|
|
237
|
-
f = t = 0;
|
|
238
|
-
}
|
|
239
|
-
if (op[0] & 5) throw op[1];
|
|
240
|
-
return {
|
|
241
|
-
value: op[0] ? op[1] : void 0,
|
|
242
|
-
done: true
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
79
|
/**
|
|
247
80
|
* A generic implementation of a restful service that assumes all verbs are implemented.
|
|
248
81
|
*
|
|
@@ -252,21 +85,8 @@ function _ts_generator(thisArg, body) {
|
|
|
252
85
|
*
|
|
253
86
|
* @param T -
|
|
254
87
|
* The type of resource being retrieved or mutated.
|
|
255
|
-
*/
|
|
256
|
-
|
|
257
|
-
var _request = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : new webigailHttp.ZHttpRequestBuilder().build();
|
|
258
|
-
_class_call_check(this, ZRestfulService);
|
|
259
|
-
_define_property(this, "_http", void 0);
|
|
260
|
-
_define_property(this, "_endpointUrl", void 0);
|
|
261
|
-
_define_property(this, "_request", void 0);
|
|
262
|
-
this._http = _http;
|
|
263
|
-
this._endpointUrl = _endpointUrl;
|
|
264
|
-
this._request = _request;
|
|
265
|
-
}
|
|
266
|
-
_create_class(ZRestfulService, [
|
|
267
|
-
{
|
|
268
|
-
key: "endpoint",
|
|
269
|
-
value: /**
|
|
88
|
+
*/ class ZRestfulService {
|
|
89
|
+
/**
|
|
270
90
|
* Gets the final endpoint for a resource.
|
|
271
91
|
*
|
|
272
92
|
* @param identification -
|
|
@@ -275,186 +95,71 @@ function _ts_generator(thisArg, body) {
|
|
|
275
95
|
*
|
|
276
96
|
* @returns
|
|
277
97
|
* A url builder that points to the target resource endpoint.
|
|
278
|
-
*/
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
{
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
this._http.request(r)
|
|
344
|
-
];
|
|
345
|
-
case 1:
|
|
346
|
-
data = _state.sent().data;
|
|
347
|
-
return [
|
|
348
|
-
2,
|
|
349
|
-
data
|
|
350
|
-
];
|
|
351
|
-
}
|
|
352
|
-
});
|
|
353
|
-
}).call(this);
|
|
354
|
-
}
|
|
355
|
-
},
|
|
356
|
-
{
|
|
357
|
-
key: "create",
|
|
358
|
-
value: function create(body) {
|
|
359
|
-
return _async_to_generator(function() {
|
|
360
|
-
var url, r, data;
|
|
361
|
-
return _ts_generator(this, function(_state) {
|
|
362
|
-
switch(_state.label){
|
|
363
|
-
case 0:
|
|
364
|
-
url = this.endpoint().build();
|
|
365
|
-
r = new webigailHttp.ZHttpRequestBuilder().copy(this._request).post(body).url(url).build();
|
|
366
|
-
return [
|
|
367
|
-
4,
|
|
368
|
-
this._http.request(r)
|
|
369
|
-
];
|
|
370
|
-
case 1:
|
|
371
|
-
data = _state.sent().data;
|
|
372
|
-
return [
|
|
373
|
-
2,
|
|
374
|
-
data
|
|
375
|
-
];
|
|
376
|
-
}
|
|
377
|
-
});
|
|
378
|
-
}).call(this);
|
|
379
|
-
}
|
|
380
|
-
},
|
|
381
|
-
{
|
|
382
|
-
key: "upsert",
|
|
383
|
-
value: function upsert(body) {
|
|
384
|
-
return _async_to_generator(function() {
|
|
385
|
-
var url, r, data;
|
|
386
|
-
return _ts_generator(this, function(_state) {
|
|
387
|
-
switch(_state.label){
|
|
388
|
-
case 0:
|
|
389
|
-
url = this.endpoint().build();
|
|
390
|
-
r = new webigailHttp.ZHttpRequestBuilder().copy(this._request).put(body).url(url).build();
|
|
391
|
-
return [
|
|
392
|
-
4,
|
|
393
|
-
this._http.request(r)
|
|
394
|
-
];
|
|
395
|
-
case 1:
|
|
396
|
-
data = _state.sent().data;
|
|
397
|
-
return [
|
|
398
|
-
2,
|
|
399
|
-
data
|
|
400
|
-
];
|
|
401
|
-
}
|
|
402
|
-
});
|
|
403
|
-
}).call(this);
|
|
404
|
-
}
|
|
405
|
-
},
|
|
406
|
-
{
|
|
407
|
-
key: "update",
|
|
408
|
-
value: function update(identification, fields) {
|
|
409
|
-
return _async_to_generator(function() {
|
|
410
|
-
var url, r, data;
|
|
411
|
-
return _ts_generator(this, function(_state) {
|
|
412
|
-
switch(_state.label){
|
|
413
|
-
case 0:
|
|
414
|
-
url = this.endpoint(identification).build();
|
|
415
|
-
r = new webigailHttp.ZHttpRequestBuilder().copy(this._request).patch(fields).url(url).build();
|
|
416
|
-
return [
|
|
417
|
-
4,
|
|
418
|
-
this._http.request(r)
|
|
419
|
-
];
|
|
420
|
-
case 1:
|
|
421
|
-
data = _state.sent().data;
|
|
422
|
-
return [
|
|
423
|
-
2,
|
|
424
|
-
data
|
|
425
|
-
];
|
|
426
|
-
}
|
|
427
|
-
});
|
|
428
|
-
}).call(this);
|
|
429
|
-
}
|
|
430
|
-
},
|
|
431
|
-
{
|
|
432
|
-
key: "delete",
|
|
433
|
-
value: function _delete(identification) {
|
|
434
|
-
return _async_to_generator(function() {
|
|
435
|
-
var url, r;
|
|
436
|
-
return _ts_generator(this, function(_state) {
|
|
437
|
-
switch(_state.label){
|
|
438
|
-
case 0:
|
|
439
|
-
url = this.endpoint(identification).build();
|
|
440
|
-
r = new webigailHttp.ZHttpRequestBuilder().copy(this._request).delete().url(url).build();
|
|
441
|
-
return [
|
|
442
|
-
4,
|
|
443
|
-
this._http.request(r)
|
|
444
|
-
];
|
|
445
|
-
case 1:
|
|
446
|
-
_state.sent();
|
|
447
|
-
return [
|
|
448
|
-
2
|
|
449
|
-
];
|
|
450
|
-
}
|
|
451
|
-
});
|
|
452
|
-
}).call(this);
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
]);
|
|
456
|
-
return ZRestfulService;
|
|
457
|
-
}();
|
|
98
|
+
*/ endpoint(identification) {
|
|
99
|
+
const url = new webigailUrl.ZUrlBuilder().parse(this._endpointUrl);
|
|
100
|
+
return identification ? url.append(`${identification}`) : url;
|
|
101
|
+
}
|
|
102
|
+
async count(req) {
|
|
103
|
+
const r = new webigailHttp.ZHttpRequestBuilder().copy(this._request).get().url(new ZRestfulUrlBuilder(this.endpoint().build()).from(req).count().build()).build();
|
|
104
|
+
const { data: page } = await this._http.request(r);
|
|
105
|
+
return page.count;
|
|
106
|
+
}
|
|
107
|
+
async retrieve(req) {
|
|
108
|
+
const r = new webigailHttp.ZHttpRequestBuilder().copy(this._request).get().url(new ZRestfulUrlBuilder(this.endpoint().build()).from(req).build()).build();
|
|
109
|
+
const { data: page } = await this._http.request(r);
|
|
110
|
+
return page.data ?? page.result;
|
|
111
|
+
}
|
|
112
|
+
async get(identification) {
|
|
113
|
+
const url = this.endpoint(identification).build();
|
|
114
|
+
const r = new webigailHttp.ZHttpRequestBuilder().copy(this._request).get().url(url).build();
|
|
115
|
+
const { data } = await this._http.request(r);
|
|
116
|
+
return data;
|
|
117
|
+
}
|
|
118
|
+
async create(body) {
|
|
119
|
+
const url = this.endpoint().build();
|
|
120
|
+
const r = new webigailHttp.ZHttpRequestBuilder().copy(this._request).post(body).url(url).build();
|
|
121
|
+
const { data } = await this._http.request(r);
|
|
122
|
+
return data;
|
|
123
|
+
}
|
|
124
|
+
async upsert(body) {
|
|
125
|
+
const url = this.endpoint().build();
|
|
126
|
+
const r = new webigailHttp.ZHttpRequestBuilder().copy(this._request).put(body).url(url).build();
|
|
127
|
+
const { data } = await this._http.request(r);
|
|
128
|
+
return data;
|
|
129
|
+
}
|
|
130
|
+
async update(identification, fields) {
|
|
131
|
+
const url = this.endpoint(identification).build();
|
|
132
|
+
const r = new webigailHttp.ZHttpRequestBuilder().copy(this._request).patch(fields).url(url).build();
|
|
133
|
+
const { data } = await this._http.request(r);
|
|
134
|
+
return data;
|
|
135
|
+
}
|
|
136
|
+
async delete(identification) {
|
|
137
|
+
const url = this.endpoint(identification).build();
|
|
138
|
+
const r = new webigailHttp.ZHttpRequestBuilder().copy(this._request).delete().url(url).build();
|
|
139
|
+
await this._http.request(r);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Initializes a new instance of this object.
|
|
143
|
+
*
|
|
144
|
+
* @param _http -
|
|
145
|
+
* The http service to use when invoking the RESTful service.
|
|
146
|
+
* @param _endpointUrl -
|
|
147
|
+
* The root url of the endpoint to hit. This can include params
|
|
148
|
+
* to send them with every request, but standard params of page,
|
|
149
|
+
* size, search, filter, and sort will be overridden depending on
|
|
150
|
+
* which method is being invoked.
|
|
151
|
+
* @param _request -
|
|
152
|
+
* The base request to use when constructing requests in the http
|
|
153
|
+
* service.
|
|
154
|
+
*/ constructor(_http, _endpointUrl, _request = new webigailHttp.ZHttpRequestBuilder().build()){
|
|
155
|
+
_define_property(this, "_http", void 0);
|
|
156
|
+
_define_property(this, "_endpointUrl", void 0);
|
|
157
|
+
_define_property(this, "_request", void 0);
|
|
158
|
+
this._http = _http;
|
|
159
|
+
this._endpointUrl = _endpointUrl;
|
|
160
|
+
this._request = _request;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
458
163
|
|
|
459
164
|
exports.ZRestfulService = ZRestfulService;
|
|
460
165
|
exports.ZRestfulUrlBuilder = ZRestfulUrlBuilder;
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/url/restful-url.mts","../src/service/restful-service.mts"],"sourcesContent":["import type { IZDataRequest } from \"@zthun/helpful-query\";\nimport { ZFilterSerialize, ZSortSerialize } from \"@zthun/helpful-query\";\nimport { ZUrlBuilder } from \"@zthun/webigail-url\";\n\n/**\n * Represents a decorator that can build a url for a restful endpoint\n * given an existing base url.\n */\nexport class ZRestfulUrlBuilder {\n private _url: ZUrlBuilder;\n\n /**\n * Initializes a new instance of this object.\n */\n public constructor(baseUrl: string) {\n this._url = new ZUrlBuilder().parse(baseUrl);\n }\n\n /**\n * Adds the request parameters to the url.\n *\n * This will add the page, size, search, filter, and sort parameters to the url if they are defined.\n *\n * @param request -\n * The request to add to the url.\n * @returns\n * This object.\n */\n public from(request: IZDataRequest): this {\n this._url = this._url\n .page(request.page)\n .size(request.size)\n .search(request.search)\n .filter(new ZFilterSerialize().serialize(request.filter))\n .sort(new ZSortSerialize().serialize(request.sort));\n return this;\n }\n\n /**\n * Updates the url to optimize to retrieve a count.\n *\n * This will add the size and page parameters to the url and remove the sort parameter.\n *\n * @returns\n * This object.\n */\n public count(): this {\n this._url = this._url.size(1).page(1).sort(null);\n return this;\n }\n\n /**\n * Returns the built url.\n *\n * @returns\n * The built url.\n */\n public build(): string {\n return this._url.build();\n }\n}\n","import type { IZDataRequest, IZDataSource } from \"@zthun/helpful-query\";\nimport type { IZHttpService } from \"@zthun/webigail-http\";\nimport { ZHttpRequestBuilder } from \"@zthun/webigail-http\";\nimport { ZUrlBuilder } from \"@zthun/webigail-url\";\nimport { ZRestfulUrlBuilder } from \"../url/restful-url.mjs\";\nimport type { IZRestfulCreate } from \"./restful-create.mjs\";\nimport type { IZRestfulDelete } from \"./restful-delete.mjs\";\nimport type { IZRestfulGet } from \"./restful-get.mjs\";\nimport type { IZRestfulUpdate } from \"./restful-update.mjs\";\nimport type { IZRestfulUpsert } from \"./restful-upsert.mjs\";\n\n/**\n * A service that conforms to all known restful standards.\n *\n * @param T -\n * The type of resource being retrieved or mutated.\n */\nexport interface IZRestfulService<T>\n extends IZRestfulCreate<T>,\n IZRestfulDelete,\n IZRestfulGet<T>,\n IZRestfulUpdate<T>,\n IZRestfulUpdate<T>,\n IZRestfulUpsert<T>,\n IZDataSource<T> {}\n\n/**\n * A generic implementation of a restful service that assumes all verbs are implemented.\n *\n * This can be used to invoke a service that is only partially implemented. If your service,\n * for example, is read only, then create, upsert, update, and delete should all return\n * not found or forbidden errors.\n *\n * @param T -\n * The type of resource being retrieved or mutated.\n */\nexport class ZRestfulService<T> implements IZRestfulService<T> {\n /**\n * Initializes a new instance of this object.\n *\n * @param _http -\n * The http service to use when invoking the RESTful service.\n * @param _endpointUrl -\n * The root url of the endpoint to hit. This can include params\n * to send them with every request, but standard params of page,\n * size, search, filter, and sort will be overridden depending on\n * which method is being invoked.\n * @param _request -\n * The base request to use when constructing requests in the http\n * service.\n */\n public constructor(\n private readonly _http: IZHttpService,\n private readonly _endpointUrl: string,\n private readonly _request = new ZHttpRequestBuilder<T>().build(),\n ) {}\n\n /**\n * Gets the final endpoint for a resource.\n *\n * @param identification -\n * The identification of a single resource. If this is falsy, then\n * the entire data scope is used.\n *\n * @returns\n * A url builder that points to the target resource endpoint.\n */\n public endpoint(identification?: number | string) {\n const url = new ZUrlBuilder().parse(this._endpointUrl);\n return identification ? url.append(`${identification}`) : url;\n }\n\n public async count(req: IZDataRequest): Promise<number> {\n const r = new ZHttpRequestBuilder()\n .copy(this._request)\n .get()\n .url(\n new ZRestfulUrlBuilder(this.endpoint().build())\n .from(req)\n .count()\n .build(),\n )\n .build();\n const { data: page } = await this._http.request<any>(r);\n return page.count;\n }\n\n public async retrieve(req: IZDataRequest): Promise<T[]> {\n const r = new ZHttpRequestBuilder()\n .copy(this._request)\n .get()\n .url(new ZRestfulUrlBuilder(this.endpoint().build()).from(req).build())\n .build();\n const { data: page } = await this._http.request<any>(r);\n return page.data ?? page.result;\n }\n\n public async get(identification: number | string): Promise<T> {\n const url = this.endpoint(identification).build();\n const r = new ZHttpRequestBuilder()\n .copy(this._request)\n .get()\n .url(url)\n .build();\n const { data } = await this._http.request<T>(r);\n return data;\n }\n\n public async create(body: T): Promise<T> {\n const url = this.endpoint().build();\n const r = new ZHttpRequestBuilder<T>()\n .copy(this._request)\n .post(body)\n .url(url)\n .build();\n const { data } = await this._http.request<T>(r);\n return data;\n }\n\n public async upsert(body: T): Promise<T> {\n const url = this.endpoint().build();\n const r = new ZHttpRequestBuilder<T>()\n .copy(this._request)\n .put(body)\n .url(url)\n .build();\n const { data } = await this._http.request<T>(r);\n return data;\n }\n\n public async update(\n identification: number | string,\n fields: Partial<T>,\n ): Promise<T> {\n const url = this.endpoint(identification).build();\n const r = new ZHttpRequestBuilder<Partial<T>>()\n .copy(this._request)\n .patch(fields)\n .url(url)\n .build();\n const { data } = await this._http.request<T>(r);\n return data;\n }\n\n public async delete(identification: number | string): Promise<void> {\n const url = this.endpoint(identification).build();\n const r = new ZHttpRequestBuilder<undefined>()\n .copy(this._request)\n .delete()\n .url(url)\n .build();\n await this._http.request(r);\n }\n}\n"],"names":["ZRestfulUrlBuilder","baseUrl","_define_property","_url","ZUrlBuilder","parse","from","request","page","size","search","filter","ZFilterSerialize","serialize","sort","ZSortSerialize","count","build","ZRestfulService","_endpointUrl","_request","ZHttpRequestBuilder","_http","endpoint","identification","url","append","req","r","copy","get","data","retrieve","result","create","body","post","upsert","put","update","fields","patch","delete"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA;;;IAIO,IAAMA,kBAAAA,iBAAN,WAAA;AAAMA,IAAAA,SAAAA,kBAAAA,CAMQC,OAAe,EAAA;AANvBD,QAAAA,mBAAAA,CAAAA,IAAAA,EAAAA,kBAAAA,CAAAA;AACX,QAAAE,kBAAA,CAAA,IAAA,EAAQC,QAAR,MAAA,CAAA;AAME,QAAA,IAAI,CAACA,IAAI,GAAG,IAAIC,uBAAAA,EAAAA,CAAcC,KAAK,CAACJ,OAAAA,CAAAA;;AAP3BD,IAAAA,eAAAA,CAAAA,kBAAAA,EAAAA;;YAoBJM,GAAAA,EAAAA,MAAAA;;;;;;;;;;MAAP,SAAOA,KAAKC,OA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A;AACR,gCAAA,OAAA;;AAAM,oCAAA,IAAI,CAACK,KAAK,CAACf,OAAO,CAACqB,CAAAA;;;AAAzB,gCAAA,MAAA,CAAA,IAAA,EAAA;;;;;;AACF,gBAAA,CAAA,CAAA,CAAA,IAAA,CAAA,IAAA,CAAA;;;;AApHWV,IAAAA,OAAAA,eAAAA;AAqHZ,CAAA;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/url/restful-url.mts","../src/service/restful-service.mts"],"sourcesContent":["import type { IZDataRequest } from \"@zthun/helpful-query\";\nimport { ZFilterSerialize, ZSortSerialize } from \"@zthun/helpful-query\";\nimport { ZUrlBuilder } from \"@zthun/webigail-url\";\n\n/**\n * Represents a decorator that can build a url for a restful endpoint\n * given an existing base url.\n */\nexport class ZRestfulUrlBuilder {\n private _url: ZUrlBuilder;\n\n /**\n * Initializes a new instance of this object.\n */\n public constructor(baseUrl: string) {\n this._url = new ZUrlBuilder().parse(baseUrl);\n }\n\n /**\n * Adds the request parameters to the url.\n *\n * This will add the page, size, search, filter, and sort parameters to the url if they are defined.\n *\n * @param request -\n * The request to add to the url.\n * @returns\n * This object.\n */\n public from(request: IZDataRequest): this {\n this._url = this._url\n .page(request.page)\n .size(request.size)\n .search(request.search)\n .filter(new ZFilterSerialize().serialize(request.filter))\n .sort(new ZSortSerialize().serialize(request.sort));\n return this;\n }\n\n /**\n * Updates the url to optimize to retrieve a count.\n *\n * This will add the size and page parameters to the url and remove the sort parameter.\n *\n * @returns\n * This object.\n */\n public count(): this {\n this._url = this._url.size(1).page(1).sort(null);\n return this;\n }\n\n /**\n * Returns the built url.\n *\n * @returns\n * The built url.\n */\n public build(): string {\n return this._url.build();\n }\n}\n","import type { IZDataRequest, IZDataSource } from \"@zthun/helpful-query\";\nimport type { IZHttpService } from \"@zthun/webigail-http\";\nimport { ZHttpRequestBuilder } from \"@zthun/webigail-http\";\nimport { ZUrlBuilder } from \"@zthun/webigail-url\";\nimport { ZRestfulUrlBuilder } from \"../url/restful-url.mjs\";\nimport type { IZRestfulCreate } from \"./restful-create.mjs\";\nimport type { IZRestfulDelete } from \"./restful-delete.mjs\";\nimport type { IZRestfulGet } from \"./restful-get.mjs\";\nimport type { IZRestfulUpdate } from \"./restful-update.mjs\";\nimport type { IZRestfulUpsert } from \"./restful-upsert.mjs\";\n\n/**\n * A service that conforms to all known restful standards.\n *\n * @param T -\n * The type of resource being retrieved or mutated.\n */\nexport interface IZRestfulService<T>\n extends IZRestfulCreate<T>,\n IZRestfulDelete,\n IZRestfulGet<T>,\n IZRestfulUpdate<T>,\n IZRestfulUpdate<T>,\n IZRestfulUpsert<T>,\n IZDataSource<T> {}\n\n/**\n * A generic implementation of a restful service that assumes all verbs are implemented.\n *\n * This can be used to invoke a service that is only partially implemented. If your service,\n * for example, is read only, then create, upsert, update, and delete should all return\n * not found or forbidden errors.\n *\n * @param T -\n * The type of resource being retrieved or mutated.\n */\nexport class ZRestfulService<T> implements IZRestfulService<T> {\n /**\n * Initializes a new instance of this object.\n *\n * @param _http -\n * The http service to use when invoking the RESTful service.\n * @param _endpointUrl -\n * The root url of the endpoint to hit. This can include params\n * to send them with every request, but standard params of page,\n * size, search, filter, and sort will be overridden depending on\n * which method is being invoked.\n * @param _request -\n * The base request to use when constructing requests in the http\n * service.\n */\n public constructor(\n private readonly _http: IZHttpService,\n private readonly _endpointUrl: string,\n private readonly _request = new ZHttpRequestBuilder<T>().build(),\n ) {}\n\n /**\n * Gets the final endpoint for a resource.\n *\n * @param identification -\n * The identification of a single resource. If this is falsy, then\n * the entire data scope is used.\n *\n * @returns\n * A url builder that points to the target resource endpoint.\n */\n public endpoint(identification?: number | string) {\n const url = new ZUrlBuilder().parse(this._endpointUrl);\n return identification ? url.append(`${identification}`) : url;\n }\n\n public async count(req: IZDataRequest): Promise<number> {\n const r = new ZHttpRequestBuilder()\n .copy(this._request)\n .get()\n .url(\n new ZRestfulUrlBuilder(this.endpoint().build())\n .from(req)\n .count()\n .build(),\n )\n .build();\n const { data: page } = await this._http.request<any>(r);\n return page.count;\n }\n\n public async retrieve(req: IZDataRequest): Promise<T[]> {\n const r = new ZHttpRequestBuilder()\n .copy(this._request)\n .get()\n .url(new ZRestfulUrlBuilder(this.endpoint().build()).from(req).build())\n .build();\n const { data: page } = await this._http.request<any>(r);\n return page.data ?? page.result;\n }\n\n public async get(identification: number | string): Promise<T> {\n const url = this.endpoint(identification).build();\n const r = new ZHttpRequestBuilder()\n .copy(this._request)\n .get()\n .url(url)\n .build();\n const { data } = await this._http.request<T>(r);\n return data;\n }\n\n public async create(body: T): Promise<T> {\n const url = this.endpoint().build();\n const r = new ZHttpRequestBuilder<T>()\n .copy(this._request)\n .post(body)\n .url(url)\n .build();\n const { data } = await this._http.request<T>(r);\n return data;\n }\n\n public async upsert(body: T): Promise<T> {\n const url = this.endpoint().build();\n const r = new ZHttpRequestBuilder<T>()\n .copy(this._request)\n .put(body)\n .url(url)\n .build();\n const { data } = await this._http.request<T>(r);\n return data;\n }\n\n public async update(\n identification: number | string,\n fields: Partial<T>,\n ): Promise<T> {\n const url = this.endpoint(identification).build();\n const r = new ZHttpRequestBuilder<Partial<T>>()\n .copy(this._request)\n .patch(fields)\n .url(url)\n .build();\n const { data } = await this._http.request<T>(r);\n return data;\n }\n\n public async delete(identification: number | string): Promise<void> {\n const url = this.endpoint(identification).build();\n const r = new ZHttpRequestBuilder<undefined>()\n .copy(this._request)\n .delete()\n .url(url)\n .build();\n await this._http.request(r);\n }\n}\n"],"names":["ZRestfulUrlBuilder","from","request","_url","page","size","search","filter","ZFilterSerialize","serialize","sort","ZSortSerialize","count","build","baseUrl","_define_property","ZUrlBuilder","parse","ZRestfulService","endpoint","identification","url","_endpointUrl","append","req","r","ZHttpRequestBuilder","copy","_request","get","data","_http","retrieve","result","create","body","post","upsert","put","update","fields","patch","delete"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAIA;;;AAGC,IACM,MAAMA,kBAAAA,CAAAA;AAUX;;;;;;;;;MAUOC,IAAAA,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IC,gCAAAA,EAAAA,CACXC,IAAI,CAAC,IAAI,CAACC,QAAQ,CAAA,CAClBa,KAAK,CAACD,MAAAA,CAAAA,CACNnB,GAAG,CAACA,KACJR,KAAK,EAAA;QACR,MAAM,EAAEiB,IAAI,EAAE,GAAG,MAAM,IAAI,CAACC,KAAK,CAAC7B,OAAO,CAAIuB,CAAAA,CAAAA;QAC7C,OAAOK,IAAAA;AACT,IAAA;IAEA,MAAaY,MAAAA,CAAOtB,cAA+B,EAAiB;AAClE,QAAA,MAAMC,MAAM,IAAI,CAACF,QAAQ,CAACC,gBAAgBP,KAAK,EAAA;AAC/C,QAAA,MAAMY,CAAAA,GAAI,IAAIC,gCAAAA,EAAAA,CACXC,IAAI,CAAC,IAAI,CAACC,QAAQ,CAAA,CAClBc,MAAM,EAAA,CACNrB,GAAG,CAACA,KACJR,KAAK,EAAA;AACR,QAAA,MAAM,IAAI,CAACkB,KAAK,CAAC7B,OAAO,CAACuB,CAAAA,CAAAA;AAC3B,IAAA;AAnHA;;;;;;;;;;;;;AAaC,MACD,WAAA,CACmBM,KAAoB,EACrC,YAAqC,EACpBH,QAAAA,GAAW,IAAIF,gCAAAA,EAAAA,CAAyBb,KAAK,EAAE,CAChE;;;;aAHiBkB,KAAAA,GAAAA,KAAAA;aACAT,YAAAA,GAAAA,YAAAA;aACAM,QAAAA,GAAAA,QAAAA;AAChB,IAAA;AAkGL;;;;;"}
|
package/dist/index.js
CHANGED
|
@@ -2,24 +2,6 @@ import { ZHttpRequestBuilder } from '@zthun/webigail-http';
|
|
|
2
2
|
import { ZUrlBuilder } from '@zthun/webigail-url';
|
|
3
3
|
import { ZFilterSerialize, ZSortSerialize } from '@zthun/helpful-query';
|
|
4
4
|
|
|
5
|
-
function _class_call_check$1(instance, Constructor) {
|
|
6
|
-
if (!(instance instanceof Constructor)) {
|
|
7
|
-
throw new TypeError("Cannot call a class as a function");
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
function _defineProperties$1(target, props) {
|
|
11
|
-
for(var i = 0; i < props.length; i++){
|
|
12
|
-
var descriptor = props[i];
|
|
13
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
|
14
|
-
descriptor.configurable = true;
|
|
15
|
-
if ("value" in descriptor) descriptor.writable = true;
|
|
16
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
function _create_class$1(Constructor, protoProps, staticProps) {
|
|
20
|
-
if (protoProps) _defineProperties$1(Constructor.prototype, protoProps);
|
|
21
|
-
return Constructor;
|
|
22
|
-
}
|
|
23
5
|
function _define_property$1(obj, key, value) {
|
|
24
6
|
if (key in obj) {
|
|
25
7
|
Object.defineProperty(obj, key, {
|
|
@@ -36,16 +18,8 @@ function _define_property$1(obj, key, value) {
|
|
|
36
18
|
/**
|
|
37
19
|
* Represents a decorator that can build a url for a restful endpoint
|
|
38
20
|
* given an existing base url.
|
|
39
|
-
*/
|
|
40
|
-
|
|
41
|
-
_class_call_check$1(this, ZRestfulUrlBuilder);
|
|
42
|
-
_define_property$1(this, "_url", void 0);
|
|
43
|
-
this._url = new ZUrlBuilder().parse(baseUrl);
|
|
44
|
-
}
|
|
45
|
-
_create_class$1(ZRestfulUrlBuilder, [
|
|
46
|
-
{
|
|
47
|
-
key: "from",
|
|
48
|
-
value: /**
|
|
21
|
+
*/ class ZRestfulUrlBuilder {
|
|
22
|
+
/**
|
|
49
23
|
* Adds the request parameters to the url.
|
|
50
24
|
*
|
|
51
25
|
* This will add the page, size, search, filter, and sort parameters to the url if they are defined.
|
|
@@ -54,87 +28,37 @@ function _define_property$1(obj, key, value) {
|
|
|
54
28
|
* The request to add to the url.
|
|
55
29
|
* @returns
|
|
56
30
|
* This object.
|
|
57
|
-
*/
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
{
|
|
63
|
-
key: "count",
|
|
64
|
-
value: /**
|
|
31
|
+
*/ from(request) {
|
|
32
|
+
this._url = this._url.page(request.page).size(request.size).search(request.search).filter(new ZFilterSerialize().serialize(request.filter)).sort(new ZSortSerialize().serialize(request.sort));
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
65
36
|
* Updates the url to optimize to retrieve a count.
|
|
66
37
|
*
|
|
67
38
|
* This will add the size and page parameters to the url and remove the sort parameter.
|
|
68
39
|
*
|
|
69
40
|
* @returns
|
|
70
41
|
* This object.
|
|
71
|
-
*/
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
{
|
|
77
|
-
key: "build",
|
|
78
|
-
value: /**
|
|
42
|
+
*/ count() {
|
|
43
|
+
this._url = this._url.size(1).page(1).sort(null);
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
79
47
|
* Returns the built url.
|
|
80
48
|
*
|
|
81
49
|
* @returns
|
|
82
50
|
* The built url.
|
|
83
|
-
*/
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
]);
|
|
88
|
-
return ZRestfulUrlBuilder;
|
|
89
|
-
}();
|
|
90
|
-
|
|
91
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
92
|
-
try {
|
|
93
|
-
var info = gen[key](arg);
|
|
94
|
-
var value = info.value;
|
|
95
|
-
} catch (error) {
|
|
96
|
-
reject(error);
|
|
97
|
-
return;
|
|
51
|
+
*/ build() {
|
|
52
|
+
return this._url.build();
|
|
98
53
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
105
|
-
function _async_to_generator(fn) {
|
|
106
|
-
return function() {
|
|
107
|
-
var self = this, args = arguments;
|
|
108
|
-
return new Promise(function(resolve, reject) {
|
|
109
|
-
var gen = fn.apply(self, args);
|
|
110
|
-
function _next(value) {
|
|
111
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
112
|
-
}
|
|
113
|
-
function _throw(err) {
|
|
114
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
115
|
-
}
|
|
116
|
-
_next(undefined);
|
|
117
|
-
});
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
function _class_call_check(instance, Constructor) {
|
|
121
|
-
if (!(instance instanceof Constructor)) {
|
|
122
|
-
throw new TypeError("Cannot call a class as a function");
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
function _defineProperties(target, props) {
|
|
126
|
-
for(var i = 0; i < props.length; i++){
|
|
127
|
-
var descriptor = props[i];
|
|
128
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
|
129
|
-
descriptor.configurable = true;
|
|
130
|
-
if ("value" in descriptor) descriptor.writable = true;
|
|
131
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
|
54
|
+
/**
|
|
55
|
+
* Initializes a new instance of this object.
|
|
56
|
+
*/ constructor(baseUrl){
|
|
57
|
+
_define_property$1(this, "_url", void 0);
|
|
58
|
+
this._url = new ZUrlBuilder().parse(baseUrl);
|
|
132
59
|
}
|
|
133
60
|
}
|
|
134
|
-
|
|
135
|
-
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
136
|
-
return Constructor;
|
|
137
|
-
}
|
|
61
|
+
|
|
138
62
|
function _define_property(obj, key, value) {
|
|
139
63
|
if (key in obj) {
|
|
140
64
|
Object.defineProperty(obj, key, {
|
|
@@ -148,97 +72,6 @@ function _define_property(obj, key, value) {
|
|
|
148
72
|
}
|
|
149
73
|
return obj;
|
|
150
74
|
}
|
|
151
|
-
function _ts_generator(thisArg, body) {
|
|
152
|
-
var f, y, t, _ = {
|
|
153
|
-
label: 0,
|
|
154
|
-
sent: function() {
|
|
155
|
-
if (t[0] & 1) throw t[1];
|
|
156
|
-
return t[1];
|
|
157
|
-
},
|
|
158
|
-
trys: [],
|
|
159
|
-
ops: []
|
|
160
|
-
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
161
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
162
|
-
return this;
|
|
163
|
-
}), g;
|
|
164
|
-
function verb(n) {
|
|
165
|
-
return function(v) {
|
|
166
|
-
return step([
|
|
167
|
-
n,
|
|
168
|
-
v
|
|
169
|
-
]);
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
function step(op) {
|
|
173
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
174
|
-
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
175
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
176
|
-
if (y = 0, t) op = [
|
|
177
|
-
op[0] & 2,
|
|
178
|
-
t.value
|
|
179
|
-
];
|
|
180
|
-
switch(op[0]){
|
|
181
|
-
case 0:
|
|
182
|
-
case 1:
|
|
183
|
-
t = op;
|
|
184
|
-
break;
|
|
185
|
-
case 4:
|
|
186
|
-
_.label++;
|
|
187
|
-
return {
|
|
188
|
-
value: op[1],
|
|
189
|
-
done: false
|
|
190
|
-
};
|
|
191
|
-
case 5:
|
|
192
|
-
_.label++;
|
|
193
|
-
y = op[1];
|
|
194
|
-
op = [
|
|
195
|
-
0
|
|
196
|
-
];
|
|
197
|
-
continue;
|
|
198
|
-
case 7:
|
|
199
|
-
op = _.ops.pop();
|
|
200
|
-
_.trys.pop();
|
|
201
|
-
continue;
|
|
202
|
-
default:
|
|
203
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
204
|
-
_ = 0;
|
|
205
|
-
continue;
|
|
206
|
-
}
|
|
207
|
-
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
208
|
-
_.label = op[1];
|
|
209
|
-
break;
|
|
210
|
-
}
|
|
211
|
-
if (op[0] === 6 && _.label < t[1]) {
|
|
212
|
-
_.label = t[1];
|
|
213
|
-
t = op;
|
|
214
|
-
break;
|
|
215
|
-
}
|
|
216
|
-
if (t && _.label < t[2]) {
|
|
217
|
-
_.label = t[2];
|
|
218
|
-
_.ops.push(op);
|
|
219
|
-
break;
|
|
220
|
-
}
|
|
221
|
-
if (t[2]) _.ops.pop();
|
|
222
|
-
_.trys.pop();
|
|
223
|
-
continue;
|
|
224
|
-
}
|
|
225
|
-
op = body.call(thisArg, _);
|
|
226
|
-
} catch (e) {
|
|
227
|
-
op = [
|
|
228
|
-
6,
|
|
229
|
-
e
|
|
230
|
-
];
|
|
231
|
-
y = 0;
|
|
232
|
-
} finally{
|
|
233
|
-
f = t = 0;
|
|
234
|
-
}
|
|
235
|
-
if (op[0] & 5) throw op[1];
|
|
236
|
-
return {
|
|
237
|
-
value: op[0] ? op[1] : void 0,
|
|
238
|
-
done: true
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
75
|
/**
|
|
243
76
|
* A generic implementation of a restful service that assumes all verbs are implemented.
|
|
244
77
|
*
|
|
@@ -248,21 +81,8 @@ function _ts_generator(thisArg, body) {
|
|
|
248
81
|
*
|
|
249
82
|
* @param T -
|
|
250
83
|
* The type of resource being retrieved or mutated.
|
|
251
|
-
*/
|
|
252
|
-
|
|
253
|
-
var _request = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : new ZHttpRequestBuilder().build();
|
|
254
|
-
_class_call_check(this, ZRestfulService);
|
|
255
|
-
_define_property(this, "_http", void 0);
|
|
256
|
-
_define_property(this, "_endpointUrl", void 0);
|
|
257
|
-
_define_property(this, "_request", void 0);
|
|
258
|
-
this._http = _http;
|
|
259
|
-
this._endpointUrl = _endpointUrl;
|
|
260
|
-
this._request = _request;
|
|
261
|
-
}
|
|
262
|
-
_create_class(ZRestfulService, [
|
|
263
|
-
{
|
|
264
|
-
key: "endpoint",
|
|
265
|
-
value: /**
|
|
84
|
+
*/ class ZRestfulService {
|
|
85
|
+
/**
|
|
266
86
|
* Gets the final endpoint for a resource.
|
|
267
87
|
*
|
|
268
88
|
* @param identification -
|
|
@@ -271,186 +91,71 @@ function _ts_generator(thisArg, body) {
|
|
|
271
91
|
*
|
|
272
92
|
* @returns
|
|
273
93
|
* A url builder that points to the target resource endpoint.
|
|
274
|
-
*/
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
{
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
this._http.request(r)
|
|
340
|
-
];
|
|
341
|
-
case 1:
|
|
342
|
-
data = _state.sent().data;
|
|
343
|
-
return [
|
|
344
|
-
2,
|
|
345
|
-
data
|
|
346
|
-
];
|
|
347
|
-
}
|
|
348
|
-
});
|
|
349
|
-
}).call(this);
|
|
350
|
-
}
|
|
351
|
-
},
|
|
352
|
-
{
|
|
353
|
-
key: "create",
|
|
354
|
-
value: function create(body) {
|
|
355
|
-
return _async_to_generator(function() {
|
|
356
|
-
var url, r, data;
|
|
357
|
-
return _ts_generator(this, function(_state) {
|
|
358
|
-
switch(_state.label){
|
|
359
|
-
case 0:
|
|
360
|
-
url = this.endpoint().build();
|
|
361
|
-
r = new ZHttpRequestBuilder().copy(this._request).post(body).url(url).build();
|
|
362
|
-
return [
|
|
363
|
-
4,
|
|
364
|
-
this._http.request(r)
|
|
365
|
-
];
|
|
366
|
-
case 1:
|
|
367
|
-
data = _state.sent().data;
|
|
368
|
-
return [
|
|
369
|
-
2,
|
|
370
|
-
data
|
|
371
|
-
];
|
|
372
|
-
}
|
|
373
|
-
});
|
|
374
|
-
}).call(this);
|
|
375
|
-
}
|
|
376
|
-
},
|
|
377
|
-
{
|
|
378
|
-
key: "upsert",
|
|
379
|
-
value: function upsert(body) {
|
|
380
|
-
return _async_to_generator(function() {
|
|
381
|
-
var url, r, data;
|
|
382
|
-
return _ts_generator(this, function(_state) {
|
|
383
|
-
switch(_state.label){
|
|
384
|
-
case 0:
|
|
385
|
-
url = this.endpoint().build();
|
|
386
|
-
r = new ZHttpRequestBuilder().copy(this._request).put(body).url(url).build();
|
|
387
|
-
return [
|
|
388
|
-
4,
|
|
389
|
-
this._http.request(r)
|
|
390
|
-
];
|
|
391
|
-
case 1:
|
|
392
|
-
data = _state.sent().data;
|
|
393
|
-
return [
|
|
394
|
-
2,
|
|
395
|
-
data
|
|
396
|
-
];
|
|
397
|
-
}
|
|
398
|
-
});
|
|
399
|
-
}).call(this);
|
|
400
|
-
}
|
|
401
|
-
},
|
|
402
|
-
{
|
|
403
|
-
key: "update",
|
|
404
|
-
value: function update(identification, fields) {
|
|
405
|
-
return _async_to_generator(function() {
|
|
406
|
-
var url, r, data;
|
|
407
|
-
return _ts_generator(this, function(_state) {
|
|
408
|
-
switch(_state.label){
|
|
409
|
-
case 0:
|
|
410
|
-
url = this.endpoint(identification).build();
|
|
411
|
-
r = new ZHttpRequestBuilder().copy(this._request).patch(fields).url(url).build();
|
|
412
|
-
return [
|
|
413
|
-
4,
|
|
414
|
-
this._http.request(r)
|
|
415
|
-
];
|
|
416
|
-
case 1:
|
|
417
|
-
data = _state.sent().data;
|
|
418
|
-
return [
|
|
419
|
-
2,
|
|
420
|
-
data
|
|
421
|
-
];
|
|
422
|
-
}
|
|
423
|
-
});
|
|
424
|
-
}).call(this);
|
|
425
|
-
}
|
|
426
|
-
},
|
|
427
|
-
{
|
|
428
|
-
key: "delete",
|
|
429
|
-
value: function _delete(identification) {
|
|
430
|
-
return _async_to_generator(function() {
|
|
431
|
-
var url, r;
|
|
432
|
-
return _ts_generator(this, function(_state) {
|
|
433
|
-
switch(_state.label){
|
|
434
|
-
case 0:
|
|
435
|
-
url = this.endpoint(identification).build();
|
|
436
|
-
r = new ZHttpRequestBuilder().copy(this._request).delete().url(url).build();
|
|
437
|
-
return [
|
|
438
|
-
4,
|
|
439
|
-
this._http.request(r)
|
|
440
|
-
];
|
|
441
|
-
case 1:
|
|
442
|
-
_state.sent();
|
|
443
|
-
return [
|
|
444
|
-
2
|
|
445
|
-
];
|
|
446
|
-
}
|
|
447
|
-
});
|
|
448
|
-
}).call(this);
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
]);
|
|
452
|
-
return ZRestfulService;
|
|
453
|
-
}();
|
|
94
|
+
*/ endpoint(identification) {
|
|
95
|
+
const url = new ZUrlBuilder().parse(this._endpointUrl);
|
|
96
|
+
return identification ? url.append(`${identification}`) : url;
|
|
97
|
+
}
|
|
98
|
+
async count(req) {
|
|
99
|
+
const r = new ZHttpRequestBuilder().copy(this._request).get().url(new ZRestfulUrlBuilder(this.endpoint().build()).from(req).count().build()).build();
|
|
100
|
+
const { data: page } = await this._http.request(r);
|
|
101
|
+
return page.count;
|
|
102
|
+
}
|
|
103
|
+
async retrieve(req) {
|
|
104
|
+
const r = new ZHttpRequestBuilder().copy(this._request).get().url(new ZRestfulUrlBuilder(this.endpoint().build()).from(req).build()).build();
|
|
105
|
+
const { data: page } = await this._http.request(r);
|
|
106
|
+
return page.data ?? page.result;
|
|
107
|
+
}
|
|
108
|
+
async get(identification) {
|
|
109
|
+
const url = this.endpoint(identification).build();
|
|
110
|
+
const r = new ZHttpRequestBuilder().copy(this._request).get().url(url).build();
|
|
111
|
+
const { data } = await this._http.request(r);
|
|
112
|
+
return data;
|
|
113
|
+
}
|
|
114
|
+
async create(body) {
|
|
115
|
+
const url = this.endpoint().build();
|
|
116
|
+
const r = new ZHttpRequestBuilder().copy(this._request).post(body).url(url).build();
|
|
117
|
+
const { data } = await this._http.request(r);
|
|
118
|
+
return data;
|
|
119
|
+
}
|
|
120
|
+
async upsert(body) {
|
|
121
|
+
const url = this.endpoint().build();
|
|
122
|
+
const r = new ZHttpRequestBuilder().copy(this._request).put(body).url(url).build();
|
|
123
|
+
const { data } = await this._http.request(r);
|
|
124
|
+
return data;
|
|
125
|
+
}
|
|
126
|
+
async update(identification, fields) {
|
|
127
|
+
const url = this.endpoint(identification).build();
|
|
128
|
+
const r = new ZHttpRequestBuilder().copy(this._request).patch(fields).url(url).build();
|
|
129
|
+
const { data } = await this._http.request(r);
|
|
130
|
+
return data;
|
|
131
|
+
}
|
|
132
|
+
async delete(identification) {
|
|
133
|
+
const url = this.endpoint(identification).build();
|
|
134
|
+
const r = new ZHttpRequestBuilder().copy(this._request).delete().url(url).build();
|
|
135
|
+
await this._http.request(r);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Initializes a new instance of this object.
|
|
139
|
+
*
|
|
140
|
+
* @param _http -
|
|
141
|
+
* The http service to use when invoking the RESTful service.
|
|
142
|
+
* @param _endpointUrl -
|
|
143
|
+
* The root url of the endpoint to hit. This can include params
|
|
144
|
+
* to send them with every request, but standard params of page,
|
|
145
|
+
* size, search, filter, and sort will be overridden depending on
|
|
146
|
+
* which method is being invoked.
|
|
147
|
+
* @param _request -
|
|
148
|
+
* The base request to use when constructing requests in the http
|
|
149
|
+
* service.
|
|
150
|
+
*/ constructor(_http, _endpointUrl, _request = new ZHttpRequestBuilder().build()){
|
|
151
|
+
_define_property(this, "_http", void 0);
|
|
152
|
+
_define_property(this, "_endpointUrl", void 0);
|
|
153
|
+
_define_property(this, "_request", void 0);
|
|
154
|
+
this._http = _http;
|
|
155
|
+
this._endpointUrl = _endpointUrl;
|
|
156
|
+
this._request = _request;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
454
159
|
|
|
455
160
|
export { ZRestfulService, ZRestfulUrlBuilder };
|
|
456
161
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/url/restful-url.mts","../src/service/restful-service.mts"],"sourcesContent":["import type { IZDataRequest } from \"@zthun/helpful-query\";\nimport { ZFilterSerialize, ZSortSerialize } from \"@zthun/helpful-query\";\nimport { ZUrlBuilder } from \"@zthun/webigail-url\";\n\n/**\n * Represents a decorator that can build a url for a restful endpoint\n * given an existing base url.\n */\nexport class ZRestfulUrlBuilder {\n private _url: ZUrlBuilder;\n\n /**\n * Initializes a new instance of this object.\n */\n public constructor(baseUrl: string) {\n this._url = new ZUrlBuilder().parse(baseUrl);\n }\n\n /**\n * Adds the request parameters to the url.\n *\n * This will add the page, size, search, filter, and sort parameters to the url if they are defined.\n *\n * @param request -\n * The request to add to the url.\n * @returns\n * This object.\n */\n public from(request: IZDataRequest): this {\n this._url = this._url\n .page(request.page)\n .size(request.size)\n .search(request.search)\n .filter(new ZFilterSerialize().serialize(request.filter))\n .sort(new ZSortSerialize().serialize(request.sort));\n return this;\n }\n\n /**\n * Updates the url to optimize to retrieve a count.\n *\n * This will add the size and page parameters to the url and remove the sort parameter.\n *\n * @returns\n * This object.\n */\n public count(): this {\n this._url = this._url.size(1).page(1).sort(null);\n return this;\n }\n\n /**\n * Returns the built url.\n *\n * @returns\n * The built url.\n */\n public build(): string {\n return this._url.build();\n }\n}\n","import type { IZDataRequest, IZDataSource } from \"@zthun/helpful-query\";\nimport type { IZHttpService } from \"@zthun/webigail-http\";\nimport { ZHttpRequestBuilder } from \"@zthun/webigail-http\";\nimport { ZUrlBuilder } from \"@zthun/webigail-url\";\nimport { ZRestfulUrlBuilder } from \"../url/restful-url.mjs\";\nimport type { IZRestfulCreate } from \"./restful-create.mjs\";\nimport type { IZRestfulDelete } from \"./restful-delete.mjs\";\nimport type { IZRestfulGet } from \"./restful-get.mjs\";\nimport type { IZRestfulUpdate } from \"./restful-update.mjs\";\nimport type { IZRestfulUpsert } from \"./restful-upsert.mjs\";\n\n/**\n * A service that conforms to all known restful standards.\n *\n * @param T -\n * The type of resource being retrieved or mutated.\n */\nexport interface IZRestfulService<T>\n extends IZRestfulCreate<T>,\n IZRestfulDelete,\n IZRestfulGet<T>,\n IZRestfulUpdate<T>,\n IZRestfulUpdate<T>,\n IZRestfulUpsert<T>,\n IZDataSource<T> {}\n\n/**\n * A generic implementation of a restful service that assumes all verbs are implemented.\n *\n * This can be used to invoke a service that is only partially implemented. If your service,\n * for example, is read only, then create, upsert, update, and delete should all return\n * not found or forbidden errors.\n *\n * @param T -\n * The type of resource being retrieved or mutated.\n */\nexport class ZRestfulService<T> implements IZRestfulService<T> {\n /**\n * Initializes a new instance of this object.\n *\n * @param _http -\n * The http service to use when invoking the RESTful service.\n * @param _endpointUrl -\n * The root url of the endpoint to hit. This can include params\n * to send them with every request, but standard params of page,\n * size, search, filter, and sort will be overridden depending on\n * which method is being invoked.\n * @param _request -\n * The base request to use when constructing requests in the http\n * service.\n */\n public constructor(\n private readonly _http: IZHttpService,\n private readonly _endpointUrl: string,\n private readonly _request = new ZHttpRequestBuilder<T>().build(),\n ) {}\n\n /**\n * Gets the final endpoint for a resource.\n *\n * @param identification -\n * The identification of a single resource. If this is falsy, then\n * the entire data scope is used.\n *\n * @returns\n * A url builder that points to the target resource endpoint.\n */\n public endpoint(identification?: number | string) {\n const url = new ZUrlBuilder().parse(this._endpointUrl);\n return identification ? url.append(`${identification}`) : url;\n }\n\n public async count(req: IZDataRequest): Promise<number> {\n const r = new ZHttpRequestBuilder()\n .copy(this._request)\n .get()\n .url(\n new ZRestfulUrlBuilder(this.endpoint().build())\n .from(req)\n .count()\n .build(),\n )\n .build();\n const { data: page } = await this._http.request<any>(r);\n return page.count;\n }\n\n public async retrieve(req: IZDataRequest): Promise<T[]> {\n const r = new ZHttpRequestBuilder()\n .copy(this._request)\n .get()\n .url(new ZRestfulUrlBuilder(this.endpoint().build()).from(req).build())\n .build();\n const { data: page } = await this._http.request<any>(r);\n return page.data ?? page.result;\n }\n\n public async get(identification: number | string): Promise<T> {\n const url = this.endpoint(identification).build();\n const r = new ZHttpRequestBuilder()\n .copy(this._request)\n .get()\n .url(url)\n .build();\n const { data } = await this._http.request<T>(r);\n return data;\n }\n\n public async create(body: T): Promise<T> {\n const url = this.endpoint().build();\n const r = new ZHttpRequestBuilder<T>()\n .copy(this._request)\n .post(body)\n .url(url)\n .build();\n const { data } = await this._http.request<T>(r);\n return data;\n }\n\n public async upsert(body: T): Promise<T> {\n const url = this.endpoint().build();\n const r = new ZHttpRequestBuilder<T>()\n .copy(this._request)\n .put(body)\n .url(url)\n .build();\n const { data } = await this._http.request<T>(r);\n return data;\n }\n\n public async update(\n identification: number | string,\n fields: Partial<T>,\n ): Promise<T> {\n const url = this.endpoint(identification).build();\n const r = new ZHttpRequestBuilder<Partial<T>>()\n .copy(this._request)\n .patch(fields)\n .url(url)\n .build();\n const { data } = await this._http.request<T>(r);\n return data;\n }\n\n public async delete(identification: number | string): Promise<void> {\n const url = this.endpoint(identification).build();\n const r = new ZHttpRequestBuilder<undefined>()\n .copy(this._request)\n .delete()\n .url(url)\n .build();\n await this._http.request(r);\n }\n}\n"],"names":["ZRestfulUrlBuilder","baseUrl","_define_property","_url","ZUrlBuilder","parse","from","request","page","size","search","filter","ZFilterSerialize","serialize","sort","ZSortSerialize","count","build","ZRestfulService","_endpointUrl","_request","ZHttpRequestBuilder","_http","endpoint","identification","url","append","req","r","copy","get","data","retrieve","result","create","body","post","upsert","put","update","fields","patch","delete"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA;;;IAIO,IAAMA,kBAAAA,iBAAN,WAAA;AAAMA,IAAAA,SAAAA,kBAAAA,CAMQC,OAAe,EAAA;AANvBD,QAAAA,mBAAAA,CAAAA,IAAAA,EAAAA,kBAAAA,CAAAA;AACX,QAAAE,kBAAA,CAAA,IAAA,EAAQC,QAAR,MAAA,CAAA;AAME,QAAA,IAAI,CAACA,IAAI,GAAG,IAAIC,WAAAA,EAAAA,CAAcC,KAAK,CAACJ,OAAAA,CAAAA;;AAP3BD,IAAAA,eAAAA,CAAAA,kBAAAA,EAAAA;;YAoBJM,GAAAA,EAAAA,MAAAA;;;;;;;;;;MAAP,SAAOA,KAAKC,OAAsB,EAAA;AAChC,gBAAA,IAAI,CAACJ,IAAI,GAAG,IAAI,CAACA,IAAI,CAClBK,IAAI,CAACD,OAAAA,CAAQC,IAAI,CAAA,CACjBC,IAAI,CAACF,OAAAA,CAAQE,IAAI,EACjBC,MAAM,CAACH,OAAAA,CAAQG,MAAM,EACrBC,MAAM,CAAC,IAAIC,gBAAAA,EAAAA,CAAmBC,SAAS,CAACN,OAAAA,CAAQI,MAAM,CAAA,CAAA,CACtDG,IAAI,CAAC,IAAIC,iBAAiBF,SAAS,CAACN,QAAQO,IAAI,CAAA,CAAA;AACnD,gBAAA,OAAO,IAAI;AACb,YAAA;;;YAUOE,GAAAA,EAAAA,OAAAA;;;;;;;;AADN,MACD,SAAOA,KAAAA,GAAAA;AACL,gBAAA,IAAI,CAACb,IAAI,GAAG,IAAI,CAACA,IAAI,CAACM,IAAI,CAAC,CAAA,CAAA,CAAGD,IAAI,CAAC,CAAA,CAAA,CAAGM,IAAI,CAAC,IAAA,CAAA;AAC3C,gBAAA,OAAO,IAAI;AACb,YAAA;;;YAQOG,GAAAA,EAAAA,OAAAA;;;;;;AADN,MACD,SAAOA,KAAAA,GA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CAAAA;;;AAArCG,gCAAAA,IAAAA,GAAS,MAAA,CAAA,IAAA,EAAA,CAATA,IAAAA;AACR,gCAAA,OAAA;;AAAOA,oCAAAA;;;;AACT,gBAAA,CAAA,CAAA,CAAA,IAAA,CAAA,IAAA,CAAA;;;;YAEaW,GAAAA,EAAAA,QAAAA;AAAb,YAAA,KAAA,EAAA,SAAaA,QAAOlB,cAA+B,EAAA;;wBAC3CC,GAAAA,EACAG,CAAAA;;;;AADAH,gCAAAA,GAAAA,GAAM,IAAI,CAACF,QAAQ,CAACC,gBAAgBP,KAAK,EAAA;AACzCW,gCAAAA,CAAAA,GAAI,IAAIP,mBAAAA,EAAAA,CACXQ,IAAI,CAAC,IAAI,CAACT,QAAQ,CAAA,CAClBsB,MAAM,EAAA,CACNjB,GAAG,CAACA,KACJR,KAAK,EAAA;AACR,gCAAA,OAAA;;AAAM,oCAAA,IAAI,CAACK,KAAK,CAACf,OAAO,CAACqB,CAAAA;;;AAAzB,gCAAA,MAAA,CAAA,IAAA,EAAA;;;;;;AACF,gBAAA,CAAA,CAAA,CAAA,IAAA,CAAA,IAAA,CAAA;;;;AApHWV,IAAAA,OAAAA,eAAAA;AAqHZ,CAAA;;;;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/url/restful-url.mts","../src/service/restful-service.mts"],"sourcesContent":["import type { IZDataRequest } from \"@zthun/helpful-query\";\nimport { ZFilterSerialize, ZSortSerialize } from \"@zthun/helpful-query\";\nimport { ZUrlBuilder } from \"@zthun/webigail-url\";\n\n/**\n * Represents a decorator that can build a url for a restful endpoint\n * given an existing base url.\n */\nexport class ZRestfulUrlBuilder {\n private _url: ZUrlBuilder;\n\n /**\n * Initializes a new instance of this object.\n */\n public constructor(baseUrl: string) {\n this._url = new ZUrlBuilder().parse(baseUrl);\n }\n\n /**\n * Adds the request parameters to the url.\n *\n * This will add the page, size, search, filter, and sort parameters to the url if they are defined.\n *\n * @param request -\n * The request to add to the url.\n * @returns\n * This object.\n */\n public from(request: IZDataRequest): this {\n this._url = this._url\n .page(request.page)\n .size(request.size)\n .search(request.search)\n .filter(new ZFilterSerialize().serialize(request.filter))\n .sort(new ZSortSerialize().serialize(request.sort));\n return this;\n }\n\n /**\n * Updates the url to optimize to retrieve a count.\n *\n * This will add the size and page parameters to the url and remove the sort parameter.\n *\n * @returns\n * This object.\n */\n public count(): this {\n this._url = this._url.size(1).page(1).sort(null);\n return this;\n }\n\n /**\n * Returns the built url.\n *\n * @returns\n * The built url.\n */\n public build(): string {\n return this._url.build();\n }\n}\n","import type { IZDataRequest, IZDataSource } from \"@zthun/helpful-query\";\nimport type { IZHttpService } from \"@zthun/webigail-http\";\nimport { ZHttpRequestBuilder } from \"@zthun/webigail-http\";\nimport { ZUrlBuilder } from \"@zthun/webigail-url\";\nimport { ZRestfulUrlBuilder } from \"../url/restful-url.mjs\";\nimport type { IZRestfulCreate } from \"./restful-create.mjs\";\nimport type { IZRestfulDelete } from \"./restful-delete.mjs\";\nimport type { IZRestfulGet } from \"./restful-get.mjs\";\nimport type { IZRestfulUpdate } from \"./restful-update.mjs\";\nimport type { IZRestfulUpsert } from \"./restful-upsert.mjs\";\n\n/**\n * A service that conforms to all known restful standards.\n *\n * @param T -\n * The type of resource being retrieved or mutated.\n */\nexport interface IZRestfulService<T>\n extends IZRestfulCreate<T>,\n IZRestfulDelete,\n IZRestfulGet<T>,\n IZRestfulUpdate<T>,\n IZRestfulUpdate<T>,\n IZRestfulUpsert<T>,\n IZDataSource<T> {}\n\n/**\n * A generic implementation of a restful service that assumes all verbs are implemented.\n *\n * This can be used to invoke a service that is only partially implemented. If your service,\n * for example, is read only, then create, upsert, update, and delete should all return\n * not found or forbidden errors.\n *\n * @param T -\n * The type of resource being retrieved or mutated.\n */\nexport class ZRestfulService<T> implements IZRestfulService<T> {\n /**\n * Initializes a new instance of this object.\n *\n * @param _http -\n * The http service to use when invoking the RESTful service.\n * @param _endpointUrl -\n * The root url of the endpoint to hit. This can include params\n * to send them with every request, but standard params of page,\n * size, search, filter, and sort will be overridden depending on\n * which method is being invoked.\n * @param _request -\n * The base request to use when constructing requests in the http\n * service.\n */\n public constructor(\n private readonly _http: IZHttpService,\n private readonly _endpointUrl: string,\n private readonly _request = new ZHttpRequestBuilder<T>().build(),\n ) {}\n\n /**\n * Gets the final endpoint for a resource.\n *\n * @param identification -\n * The identification of a single resource. If this is falsy, then\n * the entire data scope is used.\n *\n * @returns\n * A url builder that points to the target resource endpoint.\n */\n public endpoint(identification?: number | string) {\n const url = new ZUrlBuilder().parse(this._endpointUrl);\n return identification ? url.append(`${identification}`) : url;\n }\n\n public async count(req: IZDataRequest): Promise<number> {\n const r = new ZHttpRequestBuilder()\n .copy(this._request)\n .get()\n .url(\n new ZRestfulUrlBuilder(this.endpoint().build())\n .from(req)\n .count()\n .build(),\n )\n .build();\n const { data: page } = await this._http.request<any>(r);\n return page.count;\n }\n\n public async retrieve(req: IZDataRequest): Promise<T[]> {\n const r = new ZHttpRequestBuilder()\n .copy(this._request)\n .get()\n .url(new ZRestfulUrlBuilder(this.endpoint().build()).from(req).build())\n .build();\n const { data: page } = await this._http.request<any>(r);\n return page.data ?? page.result;\n }\n\n public async get(identification: number | string): Promise<T> {\n const url = this.endpoint(identification).build();\n const r = new ZHttpRequestBuilder()\n .copy(this._request)\n .get()\n .url(url)\n .build();\n const { data } = await this._http.request<T>(r);\n return data;\n }\n\n public async create(body: T): Promise<T> {\n const url = this.endpoint().build();\n const r = new ZHttpRequestBuilder<T>()\n .copy(this._request)\n .post(body)\n .url(url)\n .build();\n const { data } = await this._http.request<T>(r);\n return data;\n }\n\n public async upsert(body: T): Promise<T> {\n const url = this.endpoint().build();\n const r = new ZHttpRequestBuilder<T>()\n .copy(this._request)\n .put(body)\n .url(url)\n .build();\n const { data } = await this._http.request<T>(r);\n return data;\n }\n\n public async update(\n identification: number | string,\n fields: Partial<T>,\n ): Promise<T> {\n const url = this.endpoint(identification).build();\n const r = new ZHttpRequestBuilder<Partial<T>>()\n .copy(this._request)\n .patch(fields)\n .url(url)\n .build();\n const { data } = await this._http.request<T>(r);\n return data;\n }\n\n public async delete(identification: number | string): Promise<void> {\n const url = this.endpoint(identification).build();\n const r = new ZHttpRequestBuilder<undefined>()\n .copy(this._request)\n .delete()\n .url(url)\n .build();\n await this._http.request(r);\n }\n}\n"],"names":["ZRestfulUrlBuilder","from","request","_url","page","size","search","filter","ZFilterSerialize","serialize","sort","ZSortSerialize","count","build","baseUrl","_define_property","ZUrlBuilder","parse","ZRestfulService","endpoint","identification","url","_endpointUrl","append","req","r","ZHttpRequestBuilder","copy","_request","get","data","_http","retrieve","result","create","body","post","upsert","put","update","fields","patch","delete"],"mappings":";;;;;;;;;;;;;;;;;AAIA;;;AAGC,IACM,MAAMA,kBAAAA,CAAAA;AAUX;;;;;;;;;MAUOC,IAAAA,CAAKC,OAAsB,EAAQ;AACxC,QAAA,IAAI,CAACC,IAAI,GAAG,IAAI,CAACA,IAAI,CAClBC,IAAI,CAACF,OAAAA,CAAQE,IAAI,CAAA,CACjBC,IAAI,CAACH,OAAAA,CAAQG,IAAI,EACjBC,MAAM,CAACJ,OAAAA,CAAQI,MAAM,EACrBC,MAAM,CAAC,IAAIC,gBAAAA,EAAAA,CAAmBC,SAAS,CAACP,OAAAA,CAAQK,MAAM,CAAA,CAAA,CACtDG,IAAI,CAAC,IAAIC,iBAAiBF,SAAS,CAACP,QAAQQ,IAAI,CAAA,CAAA;AACnD,QAAA,OAAO,IAAI;AACb,IAAA;AAEA;;;;;;;AAOC,MACD,KAAOE,GAAc;AACnB,QAAA,IAAI,CAACT,IAAI,GAAG,IAAI,CAACA,IAAI,CAACE,IAAI,CAAC,CAAA,CAAA,CAAGD,IAAI,CAAC,CAAA,CAAA,CAAGM,IAAI,CAAC,IAAA,CAAA;AAC3C,QAAA,OAAO,IAAI;AACb,IAAA;AAEA;;;;;AAKC,MACD,KAAOG,GAAgB;AACrB,QAAA,OAAO,IAAI,CAACV,IAAI,CAACU,KAAK,EAAA;AACxB,IA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AA,CAAyBb,KAAK,EAAE,CAChE;;;;aAHiBkB,KAAAA,GAAAA,KAAAA;aACAT,YAAAA,GAAAA,YAAAA;aACAM,QAAAA,GAAAA,QAAAA;AAChB,IAAA;AAkGL;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zthun/webigail-rest",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.1",
|
|
4
4
|
"description": "Helpful services for querying rest services based on specific standards.",
|
|
5
5
|
"author": "Anthony Bonta",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,19 +25,19 @@
|
|
|
25
25
|
"access": "public"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@zthun/helpful-fn": "^9.
|
|
29
|
-
"@zthun/helpful-query": "^9.
|
|
30
|
-
"@zthun/webigail-http": "^
|
|
31
|
-
"@zthun/webigail-url": "^
|
|
28
|
+
"@zthun/helpful-fn": "^9.10.1",
|
|
29
|
+
"@zthun/helpful-query": "^9.10.1",
|
|
30
|
+
"@zthun/webigail-http": "^5.0.1",
|
|
31
|
+
"@zthun/webigail-url": "^5.0.1"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@zthun/janitor-build-config": "^19.
|
|
35
|
-
"vite": "^7.1.
|
|
36
|
-
"vitest": "^
|
|
34
|
+
"@zthun/janitor-build-config": "^19.4.2",
|
|
35
|
+
"vite": "^7.1.12",
|
|
36
|
+
"vitest": "^4.0.5"
|
|
37
37
|
},
|
|
38
38
|
"files": [
|
|
39
39
|
"dist"
|
|
40
40
|
],
|
|
41
41
|
"sideEffects": false,
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "bbaa81daa0d781b809ea5b4eb4395c7bac635436"
|
|
43
43
|
}
|