-
Notifications
You must be signed in to change notification settings - Fork 1
/
tapjaw-adapter.ts
63 lines (58 loc) · 2.08 KB
/
tapjaw-adapter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* @module TapjawAdapter
*/
import TapjawMessage from '../messages/tapjaw-message';
/**
* @deprecated
*/
export type TapjawAdapterArguments = Record<string, unknown>;
/**
* The default yielding method for a `TapjawAdapter` class.
*
* @ref {@link TapjawAdapter}
*/
export type TapjawAdapterCallback<T extends TapjawMessage> = () => AsyncGenerator<T>;
/**
* An abstract method type used in the `TapjawAdapter` for defining methods.
*
* @ref {@link TapjawAdapter}
*/
export type GenericMethod = () => unknown;
/**
* Default TapjawAdapter type, used as a link between a {@link TapjawApiCommand} and a {@link TapjawConnector} to handle
* incoming payloads from the {@link TapjawConnector}.
*
* The Adapter implementation gets designed to understand the schema from a {@link TapjawConnector} response, allowing
* the adapter to iterate and yield each entity from the payload to a defined {@link TapjawAdapterCallback} callback
* on the {@link TapjawApiCommand}.
*
* Each Adapter class created **must** include at least one {@link TapjawAdapterCallback} method to yield
* the results of the {@link TapjawConnector} payload.
*
* For example:
* ```
* class MyAdapter implements TapjawAdapter<MyAdapter, MyTapjawMessage> {
* construtor(private readonly connector: TapjawConnector) {}
*
* // TapjawAdapterCallback method...
* protected async * getMesssages(): AsyncGenerator<MyTapjawMessage> {
* const response = await this.connector.getSomething();
* // validate response...
* for (const item of response.items) {
* // Yield to TapjawApiCommand configured iterator.
* yield new MyAdapterMessage('...', item);
* }
* }
*
* // Generic methods...
* public something(): string {
* return 'something';
* }
* }
* ```
* Please refer to {@link TapjawApiCommand} to understand how a {@link TapjawAdapter} is used.
*/
type TapjawAdapter<T, U extends TapjawMessage> = {
[P in keyof T]: TapjawAdapterCallback<U> | GenericMethod | number | boolean | string | unknown[];
};
export default TapjawAdapter;