@@ -20,11 +20,14 @@ import { KeyValueType } from "../../../types/KeyValue.type";
20
20
import { StateQueryType } from "../../../types/state/StateQuery.type" ;
21
21
import { StateQueryResponseType } from "../../../types/state/StateQueryResponse.type" ;
22
22
import { StateGetBulkOptions } from "../../../types/state/StateGetBulkOptions.type" ;
23
- import { createHTTPMetadataQueryParam } from "../../../utils/Client.util" ;
23
+ import { createHTTPQueryParam , getStateConcurrencyValue , getStateConsistencyValue } from "../../../utils/Client.util" ;
24
24
import { Settings } from "../../../utils/Settings.util" ;
25
25
import { Logger } from "../../../logger/Logger" ;
26
26
import { StateSaveResponseType } from "../../../types/state/StateSaveResponseType" ;
27
27
import { StateSaveOptions } from "../../../types/state/StateSaveOptions.type" ;
28
+ import { StateDeleteOptions } from "../../../types/state/StateDeleteOptions.type" ;
29
+ import { THTTPExecuteParams } from "../../../types/http/THTTPExecuteParams.type" ;
30
+ import { StateGetOptions } from "../../../types/state/StateGetOptions.type" ;
28
31
29
32
// https://docs.dapr.io/reference/api/state_api/
30
33
export default class HTTPClientState implements IClientState {
@@ -41,7 +44,15 @@ export default class HTTPClientState implements IClientState {
41
44
stateObjects : KeyValuePairType [ ] ,
42
45
options : StateSaveOptions = { } ,
43
46
) : Promise < StateSaveResponseType > {
44
- const queryParams = createHTTPMetadataQueryParam ( options . metadata ) ;
47
+ const queryParams = createHTTPQueryParam ( { data : options ?. metadata , type : "metadata" } ) ;
48
+
49
+ for ( const so of stateObjects ) {
50
+ const behavior = {
51
+ consistency : getStateConsistencyValue ( so ?. options ?. consistency ) ,
52
+ concurrency : getStateConcurrencyValue ( so ?. options ?. concurrency ) ,
53
+ } ;
54
+ so . options = Object . assign ( { } , so . options , behavior ) ;
55
+ }
45
56
46
57
try {
47
58
await this . client . execute ( `/state/${ storeName } ?${ queryParams } ` , {
@@ -56,36 +67,72 @@ export default class HTTPClientState implements IClientState {
56
67
return { } ;
57
68
}
58
69
59
- async get ( storeName : string , key : string ) : Promise < KeyValueType | string > {
60
- const result = await this . client . execute ( `/state/${ storeName } /${ key } ` ) ;
70
+ async get ( storeName : string , key : string , options ?: Partial < StateGetOptions > ) : Promise < KeyValueType | string > {
71
+ const behavior = {
72
+ consistency : getStateConsistencyValue ( options ?. consistency ) ,
73
+ } ;
74
+
75
+ const queryParams = createHTTPQueryParam ( { data : options ?. metadata , type : "metadata" } , { data : behavior } ) ;
76
+
77
+ const result = await this . client . execute ( `/state/${ storeName } /${ key } ?${ queryParams } ` ) ;
78
+
61
79
return result as KeyValueType ;
62
80
}
63
81
64
- async getBulk ( storeName : string , keys : string [ ] , options : StateGetBulkOptions = { } ) : Promise < KeyValueType [ ] > {
65
- const queryParams = createHTTPMetadataQueryParam ( options . metadata ) ;
82
+ async getBulk ( storeName : string , keys : string [ ] , options ? : StateGetBulkOptions ) : Promise < KeyValueType [ ] > {
83
+ const queryParams = createHTTPQueryParam ( { data : options ? .metadata , type : "metadata" } ) ;
66
84
67
85
const result = await this . client . execute ( `/state/${ storeName } /bulk?${ queryParams } ` , {
68
86
method : "POST" ,
69
87
body : {
70
88
keys,
71
- parallelism : options . parallelism ?? Settings . getDefaultStateGetBulkParallelism ,
89
+ parallelism : options ? .parallelism ?? Settings . getDefaultStateGetBulkParallelism ,
72
90
} ,
73
91
} ) ;
74
92
75
93
return result as KeyValueType [ ] ;
76
94
}
77
95
78
- async delete ( storeName : string , key : string ) : Promise < void > {
79
- await this . client . execute ( `/state/${ storeName } /${ key } ` , {
80
- method : "DELETE" ,
81
- } ) ;
96
+ async delete ( storeName : string , key : string , options ?: Partial < StateDeleteOptions > ) : Promise < StateSaveResponseType > {
97
+ const behavior = {
98
+ concurrency : getStateConcurrencyValue ( options ?. concurrency ) ,
99
+ consistency : getStateConsistencyValue ( options ?. consistency ) ,
100
+ } ;
101
+
102
+ const queryParams = createHTTPQueryParam ( { data : options ?. metadata , type : "metadata" } , { data : behavior } ) ;
103
+
104
+ // Managed headers
105
+ const headers : THTTPExecuteParams [ "headers" ] = { } ;
106
+ if ( options ?. etag ) {
107
+ headers [ "If-Match" ] = options . etag ;
108
+ }
109
+
110
+ try {
111
+ await this . client . execute ( `/state/${ storeName } /${ key } ?${ queryParams } ` , {
112
+ method : "DELETE" ,
113
+ headers,
114
+ } ) ;
115
+ } catch ( e : any ) {
116
+ this . logger . error ( `Error deleting state from store ${ storeName } , error: ${ e } ` ) ;
117
+ return { error : e } ;
118
+ }
119
+
120
+ return { } ;
82
121
}
83
122
84
123
async transaction (
85
124
storeName : string ,
86
125
operations : OperationType [ ] = [ ] ,
87
126
metadata : IRequestMetadata | null = null ,
88
127
) : Promise < void > {
128
+ for ( const op of operations ) {
129
+ const behavior = {
130
+ consistency : getStateConsistencyValue ( op ?. request ?. options ?. consistency ) ,
131
+ concurrency : getStateConcurrencyValue ( op ?. request . options ?. concurrency ) ,
132
+ } ;
133
+ op . request . options = Object . assign ( { } , op . request . options , behavior ) ;
134
+ }
135
+
89
136
await this . client . execute ( `/state/${ storeName } /transaction` , {
90
137
method : "POST" ,
91
138
body : {
0 commit comments