@@ -47,6 +47,8 @@ export type AccessTokens = {
47
47
* Can be passed to HttpApi instance as {@link IHttpOpts.tokenRefreshFunction} during client creation {@link ICreateClientOpts}
48
48
*/
49
49
export type TokenRefreshFunction = ( refreshToken : string ) => Promise < AccessTokens > ;
50
+
51
+ /** Options object for `FetchHttpApi` and {@link MatrixHttpApi}. */
50
52
export interface IHttpOpts {
51
53
fetchFn ?: typeof globalThis . fetch ;
52
54
@@ -67,24 +69,20 @@ export interface IHttpOpts {
67
69
tokenRefreshFunction ?: TokenRefreshFunction ;
68
70
useAuthorizationHeader ?: boolean ; // defaults to true
69
71
72
+ /**
73
+ * Normally, methods in `FetchHttpApi` will return a {@link https://developer.mozilla.org/en-US/docs/Web/API/Response Response} object.
74
+ * If this is set to `true`, they instead return the response body.
75
+ */
70
76
onlyData ?: boolean ;
77
+
71
78
localTimeoutMs ?: number ;
72
79
73
80
/** Optional logger instance. If provided, requests and responses will be logged. */
74
81
logger ?: Logger ;
75
82
}
76
83
77
- export interface IRequestOpts extends Pick < RequestInit , "priority" > {
78
- /**
79
- * The alternative base url to use.
80
- * If not specified, uses this.opts.baseUrl
81
- */
82
- baseUrl ?: string ;
83
- /**
84
- * The full prefix to use e.g.
85
- * "/_matrix/client/v2_alpha". If not specified, uses this.opts.prefix.
86
- */
87
- prefix ?: string ;
84
+ /** Options object for `FetchHttpApi.requestOtherUrl`. */
85
+ export interface BaseRequestOpts extends Pick < RequestInit , "priority" > {
88
86
/**
89
87
* map of additional request headers
90
88
*/
@@ -96,7 +94,48 @@ export interface IRequestOpts extends Pick<RequestInit, "priority"> {
96
94
*/
97
95
localTimeoutMs ?: number ;
98
96
keepAlive ?: boolean ; // defaults to false
99
- json ?: boolean ; // defaults to true
97
+
98
+ /**
99
+ * By default, we will:
100
+ *
101
+ * * If the `body` is an object, JSON-encode it and set `Content-Type: application/json` in the
102
+ * request headers (unless overridden by {@link headers}).
103
+ *
104
+ * * Set `Accept: application/json` in the request headers (again, unless overridden by {@link headers}).
105
+ *
106
+ * * If `IHTTPOpts.onlyData` is set to `true` on the `FetchHttpApi` instance, parse the response as
107
+ * JSON and return the parsed response.
108
+ *
109
+ * Setting this to `false` inhibits all three behaviors, and (if `IHTTPOpts.onlyData` is set to `true`) the response
110
+ * is instead parsed as a UTF-8 string. It defaults to `true`, unless {@link rawResponseBody} is set.
111
+ *
112
+ * @deprecated Instead of setting this to `false`, set {@link rawResponseBody} to `true`.
113
+ */
114
+ json ?: boolean ;
115
+
116
+ /**
117
+ * Setting this to `true` does two things:
118
+ *
119
+ * * Inhibits the automatic addition of `Accept: application/json` in the request headers.
120
+ *
121
+ * * Assuming `IHTTPOpts.onlyData` is set to `true` on the `FetchHttpApi` instance, causes the
122
+ * raw response to be returned as a {@link https://developer.mozilla.org/en-US/docs/Web/API/Blob|Blob}
123
+ * instead of parsing it as `json`.
124
+ */
125
+ rawResponseBody ?: boolean ;
126
+ }
127
+
128
+ export interface IRequestOpts extends BaseRequestOpts {
129
+ /**
130
+ * The alternative base url to use.
131
+ * If not specified, uses this.opts.baseUrl
132
+ */
133
+ baseUrl ?: string ;
134
+ /**
135
+ * The full prefix to use e.g.
136
+ * "/_matrix/client/v2_alpha". If not specified, uses this.opts.prefix.
137
+ */
138
+ prefix ?: string ;
100
139
101
140
// Set to true to prevent the request function from emitting a Session.logged_out event.
102
141
// This is intended for use on endpoints where M_UNKNOWN_TOKEN is a valid/notable error response,
0 commit comments