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