@decaf-ts/for-http 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/HttpDispatcher.cjs +3 -2
- package/lib/cjs/HttpDispatcher.cjs.map +1 -0
- package/lib/cjs/HttpPaginator.cjs +1 -0
- package/lib/cjs/HttpPaginator.cjs.map +1 -0
- package/lib/cjs/HttpStatement.cjs +1 -0
- package/lib/cjs/HttpStatement.cjs.map +1 -0
- package/lib/cjs/RestRepository.cjs +11 -5
- package/lib/cjs/RestRepository.cjs.map +1 -0
- package/lib/cjs/RestService.cjs +3 -2
- package/lib/cjs/RestService.cjs.map +1 -0
- package/lib/cjs/adapter.cjs +11 -10
- package/lib/cjs/adapter.cjs.map +1 -0
- package/lib/cjs/axios/axios.cjs +6 -5
- package/lib/cjs/axios/axios.cjs.map +1 -0
- package/lib/cjs/axios/constants.cjs +5 -4
- package/lib/cjs/axios/constants.cjs.map +1 -0
- package/lib/cjs/axios/index.cjs +3 -2
- package/lib/cjs/axios/index.cjs.map +1 -0
- package/lib/cjs/axios/types.cjs +1 -0
- package/lib/cjs/axios/types.cjs.map +1 -0
- package/lib/cjs/constants.cjs +2 -2
- package/lib/cjs/constants.cjs.map +1 -0
- package/lib/cjs/event/ServerEventConnector.cjs +1 -0
- package/lib/cjs/event/ServerEventConnector.cjs.map +1 -0
- package/lib/cjs/event/index.cjs +1 -0
- package/lib/cjs/event/index.cjs.map +1 -0
- package/lib/cjs/event/types.cjs +1 -0
- package/lib/cjs/event/types.cjs.map +1 -0
- package/lib/cjs/index.cjs +2 -1
- package/lib/cjs/index.cjs.map +1 -0
- package/lib/cjs/types.cjs +1 -0
- package/lib/cjs/types.cjs.map +1 -0
- package/lib/esm/index.js +1 -1
- package/lib/types/HttpDispatcher.d.cts +18 -0
- package/lib/types/HttpDispatcher.d.mts +18 -0
- package/lib/types/HttpPaginator.d.cts +10 -0
- package/lib/types/HttpPaginator.d.mts +10 -0
- package/lib/types/HttpStatement.d.cts +11 -0
- package/lib/types/HttpStatement.d.mts +11 -0
- package/lib/types/RestRepository.d.cts +63 -0
- package/lib/types/RestRepository.d.mts +63 -0
- package/lib/types/RestService.d.cts +74 -0
- package/lib/types/RestService.d.mts +74 -0
- package/lib/types/adapter.d.cts +230 -0
- package/lib/types/adapter.d.mts +230 -0
- package/lib/types/axios/axios.d.cts +76 -0
- package/lib/types/axios/axios.d.mts +76 -0
- package/lib/types/axios/constants.d.cts +9 -0
- package/lib/types/axios/constants.d.mts +9 -0
- package/lib/types/axios/index.d.cts +3 -0
- package/lib/types/axios/index.d.mts +3 -0
- package/lib/types/axios/types.d.cts +8 -0
- package/lib/types/axios/types.d.mts +8 -0
- package/lib/types/constants.d.cts +5 -0
- package/lib/types/constants.d.mts +5 -0
- package/lib/types/event/ServerEventConnector.d.cts +39 -0
- package/lib/types/event/ServerEventConnector.d.mts +39 -0
- package/lib/types/event/index.d.cts +2 -0
- package/lib/types/event/index.d.mts +2 -0
- package/lib/types/event/types.d.cts +26 -0
- package/lib/types/event/types.d.mts +26 -0
- package/lib/types/index.d.cts +25 -0
- package/lib/types/index.d.mts +25 -0
- package/lib/types/index.d.ts +1 -1
- package/lib/types/types.d.cts +65 -0
- package/lib/types/types.d.mts +65 -0
- package/package.json +4 -4
- package/lib/cjs/HttpDispatcher.js.map +0 -1
- package/lib/cjs/HttpPaginator.js.map +0 -1
- package/lib/cjs/HttpStatement.js.map +0 -1
- package/lib/cjs/RestRepository.js.map +0 -1
- package/lib/cjs/RestService.js.map +0 -1
- package/lib/cjs/adapter.js.map +0 -1
- package/lib/cjs/axios/axios.js.map +0 -1
- package/lib/cjs/axios/constants.js.map +0 -1
- package/lib/cjs/axios/index.js.map +0 -1
- package/lib/cjs/axios/types.js.map +0 -1
- package/lib/cjs/constants.js.map +0 -1
- package/lib/cjs/event/ServerEventConnector.js.map +0 -1
- package/lib/cjs/event/index.js.map +0 -1
- package/lib/cjs/event/types.js.map +0 -1
- package/lib/cjs/index.js.map +0 -1
- package/lib/cjs/types.js.map +0 -1
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { DirectionLimitOffset, OrderDirection, Repository } from "@decaf-ts/core";
|
|
2
|
+
import type { ContextOf, MaybeContextualArg, SerializedPage } from "@decaf-ts/core";
|
|
3
|
+
import { Model } from "@decaf-ts/decorator-validation";
|
|
4
|
+
import { Constructor } from "@decaf-ts/decoration";
|
|
5
|
+
import { HttpAdapter } from "./adapter.cjs";
|
|
6
|
+
/**
|
|
7
|
+
* @description Repository for REST API interactions
|
|
8
|
+
* @summary A specialized repository implementation for interacting with REST APIs.
|
|
9
|
+
* This class extends the core Repository class and works with HTTP adapters to
|
|
10
|
+
* provide CRUD operations for models via REST endpoints.
|
|
11
|
+
* This Is NOT the default repository for the HTTP adapter. That would be {@link RestService}.
|
|
12
|
+
* Use this only in the specific case of needing to run the CURD model logic (decoration) before submitting to the backend
|
|
13
|
+
* @template M - The model type, extending Model
|
|
14
|
+
* @template Q - The query type used by the adapter
|
|
15
|
+
* @template A - The HTTP adapter type, extending HttpAdapter
|
|
16
|
+
* @template F - The HTTP flags type, extending HttpFlags
|
|
17
|
+
* @template C - The context type, extending Context<F>
|
|
18
|
+
* @param {A} adapter - The HTTP adapter instance
|
|
19
|
+
* @param {Constructor<M>} [clazz] - Optional constructor for the model class
|
|
20
|
+
* @class RestRepository
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* // Create a repository for User model with Axios adapter
|
|
24
|
+
* const axiosAdapter = new AxiosAdapter({
|
|
25
|
+
* protocol: 'https',
|
|
26
|
+
* host: 'api.example.com'
|
|
27
|
+
* });
|
|
28
|
+
* const userRepository = new RestRepository(axiosAdapter, User);
|
|
29
|
+
*
|
|
30
|
+
* // Use the repository for CRUD operations
|
|
31
|
+
* const user = await userRepository.findById('123');
|
|
32
|
+
* ```
|
|
33
|
+
* @see {@link RestService}
|
|
34
|
+
*/
|
|
35
|
+
export declare class RestRepository<M extends Model, A extends HttpAdapter<any, any, any, any, any>, Q = A extends HttpAdapter<any, any, any, infer Q, any> ? Q : never> extends Repository<M, A> {
|
|
36
|
+
protected _overrides: Partial<import("@decaf-ts/core").FlagsOf<ContextOf<A>>> & {
|
|
37
|
+
allowRawStatements: boolean;
|
|
38
|
+
forcePrepareSimpleQueries: boolean;
|
|
39
|
+
forcePrepareComplexQueries: boolean;
|
|
40
|
+
};
|
|
41
|
+
constructor(adapter: A, clazz?: Constructor<M>);
|
|
42
|
+
url<M extends Model>(tableName: string | Constructor<M>): string;
|
|
43
|
+
url<M extends Model>(tableName: string | Constructor<M>, pathParams: string[]): string;
|
|
44
|
+
url<M extends Model>(tableName: string | Constructor<M>, queryParams: Record<string, string | number> | undefined): string;
|
|
45
|
+
paginateBy(key: keyof M, order: OrderDirection, ref?: Omit<DirectionLimitOffset, "direction">, ...args: MaybeContextualArg<ContextOf<A>>): Promise<SerializedPage<M>>;
|
|
46
|
+
listBy(key: keyof M, order: OrderDirection, ...args: MaybeContextualArg<ContextOf<A>>): Promise<any>;
|
|
47
|
+
findBy(key: keyof M, value: any, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M[]>;
|
|
48
|
+
findOneBy(key: keyof M, value: any, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M>;
|
|
49
|
+
find(value: string, order?: OrderDirection, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M[]>;
|
|
50
|
+
page(value: string, direction?: OrderDirection, ref?: Omit<DirectionLimitOffset, "direction">, ...args: MaybeContextualArg<ContextOf<A>>): Promise<SerializedPage<M>>;
|
|
51
|
+
statement(name: string, ...args: MaybeContextualArg<ContextOf<A>>): Promise<any>;
|
|
52
|
+
private convertStatementResult;
|
|
53
|
+
private convertPageResult;
|
|
54
|
+
private revertRecord;
|
|
55
|
+
request<R>(details: Q, ...args: MaybeContextualArg<ContextOf<A>>): Promise<R>;
|
|
56
|
+
countOf(key?: keyof M, ...args: MaybeContextualArg<ContextOf<A>>): Promise<number>;
|
|
57
|
+
maxOf<K extends keyof M>(key: K, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M[K]>;
|
|
58
|
+
minOf<K extends keyof M>(key: K, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M[K]>;
|
|
59
|
+
avgOf<K extends keyof M>(key: K, ...args: MaybeContextualArg<ContextOf<A>>): Promise<number>;
|
|
60
|
+
sumOf<K extends keyof M>(key: K, ...args: MaybeContextualArg<ContextOf<A>>): Promise<number>;
|
|
61
|
+
distinctOf<K extends keyof M>(key: K, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M[K][]>;
|
|
62
|
+
groupOf<K extends keyof M>(key: K, ...args: MaybeContextualArg<ContextOf<A>>): Promise<Record<string, M[]>>;
|
|
63
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { DirectionLimitOffset, OrderDirection, Repository } from "@decaf-ts/core";
|
|
2
|
+
import type { ContextOf, MaybeContextualArg, SerializedPage } from "@decaf-ts/core";
|
|
3
|
+
import { Model } from "@decaf-ts/decorator-validation";
|
|
4
|
+
import { Constructor } from "@decaf-ts/decoration";
|
|
5
|
+
import { HttpAdapter } from "./adapter.js";
|
|
6
|
+
/**
|
|
7
|
+
* @description Repository for REST API interactions
|
|
8
|
+
* @summary A specialized repository implementation for interacting with REST APIs.
|
|
9
|
+
* This class extends the core Repository class and works with HTTP adapters to
|
|
10
|
+
* provide CRUD operations for models via REST endpoints.
|
|
11
|
+
* This Is NOT the default repository for the HTTP adapter. That would be {@link RestService}.
|
|
12
|
+
* Use this only in the specific case of needing to run the CURD model logic (decoration) before submitting to the backend
|
|
13
|
+
* @template M - The model type, extending Model
|
|
14
|
+
* @template Q - The query type used by the adapter
|
|
15
|
+
* @template A - The HTTP adapter type, extending HttpAdapter
|
|
16
|
+
* @template F - The HTTP flags type, extending HttpFlags
|
|
17
|
+
* @template C - The context type, extending Context<F>
|
|
18
|
+
* @param {A} adapter - The HTTP adapter instance
|
|
19
|
+
* @param {Constructor<M>} [clazz] - Optional constructor for the model class
|
|
20
|
+
* @class RestRepository
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* // Create a repository for User model with Axios adapter
|
|
24
|
+
* const axiosAdapter = new AxiosAdapter({
|
|
25
|
+
* protocol: 'https',
|
|
26
|
+
* host: 'api.example.com'
|
|
27
|
+
* });
|
|
28
|
+
* const userRepository = new RestRepository(axiosAdapter, User);
|
|
29
|
+
*
|
|
30
|
+
* // Use the repository for CRUD operations
|
|
31
|
+
* const user = await userRepository.findById('123');
|
|
32
|
+
* ```
|
|
33
|
+
* @see {@link RestService}
|
|
34
|
+
*/
|
|
35
|
+
export declare class RestRepository<M extends Model, A extends HttpAdapter<any, any, any, any, any>, Q = A extends HttpAdapter<any, any, any, infer Q, any> ? Q : never> extends Repository<M, A> {
|
|
36
|
+
protected _overrides: Partial<import("@decaf-ts/core").FlagsOf<ContextOf<A>>> & {
|
|
37
|
+
allowRawStatements: boolean;
|
|
38
|
+
forcePrepareSimpleQueries: boolean;
|
|
39
|
+
forcePrepareComplexQueries: boolean;
|
|
40
|
+
};
|
|
41
|
+
constructor(adapter: A, clazz?: Constructor<M>);
|
|
42
|
+
url<M extends Model>(tableName: string | Constructor<M>): string;
|
|
43
|
+
url<M extends Model>(tableName: string | Constructor<M>, pathParams: string[]): string;
|
|
44
|
+
url<M extends Model>(tableName: string | Constructor<M>, queryParams: Record<string, string | number> | undefined): string;
|
|
45
|
+
paginateBy(key: keyof M, order: OrderDirection, ref?: Omit<DirectionLimitOffset, "direction">, ...args: MaybeContextualArg<ContextOf<A>>): Promise<SerializedPage<M>>;
|
|
46
|
+
listBy(key: keyof M, order: OrderDirection, ...args: MaybeContextualArg<ContextOf<A>>): Promise<any>;
|
|
47
|
+
findBy(key: keyof M, value: any, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M[]>;
|
|
48
|
+
findOneBy(key: keyof M, value: any, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M>;
|
|
49
|
+
find(value: string, order?: OrderDirection, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M[]>;
|
|
50
|
+
page(value: string, direction?: OrderDirection, ref?: Omit<DirectionLimitOffset, "direction">, ...args: MaybeContextualArg<ContextOf<A>>): Promise<SerializedPage<M>>;
|
|
51
|
+
statement(name: string, ...args: MaybeContextualArg<ContextOf<A>>): Promise<any>;
|
|
52
|
+
private convertStatementResult;
|
|
53
|
+
private convertPageResult;
|
|
54
|
+
private revertRecord;
|
|
55
|
+
request<R>(details: Q, ...args: MaybeContextualArg<ContextOf<A>>): Promise<R>;
|
|
56
|
+
countOf(key?: keyof M, ...args: MaybeContextualArg<ContextOf<A>>): Promise<number>;
|
|
57
|
+
maxOf<K extends keyof M>(key: K, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M[K]>;
|
|
58
|
+
minOf<K extends keyof M>(key: K, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M[K]>;
|
|
59
|
+
avgOf<K extends keyof M>(key: K, ...args: MaybeContextualArg<ContextOf<A>>): Promise<number>;
|
|
60
|
+
sumOf<K extends keyof M>(key: K, ...args: MaybeContextualArg<ContextOf<A>>): Promise<number>;
|
|
61
|
+
distinctOf<K extends keyof M>(key: K, ...args: MaybeContextualArg<ContextOf<A>>): Promise<M[K][]>;
|
|
62
|
+
groupOf<K extends keyof M>(key: K, ...args: MaybeContextualArg<ContextOf<A>>): Promise<Record<string, M[]>>;
|
|
63
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Model } from "@decaf-ts/decorator-validation";
|
|
2
|
+
import { Constructor } from "@decaf-ts/decoration";
|
|
3
|
+
import { HttpAdapter } from "./adapter.cjs";
|
|
4
|
+
import { RestRepository } from "./RestRepository.cjs";
|
|
5
|
+
/**
|
|
6
|
+
* @description Service class for REST API operations
|
|
7
|
+
* @summary Provides a comprehensive implementation for interacting with REST APIs.
|
|
8
|
+
* This class implements CRUD operations for single and bulk operations, as well as
|
|
9
|
+
* the Observable pattern to notify observers of changes. It works with HTTP adapters
|
|
10
|
+
* to perform the actual API requests and handles model conversion.
|
|
11
|
+
* @template M - The model type, extending Model
|
|
12
|
+
* @template Q - The query type used by the adapter
|
|
13
|
+
* @template A - The HTTP adapter type, extending HttpAdapter
|
|
14
|
+
* @template F - The HTTP flags type, extending HttpFlags
|
|
15
|
+
* @template C - The context type, extending Context<F>
|
|
16
|
+
* @param {A} adapter - The HTTP adapter instance
|
|
17
|
+
* @param {Constructor<M>} [clazz] - Optional constructor for the model class
|
|
18
|
+
* @class RestService
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* // Create a service for User model with Axios adapter
|
|
22
|
+
* const axiosAdapter = new AxiosAdapter({
|
|
23
|
+
* protocol: 'https',
|
|
24
|
+
* host: 'api.example.com'
|
|
25
|
+
* });
|
|
26
|
+
* const userService = new RestService(axiosAdapter, User);
|
|
27
|
+
*
|
|
28
|
+
* // Create a new user
|
|
29
|
+
* const user = new User({ name: 'John Doe', email: 'john@example.com' });
|
|
30
|
+
* const createdUser = await userService.create(user);
|
|
31
|
+
*
|
|
32
|
+
* // Update a user
|
|
33
|
+
* createdUser.name = 'Jane Doe';
|
|
34
|
+
* const updatedUser = await userService.update(createdUser);
|
|
35
|
+
*
|
|
36
|
+
* // Delete a user
|
|
37
|
+
* await userService.delete(updatedUser.id);
|
|
38
|
+
* ```
|
|
39
|
+
* @mermaid
|
|
40
|
+
* sequenceDiagram
|
|
41
|
+
* participant Client
|
|
42
|
+
* participant Service as RestService
|
|
43
|
+
* participant Adapter as HttpAdapter
|
|
44
|
+
* participant API
|
|
45
|
+
* Client->>Service: create(model)
|
|
46
|
+
* Service->>Adapter: prepare(model, pk)
|
|
47
|
+
* Service->>Adapter: create(table, id, record)
|
|
48
|
+
* Adapter->>API: HTTP POST
|
|
49
|
+
* API-->>Adapter: 201 Created
|
|
50
|
+
* Adapter-->>Service: record
|
|
51
|
+
* Service-->>Client: revert(record)
|
|
52
|
+
*/
|
|
53
|
+
export declare class RestService<M extends Model, A extends HttpAdapter<any, any, any, any, any>, Q = A extends HttpAdapter<any, any, any, infer Q, any> ? Q : never> extends RestRepository<M, A, Q> {
|
|
54
|
+
protected _overrides: Partial<import("@decaf-ts/core").FlagsOf<import("@decaf-ts/core").ContextOf<A>>> & {
|
|
55
|
+
allowRawStatements: boolean;
|
|
56
|
+
forcePrepareSimpleQueries: boolean;
|
|
57
|
+
forcePrepareComplexQueries: boolean;
|
|
58
|
+
} & {
|
|
59
|
+
ignoreValidation: boolean;
|
|
60
|
+
ignoreHandlers: boolean;
|
|
61
|
+
allowRawStatements: boolean;
|
|
62
|
+
forcePrepareSimpleQueries: boolean;
|
|
63
|
+
forcePrepareComplexQueries: boolean;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* @description Initializes a new RestService instance
|
|
67
|
+
* @summary Creates a new service instance with the specified adapter and optional model class.
|
|
68
|
+
* The constructor stores the adapter and model class for later use in CRUD operations.
|
|
69
|
+
* @param {A} adapter - The HTTP adapter instance to use for API requests
|
|
70
|
+
* @param {Constructor<M>} [clazz] - Optional constructor for the model class
|
|
71
|
+
*/
|
|
72
|
+
constructor(adapter: A, clazz?: Constructor<M>);
|
|
73
|
+
toString(): string;
|
|
74
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Model } from "@decaf-ts/decorator-validation";
|
|
2
|
+
import { Constructor } from "@decaf-ts/decoration";
|
|
3
|
+
import { HttpAdapter } from "./adapter.js";
|
|
4
|
+
import { RestRepository } from "./RestRepository.js";
|
|
5
|
+
/**
|
|
6
|
+
* @description Service class for REST API operations
|
|
7
|
+
* @summary Provides a comprehensive implementation for interacting with REST APIs.
|
|
8
|
+
* This class implements CRUD operations for single and bulk operations, as well as
|
|
9
|
+
* the Observable pattern to notify observers of changes. It works with HTTP adapters
|
|
10
|
+
* to perform the actual API requests and handles model conversion.
|
|
11
|
+
* @template M - The model type, extending Model
|
|
12
|
+
* @template Q - The query type used by the adapter
|
|
13
|
+
* @template A - The HTTP adapter type, extending HttpAdapter
|
|
14
|
+
* @template F - The HTTP flags type, extending HttpFlags
|
|
15
|
+
* @template C - The context type, extending Context<F>
|
|
16
|
+
* @param {A} adapter - The HTTP adapter instance
|
|
17
|
+
* @param {Constructor<M>} [clazz] - Optional constructor for the model class
|
|
18
|
+
* @class RestService
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* // Create a service for User model with Axios adapter
|
|
22
|
+
* const axiosAdapter = new AxiosAdapter({
|
|
23
|
+
* protocol: 'https',
|
|
24
|
+
* host: 'api.example.com'
|
|
25
|
+
* });
|
|
26
|
+
* const userService = new RestService(axiosAdapter, User);
|
|
27
|
+
*
|
|
28
|
+
* // Create a new user
|
|
29
|
+
* const user = new User({ name: 'John Doe', email: 'john@example.com' });
|
|
30
|
+
* const createdUser = await userService.create(user);
|
|
31
|
+
*
|
|
32
|
+
* // Update a user
|
|
33
|
+
* createdUser.name = 'Jane Doe';
|
|
34
|
+
* const updatedUser = await userService.update(createdUser);
|
|
35
|
+
*
|
|
36
|
+
* // Delete a user
|
|
37
|
+
* await userService.delete(updatedUser.id);
|
|
38
|
+
* ```
|
|
39
|
+
* @mermaid
|
|
40
|
+
* sequenceDiagram
|
|
41
|
+
* participant Client
|
|
42
|
+
* participant Service as RestService
|
|
43
|
+
* participant Adapter as HttpAdapter
|
|
44
|
+
* participant API
|
|
45
|
+
* Client->>Service: create(model)
|
|
46
|
+
* Service->>Adapter: prepare(model, pk)
|
|
47
|
+
* Service->>Adapter: create(table, id, record)
|
|
48
|
+
* Adapter->>API: HTTP POST
|
|
49
|
+
* API-->>Adapter: 201 Created
|
|
50
|
+
* Adapter-->>Service: record
|
|
51
|
+
* Service-->>Client: revert(record)
|
|
52
|
+
*/
|
|
53
|
+
export declare class RestService<M extends Model, A extends HttpAdapter<any, any, any, any, any>, Q = A extends HttpAdapter<any, any, any, infer Q, any> ? Q : never> extends RestRepository<M, A, Q> {
|
|
54
|
+
protected _overrides: Partial<import("@decaf-ts/core").FlagsOf<import("@decaf-ts/core").ContextOf<A>>> & {
|
|
55
|
+
allowRawStatements: boolean;
|
|
56
|
+
forcePrepareSimpleQueries: boolean;
|
|
57
|
+
forcePrepareComplexQueries: boolean;
|
|
58
|
+
} & {
|
|
59
|
+
ignoreValidation: boolean;
|
|
60
|
+
ignoreHandlers: boolean;
|
|
61
|
+
allowRawStatements: boolean;
|
|
62
|
+
forcePrepareSimpleQueries: boolean;
|
|
63
|
+
forcePrepareComplexQueries: boolean;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* @description Initializes a new RestService instance
|
|
67
|
+
* @summary Creates a new service instance with the specified adapter and optional model class.
|
|
68
|
+
* The constructor stores the adapter and model class for later use in CRUD operations.
|
|
69
|
+
* @param {A} adapter - The HTTP adapter instance to use for API requests
|
|
70
|
+
* @param {Constructor<M>} [clazz] - Optional constructor for the model class
|
|
71
|
+
*/
|
|
72
|
+
constructor(adapter: A, clazz?: Constructor<M>);
|
|
73
|
+
toString(): string;
|
|
74
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { Adapter, AdapterFlags, Condition, Context, ContextualArgs, FlagsOf, MaybeContextualArg, Paginator, PreparedModel, PreparedStatement, Repository, Sequence, SequenceOptions, Statement } from "@decaf-ts/core";
|
|
2
|
+
import { BaseError, OperationKeys, PrimaryKeyType } from "@decaf-ts/db-decorators";
|
|
3
|
+
import { HttpConfig, HttpFlags, HttpMethod, HttpRequestOptions, HttpResponse } from "./types.cjs";
|
|
4
|
+
import { Model } from "@decaf-ts/decorator-validation";
|
|
5
|
+
import { Constructor } from "@decaf-ts/decoration";
|
|
6
|
+
export declare function suffixMethod(obj: any, before: (...args: any[]) => any, suffix: (...args: any[]) => any, beforeName?: string): void;
|
|
7
|
+
/**
|
|
8
|
+
* @description Abstract HTTP adapter for REST API interactions
|
|
9
|
+
* @summary Provides a base implementation for HTTP adapters with methods for CRUD operations,
|
|
10
|
+
* URL construction, and error handling. This class extends the core Adapter class and
|
|
11
|
+
* implements the necessary methods for HTTP communication. Concrete implementations
|
|
12
|
+
* must provide specific HTTP client functionality.
|
|
13
|
+
* @template Y - The native HTTP client type
|
|
14
|
+
* @template Q - The query type used by the adapter
|
|
15
|
+
* @template F - The HTTP flags type, extending HttpFlags
|
|
16
|
+
* @template C - The context type, extending Context<F>
|
|
17
|
+
* @param {Y} native - The native HTTP client instance
|
|
18
|
+
* @param {HttpConfig} config - Configuration for the HTTP adapter
|
|
19
|
+
* @param {string} flavour - The adapter flavor identifier
|
|
20
|
+
* @param {string} [alias] - Optional alias for the adapter
|
|
21
|
+
* @class HttpAdapter
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* // Example implementation with Axios
|
|
25
|
+
* class AxiosAdapter extends HttpAdapter<AxiosInstance, AxiosRequestConfig> {
|
|
26
|
+
* constructor(config: HttpConfig) {
|
|
27
|
+
* super(axios.create(), config, 'axios');
|
|
28
|
+
* }
|
|
29
|
+
*
|
|
30
|
+
* async request<V>(details: AxiosRequestConfig): Promise<V> {
|
|
31
|
+
* const response = await this.native.request(details);
|
|
32
|
+
* return response.data;
|
|
33
|
+
* }
|
|
34
|
+
*
|
|
35
|
+
* // Implement other abstract methods...
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare abstract class HttpAdapter<CONF extends HttpConfig, CON, REQ, Q extends PreparedStatement<any> = PreparedStatement<any>, C extends Context<HttpFlags> = Context<HttpFlags>> extends Adapter<CONF, CON, Q, C> {
|
|
40
|
+
protected constructor(config: CONF, flavour: string, alias?: string);
|
|
41
|
+
/**
|
|
42
|
+
* @description Generates operation flags with HTTP headers
|
|
43
|
+
* @summary Extends the base flags method to ensure HTTP headers exist on the flags payload.
|
|
44
|
+
* @template M - The model type
|
|
45
|
+
* @param {OperationKeys|string} operation - The type of operation being performed
|
|
46
|
+
* @param {Constructor | Constructor[]} model - The target model constructor(s)
|
|
47
|
+
* @param {Partial<FlagsOf<C>>} overrides - Optional flag overrides
|
|
48
|
+
* @param {...any[]} args - Additional arguments forwarded to the base implementation
|
|
49
|
+
* @return {Promise<FlagsOf<C>>} The flags object with headers
|
|
50
|
+
*/
|
|
51
|
+
protected flags<M extends Model>(operation: OperationKeys | string, model: Constructor<M> | Constructor<M>[], overrides: Partial<FlagsOf<C>>): Promise<FlagsOf<C>>;
|
|
52
|
+
protected Dispatch(): any;
|
|
53
|
+
protected getEventHeaders(): Promise<{}>;
|
|
54
|
+
/**
|
|
55
|
+
* @description Returns the repository constructor for this adapter
|
|
56
|
+
* @summary Provides the RestService class as the repository implementation for this HTTP adapter.
|
|
57
|
+
* This method is used to create repository instances that work with this adapter type.
|
|
58
|
+
* @template R - Repository subtype working with this adapter
|
|
59
|
+
* @return {Constructor<R>} The repository constructor
|
|
60
|
+
*/
|
|
61
|
+
repository<R extends Repository<any, Adapter<CONF, CON, Q, C>>>(): Constructor<R>;
|
|
62
|
+
/**
|
|
63
|
+
* @description Prepares a model for persistence
|
|
64
|
+
* @summary Converts a model instance into a format suitable for database storage,
|
|
65
|
+
* handling column mapping and separating transient properties
|
|
66
|
+
* @template M - The model type
|
|
67
|
+
* @param {M} model - The model instance to prepare
|
|
68
|
+
* @param pk - The primary key property name
|
|
69
|
+
* @param args
|
|
70
|
+
* @return The prepared data
|
|
71
|
+
*/
|
|
72
|
+
prepare<M extends Model>(model: M, ...args: ContextualArgs<C>): PreparedModel;
|
|
73
|
+
/**
|
|
74
|
+
* @description Converts database data back into a model instance
|
|
75
|
+
* @summary Reconstructs a model instance from database data, handling column mapping
|
|
76
|
+
* and reattaching transient properties
|
|
77
|
+
* @template M - The model type
|
|
78
|
+
* @param obj - The database record
|
|
79
|
+
* @param {string|Constructor<M>} clazz - The model class or name
|
|
80
|
+
* @param pk - The primary key property name
|
|
81
|
+
* @param {string|number|bigint} id - The primary key value
|
|
82
|
+
* @return {M} The reconstructed model instance
|
|
83
|
+
*/
|
|
84
|
+
revert<M extends Model>(obj: Record<string, any>, clazz: string | Constructor<M>, id: PrimaryKeyType, ...args: ContextualArgs<C>): M;
|
|
85
|
+
protected toTableName<M extends Model>(t: string | Constructor<M>): string;
|
|
86
|
+
protected toHeaders(ctx: C): {
|
|
87
|
+
[x: string]: any;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* @description Constructs a URL for API requests
|
|
91
|
+
* @summary Builds a complete URL for API requests using the configured protocol and host,
|
|
92
|
+
* the specified table name, and optional query parameters. The method handles URL encoding.
|
|
93
|
+
* @param {string | Constructor} tableName - The name of the table or endpoint
|
|
94
|
+
* @return {string} The encoded URL string
|
|
95
|
+
*/
|
|
96
|
+
url<M extends Model>(tableName: string | Constructor<M>): string;
|
|
97
|
+
/**
|
|
98
|
+
* @description Constructs a URL for API requests
|
|
99
|
+
* @summary Builds a complete URL for API requests using the configured protocol and host,
|
|
100
|
+
* the specified table name, and optional query parameters. The method handles URL encoding.
|
|
101
|
+
* @param {string | Constructor} tableName - The name of the table or endpoint
|
|
102
|
+
* @param {string[]} pathParams - Optional query parameters
|
|
103
|
+
* @return {string} The encoded URL string
|
|
104
|
+
*/
|
|
105
|
+
url<M extends Model>(tableName: string | Constructor<M>, pathParams: string[]): string;
|
|
106
|
+
/**
|
|
107
|
+
* @description Constructs a URL for API requests
|
|
108
|
+
* @summary Builds a complete URL for API requests using the configured protocol and host,
|
|
109
|
+
* the specified table name, and optional query parameters. The method handles URL encoding.
|
|
110
|
+
* @param {string | Constructor} tableName - The name of the table or endpoint
|
|
111
|
+
* @param {Record<string, string | number>} queryParams - Optional query parameters
|
|
112
|
+
* @return {string} The encoded URL string
|
|
113
|
+
*/
|
|
114
|
+
url<M extends Model>(tableName: string | Constructor<M>, queryParams: Record<string, string | number>): string;
|
|
115
|
+
url<M extends Model>(tableName: string | Constructor<M>, pathParams: string[], queryParams: Record<string, string | number>): string;
|
|
116
|
+
abstract toRequest(query: Q): REQ;
|
|
117
|
+
abstract toRequest(ctx: C): REQ;
|
|
118
|
+
abstract toRequest(query: Q, ctx: C): REQ;
|
|
119
|
+
abstract toRequest(method: HttpMethod, url: string, data?: unknown, options?: HttpRequestOptions): REQ;
|
|
120
|
+
abstract toRequest(ctxOrQueryOrMethod: C | Q | HttpMethod, ctxOrUrl?: C | string, data?: unknown, options?: HttpRequestOptions): REQ;
|
|
121
|
+
protected toHttpResponse<V = any, E = unknown>(parsed: any, raw?: any): HttpResponse<V, E>;
|
|
122
|
+
get<V = any, E = unknown>(url: string, options?: HttpRequestOptions, ...args: MaybeContextualArg<C>): Promise<HttpResponse<V, E>>;
|
|
123
|
+
post<V = any, E = unknown>(url: string, data: unknown, options?: HttpRequestOptions, ...args: MaybeContextualArg<C>): Promise<HttpResponse<V, E>>;
|
|
124
|
+
put<V = any, E = unknown>(url: string, data: unknown, options?: HttpRequestOptions, ...args: MaybeContextualArg<C>): Promise<HttpResponse<V, E>>;
|
|
125
|
+
/**
|
|
126
|
+
* @description Sends an HTTP request
|
|
127
|
+
* @summary Abstract method that must be implemented by subclasses to send HTTP requests
|
|
128
|
+
* using the native HTTP client. This is the core method for making API calls.
|
|
129
|
+
* @template V - The response value type
|
|
130
|
+
* @param {REQ} details - The request details specific to the HTTP client
|
|
131
|
+
* @return {Promise<V>} A promise that resolves with the response data
|
|
132
|
+
*/
|
|
133
|
+
abstract request<V>(details: REQ, ...args: MaybeContextualArg<C>): Promise<V>;
|
|
134
|
+
protected extractIdArgs<M extends Model>(model: Constructor<M> | string, id: PrimaryKeyType): string[];
|
|
135
|
+
parseResponse<M extends Model>(clazz: Constructor<M> | undefined, method: OperationKeys | string, res: any): any;
|
|
136
|
+
/**
|
|
137
|
+
* @description Creates a new resource
|
|
138
|
+
* @summary Abstract method that must be implemented by subclasses to create a new resource
|
|
139
|
+
* via HTTP. This typically corresponds to a POST request.
|
|
140
|
+
* @param {string} tableName - The name of the table or endpoint
|
|
141
|
+
* @param {string|number} id - The identifier for the resource
|
|
142
|
+
* @param {Record<string, any>} model - The data model to create
|
|
143
|
+
* @param {...any[]} args - Additional arguments
|
|
144
|
+
* @return {Promise<Record<string, any>>} A promise that resolves with the created resource
|
|
145
|
+
*/
|
|
146
|
+
create<M extends Model>(tableName: Constructor<M>, id: PrimaryKeyType, model: Record<string, any>, ...args: ContextualArgs<C>): Promise<Record<string, any>>;
|
|
147
|
+
createAll<M extends Model>(tableName: Constructor<M>, ids: PrimaryKeyType[], model: Record<string, any>[], ...args: ContextualArgs<C>): Promise<Record<string, any>[]>;
|
|
148
|
+
/**
|
|
149
|
+
* @description Retrieves a resource by ID
|
|
150
|
+
* @summary Abstract method that must be implemented by subclasses to retrieve a resource
|
|
151
|
+
* via HTTP. This typically corresponds to a GET request.
|
|
152
|
+
* @param {string} tableName - The name of the table or endpoint
|
|
153
|
+
* @param {string|number|bigint} id - The identifier for the resource
|
|
154
|
+
* @param {...any[]} args - Additional arguments
|
|
155
|
+
* @return {Promise<Record<string, any>>} A promise that resolves with the retrieved resource
|
|
156
|
+
*/
|
|
157
|
+
read<M extends Model>(tableName: Constructor<M>, id: PrimaryKeyType, ...args: ContextualArgs<C>): Promise<Record<string, any>>;
|
|
158
|
+
readAll<M extends Model>(tableName: Constructor<M>, ids: PrimaryKeyType[], ...args: ContextualArgs<C>): Promise<Record<string, any>[]>;
|
|
159
|
+
/**
|
|
160
|
+
* @description Updates an existing resource
|
|
161
|
+
* @summary Abstract method that must be implemented by subclasses to update a resource
|
|
162
|
+
* via HTTP. This typically corresponds to a PUT or PATCH request.
|
|
163
|
+
* @param {string} tableName - The name of the table or endpoint
|
|
164
|
+
* @param {string|number} id - The identifier for the resource
|
|
165
|
+
* @param {Record<string, any>} model - The updated data model
|
|
166
|
+
* @param {...any[]} args - Additional arguments
|
|
167
|
+
* @return {Promise<Record<string, any>>} A promise that resolves with the updated resource
|
|
168
|
+
*/
|
|
169
|
+
update<M extends Model>(tableName: Constructor<M>, id: string | number, model: Record<string, any>, ...args: ContextualArgs<C>): Promise<Record<string, any>>;
|
|
170
|
+
updateAll<M extends Model>(tableName: Constructor<M>, ids: PrimaryKeyType[], model: Record<string, any>[], ...args: ContextualArgs<C>): Promise<Record<string, any>[]>;
|
|
171
|
+
/**
|
|
172
|
+
* @description Deletes a resource by ID
|
|
173
|
+
* @summary Abstract method that must be implemented by subclasses to delete a resource
|
|
174
|
+
* via HTTP. This typically corresponds to a DELETE request.
|
|
175
|
+
* @param {string} tableName - The name of the table or endpoint
|
|
176
|
+
* @param {string|number|bigint} id - The identifier for the resource to delete
|
|
177
|
+
* @param {...any[]} args - Additional arguments
|
|
178
|
+
* @return {Promise<Record<string, any>>} A promise that resolves with the deletion result
|
|
179
|
+
*/
|
|
180
|
+
delete<M extends Model>(tableName: Constructor<M>, id: PrimaryKeyType, ...args: ContextualArgs<C>): Promise<Record<string, any>>;
|
|
181
|
+
delete<V = any>(url: string, options?: HttpRequestOptions, ...args: MaybeContextualArg<C>): Promise<HttpResponse<V>>;
|
|
182
|
+
deleteAll<M extends Model>(tableName: Constructor<M>, ids: PrimaryKeyType[], ...args: ContextualArgs<C>): Promise<Record<string, any>[]>;
|
|
183
|
+
/**
|
|
184
|
+
* @description Executes a raw query
|
|
185
|
+
* @summary Method for executing raw queries directly with the HTTP client.
|
|
186
|
+
* This method is not supported by default in HTTP adapters and throws an UnsupportedError.
|
|
187
|
+
* Subclasses can override this method to provide implementation.
|
|
188
|
+
* @template R - The result type
|
|
189
|
+
* @param {Q} rawInput - The raw query input
|
|
190
|
+
* @param {boolean} process - Whether to process the result
|
|
191
|
+
* @param {...any[]} args - Additional arguments
|
|
192
|
+
* @return {Promise<R>} A promise that resolves with the query result
|
|
193
|
+
* @throws {UnsupportedError} Always throws as this method is not supported by default
|
|
194
|
+
*/
|
|
195
|
+
raw<R>(rawInput: Q, ...args: ContextualArgs<C>): Promise<R>;
|
|
196
|
+
/**
|
|
197
|
+
* @description Creates a sequence
|
|
198
|
+
* @summary Method for creating a sequence for generating unique identifiers.
|
|
199
|
+
* This method is not supported by default in HTTP adapters and throws an UnsupportedError.
|
|
200
|
+
* Subclasses can override this method to provide implementation.
|
|
201
|
+
* @param {SequenceOptions} options - Options for creating the sequence
|
|
202
|
+
* @return {Promise<Sequence>} A promise that resolves with the created sequence
|
|
203
|
+
* @throws {UnsupportedError} Always throws as this method is not supported by default
|
|
204
|
+
*/
|
|
205
|
+
Sequence(options: SequenceOptions): Promise<Sequence>;
|
|
206
|
+
/**
|
|
207
|
+
* @description Creates a statement for querying
|
|
208
|
+
* @summary Method for creating a statement for building and executing queries.
|
|
209
|
+
* This method is not supported by default in HTTP adapters and throws an UnsupportedError.
|
|
210
|
+
* Subclasses can override this method to provide implementation.
|
|
211
|
+
* @template M - The model type
|
|
212
|
+
* @template ! - The raw query type
|
|
213
|
+
* @return {Statement<Q, M, any>} A statement object for building queries
|
|
214
|
+
* @throws {UnsupportedError} Always throws as this method is not supported by default
|
|
215
|
+
*/
|
|
216
|
+
Statement<M extends Model>(overrides?: Partial<AdapterFlags>): Statement<M, Adapter<CONF, CON, Q, C>, any>;
|
|
217
|
+
Paginator<M extends Model>(query: Q, size: number, clazz: Constructor<M>): Paginator<M, M, Q>;
|
|
218
|
+
/**
|
|
219
|
+
* @description Parses a condition into a query
|
|
220
|
+
* @summary Method for parsing a condition object into a query format understood by the HTTP client.
|
|
221
|
+
* This method is not supported by default in HTTP adapters and throws an UnsupportedError.
|
|
222
|
+
* Subclasses can override this method to provide implementation.
|
|
223
|
+
* @param {Condition<any>} condition - The condition to parse
|
|
224
|
+
* @return {Q} The parsed query
|
|
225
|
+
* @throws {UnsupportedError} Always throws as this method is not supported by default
|
|
226
|
+
*/
|
|
227
|
+
parseCondition(condition: Condition<any>): Q;
|
|
228
|
+
static parseError<E extends BaseError>(err: Error | string, ...args: any[]): E;
|
|
229
|
+
static decoration(): void;
|
|
230
|
+
}
|