-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
DaprStateStore.impl.ts
109 lines (89 loc) · 3.19 KB
/
DaprStateStore.impl.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { join } from 'node:path'
import type { ObjectWithKeysFromStringArray, StoreBaseConfig } from '@purista/core'
import { HttpClient, StateStoreBaseClass } from '@purista/core'
import type { DaprClientConfig } from '../DaprClient/index.js'
import { getDefaultClientConfig } from '../DaprClient/index.js'
import { DAPR_API_VERSION } from '../types/index.js'
import { puristaVersion } from '../version.js'
import type { DaprStateStoreConfig } from './types/index.js'
/**
* DaprStateStore is an adapter which connects to the state store provided by the underlaying Dapr infrastructure
*/
export class DaprStateStore extends StateStoreBaseClass<DaprStateStoreConfig> {
private client: HttpClient<DaprClientConfig>
constructor(config?: StoreBaseConfig<DaprStateStoreConfig>) {
super(config?.stateStoreName ?? 'DaprStateStore', { ...config })
const logger = this.logger
const conf = {
stateStoreName: 'stateStore',
logger,
...config,
clientConfig: {
...getDefaultClientConfig(),
...config?.clientConfig,
},
}
let baseUrl = `${conf.clientConfig.daprHost}:${conf.clientConfig.daprPort}`
if (!baseUrl.startsWith('http://') && !baseUrl.startsWith('https://')) {
baseUrl = `http://${baseUrl}`
}
const defaultHeaders: Record<string, string> = {
'content-type': 'application/json; charset=utf-8',
}
if (conf.clientConfig.daprApiToken) {
defaultHeaders['dapr-api-token'] = conf.clientConfig.daprApiToken
defaultHeaders['user-agent'] = `purista-dapr-client/v${puristaVersion} http/1`
}
this.client = new HttpClient<DaprClientConfig>({
logger,
baseUrl,
defaultHeaders,
...conf.clientConfig,
})
}
protected async getStateImpl<StateNames extends string[]>(
...stateNames: StateNames
): Promise<ObjectWithKeysFromStringArray<StateNames>> {
const fetchStatesFromStore = async (stateName: string) => {
const path = join(
this.config.clientConfig?.daprApiToken ?? DAPR_API_VERSION,
'state',
this.config.stateStoreName as string,
stateName,
)
const query: Record<string, string> = {
'metadata.contentType': 'application/json',
}
return this.client.get<string>(path, { query })
}
const result = await Promise.all(stateNames.map((stateName) => fetchStatesFromStore(stateName)))
const returnValue: Record<string, string> = {}
stateNames.forEach((value, index) => {
returnValue[value] = result[index]
})
return returnValue as ObjectWithKeysFromStringArray<StateNames>
}
protected async setStateImpl(stateName: string, stateValue: unknown) {
const path = join(
this.config.clientConfig?.daprApiToken ?? DAPR_API_VERSION,
'state',
this.config.stateStoreName as string,
)
const payload = [
{
key: stateName,
value: stateValue,
},
]
await this.client.post(path, payload)
}
protected async removeStateImpl(stateName: string) {
const path = join(
this.config.clientConfig?.daprApiToken ?? DAPR_API_VERSION,
'state',
this.config.stateStoreName as string,
stateName,
)
await this.client.delete(path)
}
}