diff options
Diffstat (limited to 'shared/extra-utils/requests')
-rw-r--r-- | shared/extra-utils/requests/requests.ts | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts index 70f790222..e3ecd1af2 100644 --- a/shared/extra-utils/requests/requests.ts +++ b/shared/extra-utils/requests/requests.ts | |||
@@ -121,6 +121,20 @@ function unwrapText (test: request.Test): Promise<string> { | |||
121 | return test.then(res => res.text) | 121 | return test.then(res => res.text) |
122 | } | 122 | } |
123 | 123 | ||
124 | function unwrapBodyOrDecodeToJSON <T> (test: request.Test): Promise<T> { | ||
125 | return test.then(res => { | ||
126 | if (res.body instanceof Buffer) { | ||
127 | return JSON.parse(new TextDecoder().decode(res.body)) | ||
128 | } | ||
129 | |||
130 | return res.body | ||
131 | }) | ||
132 | } | ||
133 | |||
134 | function unwrapTextOrDecode (test: request.Test): Promise<string> { | ||
135 | return test.then(res => res.text || new TextDecoder().decode(res.body)) | ||
136 | } | ||
137 | |||
124 | // --------------------------------------------------------------------------- | 138 | // --------------------------------------------------------------------------- |
125 | 139 | ||
126 | export { | 140 | export { |
@@ -134,6 +148,8 @@ export { | |||
134 | makeRawRequest, | 148 | makeRawRequest, |
135 | makeActivityPubGetRequest, | 149 | makeActivityPubGetRequest, |
136 | unwrapBody, | 150 | unwrapBody, |
151 | unwrapTextOrDecode, | ||
152 | unwrapBodyOrDecodeToJSON, | ||
137 | unwrapText | 153 | unwrapText |
138 | } | 154 | } |
139 | 155 | ||