@decaf-ts/for-http 0.2.1 → 0.2.2
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.md +21 -157
- package/dist/for-http.cjs +310 -12
- package/dist/for-http.esm.cjs +310 -12
- package/lib/RestRepository.cjs +30 -1
- package/lib/RestRepository.d.ts +29 -0
- package/lib/RestService.cjs +154 -11
- package/lib/RestService.d.ts +153 -10
- package/lib/adapter.cjs +114 -1
- package/lib/adapter.d.ts +159 -0
- package/lib/axios/axios.cjs +95 -1
- package/lib/axios/axios.d.ts +94 -0
- package/lib/axios/constants.cjs +7 -1
- package/lib/axios/constants.d.ts +6 -0
- package/lib/axios/index.cjs +10 -1
- package/lib/axios/types.cjs +1 -1
- package/lib/axios/types.d.ts +6 -0
- package/lib/esm/RestRepository.d.ts +29 -0
- package/lib/esm/RestRepository.js +30 -1
- package/lib/esm/RestService.d.ts +153 -10
- package/lib/esm/RestService.js +154 -11
- package/lib/esm/adapter.d.ts +159 -0
- package/lib/esm/adapter.js +115 -2
- package/lib/esm/axios/axios.d.ts +94 -0
- package/lib/esm/axios/axios.js +97 -3
- package/lib/esm/axios/constants.d.ts +6 -0
- package/lib/esm/axios/constants.js +7 -1
- package/lib/esm/axios/index.js +14 -5
- package/lib/esm/axios/types.d.ts +6 -0
- package/lib/esm/axios/types.js +1 -1
- package/lib/esm/index.d.ts +14 -1
- package/lib/esm/index.js +19 -6
- package/lib/esm/types.d.ts +15 -0
- package/lib/esm/types.js +1 -1
- package/lib/index.cjs +15 -2
- package/lib/index.d.ts +14 -1
- package/lib/types.cjs +1 -1
- package/lib/types.d.ts +15 -0
- package/package.json +2 -2
package/lib/axios/axios.d.ts
CHANGED
|
@@ -3,12 +3,106 @@ import { Axios, AxiosRequestConfig } from "axios";
|
|
|
3
3
|
import { HttpConfig } from "../types";
|
|
4
4
|
import { AxiosFlags } from "./types";
|
|
5
5
|
import { Context } from "@decaf-ts/db-decorators";
|
|
6
|
+
/**
|
|
7
|
+
* @description Axios implementation of the HTTP adapter
|
|
8
|
+
* @summary Concrete implementation of HttpAdapter using Axios as the HTTP client.
|
|
9
|
+
* This adapter provides CRUD operations for RESTful APIs using Axios for HTTP requests.
|
|
10
|
+
* @template Axios - The Axios client type
|
|
11
|
+
* @template AxiosRequestConfig - The Axios request configuration type
|
|
12
|
+
* @template AxiosFlags - The flags type extending HttpFlags
|
|
13
|
+
* @template Context<AxiosFlags> - The context type for this adapter
|
|
14
|
+
* @param {Axios} native - The Axios instance
|
|
15
|
+
* @param {HttpConfig} config - Configuration for the HTTP adapter
|
|
16
|
+
* @param {string} [alias] - Optional alias for the adapter
|
|
17
|
+
* @class
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* import axios from 'axios';
|
|
21
|
+
* import { AxiosHttpAdapter } from '@decaf-ts/for-http';
|
|
22
|
+
*
|
|
23
|
+
* const config = { protocol: 'https', host: 'api.example.com' };
|
|
24
|
+
* const adapter = new AxiosHttpAdapter(axios.create(), config);
|
|
25
|
+
*
|
|
26
|
+
* // Use the adapter with a repository
|
|
27
|
+
* const userRepo = adapter.getRepository(User);
|
|
28
|
+
* const user = await userRepo.findById('123');
|
|
29
|
+
* ```
|
|
30
|
+
* @mermaid
|
|
31
|
+
* sequenceDiagram
|
|
32
|
+
* participant Client
|
|
33
|
+
* participant AxiosHttpAdapter
|
|
34
|
+
* participant Axios
|
|
35
|
+
* participant API
|
|
36
|
+
*
|
|
37
|
+
* Client->>AxiosHttpAdapter: create(table, id, data)
|
|
38
|
+
* AxiosHttpAdapter->>AxiosHttpAdapter: url(table)
|
|
39
|
+
* AxiosHttpAdapter->>Axios: post(url, data)
|
|
40
|
+
* Axios->>API: HTTP POST Request
|
|
41
|
+
* API-->>Axios: Response
|
|
42
|
+
* Axios-->>AxiosHttpAdapter: Response Data
|
|
43
|
+
* AxiosHttpAdapter-->>Client: Created Resource
|
|
44
|
+
*
|
|
45
|
+
* Client->>AxiosHttpAdapter: read(table, id)
|
|
46
|
+
* AxiosHttpAdapter->>AxiosHttpAdapter: url(table, {id})
|
|
47
|
+
* AxiosHttpAdapter->>Axios: get(url)
|
|
48
|
+
* Axios->>API: HTTP GET Request
|
|
49
|
+
* API-->>Axios: Response
|
|
50
|
+
* Axios-->>AxiosHttpAdapter: Response Data
|
|
51
|
+
* AxiosHttpAdapter-->>Client: Resource Data
|
|
52
|
+
*/
|
|
6
53
|
export declare class AxiosHttpAdapter extends HttpAdapter<Axios, AxiosRequestConfig, AxiosFlags, Context<AxiosFlags>> {
|
|
7
54
|
constructor(native: Axios, config: HttpConfig, alias?: string);
|
|
55
|
+
/**
|
|
56
|
+
* @description Sends an HTTP request using Axios
|
|
57
|
+
* @summary Implementation of the abstract request method from HttpAdapter.
|
|
58
|
+
* This method uses the Axios instance to send HTTP requests with the provided configuration.
|
|
59
|
+
* @template V - The response value type
|
|
60
|
+
* @param {AxiosRequestConfig} details - The Axios request configuration
|
|
61
|
+
* @return {Promise<V>} A promise that resolves with the response data
|
|
62
|
+
*/
|
|
8
63
|
request<V>(details: AxiosRequestConfig): Promise<V>;
|
|
64
|
+
/**
|
|
65
|
+
* @description Creates a new resource via HTTP POST
|
|
66
|
+
* @summary Implementation of the abstract create method from HttpAdapter.
|
|
67
|
+
* This method sends a POST request to the specified endpoint with the model data.
|
|
68
|
+
* @param {string} tableName - The name of the table or endpoint
|
|
69
|
+
* @param {string|number} id - The identifier for the resource (not used in URL for POST)
|
|
70
|
+
* @param {Record<string, any>} model - The data model to create
|
|
71
|
+
* @return {Promise<Record<string, any>>} A promise that resolves with the created resource
|
|
72
|
+
*/
|
|
9
73
|
create(tableName: string, id: string | number, model: Record<string, any>): Promise<Record<string, any>>;
|
|
74
|
+
/**
|
|
75
|
+
* @description Retrieves a resource by ID via HTTP GET
|
|
76
|
+
* @summary Implementation of the abstract read method from HttpAdapter.
|
|
77
|
+
* This method sends a GET request to the specified endpoint with the ID as a query parameter.
|
|
78
|
+
* @param {string} tableName - The name of the table or endpoint
|
|
79
|
+
* @param {string|number|bigint} id - The identifier for the resource to retrieve
|
|
80
|
+
* @return {Promise<Record<string, any>>} A promise that resolves with the retrieved resource
|
|
81
|
+
*/
|
|
10
82
|
read(tableName: string, id: string | number | bigint): Promise<Record<string, any>>;
|
|
83
|
+
/**
|
|
84
|
+
* @description Updates an existing resource via HTTP PUT
|
|
85
|
+
* @summary Implementation of the abstract update method from HttpAdapter.
|
|
86
|
+
* This method sends a PUT request to the specified endpoint with the updated model data.
|
|
87
|
+
* @param {string} tableName - The name of the table or endpoint
|
|
88
|
+
* @param {string|number} id - The identifier for the resource (not used in URL for PUT)
|
|
89
|
+
* @param {Record<string, any>} model - The updated data model
|
|
90
|
+
* @return {Promise<Record<string, any>>} A promise that resolves with the updated resource
|
|
91
|
+
*/
|
|
11
92
|
update(tableName: string, id: string | number, model: Record<string, any>): Promise<Record<string, any>>;
|
|
93
|
+
/**
|
|
94
|
+
* @description Deletes a resource by ID via HTTP DELETE
|
|
95
|
+
* @summary Implementation of the abstract delete method from HttpAdapter.
|
|
96
|
+
* This method sends a DELETE request to the specified endpoint with the ID as a query parameter.
|
|
97
|
+
* @param {string} tableName - The name of the table or endpoint
|
|
98
|
+
* @param {string|number|bigint} id - The identifier for the resource to delete
|
|
99
|
+
* @return {Promise<Record<string, any>>} A promise that resolves with the deletion result
|
|
100
|
+
*/
|
|
12
101
|
delete(tableName: string, id: string | number | bigint): Promise<Record<string, any>>;
|
|
102
|
+
/**
|
|
103
|
+
* @description Static decoration method for the AxiosHttpAdapter class
|
|
104
|
+
* @summary Placeholder method for class decoration functionality.
|
|
105
|
+
* This method is currently empty but can be used for decorator-based configuration.
|
|
106
|
+
*/
|
|
13
107
|
static decoration(): void;
|
|
14
108
|
}
|
package/lib/axios/constants.cjs
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AxiosFlavour = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @description Axios adapter flavor identifier
|
|
6
|
+
* @summary Constant string identifier used to identify the Axios implementation of the HTTP adapter
|
|
7
|
+
* @const {string} AxiosFlavour
|
|
8
|
+
* @memberOf module:for-http.axios
|
|
9
|
+
*/
|
|
4
10
|
exports.AxiosFlavour = "axios";
|
|
5
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
11
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29uc3RhbnRzLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2F4aW9zL2NvbnN0YW50cy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUFBQTs7Ozs7R0FLRztBQUNVLFFBQUEsWUFBWSxHQUFHLE9BQU8sQ0FBQyIsInNvdXJjZXNDb250ZW50IjpbIi8qKlxuICogQGRlc2NyaXB0aW9uIEF4aW9zIGFkYXB0ZXIgZmxhdm9yIGlkZW50aWZpZXJcbiAqIEBzdW1tYXJ5IENvbnN0YW50IHN0cmluZyBpZGVudGlmaWVyIHVzZWQgdG8gaWRlbnRpZnkgdGhlIEF4aW9zIGltcGxlbWVudGF0aW9uIG9mIHRoZSBIVFRQIGFkYXB0ZXJcbiAqIEBjb25zdCB7c3RyaW5nfSBBeGlvc0ZsYXZvdXJcbiAqIEBtZW1iZXJPZiBtb2R1bGU6Zm9yLWh0dHAuYXhpb3NcbiAqL1xuZXhwb3J0IGNvbnN0IEF4aW9zRmxhdm91ciA9IFwiYXhpb3NcIjtcbiJdfQ==
|
package/lib/axios/constants.d.ts
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description Axios adapter flavor identifier
|
|
3
|
+
* @summary Constant string identifier used to identify the Axios implementation of the HTTP adapter
|
|
4
|
+
* @const {string} AxiosFlavour
|
|
5
|
+
* @memberOf module:for-http.axios
|
|
6
|
+
*/
|
|
1
7
|
export declare const AxiosFlavour = "axios";
|
package/lib/axios/index.cjs
CHANGED
|
@@ -15,8 +15,17 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
const axios_1 = require("./axios.cjs");
|
|
18
|
+
/**
|
|
19
|
+
* @description HTTP client module for REST API interactions
|
|
20
|
+
* @summary This module provides classes and utilities for interacting with REST APIs.
|
|
21
|
+
* It exposes repository and service classes for making HTTP requests, along with
|
|
22
|
+
* type definitions and adapters for different HTTP clients. The module includes
|
|
23
|
+
* {@link RestRepository} and {@link RestService} for API interactions.
|
|
24
|
+
* @namespace axios
|
|
25
|
+
* @memberOf module:for-http
|
|
26
|
+
*/
|
|
18
27
|
axios_1.AxiosHttpAdapter.decoration();
|
|
19
28
|
__exportStar(require("./axios.cjs"), exports);
|
|
20
29
|
__exportStar(require("./constants.cjs"), exports);
|
|
21
30
|
__exportStar(require("./types.cjs"), exports);
|
|
22
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
31
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvYXhpb3MvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7OztBQUFBLHVDQUEyQztBQUUzQzs7Ozs7Ozs7R0FRRztBQUVILHdCQUFnQixDQUFDLFVBQVUsRUFBRSxDQUFDO0FBRTlCLDhDQUF3QjtBQUN4QixrREFBNEI7QUFDNUIsOENBQXdCIiwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IHsgQXhpb3NIdHRwQWRhcHRlciB9IGZyb20gXCIuL2F4aW9zXCI7XG5cbi8qKlxuICogQGRlc2NyaXB0aW9uIEhUVFAgY2xpZW50IG1vZHVsZSBmb3IgUkVTVCBBUEkgaW50ZXJhY3Rpb25zXG4gKiBAc3VtbWFyeSBUaGlzIG1vZHVsZSBwcm92aWRlcyBjbGFzc2VzIGFuZCB1dGlsaXRpZXMgZm9yIGludGVyYWN0aW5nIHdpdGggUkVTVCBBUElzLlxuICogSXQgZXhwb3NlcyByZXBvc2l0b3J5IGFuZCBzZXJ2aWNlIGNsYXNzZXMgZm9yIG1ha2luZyBIVFRQIHJlcXVlc3RzLCBhbG9uZyB3aXRoXG4gKiB0eXBlIGRlZmluaXRpb25zIGFuZCBhZGFwdGVycyBmb3IgZGlmZmVyZW50IEhUVFAgY2xpZW50cy4gVGhlIG1vZHVsZSBpbmNsdWRlc1xuICoge0BsaW5rIFJlc3RSZXBvc2l0b3J5fSBhbmQge0BsaW5rIFJlc3RTZXJ2aWNlfSBmb3IgQVBJIGludGVyYWN0aW9ucy5cbiAqIEBuYW1lc3BhY2UgYXhpb3NcbiAqIEBtZW1iZXJPZiBtb2R1bGU6Zm9yLWh0dHBcbiAqL1xuXG5BeGlvc0h0dHBBZGFwdGVyLmRlY29yYXRpb24oKTtcblxuZXhwb3J0ICogZnJvbSBcIi4vYXhpb3NcIjtcbmV4cG9ydCAqIGZyb20gXCIuL2NvbnN0YW50c1wiO1xuZXhwb3J0ICogZnJvbSBcIi4vdHlwZXNcIjtcbiJdfQ==
|
package/lib/axios/types.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
3
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZXMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvYXhpb3MvdHlwZXMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IEh0dHBGbGFncyB9IGZyb20gXCIuLi90eXBlc1wiO1xuXG4vKipcbiAqIEBkZXNjcmlwdGlvbiBBeGlvcy1zcGVjaWZpYyBIVFRQIGZsYWdzIHR5cGVcbiAqIEBzdW1tYXJ5IFR5cGUgYWxpYXMgZm9yIEh0dHBGbGFncyB1c2VkIHNwZWNpZmljYWxseSB3aXRoIHRoZSBBeGlvcyBhZGFwdGVyIGltcGxlbWVudGF0aW9uXG4gKiBAdHlwZWRlZiB7SHR0cEZsYWdzfSBBeGlvc0ZsYWdzXG4gKiBAbWVtYmVyT2YgbW9kdWxlOmZvci1odHRwLmF4aW9zXG4gKi9cbmV4cG9ydCB0eXBlIEF4aW9zRmxhZ3MgPSBIdHRwRmxhZ3M7XG4iXX0=
|
package/lib/axios/types.d.ts
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
1
|
import { HttpFlags } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
* @description Axios-specific HTTP flags type
|
|
4
|
+
* @summary Type alias for HttpFlags used specifically with the Axios adapter implementation
|
|
5
|
+
* @typedef {HttpFlags} AxiosFlags
|
|
6
|
+
* @memberOf module:for-http.axios
|
|
7
|
+
*/
|
|
2
8
|
export type AxiosFlags = HttpFlags;
|
|
@@ -3,6 +3,35 @@ import { Constructor, Model } from "@decaf-ts/decorator-validation";
|
|
|
3
3
|
import { HttpAdapter } from "./adapter";
|
|
4
4
|
import { Context } from "@decaf-ts/db-decorators";
|
|
5
5
|
import { HttpFlags } from "./types";
|
|
6
|
+
/**
|
|
7
|
+
* @description Repository for REST API interactions
|
|
8
|
+
* @summary A specialized repository implementation for interacting with REST APIs.
|
|
9
|
+
* This class extends the core Repository class and works with HTTP adapters to
|
|
10
|
+
* provide CRUD operations for models via REST endpoints.
|
|
11
|
+
* This Is NOT the default repository for the HTTP adapter. That would be {@link RestService}.
|
|
12
|
+
* Use this only in the specific case of needing to run the CURD model logic (decoration) before submitting to the backend
|
|
13
|
+
* @template M - The model type, extending Model
|
|
14
|
+
* @template Q - The query type used by the adapter
|
|
15
|
+
* @template A - The HTTP adapter type, extending HttpAdapter
|
|
16
|
+
* @template F - The HTTP flags type, extending HttpFlags
|
|
17
|
+
* @template C - The context type, extending Context<F>
|
|
18
|
+
* @param {A} adapter - The HTTP adapter instance
|
|
19
|
+
* @param {Constructor<M>} [clazz] - Optional constructor for the model class
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* // Create a repository for User model with Axios adapter
|
|
23
|
+
* const axiosAdapter = new AxiosAdapter({
|
|
24
|
+
* protocol: 'https',
|
|
25
|
+
* host: 'api.example.com'
|
|
26
|
+
* });
|
|
27
|
+
* const userRepository = new RestRepository(axiosAdapter, User);
|
|
28
|
+
*
|
|
29
|
+
* // Use the repository for CRUD operations
|
|
30
|
+
* const user = await userRepository.findById('123');
|
|
31
|
+
* ```
|
|
32
|
+
* @class RestRepository
|
|
33
|
+
* @see {@link RestService}
|
|
34
|
+
*/
|
|
6
35
|
export declare class RestRepository<M extends Model, Q, A extends HttpAdapter<any, Q, F, C>, F extends HttpFlags = HttpFlags, C extends Context<F> = Context<F>> extends Repository<M, Q, A> {
|
|
7
36
|
constructor(adapter: A, clazz?: Constructor<M>);
|
|
8
37
|
}
|
|
@@ -1,7 +1,36 @@
|
|
|
1
1
|
import { Repository } from "@decaf-ts/core";
|
|
2
|
+
/**
|
|
3
|
+
* @description Repository for REST API interactions
|
|
4
|
+
* @summary A specialized repository implementation for interacting with REST APIs.
|
|
5
|
+
* This class extends the core Repository class and works with HTTP adapters to
|
|
6
|
+
* provide CRUD operations for models via REST endpoints.
|
|
7
|
+
* This Is NOT the default repository for the HTTP adapter. That would be {@link RestService}.
|
|
8
|
+
* Use this only in the specific case of needing to run the CURD model logic (decoration) before submitting to the backend
|
|
9
|
+
* @template M - The model type, extending Model
|
|
10
|
+
* @template Q - The query type used by the adapter
|
|
11
|
+
* @template A - The HTTP adapter type, extending HttpAdapter
|
|
12
|
+
* @template F - The HTTP flags type, extending HttpFlags
|
|
13
|
+
* @template C - The context type, extending Context<F>
|
|
14
|
+
* @param {A} adapter - The HTTP adapter instance
|
|
15
|
+
* @param {Constructor<M>} [clazz] - Optional constructor for the model class
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // Create a repository for User model with Axios adapter
|
|
19
|
+
* const axiosAdapter = new AxiosAdapter({
|
|
20
|
+
* protocol: 'https',
|
|
21
|
+
* host: 'api.example.com'
|
|
22
|
+
* });
|
|
23
|
+
* const userRepository = new RestRepository(axiosAdapter, User);
|
|
24
|
+
*
|
|
25
|
+
* // Use the repository for CRUD operations
|
|
26
|
+
* const user = await userRepository.findById('123');
|
|
27
|
+
* ```
|
|
28
|
+
* @class RestRepository
|
|
29
|
+
* @see {@link RestService}
|
|
30
|
+
*/
|
|
2
31
|
export class RestRepository extends Repository {
|
|
3
32
|
constructor(adapter, clazz) {
|
|
4
33
|
super(adapter, clazz);
|
|
5
34
|
}
|
|
6
35
|
}
|
|
7
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
36
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiUmVzdFJlcG9zaXRvcnkuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvUmVzdFJlcG9zaXRvcnkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFVBQVUsRUFBRSxNQUFNLGdCQUFnQixDQUFDO0FBTTVDOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0dBNEJHO0FBQ0gsTUFBTSxPQUFPLGNBTVgsU0FBUSxVQUFtQjtJQUMzQixZQUFZLE9BQVUsRUFBRSxLQUFzQjtRQUM1QyxLQUFLLENBQUMsT0FBTyxFQUFFLEtBQUssQ0FBQyxDQUFDO0lBQ3hCLENBQUM7Q0FDRiIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IFJlcG9zaXRv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fVxuICovXG5leHBvcnQgY2xhc3MgUmVzdFJlcG9zaXRvcnk8XG4gIE0gZXh0ZW5kcyBNb2RlbCxcbiAgUSxcbiAgQSBleHRlbmRzIEh0dHBBZGFwdGVyPGFueSwgUSwgRiwgQz4sXG4gIEYgZXh0ZW5kcyBIdHRwRmxhZ3MgPSBIdHRwRmxhZ3MsXG4gIEMgZXh0ZW5kcyBDb250ZXh0PEY+ID0gQ29udGV4dDxGPixcbj4gZXh0ZW5kcyBSZXBvc2l0b3J5PE0sIFEsIEE+IHtcbiAgY29uc3RydWN0b3IoYWRhcHRlcjogQSwgY2xheno/OiBDb25zdHJ1Y3RvcjxNPikge1xuICAgIHN1cGVyKGFkYXB0ZXIsIGNsYXp6KTtcbiAgfVxufVxuIl19
|
package/lib/esm/RestService.d.ts
CHANGED
|
@@ -3,42 +3,185 @@ import { Constructor, Model } from "@decaf-ts/decorator-validation";
|
|
|
3
3
|
import { Observable, Observer } from "@decaf-ts/core";
|
|
4
4
|
import { HttpAdapter } from "./adapter";
|
|
5
5
|
import { HttpFlags } from "./types";
|
|
6
|
+
/**
|
|
7
|
+
* @description Service class for REST API operations
|
|
8
|
+
* @summary Provides a comprehensive implementation for interacting with REST APIs.
|
|
9
|
+
* This class implements CRUD operations for single and bulk operations, as well as
|
|
10
|
+
* the Observable pattern to notify observers of changes. It works with HTTP adapters
|
|
11
|
+
* to perform the actual API requests and handles model conversion.
|
|
12
|
+
* @template M - The model type, extending Model
|
|
13
|
+
* @template Q - The query type used by the adapter
|
|
14
|
+
* @template A - The HTTP adapter type, extending HttpAdapter
|
|
15
|
+
* @template F - The HTTP flags type, extending HttpFlags
|
|
16
|
+
* @template C - The context type, extending Context<F>
|
|
17
|
+
* @param {A} adapter - The HTTP adapter instance
|
|
18
|
+
* @param {Constructor<M>} [clazz] - Optional constructor for the model class
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* // Create a service for User model with Axios adapter
|
|
22
|
+
* const axiosAdapter = new AxiosAdapter({
|
|
23
|
+
* protocol: 'https',
|
|
24
|
+
* host: 'api.example.com'
|
|
25
|
+
* });
|
|
26
|
+
* const userService = new RestService(axiosAdapter, User);
|
|
27
|
+
*
|
|
28
|
+
* // Create a new user
|
|
29
|
+
* const user = new User({ name: 'John Doe', email: 'john@example.com' });
|
|
30
|
+
* const createdUser = await userService.create(user);
|
|
31
|
+
*
|
|
32
|
+
* // Update a user
|
|
33
|
+
* createdUser.name = 'Jane Doe';
|
|
34
|
+
* const updatedUser = await userService.update(createdUser);
|
|
35
|
+
*
|
|
36
|
+
* // Delete a user
|
|
37
|
+
* await userService.delete(updatedUser.id);
|
|
38
|
+
* ```
|
|
39
|
+
* @class
|
|
40
|
+
*/
|
|
6
41
|
export declare class RestService<M extends Model, Q, A extends HttpAdapter<any, Q, F, C>, F extends HttpFlags = HttpFlags, C extends Context<F> = Context<F>> implements CrudOperator<M>, BulkCrudOperator<M>, Observable {
|
|
7
42
|
private readonly _class;
|
|
8
43
|
private _pk;
|
|
44
|
+
/**
|
|
45
|
+
* @description Gets the model class constructor
|
|
46
|
+
* @summary Retrieves the model class constructor associated with this service.
|
|
47
|
+
* Throws an error if no class definition is found.
|
|
48
|
+
* @return {Constructor<M>} The model class constructor
|
|
49
|
+
* @throws {InternalError} If no class definition is found
|
|
50
|
+
*/
|
|
9
51
|
get class(): Constructor<M>;
|
|
52
|
+
/**
|
|
53
|
+
* @description Gets the primary key property name
|
|
54
|
+
* @summary Retrieves the name of the primary key property for the model.
|
|
55
|
+
* If not already determined, it finds the primary key using the model class.
|
|
56
|
+
* @return The primary key property name
|
|
57
|
+
*/
|
|
10
58
|
get pk(): keyof M;
|
|
11
59
|
protected observers: Observer[];
|
|
12
60
|
private readonly _adapter;
|
|
13
61
|
private _tableName;
|
|
62
|
+
/**
|
|
63
|
+
* @description Gets the HTTP adapter
|
|
64
|
+
* @summary Retrieves the HTTP adapter associated with this service.
|
|
65
|
+
* Throws an error if no adapter is found.
|
|
66
|
+
* @return {A} The HTTP adapter instance
|
|
67
|
+
* @throws {InternalError} If no adapter is found
|
|
68
|
+
*/
|
|
14
69
|
protected get adapter(): A;
|
|
70
|
+
/**
|
|
71
|
+
* @description Gets the table name for the model
|
|
72
|
+
* @summary Retrieves the table name associated with the model class.
|
|
73
|
+
* If not already determined, it gets the table name from the Repository utility.
|
|
74
|
+
* @return {string} The table name
|
|
75
|
+
*/
|
|
15
76
|
protected get tableName(): string;
|
|
77
|
+
/**
|
|
78
|
+
* @description Initializes a new RestService instance
|
|
79
|
+
* @summary Creates a new service instance with the specified adapter and optional model class.
|
|
80
|
+
* The constructor stores the adapter and model class for later use in CRUD operations.
|
|
81
|
+
* @param {A} adapter - The HTTP adapter instance to use for API requests
|
|
82
|
+
* @param {Constructor<M>} [clazz] - Optional constructor for the model class
|
|
83
|
+
*/
|
|
16
84
|
constructor(adapter: A, clazz?: Constructor<M>);
|
|
85
|
+
/**
|
|
86
|
+
* @description Creates a new resource
|
|
87
|
+
* @summary Creates a new resource in the REST API using the provided model.
|
|
88
|
+
* The method prepares the model for the adapter, sends the create request,
|
|
89
|
+
* and then converts the response back to a model instance.
|
|
90
|
+
* @param {M} model - The model instance to create
|
|
91
|
+
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
92
|
+
* @return {Promise<M>} A promise that resolves with the created model instance
|
|
93
|
+
*/
|
|
17
94
|
create(model: M, ...args: any[]): Promise<M>;
|
|
95
|
+
/**
|
|
96
|
+
* @description Retrieves a resource by ID
|
|
97
|
+
* @summary Fetches a resource from the REST API using the provided ID.
|
|
98
|
+
* The method sends the read request and converts the response to a model instance.
|
|
99
|
+
* @param {string|number} id - The identifier of the resource to retrieve
|
|
100
|
+
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
101
|
+
* @return {Promise<M>} A promise that resolves with the retrieved model instance
|
|
102
|
+
*/
|
|
18
103
|
read(id: string | number, ...args: any[]): Promise<M>;
|
|
104
|
+
/**
|
|
105
|
+
* @description Updates an existing resource
|
|
106
|
+
* @summary Updates an existing resource in the REST API using the provided model.
|
|
107
|
+
* The method prepares the model for the adapter, sends the update request,
|
|
108
|
+
* and then converts the response back to a model instance.
|
|
109
|
+
* @param {M} model - The model instance with updated data
|
|
110
|
+
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
111
|
+
* @return {Promise<M>} A promise that resolves with the updated model instance
|
|
112
|
+
*/
|
|
19
113
|
update(model: M, ...args: any[]): Promise<M>;
|
|
114
|
+
/**
|
|
115
|
+
* @description Deletes a resource by ID
|
|
116
|
+
* @summary Removes a resource from the REST API using the provided ID.
|
|
117
|
+
* The method sends the delete request and converts the response to a model instance.
|
|
118
|
+
* @param {string|number} id - The identifier of the resource to delete
|
|
119
|
+
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
120
|
+
* @return {Promise<M>} A promise that resolves with the deleted model instance
|
|
121
|
+
*/
|
|
20
122
|
delete(id: string | number, ...args: any[]): Promise<M>;
|
|
123
|
+
/**
|
|
124
|
+
* @description Creates multiple resources
|
|
125
|
+
* @summary Creates multiple resources in the REST API using the provided models.
|
|
126
|
+
* The method prepares each model for the adapter, sends a bulk create request,
|
|
127
|
+
* and then converts the responses back to model instances.
|
|
128
|
+
* @param {M[]} models - The model instances to create
|
|
129
|
+
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
130
|
+
* @return {Promise<M[]>} A promise that resolves with an array of created model instances
|
|
131
|
+
*/
|
|
21
132
|
createAll(models: M[], ...args: any[]): Promise<M[]>;
|
|
133
|
+
/**
|
|
134
|
+
* @description Deletes multiple resources by IDs
|
|
135
|
+
* @summary Removes multiple resources from the REST API using the provided IDs.
|
|
136
|
+
* The method sends a bulk delete request and converts the responses to model instances.
|
|
137
|
+
* @param {string[]|number[]} keys - The identifiers of the resources to delete
|
|
138
|
+
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
139
|
+
* @return {Promise<M[]>} A promise that resolves with an array of deleted model instances
|
|
140
|
+
*/
|
|
22
141
|
deleteAll(keys: string[] | number[], ...args: any[]): Promise<M[]>;
|
|
142
|
+
/**
|
|
143
|
+
* @description Retrieves multiple resources by IDs
|
|
144
|
+
* @summary Fetches multiple resources from the REST API using the provided IDs.
|
|
145
|
+
* The method sends a bulk read request and converts the responses to model instances.
|
|
146
|
+
* @param {string[]|number[]} keys - The identifiers of the resources to retrieve
|
|
147
|
+
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
148
|
+
* @return {Promise<M[]>} A promise that resolves with an array of retrieved model instances
|
|
149
|
+
*/
|
|
23
150
|
readAll(keys: string[] | number[], ...args: any[]): Promise<M[]>;
|
|
151
|
+
/**
|
|
152
|
+
* @description Updates multiple resources
|
|
153
|
+
* @summary Updates multiple resources in the REST API using the provided models.
|
|
154
|
+
* The method prepares each model for the adapter, sends a bulk update request,
|
|
155
|
+
* and then converts the responses back to model instances.
|
|
156
|
+
* @param {M[]} models - The model instances with updated data
|
|
157
|
+
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
158
|
+
* @return {Promise<M[]>} A promise that resolves with an array of updated model instances
|
|
159
|
+
*/
|
|
24
160
|
updateAll(models: M[], ...args: any[]): Promise<M[]>;
|
|
25
161
|
/**
|
|
26
|
-
* @
|
|
27
|
-
* @
|
|
28
|
-
*
|
|
29
|
-
* @
|
|
162
|
+
* @description Registers an observer
|
|
163
|
+
* @summary Adds an observer to the list of observers that will be notified of changes.
|
|
164
|
+
* Throws an error if the observer is already registered.
|
|
165
|
+
* @param {Observer} observer - The observer to register
|
|
166
|
+
* @return {void}
|
|
167
|
+
* @throws {InternalError} If the observer is already registered
|
|
30
168
|
*/
|
|
31
169
|
observe(observer: Observer): void;
|
|
32
170
|
/**
|
|
33
|
-
* @
|
|
34
|
-
* @
|
|
35
|
-
*
|
|
36
|
-
* @
|
|
171
|
+
* @description Unregisters an observer
|
|
172
|
+
* @summary Removes an observer from the list of observers.
|
|
173
|
+
* Throws an error if the observer is not found.
|
|
174
|
+
* @param {Observer} observer - The observer to unregister
|
|
175
|
+
* @return {void}
|
|
176
|
+
* @throws {InternalError} If the observer is not found
|
|
37
177
|
*/
|
|
38
178
|
unObserve(observer: Observer): void;
|
|
39
179
|
/**
|
|
40
|
-
* @
|
|
41
|
-
* @
|
|
180
|
+
* @description Notifies all registered observers
|
|
181
|
+
* @summary Calls the refresh method on all registered observers to update themselves.
|
|
182
|
+
* Any errors during observer refresh are logged as warnings but don't stop the process.
|
|
183
|
+
* @param {...any[]} [args] - Optional arguments to pass to the observer refresh method
|
|
184
|
+
* @return {Promise<void>} A promise that resolves when all observers have been updated
|
|
42
185
|
*/
|
|
43
186
|
updateObservers(...args: any[]): Promise<void>;
|
|
44
187
|
}
|