6
6
} from "./sdk/models/operations" ;
7
7
import { SDKOptions } from "./lib/config" ;
8
8
import { HTTPClient } from "./lib/http" ;
9
+ import { RequestOptions as FetchOptions } from "./lib/sdks" ;
9
10
10
11
export type { Input , Result } ;
11
12
@@ -29,6 +30,13 @@ export type Options = {
29
30
sdk ?: SDKOptions ;
30
31
} ;
31
32
33
+ /** Extra per-request options for using the high-level SDK's
34
+ * evaluate/evaluateDefault methods.
35
+ */
36
+ export interface RequestOptions < Res > extends FetchOptions {
37
+ fromResult ?: ( res ?: Result ) => Res ;
38
+ }
39
+
32
40
/** OPAClient is the starting point for using the high-level API.
33
41
*
34
42
* Use {@link Opa} if you need some low-level customization.
@@ -60,50 +68,62 @@ export class OPAClient {
60
68
*
61
69
* @param path - The path to the policy, without `/v1/data`: use `authz/allow` to evaluate policy `data.authz.allow`.
62
70
* @param input - The input to the policy, if needed.
63
- * @param fromResult - A function that is used to transform the policy evaluation result (which could be `undefined`).
71
+ * @param opts - Per-request options to control how the policy evaluation result is to be transformed
72
+ * into `Res` (via `fromResult`), and low-level fetch options.
64
73
*/
65
74
async evaluate < In extends Input | ToInput , Res > (
66
75
path : string ,
67
76
input ?: In ,
68
- fromResult ?: ( res ?: Result ) => Res ,
77
+ opts ?: RequestOptions < Res > ,
69
78
) : Promise < Res > {
70
79
let result : ExecutePolicyWithInputResponse | ExecutePolicyResponse ;
71
80
72
81
if ( input === undefined ) {
73
- result = await this . opa . executePolicy ( { path } ) ;
82
+ result = await this . opa . executePolicy ( { path } , opts ) ;
74
83
} else {
75
84
let inp : Input ;
76
85
if ( implementsToInput ( input ) ) {
77
86
inp = input . toInput ( ) ;
78
87
} else {
79
88
inp = input ;
80
89
}
81
- result = await this . opa . executePolicyWithInput ( {
82
- path,
83
- requestBody : { input : inp } ,
84
- } ) ;
90
+ result = await this . opa . executePolicyWithInput (
91
+ {
92
+ path,
93
+ requestBody : { input : inp } ,
94
+ } ,
95
+ opts ,
96
+ ) ;
85
97
}
86
98
if ( ! result . successfulPolicyEvaluation ) throw `no result in API response` ;
87
99
const res = result . successfulPolicyEvaluation . result ;
100
+ const fromResult = opts ?. fromResult ;
88
101
return fromResult ? fromResult ( res ) : ( res as Res ) ;
89
102
}
90
103
91
104
/** `evaluateDefault` is used to evaluate the server's default policy with optional input.
92
105
*
93
106
* @param input - The input to the default policy, defaults to `{}`.
94
- * @param fromResult - A function that is used to transform the policy evaluation result (which could be `undefined`).
107
+ * @param opts - Per-request options to control how the policy evaluation result is to be transformed
108
+ * into `Res` (via `fromResult`), and low-level fetch options.
95
109
*/
96
110
async evaluateDefault < In extends Input | ToInput , Res > (
97
111
input ?: In ,
98
- fromResult ?: ( res ?: Result ) => Res ,
112
+ opts ?: RequestOptions < Res > ,
99
113
) : Promise < Res > {
100
114
let inp = input ?? { } ;
101
115
if ( implementsToInput ( inp ) ) {
102
116
inp = inp . toInput ( ) ;
103
117
}
104
- const resp = await this . opa . executeDefaultPolicyWithInput ( inp ) ;
118
+ const resp = await this . opa . executeDefaultPolicyWithInput (
119
+ inp ,
120
+ undefined , // pretty
121
+ undefined , // gzipEncoding
122
+ opts ,
123
+ ) ;
105
124
if ( ! resp . result ) throw `no result in API response` ;
106
125
const res = resp . result ;
126
+ const fromResult = opts ?. fromResult ;
107
127
return fromResult ? fromResult ( res ) : ( res as Res ) ;
108
128
}
109
129
}
0 commit comments