@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.
@@ -1,9 +1,8 @@
1
- import { BulkCrudOperator, Context, CrudOperator } from "@decaf-ts/db-decorators";
2
- import { Constructor, Model } from "@decaf-ts/decorator-validation";
3
- import { Observable, Observer } from "@decaf-ts/core";
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 { HttpFlags } from "./types";
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, 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 {
56
- private readonly _class;
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
  }
@@ -1,6 +1,5 @@
1
- import { findPrimaryKey, InternalError, } from "@decaf-ts/db-decorators";
2
- import { Repository } from "@decaf-ts/core";
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 LoggedClass {
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.observers = [];
114
- this._adapter = adapter;
115
- if (clazz)
116
- this._class = clazz;
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.name} rest service`;
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,EAIL,cAAc,EACd,aAAa,GACd,MAAM,yBAAyB,CAAC;AAEjC,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,cAAc,CAAC,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,EAAE,CAAC;QAC9D,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KAAK,CAAC,IAAI,eAAe,CAAC;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,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 { Constructor, Model } from "@decaf-ts/decorator-validation";
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<Y extends HttpConfig, CON, Q, F extends HttpFlags = HttpFlags, C extends Context<F> = Context<F>> extends Adapter<Y, CON, Q, F, C> {
39
- 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);
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<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>> & {
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 M - The model type
59
- * @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
60
61
  */
61
- 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>;
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, pk: keyof 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>, 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;
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, queryParams?: Record<string, string | number>): 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: 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>>;
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: 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>>;
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: 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>>;
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: 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>>;
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, process: boolean, ...args: any[]): Promise<R>;
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<Q, M, any>;
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.