@decaf-ts/for-http 0.2.13 → 0.3.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/README.md +1 -1
- package/dist/for-http.cjs +1 -1
- package/dist/for-http.cjs.map +1 -1
- package/dist/for-http.js +1 -1
- package/dist/for-http.js.map +1 -1
- package/lib/RestRepository.d.ts +5 -6
- package/lib/RestRepository.js.map +1 -1
- package/lib/RestService.cjs +9 -229
- package/lib/RestService.d.ts +6 -157
- package/lib/RestService.js.map +1 -1
- package/lib/adapter.cjs +13 -8
- package/lib/adapter.d.ts +21 -22
- package/lib/adapter.js.map +1 -1
- package/lib/axios/axios.cjs +21 -8
- package/lib/axios/axios.d.ts +10 -7
- package/lib/axios/axios.js.map +1 -1
- package/lib/esm/RestRepository.d.ts +5 -6
- package/lib/esm/RestRepository.js.map +1 -1
- package/lib/esm/RestService.d.ts +6 -157
- package/lib/esm/RestService.js +9 -229
- package/lib/esm/RestService.js.map +1 -1
- package/lib/esm/adapter.d.ts +21 -22
- package/lib/esm/adapter.js +14 -9
- package/lib/esm/adapter.js.map +1 -1
- package/lib/esm/axios/axios.d.ts +10 -7
- package/lib/esm/axios/axios.js +21 -8
- package/lib/esm/axios/axios.js.map +1 -1
- package/lib/esm/index.d.ts +1 -1
- package/lib/esm/index.js +1 -1
- package/lib/index.cjs +1 -1
- package/lib/index.d.ts +1 -1
- package/package.json +5 -4
package/lib/esm/RestService.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Constructor
|
|
3
|
-
import {
|
|
1
|
+
import { Model } from "@decaf-ts/decorator-validation";
|
|
2
|
+
import { Constructor } from "@decaf-ts/decoration";
|
|
3
|
+
import { FlagsOf } from "@decaf-ts/core";
|
|
4
4
|
import { HttpAdapter } from "./adapter";
|
|
5
|
-
import {
|
|
6
|
-
import { LoggedClass, Logger } from "@decaf-ts/logging";
|
|
5
|
+
import { RestRepository } from "./RestRepository";
|
|
7
6
|
/**
|
|
8
7
|
* @description Service class for REST API operations
|
|
9
8
|
* @summary Provides a comprehensive implementation for interacting with REST APIs.
|
|
@@ -52,44 +51,8 @@ import { LoggedClass, Logger } from "@decaf-ts/logging";
|
|
|
52
51
|
* Adapter-->>Service: record
|
|
53
52
|
* Service-->>Client: revert(record)
|
|
54
53
|
*/
|
|
55
|
-
export declare class RestService<M extends Model,
|
|
56
|
-
|
|
57
|
-
private _pk;
|
|
58
|
-
private logger?;
|
|
59
|
-
/**
|
|
60
|
-
* @description Gets the model class constructor
|
|
61
|
-
* @summary Retrieves the model class constructor associated with this service.
|
|
62
|
-
* Throws an error if no class definition is found.
|
|
63
|
-
* @return {Constructor<M>} The model class constructor
|
|
64
|
-
* @throws {InternalError} If no class definition is found
|
|
65
|
-
*/
|
|
66
|
-
get class(): Constructor<M>;
|
|
67
|
-
protected get log(): Logger;
|
|
68
|
-
/**
|
|
69
|
-
* @description Gets the primary key property name
|
|
70
|
-
* @summary Retrieves the name of the primary key property for the model.
|
|
71
|
-
* If not already determined, it finds the primary key using the model class.
|
|
72
|
-
* @return The primary key property name
|
|
73
|
-
*/
|
|
74
|
-
get pk(): keyof M;
|
|
75
|
-
protected observers: Observer[];
|
|
76
|
-
private readonly _adapter;
|
|
77
|
-
private _tableName;
|
|
78
|
-
/**
|
|
79
|
-
* @description Gets the HTTP adapter
|
|
80
|
-
* @summary Retrieves the HTTP adapter associated with this service.
|
|
81
|
-
* Throws an error if no adapter is found.
|
|
82
|
-
* @return {A} The HTTP adapter instance
|
|
83
|
-
* @throws {InternalError} If no adapter is found
|
|
84
|
-
*/
|
|
85
|
-
protected get adapter(): A;
|
|
86
|
-
/**
|
|
87
|
-
* @description Gets the table name for the model
|
|
88
|
-
* @summary Retrieves the table name associated with the model class.
|
|
89
|
-
* If not already determined, it gets the table name from the Repository utility.
|
|
90
|
-
* @return {string} The table name
|
|
91
|
-
*/
|
|
92
|
-
protected get tableName(): string;
|
|
54
|
+
export declare class RestService<M extends Model, A extends HttpAdapter<any, any, any, any>, Q = A extends HttpAdapter<any, any, infer Q, any> ? Q : never> extends RestRepository<M, A, Q> {
|
|
55
|
+
protected _overrides: Partial<FlagsOf<A>>;
|
|
93
56
|
/**
|
|
94
57
|
* @description Initializes a new RestService instance
|
|
95
58
|
* @summary Creates a new service instance with the specified adapter and optional model class.
|
|
@@ -98,119 +61,5 @@ export declare class RestService<M extends Model, Q, A extends HttpAdapter<any,
|
|
|
98
61
|
* @param {Constructor<M>} [clazz] - Optional constructor for the model class
|
|
99
62
|
*/
|
|
100
63
|
constructor(adapter: A, clazz?: Constructor<M>);
|
|
101
|
-
url(path: string, queryParams?: Record<string, string | number>): string;
|
|
102
|
-
/**
|
|
103
|
-
* @description Creates a new resource
|
|
104
|
-
* @summary Creates a new resource in the REST API using the provided model.
|
|
105
|
-
* The method prepares the model for the adapter, sends the create request,
|
|
106
|
-
* and then converts the response back to a model instance.
|
|
107
|
-
* @param {M} model - The model instance to create
|
|
108
|
-
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
109
|
-
* @return {Promise<M>} A promise that resolves with the created model instance
|
|
110
|
-
*/
|
|
111
|
-
create(model: M, ...args: any[]): Promise<M>;
|
|
112
|
-
/**
|
|
113
|
-
* @description Retrieves a resource by ID
|
|
114
|
-
* @summary Fetches a resource from the REST API using the provided ID.
|
|
115
|
-
* The method sends the read request and converts the response to a model instance.
|
|
116
|
-
* @param {string|number} id - The identifier of the resource to retrieve
|
|
117
|
-
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
118
|
-
* @return {Promise<M>} A promise that resolves with the retrieved model instance
|
|
119
|
-
*/
|
|
120
|
-
read(id: string | number, ...args: any[]): Promise<M>;
|
|
121
|
-
/**
|
|
122
|
-
* @description Updates an existing resource
|
|
123
|
-
* @summary Updates an existing resource in the REST API using the provided model.
|
|
124
|
-
* The method prepares the model for the adapter, sends the update request,
|
|
125
|
-
* and then converts the response back to a model instance.
|
|
126
|
-
* @param {M} model - The model instance with updated data
|
|
127
|
-
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
128
|
-
* @return {Promise<M>} A promise that resolves with the updated model instance
|
|
129
|
-
*/
|
|
130
|
-
update(model: M, ...args: any[]): Promise<M>;
|
|
131
|
-
/**
|
|
132
|
-
* @description Deletes a resource by ID
|
|
133
|
-
* @summary Removes a resource from the REST API using the provided ID.
|
|
134
|
-
* The method sends the delete request and converts the response to a model instance.
|
|
135
|
-
* @param {string|number} id - The identifier of the resource to delete
|
|
136
|
-
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
137
|
-
* @return {Promise<M>} A promise that resolves with the deleted model instance
|
|
138
|
-
*/
|
|
139
|
-
delete(id: string | number, ...args: any[]): Promise<M>;
|
|
140
|
-
request<V>(details: Q): Promise<V>;
|
|
141
|
-
/**
|
|
142
|
-
* @description Creates multiple resources
|
|
143
|
-
* @summary Creates multiple resources in the REST API using the provided models.
|
|
144
|
-
* The method prepares each model for the adapter, sends a bulk create request,
|
|
145
|
-
* and then converts the responses back to model instances.
|
|
146
|
-
* @param {M[]} models - The model instances to create
|
|
147
|
-
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
148
|
-
* @return {Promise<M[]>} A promise that resolves with an array of created model instances
|
|
149
|
-
* @mermaid
|
|
150
|
-
* sequenceDiagram
|
|
151
|
-
* participant Client
|
|
152
|
-
* participant Service as RestService
|
|
153
|
-
* participant Adapter as HttpAdapter
|
|
154
|
-
* Client->>Service: createAll(models)
|
|
155
|
-
* Service->>Adapter: prepare(model, pk) x N
|
|
156
|
-
* Service->>Adapter: createAll(table, ids[], records[])
|
|
157
|
-
* Adapter-->>Service: records[]
|
|
158
|
-
* Service-->>Client: revert(records[])
|
|
159
|
-
*/
|
|
160
|
-
createAll(models: M[], ...args: any[]): Promise<M[]>;
|
|
161
|
-
/**
|
|
162
|
-
* @description Deletes multiple resources by IDs
|
|
163
|
-
* @summary Removes multiple resources from the REST API using the provided IDs.
|
|
164
|
-
* The method sends a bulk delete request and converts the responses to model instances.
|
|
165
|
-
* @param {string[]|number[]} keys - The identifiers of the resources to delete
|
|
166
|
-
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
167
|
-
* @return {Promise<M[]>} A promise that resolves with an array of deleted model instances
|
|
168
|
-
*/
|
|
169
|
-
deleteAll(keys: string[] | number[], ...args: any[]): Promise<M[]>;
|
|
170
|
-
/**
|
|
171
|
-
* @description Retrieves multiple resources by IDs
|
|
172
|
-
* @summary Fetches multiple resources from the REST API using the provided IDs.
|
|
173
|
-
* The method sends a bulk read request and converts the responses to model instances.
|
|
174
|
-
* @param {string[]|number[]} keys - The identifiers of the resources to retrieve
|
|
175
|
-
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
176
|
-
* @return {Promise<M[]>} A promise that resolves with an array of retrieved model instances
|
|
177
|
-
*/
|
|
178
|
-
readAll(keys: string[] | number[], ...args: any[]): Promise<M[]>;
|
|
179
|
-
/**
|
|
180
|
-
* @description Updates multiple resources
|
|
181
|
-
* @summary Updates multiple resources in the REST API using the provided models.
|
|
182
|
-
* The method prepares each model for the adapter, sends a bulk update request,
|
|
183
|
-
* and then converts the responses back to model instances.
|
|
184
|
-
* @param {M[]} models - The model instances with updated data
|
|
185
|
-
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
186
|
-
* @return {Promise<M[]>} A promise that resolves with an array of updated model instances
|
|
187
|
-
*/
|
|
188
|
-
updateAll(models: M[], ...args: any[]): Promise<M[]>;
|
|
189
|
-
/**
|
|
190
|
-
* @description Registers an observer
|
|
191
|
-
* @summary Adds an observer to the list of observers that will be notified of changes.
|
|
192
|
-
* Throws an error if the observer is already registered.
|
|
193
|
-
* @param {Observer} observer - The observer to register
|
|
194
|
-
* @return {void}
|
|
195
|
-
* @throws {InternalError} If the observer is already registered
|
|
196
|
-
*/
|
|
197
|
-
observe(observer: Observer): void;
|
|
198
|
-
/**
|
|
199
|
-
* @description Unregisters an observer
|
|
200
|
-
* @summary Removes an observer from the list of observers.
|
|
201
|
-
* Throws an error if the observer is not found.
|
|
202
|
-
* @param {Observer} observer - The observer to unregister
|
|
203
|
-
* @return {void}
|
|
204
|
-
* @throws {InternalError} If the observer is not found
|
|
205
|
-
*/
|
|
206
|
-
unObserve(observer: Observer): void;
|
|
207
|
-
/**
|
|
208
|
-
* @description Notifies all registered observers
|
|
209
|
-
* @summary Calls the refresh method on all registered observers to update themselves.
|
|
210
|
-
* Any errors during observer refresh are logged as warnings but don't stop the process.
|
|
211
|
-
* @param {...any[]} [args] - Optional arguments to pass to the observer refresh method
|
|
212
|
-
* @return {Promise<void>} A promise that resolves when all observers have been updated
|
|
213
|
-
*/
|
|
214
|
-
updateObservers(...args: any[]): Promise<void>;
|
|
215
64
|
toString(): string;
|
|
216
65
|
}
|
package/lib/esm/RestService.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { LoggedClass } from "@decaf-ts/logging";
|
|
1
|
+
import { Model } from "@decaf-ts/decorator-validation";
|
|
2
|
+
import { RestRepository } from "./RestRepository.js";
|
|
4
3
|
/**
|
|
5
4
|
* @description Service class for REST API operations
|
|
6
5
|
* @summary Provides a comprehensive implementation for interacting with REST APIs.
|
|
@@ -49,58 +48,7 @@ import { LoggedClass } from "@decaf-ts/logging";
|
|
|
49
48
|
* Adapter-->>Service: record
|
|
50
49
|
* Service-->>Client: revert(record)
|
|
51
50
|
*/
|
|
52
|
-
export class RestService extends
|
|
53
|
-
/**
|
|
54
|
-
* @description Gets the model class constructor
|
|
55
|
-
* @summary Retrieves the model class constructor associated with this service.
|
|
56
|
-
* Throws an error if no class definition is found.
|
|
57
|
-
* @return {Constructor<M>} The model class constructor
|
|
58
|
-
* @throws {InternalError} If no class definition is found
|
|
59
|
-
*/
|
|
60
|
-
get class() {
|
|
61
|
-
if (!this._class)
|
|
62
|
-
throw new InternalError("No class definition found for this repository");
|
|
63
|
-
return this._class;
|
|
64
|
-
}
|
|
65
|
-
get log() {
|
|
66
|
-
if (!this.logger)
|
|
67
|
-
this.logger = this.adapter["log"].for(this.toString());
|
|
68
|
-
return this.logger;
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* @description Gets the primary key property name
|
|
72
|
-
* @summary Retrieves the name of the primary key property for the model.
|
|
73
|
-
* If not already determined, it finds the primary key using the model class.
|
|
74
|
-
* @return The primary key property name
|
|
75
|
-
*/
|
|
76
|
-
get pk() {
|
|
77
|
-
if (!this._pk)
|
|
78
|
-
this._pk = findPrimaryKey(new this.class()).id;
|
|
79
|
-
return this._pk;
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* @description Gets the HTTP adapter
|
|
83
|
-
* @summary Retrieves the HTTP adapter associated with this service.
|
|
84
|
-
* Throws an error if no adapter is found.
|
|
85
|
-
* @return {A} The HTTP adapter instance
|
|
86
|
-
* @throws {InternalError} If no adapter is found
|
|
87
|
-
*/
|
|
88
|
-
get adapter() {
|
|
89
|
-
if (!this._adapter)
|
|
90
|
-
throw new InternalError("No adapter found for this repository. did you use the @uses decorator or pass it in the constructor?");
|
|
91
|
-
return this._adapter;
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* @description Gets the table name for the model
|
|
95
|
-
* @summary Retrieves the table name associated with the model class.
|
|
96
|
-
* If not already determined, it gets the table name from the Repository utility.
|
|
97
|
-
* @return {string} The table name
|
|
98
|
-
*/
|
|
99
|
-
get tableName() {
|
|
100
|
-
if (!this._tableName)
|
|
101
|
-
this._tableName = Repository.table(this.class);
|
|
102
|
-
return this._tableName;
|
|
103
|
-
}
|
|
51
|
+
export class RestService extends RestRepository {
|
|
104
52
|
/**
|
|
105
53
|
* @description Initializes a new RestService instance
|
|
106
54
|
* @summary Creates a new service instance with the specified adapter and optional model class.
|
|
@@ -109,182 +57,14 @@ export class RestService extends LoggedClass {
|
|
|
109
57
|
* @param {Constructor<M>} [clazz] - Optional constructor for the model class
|
|
110
58
|
*/
|
|
111
59
|
constructor(adapter, clazz) {
|
|
112
|
-
super();
|
|
113
|
-
this.
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
url(path, queryParams) {
|
|
119
|
-
return this.adapter.url(path, queryParams);
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* @description Creates a new resource
|
|
123
|
-
* @summary Creates a new resource in the REST API using the provided model.
|
|
124
|
-
* The method prepares the model for the adapter, sends the create request,
|
|
125
|
-
* and then converts the response back to a model instance.
|
|
126
|
-
* @param {M} model - The model instance to create
|
|
127
|
-
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
128
|
-
* @return {Promise<M>} A promise that resolves with the created model instance
|
|
129
|
-
*/
|
|
130
|
-
async create(model, ...args) {
|
|
131
|
-
// eslint-disable-next-line prefer-const
|
|
132
|
-
let { record, id } = this.adapter.prepare(model, this.pk);
|
|
133
|
-
record = await this.adapter.create(this.tableName, id, record, ...args);
|
|
134
|
-
return this.adapter.revert(record, this.class, this.pk, id);
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* @description Retrieves a resource by ID
|
|
138
|
-
* @summary Fetches a resource from the REST API using the provided ID.
|
|
139
|
-
* The method sends the read request and converts the response to a model instance.
|
|
140
|
-
* @param {string|number} id - The identifier of the resource to retrieve
|
|
141
|
-
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
142
|
-
* @return {Promise<M>} A promise that resolves with the retrieved model instance
|
|
143
|
-
*/
|
|
144
|
-
async read(id, ...args) {
|
|
145
|
-
const m = await this.adapter.read(this.tableName, id, ...args);
|
|
146
|
-
return this.adapter.revert(m, this.class, this.pk, id);
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* @description Updates an existing resource
|
|
150
|
-
* @summary Updates an existing resource in the REST API using the provided model.
|
|
151
|
-
* The method prepares the model for the adapter, sends the update request,
|
|
152
|
-
* and then converts the response back to a model instance.
|
|
153
|
-
* @param {M} model - The model instance with updated data
|
|
154
|
-
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
155
|
-
* @return {Promise<M>} A promise that resolves with the updated model instance
|
|
156
|
-
*/
|
|
157
|
-
async update(model, ...args) {
|
|
158
|
-
// eslint-disable-next-line prefer-const
|
|
159
|
-
let { record, id } = this.adapter.prepare(model, this.pk);
|
|
160
|
-
record = await this.adapter.update(this.tableName, id, record, ...args);
|
|
161
|
-
return this.adapter.revert(record, this.class, this.pk, id);
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* @description Deletes a resource by ID
|
|
165
|
-
* @summary Removes a resource from the REST API using the provided ID.
|
|
166
|
-
* The method sends the delete request and converts the response to a model instance.
|
|
167
|
-
* @param {string|number} id - The identifier of the resource to delete
|
|
168
|
-
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
169
|
-
* @return {Promise<M>} A promise that resolves with the deleted model instance
|
|
170
|
-
*/
|
|
171
|
-
async delete(id, ...args) {
|
|
172
|
-
const m = await this.adapter.delete(this.tableName, id, ...args);
|
|
173
|
-
return this.adapter.revert(m, this.class, this.pk, id);
|
|
174
|
-
}
|
|
175
|
-
async request(details) {
|
|
176
|
-
return this.adapter.request(details);
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* @description Creates multiple resources
|
|
180
|
-
* @summary Creates multiple resources in the REST API using the provided models.
|
|
181
|
-
* The method prepares each model for the adapter, sends a bulk create request,
|
|
182
|
-
* and then converts the responses back to model instances.
|
|
183
|
-
* @param {M[]} models - The model instances to create
|
|
184
|
-
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
185
|
-
* @return {Promise<M[]>} A promise that resolves with an array of created model instances
|
|
186
|
-
* @mermaid
|
|
187
|
-
* sequenceDiagram
|
|
188
|
-
* participant Client
|
|
189
|
-
* participant Service as RestService
|
|
190
|
-
* participant Adapter as HttpAdapter
|
|
191
|
-
* Client->>Service: createAll(models)
|
|
192
|
-
* Service->>Adapter: prepare(model, pk) x N
|
|
193
|
-
* Service->>Adapter: createAll(table, ids[], records[])
|
|
194
|
-
* Adapter-->>Service: records[]
|
|
195
|
-
* Service-->>Client: revert(records[])
|
|
196
|
-
*/
|
|
197
|
-
async createAll(models, ...args) {
|
|
198
|
-
if (!models.length)
|
|
199
|
-
return models;
|
|
200
|
-
const prepared = models.map((m) => this.adapter.prepare(m, this.pk));
|
|
201
|
-
const ids = prepared.map((p) => p.id);
|
|
202
|
-
let records = prepared.map((p) => p.record);
|
|
203
|
-
records = await this.adapter.createAll(this.tableName, ids, records, ...args);
|
|
204
|
-
return records.map((r, i) => this.adapter.revert(r, this.class, this.pk, ids[i]));
|
|
205
|
-
}
|
|
206
|
-
/**
|
|
207
|
-
* @description Deletes multiple resources by IDs
|
|
208
|
-
* @summary Removes multiple resources from the REST API using the provided IDs.
|
|
209
|
-
* The method sends a bulk delete request and converts the responses to model instances.
|
|
210
|
-
* @param {string[]|number[]} keys - The identifiers of the resources to delete
|
|
211
|
-
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
212
|
-
* @return {Promise<M[]>} A promise that resolves with an array of deleted model instances
|
|
213
|
-
*/
|
|
214
|
-
async deleteAll(keys, ...args) {
|
|
215
|
-
const results = await this.adapter.deleteAll(this.tableName, keys, ...args);
|
|
216
|
-
return results.map((r, i) => this.adapter.revert(r, this.class, this.pk, keys[i]));
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* @description Retrieves multiple resources by IDs
|
|
220
|
-
* @summary Fetches multiple resources from the REST API using the provided IDs.
|
|
221
|
-
* The method sends a bulk read request and converts the responses to model instances.
|
|
222
|
-
* @param {string[]|number[]} keys - The identifiers of the resources to retrieve
|
|
223
|
-
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
224
|
-
* @return {Promise<M[]>} A promise that resolves with an array of retrieved model instances
|
|
225
|
-
*/
|
|
226
|
-
async readAll(keys, ...args) {
|
|
227
|
-
const records = await this.adapter.readAll(this.tableName, keys, ...args);
|
|
228
|
-
return records.map((r, i) => this.adapter.revert(r, this.class, this.pk, keys[i]));
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* @description Updates multiple resources
|
|
232
|
-
* @summary Updates multiple resources in the REST API using the provided models.
|
|
233
|
-
* The method prepares each model for the adapter, sends a bulk update request,
|
|
234
|
-
* and then converts the responses back to model instances.
|
|
235
|
-
* @param {M[]} models - The model instances with updated data
|
|
236
|
-
* @param {...any[]} args - Additional arguments to pass to the adapter
|
|
237
|
-
* @return {Promise<M[]>} A promise that resolves with an array of updated model instances
|
|
238
|
-
*/
|
|
239
|
-
async updateAll(models, ...args) {
|
|
240
|
-
const records = models.map((m) => this.adapter.prepare(m, this.pk));
|
|
241
|
-
const updated = await this.adapter.updateAll(this.tableName, records.map((r) => r.id), records.map((r) => r.record), ...args);
|
|
242
|
-
return updated.map((u, i) => this.adapter.revert(u, this.class, this.pk, records[i].id));
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* @description Registers an observer
|
|
246
|
-
* @summary Adds an observer to the list of observers that will be notified of changes.
|
|
247
|
-
* Throws an error if the observer is already registered.
|
|
248
|
-
* @param {Observer} observer - The observer to register
|
|
249
|
-
* @return {void}
|
|
250
|
-
* @throws {InternalError} If the observer is already registered
|
|
251
|
-
*/
|
|
252
|
-
observe(observer) {
|
|
253
|
-
const index = this.observers.indexOf(observer);
|
|
254
|
-
if (index !== -1)
|
|
255
|
-
throw new InternalError("Observer already registered");
|
|
256
|
-
this.observers.push(observer);
|
|
257
|
-
}
|
|
258
|
-
/**
|
|
259
|
-
* @description Unregisters an observer
|
|
260
|
-
* @summary Removes an observer from the list of observers.
|
|
261
|
-
* Throws an error if the observer is not found.
|
|
262
|
-
* @param {Observer} observer - The observer to unregister
|
|
263
|
-
* @return {void}
|
|
264
|
-
* @throws {InternalError} If the observer is not found
|
|
265
|
-
*/
|
|
266
|
-
unObserve(observer) {
|
|
267
|
-
const index = this.observers.indexOf(observer);
|
|
268
|
-
if (index === -1)
|
|
269
|
-
throw new InternalError("Failed to find Observer");
|
|
270
|
-
this.observers.splice(index, 1);
|
|
271
|
-
}
|
|
272
|
-
/**
|
|
273
|
-
* @description Notifies all registered observers
|
|
274
|
-
* @summary Calls the refresh method on all registered observers to update themselves.
|
|
275
|
-
* Any errors during observer refresh are logged as warnings but don't stop the process.
|
|
276
|
-
* @param {...any[]} [args] - Optional arguments to pass to the observer refresh method
|
|
277
|
-
* @return {Promise<void>} A promise that resolves when all observers have been updated
|
|
278
|
-
*/
|
|
279
|
-
async updateObservers(...args) {
|
|
280
|
-
const results = await Promise.allSettled(this.observers.map((o) => o.refresh(...args)));
|
|
281
|
-
results.forEach((result, i) => {
|
|
282
|
-
if (result.status === "rejected")
|
|
283
|
-
console.warn(`Failed to update observable ${this.observers[i]}: ${result.reason}`);
|
|
284
|
-
});
|
|
60
|
+
super(adapter, clazz);
|
|
61
|
+
this._overrides = {
|
|
62
|
+
ignoreValidation: true,
|
|
63
|
+
ignoreHandlers: true,
|
|
64
|
+
};
|
|
285
65
|
}
|
|
286
66
|
toString() {
|
|
287
|
-
return `${this.class
|
|
67
|
+
return `${Model.tableName(this.class)} REST service`;
|
|
288
68
|
}
|
|
289
69
|
}
|
|
290
70
|
//# sourceMappingURL=RestService.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RestService.js","sourceRoot":"","sources":["../../src/RestService.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"RestService.js","sourceRoot":"","sources":["../../src/RestService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,gCAAgC,CAAC;AAIvD,OAAO,EAAE,cAAc,EAAE,4BAAyB;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,MAAM,OAAO,WAIX,SAAQ,cAAuB;IAM/B;;;;;;OAMG;IACH,YAAY,OAAU,EAAE,KAAsB;QAC5C,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QAbL,eAAU,GAAwB;YACnD,gBAAgB,EAAE,IAAI;YACtB,cAAc,EAAE,IAAI;SACE,CAAC;IAWzB,CAAC;IAEQ,QAAQ;QACf,OAAO,GAAG,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC;IACvD,CAAC;CACF"}
|
package/lib/esm/adapter.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { Adapter, Condition, Repository, Sequence, SequenceOptions } from "@decaf-ts/core";
|
|
2
|
-
import { Context, OperationKeys } from "@decaf-ts/db-decorators";
|
|
1
|
+
import { Adapter, Condition, ContextualArgs, PreparedModel, Repository, Sequence, SequenceOptions } from "@decaf-ts/core";
|
|
2
|
+
import { Context, FlagsOf, OperationKeys, PrimaryKeyType } from "@decaf-ts/db-decorators";
|
|
3
3
|
import { HttpConfig, HttpFlags } from "./types";
|
|
4
|
-
import {
|
|
4
|
+
import { Model } from "@decaf-ts/decorator-validation";
|
|
5
|
+
import { Constructor } from "@decaf-ts/decoration";
|
|
5
6
|
import { Statement } from "@decaf-ts/core";
|
|
6
7
|
/**
|
|
7
8
|
* @description Abstract HTTP adapter for REST API interactions
|
|
@@ -35,8 +36,8 @@ import { Statement } from "@decaf-ts/core";
|
|
|
35
36
|
* }
|
|
36
37
|
* ```
|
|
37
38
|
*/
|
|
38
|
-
export declare abstract class HttpAdapter<
|
|
39
|
-
protected constructor(config:
|
|
39
|
+
export declare abstract class HttpAdapter<CONF extends HttpConfig, CON, Q, C extends Context<HttpFlags> = Context<HttpFlags>> extends Adapter<CONF, CON, Q, C> {
|
|
40
|
+
protected constructor(config: CONF, flavour: string, alias?: string);
|
|
40
41
|
/**
|
|
41
42
|
* @description Generates operation flags with HTTP headers
|
|
42
43
|
* @summary Extends the base flags method to include HTTP-specific headers for operations.
|
|
@@ -48,17 +49,17 @@ export declare abstract class HttpAdapter<Y extends HttpConfig, CON, Q, F extend
|
|
|
48
49
|
* @param {Partial<F>} overrides - Optional flag overrides
|
|
49
50
|
* @return {F} The flags object with headers
|
|
50
51
|
*/
|
|
51
|
-
flags<M extends Model>(operation: OperationKeys.CREATE | OperationKeys.READ | OperationKeys.UPDATE | OperationKeys.DELETE, model: Constructor<M>, overrides: Partial<
|
|
52
|
+
flags<M extends Model>(operation: OperationKeys.CREATE | OperationKeys.READ | OperationKeys.UPDATE | OperationKeys.DELETE, model: Constructor<M>, overrides: Partial<FlagsOf<C>>): Promise<import("@decaf-ts/core").FlagsOf<C>> & {
|
|
52
53
|
headers: {};
|
|
53
54
|
};
|
|
54
55
|
/**
|
|
55
56
|
* @description Returns the repository constructor for this adapter
|
|
56
57
|
* @summary Provides the RestService class as the repository implementation for this HTTP adapter.
|
|
57
58
|
* This method is used to create repository instances that work with this adapter type.
|
|
58
|
-
* @template
|
|
59
|
-
* @return {Constructor<
|
|
59
|
+
* @template R - Repository subtype working with this adapter
|
|
60
|
+
* @return {Constructor<R>} The repository constructor
|
|
60
61
|
*/
|
|
61
|
-
repository<
|
|
62
|
+
repository<R extends Repository<any, Adapter<CONF, CON, Q, C>>>(): Constructor<R>;
|
|
62
63
|
/**
|
|
63
64
|
* @description Prepares a model for persistence
|
|
64
65
|
* @summary Converts a model instance into a format suitable for database storage,
|
|
@@ -66,13 +67,10 @@ export declare abstract class HttpAdapter<Y extends HttpConfig, CON, Q, F extend
|
|
|
66
67
|
* @template M - The model type
|
|
67
68
|
* @param {M} model - The model instance to prepare
|
|
68
69
|
* @param pk - The primary key property name
|
|
70
|
+
* @param args
|
|
69
71
|
* @return The prepared data
|
|
70
72
|
*/
|
|
71
|
-
prepare<M extends Model>(model: M,
|
|
72
|
-
record: Record<string, any>;
|
|
73
|
-
id: string;
|
|
74
|
-
transient?: Record<string, any>;
|
|
75
|
-
};
|
|
73
|
+
prepare<M extends Model>(model: M, ...args: ContextualArgs<C>): PreparedModel;
|
|
76
74
|
/**
|
|
77
75
|
* @description Converts database data back into a model instance
|
|
78
76
|
* @summary Reconstructs a model instance from database data, handling column mapping
|
|
@@ -84,7 +82,8 @@ export declare abstract class HttpAdapter<Y extends HttpConfig, CON, Q, F extend
|
|
|
84
82
|
* @param {string|number|bigint} id - The primary key value
|
|
85
83
|
* @return {M} The reconstructed model instance
|
|
86
84
|
*/
|
|
87
|
-
revert<M extends Model>(obj: Record<string, any>, clazz: string | Constructor<M>,
|
|
85
|
+
revert<M extends Model>(obj: Record<string, any>, clazz: string | Constructor<M>, id: PrimaryKeyType, ...args: ContextualArgs<C>): M;
|
|
86
|
+
protected toTableName<M extends Model>(t: string | Constructor<M>): string;
|
|
88
87
|
/**
|
|
89
88
|
* @description Constructs a URL for API requests
|
|
90
89
|
* @summary Builds a complete URL for API requests using the configured protocol and host,
|
|
@@ -93,7 +92,7 @@ export declare abstract class HttpAdapter<Y extends HttpConfig, CON, Q, F extend
|
|
|
93
92
|
* @param {Record<string, string | number>} [queryParams] - Optional query parameters
|
|
94
93
|
* @return {string} The encoded URL string
|
|
95
94
|
*/
|
|
96
|
-
url(tableName: string
|
|
95
|
+
url<M extends Model>(tableName: string | Constructor<M>, queryParams?: Record<string, string | number>): string;
|
|
97
96
|
/**
|
|
98
97
|
* @description Sends an HTTP request
|
|
99
98
|
* @summary Abstract method that must be implemented by subclasses to send HTTP requests
|
|
@@ -113,7 +112,7 @@ export declare abstract class HttpAdapter<Y extends HttpConfig, CON, Q, F extend
|
|
|
113
112
|
* @param {...any[]} args - Additional arguments
|
|
114
113
|
* @return {Promise<Record<string, any>>} A promise that resolves with the created resource
|
|
115
114
|
*/
|
|
116
|
-
abstract create(tableName: string, id:
|
|
115
|
+
abstract create<M extends Model>(tableName: Constructor<M> | string, id: PrimaryKeyType, model: Record<string, any>, ...args: ContextualArgs<C>): Promise<Record<string, any>>;
|
|
117
116
|
/**
|
|
118
117
|
* @description Retrieves a resource by ID
|
|
119
118
|
* @summary Abstract method that must be implemented by subclasses to retrieve a resource
|
|
@@ -123,7 +122,7 @@ export declare abstract class HttpAdapter<Y extends HttpConfig, CON, Q, F extend
|
|
|
123
122
|
* @param {...any[]} args - Additional arguments
|
|
124
123
|
* @return {Promise<Record<string, any>>} A promise that resolves with the retrieved resource
|
|
125
124
|
*/
|
|
126
|
-
abstract read(tableName: string, id:
|
|
125
|
+
abstract read<M extends Model>(tableName: Constructor<M> | string, id: PrimaryKeyType, ...args: ContextualArgs<C>): Promise<Record<string, any>>;
|
|
127
126
|
/**
|
|
128
127
|
* @description Updates an existing resource
|
|
129
128
|
* @summary Abstract method that must be implemented by subclasses to update a resource
|
|
@@ -134,7 +133,7 @@ export declare abstract class HttpAdapter<Y extends HttpConfig, CON, Q, F extend
|
|
|
134
133
|
* @param {...any[]} args - Additional arguments
|
|
135
134
|
* @return {Promise<Record<string, any>>} A promise that resolves with the updated resource
|
|
136
135
|
*/
|
|
137
|
-
abstract update(tableName: string, id: string | number, model: Record<string, any>, ...args:
|
|
136
|
+
abstract update<M extends Model>(tableName: Constructor<M> | string, id: string | number, model: Record<string, any>, ...args: ContextualArgs<C>): Promise<Record<string, any>>;
|
|
138
137
|
/**
|
|
139
138
|
* @description Deletes a resource by ID
|
|
140
139
|
* @summary Abstract method that must be implemented by subclasses to delete a resource
|
|
@@ -144,7 +143,7 @@ export declare abstract class HttpAdapter<Y extends HttpConfig, CON, Q, F extend
|
|
|
144
143
|
* @param {...any[]} args - Additional arguments
|
|
145
144
|
* @return {Promise<Record<string, any>>} A promise that resolves with the deletion result
|
|
146
145
|
*/
|
|
147
|
-
abstract delete(tableName: string, id:
|
|
146
|
+
abstract delete<M extends Model>(tableName: Constructor<M> | string, id: PrimaryKeyType, ...args: ContextualArgs<C>): Promise<Record<string, any>>;
|
|
148
147
|
/**
|
|
149
148
|
* @description Executes a raw query
|
|
150
149
|
* @summary Method for executing raw queries directly with the HTTP client.
|
|
@@ -157,7 +156,7 @@ export declare abstract class HttpAdapter<Y extends HttpConfig, CON, Q, F extend
|
|
|
157
156
|
* @return {Promise<R>} A promise that resolves with the query result
|
|
158
157
|
* @throws {UnsupportedError} Always throws as this method is not supported by default
|
|
159
158
|
*/
|
|
160
|
-
raw<R>(rawInput: Q,
|
|
159
|
+
raw<R>(rawInput: Q, ...args: ContextualArgs<C>): Promise<R>;
|
|
161
160
|
/**
|
|
162
161
|
* @description Creates a sequence
|
|
163
162
|
* @summary Method for creating a sequence for generating unique identifiers.
|
|
@@ -178,7 +177,7 @@ export declare abstract class HttpAdapter<Y extends HttpConfig, CON, Q, F extend
|
|
|
178
177
|
* @return {Statement<Q, M, any>} A statement object for building queries
|
|
179
178
|
* @throws {UnsupportedError} Always throws as this method is not supported by default
|
|
180
179
|
*/
|
|
181
|
-
Statement<M extends Model>(): Statement<
|
|
180
|
+
Statement<M extends Model>(): Statement<M, Adapter<CONF, CON, Q, C>, any>;
|
|
182
181
|
/**
|
|
183
182
|
* @description Parses a condition into a query
|
|
184
183
|
* @summary Method for parsing a condition object into a query format understood by the HTTP client.
|