@zthun/webigail-rest 2.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Anthony Bonta
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # Webigail REST
2
+
3
+ Contains services and classes that help with querying REST services based on specific standards.
4
+
5
+ ## Build Status
6
+
7
+ [![CircleCI](https://dl.circleci.com/status-badge/img/gh/zthun/webigail/tree/latest.svg?style=shield)](https://dl.circleci.com/status-badge/redirect/gh/zthun/webigail/tree/latest)
8
+
9
+ ## Usage
10
+
11
+ Webigail is built in TypeScript and it exports both ESM and CJS modules.
12
+
13
+ ```sh
14
+ # NPM
15
+ npm install @zthun/webigail-rest
16
+ # Yarn
17
+ yarn add @zthun/webigail-rest
18
+ ```
19
+
20
+ ```ts
21
+ import { ZDataRequestBuilder } from '@zthun/helpful-query';
22
+ import { ZHttpService } from '@zthun/webigail-http';
23
+ import { ZRestfulService } from '@zthun/webigail-rest';
24
+
25
+ const endpoint = 'https://pokeapi.co/api/v2/pokemon-species';
26
+ const service = new ZRestfulService(new ZHttpService(), endpoint);
27
+
28
+ // Note that the following 2 requests have the expectation that the result for the
29
+ // API is in the form of { count, data } or { count, result };
30
+ // Counts the resources that match the request - uses GET verb
31
+ service.count(new ZDataRequestBuilder().search('bulbasaur').build()).then((count) => console.log(count));
32
+ // Get a page of resources - uses GET verb
33
+ service.request(new ZDataRequestBuilder().page(1).size(50).build()).then((page) => console.log(page));
34
+
35
+ // Get a single resource - uses GET verb
36
+ service.get('ditto').then((ditto) => console.log(ditto));
37
+
38
+ // Note: The pokemon API is read only, but these would still invoke the endpoint with the given verbs
39
+ // The would just fail with 404s, but these are here to illustrate the usage of a full rest service.
40
+ // There is also an assumption that the return values on the services return the data that was mutated.
41
+ // Delete is assumed to return a 204 - No Content if successful.
42
+
43
+ // Create a new resource - uses POST verb
44
+ service.create({ name: 'not-really-a-pokemon' }).then((created) => console.log(created));
45
+ // Upsert a resource - Creates if not exists, updates if exists - uses PUT verb
46
+ service.upsert({ id: 'missing-no', name: 'MissingNo' }).then((upserted) => console.log(upserted));
47
+ // Updates a resource - uses PATCH verb
48
+ service.update('pikachu', { name: 'Pika-who?' }).then((updated) => console.log(updated));
49
+ // Deletes a resource - uses DELETE verb
50
+ service.delete('pikachu').then(() => console.log('Pikachu has fainted from existence'));
51
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const webigailHttp = require("@zthun/webigail-http");
4
+ const webigailUrl = require("@zthun/webigail-url");
5
+ class ZRestfulService {
6
+ /**
7
+ * Initializes a new instance of this object.
8
+ *
9
+ * @param _http -
10
+ * The http service to use when invoking the RESTful service.
11
+ * @param _endpointUrl -
12
+ * The root url of the endpoint to hit. This can include params
13
+ * to send them with every request, but standard params of page,
14
+ * size, search, filter, and sort will be overridden depending on
15
+ * which method is being invoked.
16
+ * @param _request -
17
+ * The base request to use when constructing requests in the http
18
+ * service.
19
+ */
20
+ constructor(_http, _endpointUrl, _request = new webigailHttp.ZHttpRequestBuilder().build()) {
21
+ this._http = _http;
22
+ this._endpointUrl = _endpointUrl;
23
+ this._request = _request;
24
+ }
25
+ /**
26
+ * Gets the final endpoint for a resource.
27
+ *
28
+ * @param identification -
29
+ * The identification of a single resource. If this is falsy, then
30
+ * the entire data scope is used.
31
+ *
32
+ * @returns
33
+ * A url builder that points to the target resource endpoint.
34
+ */
35
+ endpoint(identification) {
36
+ const url = new webigailUrl.ZUrlBuilder().parse(this._endpointUrl);
37
+ return identification ? url.append(`${identification}`) : url;
38
+ }
39
+ async count(req) {
40
+ const url = this.endpoint().page(1).size(1).search(req.search).build();
41
+ const r = new webigailHttp.ZHttpRequestBuilder().copy(this._request).get().url(url).build();
42
+ const { data: page } = await this._http.request(r);
43
+ return page.count;
44
+ }
45
+ async retrieve(req) {
46
+ const url = this.endpoint().page(req.page).size(req.size).search(req.search).build();
47
+ const r = new webigailHttp.ZHttpRequestBuilder().copy(this._request).get().url(url).build();
48
+ const { data: page } = await this._http.request(r);
49
+ return page.data ?? page.result;
50
+ }
51
+ async get(identification) {
52
+ const url = this.endpoint(identification).build();
53
+ const r = new webigailHttp.ZHttpRequestBuilder().copy(this._request).get().url(url).build();
54
+ const { data } = await this._http.request(r);
55
+ return data;
56
+ }
57
+ async create(body) {
58
+ const url = this.endpoint().build();
59
+ const r = new webigailHttp.ZHttpRequestBuilder().copy(this._request).post(body).url(url).build();
60
+ const { data } = await this._http.request(r);
61
+ return data;
62
+ }
63
+ async upsert(body) {
64
+ const url = this.endpoint().build();
65
+ const r = new webigailHttp.ZHttpRequestBuilder().copy(this._request).put(body).url(url).build();
66
+ const { data } = await this._http.request(r);
67
+ return data;
68
+ }
69
+ async update(identification, fields) {
70
+ const url = this.endpoint(identification).build();
71
+ const r = new webigailHttp.ZHttpRequestBuilder().copy(this._request).patch(fields).url(url).build();
72
+ const { data } = await this._http.request(r);
73
+ return data;
74
+ }
75
+ async delete(identification) {
76
+ const url = this.endpoint(identification).build();
77
+ const r = new webigailHttp.ZHttpRequestBuilder().copy(this._request).delete().url(url).build();
78
+ await this._http.request(r);
79
+ }
80
+ }
81
+ exports.ZRestfulService = ZRestfulService;
82
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../src/service/restful-service.mts"],"sourcesContent":["import { IZDataRequest, IZDataSource } from '@zthun/helpful-query';\nimport { IZHttpService, ZHttpRequestBuilder } from '@zthun/webigail-http';\nimport { ZUrlBuilder } from '@zthun/webigail-url';\n\n/**\n * A service that conforms to restful standards.\n */\nexport interface IZRestfulService<T> extends IZDataSource<T> {\n /**\n * Retrieves a single item by it's identification.\n *\n * This uses a GET verb.\n *\n * @param identification -\n * The identification of the resource to retrieve.\n *\n * @returns\n * The json representation of the entity.\n */\n get(identification: number | string): Promise<T>;\n\n /**\n * Creates a new entity.\n *\n * This uses a POST verb.\n *\n * @param body -\n * The post body that represents the resource to create.\n *\n * @returns\n * The resource that was created.\n */\n create(body: T): Promise<T>;\n\n /**\n * Creates a new entity or updates an existing entity.\n *\n * This is determined by the body parameters on whether\n * or not an entity already exists.\n *\n * This uses the PUT verb.\n *\n * @param body -\n * The post body that represents the resource to create\n * or update.\n *\n * @returns\n * The resource that was created or updated.\n */\n upsert(body: T): Promise<T>;\n\n /**\n * Partially updates an existing resource entity.\n *\n * This uses the PATCH verb.\n *\n * @param identification -\n * The identification of the resource to update.\n * @param fields -\n * The partial fields to update.\n *\n * @returns\n * The resource that was updated.\n */\n update(identification: string, fields: Partial<T>): Promise<T>;\n\n /**\n * Deletes a resource entity.\n *\n * This uses the DELETE verb.\n *\n * @param identification -\n * The identification of the resource to delete.\n */\n delete(identification: string): Promise<void>;\n}\n\n/**\n * A factory for constructing basic http request builders.\n *\n * This is used to create a common construction for http requests\n * that contain common options like headers and timeouts.\n */\nexport type ZHttpRequestFactory = () => ZHttpRequestBuilder;\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 */\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 url = this.endpoint().page(1).size(1).search(req.search).build();\n const r = new ZHttpRequestBuilder().copy(this._request).get().url(url).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 url = this.endpoint().page(req.page).size(req.size).search(req.search).build();\n const r = new ZHttpRequestBuilder().copy(this._request).get().url(url).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().copy(this._request).get().url(url).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>().copy(this._request).post(body).url(url).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>().copy(this._request).put(body).url(url).build();\n const { data } = await this._http.request<T>(r);\n return data;\n }\n\n public async update(identification: number | string, fields: Partial<T>): Promise<T> {\n const url = this.endpoint(identification).build();\n const r = new ZHttpRequestBuilder<Partial<T>>().copy(this._request).patch(fields).url(url).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>().copy(this._request).delete().url(url).build();\n await this._http.request(r);\n }\n}\n"],"names":["ZHttpRequestBuilder","ZUrlBuilder"],"mappings":";;;;AA4FO,MAAM,gBAAkD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAetD,YACY,OACA,cACA,WAAW,IAAIA,aAAuB,oBAAA,EAAE,SACzD;AAHiB,SAAA,QAAA;AACA,SAAA,eAAA;AACA,SAAA,WAAA;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYI,SAAS,gBAAkC;AAChD,UAAM,MAAM,IAAIC,YAAA,YAAA,EAAc,MAAM,KAAK,YAAY;AACrD,WAAO,iBAAiB,IAAI,OAAO,GAAG,cAAc,EAAE,IAAI;AAAA,EAC5D;AAAA,EAEA,MAAa,MAAM,KAAqC;AACtD,UAAM,MAAM,KAAK,SAAS,EAAE,KAAK,CAAC,EAAE,KAAK,CAAC,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM;AACrE,UAAM,IAAI,IAAID,aAAAA,sBAAsB,KAAK,KAAK,QAAQ,EAAE,IAAI,EAAE,IAAI,GAAG,EAAE,MAAM;AACvE,UAAA,EAAE,MAAM,SAAS,MAAM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AAO,gBAAiC,QAAgC;AACnF,UAAM,MAAM,KAAK,SAAS,cAAc,EAAE,MAAM;AAChD,UAAM,IAAI,IAAIA,aAAAA,oBAAgC,EAAE,KAAK,KAAK,QAAQ,EAAE,MAAM,MAAM,EAAE,IAAI,GAAG,EAAE,MAAM;AACjG,UAAM,EAAE,KAAK,IAAI,MAAM,KAAK,MAAM,QAAW,CAAC;AACvC,WAAA;AAAA,EACT;AAAA,EAEA,MAAa,OAAO,gBAAgD;AAClE,UAAM,MAAM,KAAK,SAAS,cAAc,EAAE,MAAM;AAChD,UAAM,IAAI,IAAIA,aAAAA,sBAAiC,KAAK,KAAK,QAAQ,EAAE,OAAO,EAAE,IAAI,GAAG,EAAE,MAAM;AACrF,UAAA,KAAK,MAAM,QAAQ,CAAC;AAAA,EAC5B;AACF;;"}
@@ -0,0 +1 @@
1
+ export * from './service/restful-service.mjs';
package/dist/index.js ADDED
@@ -0,0 +1,82 @@
1
+ import { ZHttpRequestBuilder } from "@zthun/webigail-http";
2
+ import { ZUrlBuilder } from "@zthun/webigail-url";
3
+ class ZRestfulService {
4
+ /**
5
+ * Initializes a new instance of this object.
6
+ *
7
+ * @param _http -
8
+ * The http service to use when invoking the RESTful service.
9
+ * @param _endpointUrl -
10
+ * The root url of the endpoint to hit. This can include params
11
+ * to send them with every request, but standard params of page,
12
+ * size, search, filter, and sort will be overridden depending on
13
+ * which method is being invoked.
14
+ * @param _request -
15
+ * The base request to use when constructing requests in the http
16
+ * service.
17
+ */
18
+ constructor(_http, _endpointUrl, _request = new ZHttpRequestBuilder().build()) {
19
+ this._http = _http;
20
+ this._endpointUrl = _endpointUrl;
21
+ this._request = _request;
22
+ }
23
+ /**
24
+ * Gets the final endpoint for a resource.
25
+ *
26
+ * @param identification -
27
+ * The identification of a single resource. If this is falsy, then
28
+ * the entire data scope is used.
29
+ *
30
+ * @returns
31
+ * A url builder that points to the target resource endpoint.
32
+ */
33
+ endpoint(identification) {
34
+ const url = new ZUrlBuilder().parse(this._endpointUrl);
35
+ return identification ? url.append(`${identification}`) : url;
36
+ }
37
+ async count(req) {
38
+ const url = this.endpoint().page(1).size(1).search(req.search).build();
39
+ const r = new ZHttpRequestBuilder().copy(this._request).get().url(url).build();
40
+ const { data: page } = await this._http.request(r);
41
+ return page.count;
42
+ }
43
+ async retrieve(req) {
44
+ const url = this.endpoint().page(req.page).size(req.size).search(req.search).build();
45
+ const r = new ZHttpRequestBuilder().copy(this._request).get().url(url).build();
46
+ const { data: page } = await this._http.request(r);
47
+ return page.data ?? page.result;
48
+ }
49
+ async get(identification) {
50
+ const url = this.endpoint(identification).build();
51
+ const r = new ZHttpRequestBuilder().copy(this._request).get().url(url).build();
52
+ const { data } = await this._http.request(r);
53
+ return data;
54
+ }
55
+ async create(body) {
56
+ const url = this.endpoint().build();
57
+ const r = new ZHttpRequestBuilder().copy(this._request).post(body).url(url).build();
58
+ const { data } = await this._http.request(r);
59
+ return data;
60
+ }
61
+ async upsert(body) {
62
+ const url = this.endpoint().build();
63
+ const r = new ZHttpRequestBuilder().copy(this._request).put(body).url(url).build();
64
+ const { data } = await this._http.request(r);
65
+ return data;
66
+ }
67
+ async update(identification, fields) {
68
+ const url = this.endpoint(identification).build();
69
+ const r = new ZHttpRequestBuilder().copy(this._request).patch(fields).url(url).build();
70
+ const { data } = await this._http.request(r);
71
+ return data;
72
+ }
73
+ async delete(identification) {
74
+ const url = this.endpoint(identification).build();
75
+ const r = new ZHttpRequestBuilder().copy(this._request).delete().url(url).build();
76
+ await this._http.request(r);
77
+ }
78
+ }
79
+ export {
80
+ ZRestfulService
81
+ };
82
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/service/restful-service.mts"],"sourcesContent":["import { IZDataRequest, IZDataSource } from '@zthun/helpful-query';\nimport { IZHttpService, ZHttpRequestBuilder } from '@zthun/webigail-http';\nimport { ZUrlBuilder } from '@zthun/webigail-url';\n\n/**\n * A service that conforms to restful standards.\n */\nexport interface IZRestfulService<T> extends IZDataSource<T> {\n /**\n * Retrieves a single item by it's identification.\n *\n * This uses a GET verb.\n *\n * @param identification -\n * The identification of the resource to retrieve.\n *\n * @returns\n * The json representation of the entity.\n */\n get(identification: number | string): Promise<T>;\n\n /**\n * Creates a new entity.\n *\n * This uses a POST verb.\n *\n * @param body -\n * The post body that represents the resource to create.\n *\n * @returns\n * The resource that was created.\n */\n create(body: T): Promise<T>;\n\n /**\n * Creates a new entity or updates an existing entity.\n *\n * This is determined by the body parameters on whether\n * or not an entity already exists.\n *\n * This uses the PUT verb.\n *\n * @param body -\n * The post body that represents the resource to create\n * or update.\n *\n * @returns\n * The resource that was created or updated.\n */\n upsert(body: T): Promise<T>;\n\n /**\n * Partially updates an existing resource entity.\n *\n * This uses the PATCH verb.\n *\n * @param identification -\n * The identification of the resource to update.\n * @param fields -\n * The partial fields to update.\n *\n * @returns\n * The resource that was updated.\n */\n update(identification: string, fields: Partial<T>): Promise<T>;\n\n /**\n * Deletes a resource entity.\n *\n * This uses the DELETE verb.\n *\n * @param identification -\n * The identification of the resource to delete.\n */\n delete(identification: string): Promise<void>;\n}\n\n/**\n * A factory for constructing basic http request builders.\n *\n * This is used to create a common construction for http requests\n * that contain common options like headers and timeouts.\n */\nexport type ZHttpRequestFactory = () => ZHttpRequestBuilder;\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 */\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 url = this.endpoint().page(1).size(1).search(req.search).build();\n const r = new ZHttpRequestBuilder().copy(this._request).get().url(url).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 url = this.endpoint().page(req.page).size(req.size).search(req.search).build();\n const r = new ZHttpRequestBuilder().copy(this._request).get().url(url).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().copy(this._request).get().url(url).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>().copy(this._request).post(body).url(url).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>().copy(this._request).put(body).url(url).build();\n const { data } = await this._http.request<T>(r);\n return data;\n }\n\n public async update(identification: number | string, fields: Partial<T>): Promise<T> {\n const url = this.endpoint(identification).build();\n const r = new ZHttpRequestBuilder<Partial<T>>().copy(this._request).patch(fields).url(url).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>().copy(this._request).delete().url(url).build();\n await this._http.request(r);\n }\n}\n"],"names":[],"mappings":";;AA4FO,MAAM,gBAAkD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;A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,IAAI,MAAM,KAAK,MAAM,QAAW,CAAC;AACvC,WAAA;AAAA,EACT;AAAA,EAEA,MAAa,OAAO,MAAqB;AACvC,UAAM,MAAM,KAAK,SAAS,EAAE,MAAM;AAClC,UAAM,IAAI,IAAI,oBAAuB,EAAE,KAAK,KAAK,QAAQ,EAAE,KAAK,IAAI,EAAE,IAAI,GAAG,EAAE,MAAM;AACrF,UAAM,EAAE,KAAK,IAAI,MAAM,KAAK,MAAM,QAAW,CAAC;AACvC,WAAA;AAAA,EACT;AAAA,EAEA,MAAa,OAAO,MAAqB;AACvC,UAAM,MAAM,KAAK,SAAS,EAAE,MAAM;AAClC,UAAM,IAAI,IAAI,oBAAuB,EAAE,KAAK,KAAK,QAAQ,EAAE,IAAI,IAAI,EAAE,IAAI,GAAG,EAAE,MAAM;AACpF,UAAM,EAAE,KAAK,IAAI,MAAM,KAAK,MAAM,QAAW,CAAC;AACvC,WAAA;AAAA,EACT;AAAA,EAEA,MAAa,OAAO,gBAAiC,QAAgC;AACnF,UAAM,MAAM,KAAK,SAAS,cAAc,EAAE,MAAM;AAChD,UAAM,IAAI,IAAI,oBAAgC,EAAE,KAAK,KAAK,QAAQ,EAAE,MAAM,MAAM,EAAE,IAAI,GAAG,EAAE,MAAM;AACjG,UAAM,EAAE,KAAK,IAAI,MAAM,KAAK,MAAM,QAAW,CAAC;AACvC,WAAA;AAAA,EACT;AAAA,EAEA,MAAa,OAAO,gBAAgD;AAClE,UAAM,MAAM,KAAK,SAAS,cAAc,EAAE,MAAM;AAChD,UAAM,IAAI,IAAI,sBAAiC,KAAK,KAAK,QAAQ,EAAE,OAAO,EAAE,IAAI,GAAG,EAAE,MAAM;AACrF,UAAA,KAAK,MAAM,QAAQ,CAAC;AAAA,EAC5B;AACF;"}
@@ -0,0 +1,25 @@
1
+ import { IZDataRequest, IZDataSource } from '@zthun/helpful-query';
2
+ import { IZHttpService, ZHttpRequestBuilder } from '@zthun/webigail-http';
3
+ import { ZUrlBuilder } from '@zthun/webigail-url';
4
+ export interface IZRestfulService<T> extends IZDataSource<T> {
5
+ get(identification: number | string): Promise<T>;
6
+ create(body: T): Promise<T>;
7
+ upsert(body: T): Promise<T>;
8
+ update(identification: string, fields: Partial<T>): Promise<T>;
9
+ delete(identification: string): Promise<void>;
10
+ }
11
+ export type ZHttpRequestFactory = () => ZHttpRequestBuilder;
12
+ export declare class ZRestfulService<T> implements IZRestfulService<T> {
13
+ private readonly _http;
14
+ private readonly _endpointUrl;
15
+ private readonly _request;
16
+ constructor(_http: IZHttpService, _endpointUrl: string, _request?: import("@zthun/webigail-http").IZHttpRequest<any>);
17
+ endpoint(identification?: number | string): ZUrlBuilder;
18
+ count(req: IZDataRequest): Promise<number>;
19
+ retrieve(req: IZDataRequest): Promise<T[]>;
20
+ get(identification: number | string): Promise<T>;
21
+ create(body: T): Promise<T>;
22
+ upsert(body: T): Promise<T>;
23
+ update(identification: number | string, fields: Partial<T>): Promise<T>;
24
+ delete(identification: number | string): Promise<void>;
25
+ }
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@zthun/webigail-rest",
3
+ "version": "2.1.0",
4
+ "description": "Helpful services for querying rest services based on specific standards.",
5
+ "author": "Anthony Bonta",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/zthun/webigail",
10
+ "directory": "packages/webigail-rest"
11
+ },
12
+ "main": "./dist/index.cjs",
13
+ "module": "./dist/index.js",
14
+ "types": "./dist/index.d.ts",
15
+ "exports": {
16
+ "import": "./dist/index.js",
17
+ "require": "./dist/index.cjs",
18
+ "types": "./dist/index.d.ts"
19
+ },
20
+ "type": "module",
21
+ "scripts": {
22
+ "build": "vite build"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "devDependencies": {
28
+ "vite": "^5.0.12",
29
+ "vitest": "^1.2.2"
30
+ },
31
+ "files": [
32
+ "dist"
33
+ ],
34
+ "dependencies": {
35
+ "@zthun/helpful-fn": "^3.2.0",
36
+ "@zthun/helpful-query": "^3.2.0",
37
+ "@zthun/webigail-http": "^2.1.0",
38
+ "@zthun/webigail-url": "^2.1.0"
39
+ },
40
+ "sideEffects": false,
41
+ "gitHead": "a013c6401a9b2cca4bfc113987c09d3f2f23ed44"
42
+ }