X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=shared%2Fextra-utils%2Frequests%2Frequests.ts;h=5a2b09dccf95d14981bd17feffce745235cd9526;hb=40e7ed0714f96c01e16de3ac971a4b28116294e1;hp=70f7902222d2fb795fe1670e9c2a6068c61f8daa;hpb=9e8789497377cac5554a622da605f5b89587aa9c;p=github%2FChocobozzz%2FPeerTube.git diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts index 70f790222..5a2b09dcc 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 { return test.then(res => res.text) } +function unwrapBodyOrDecodeToJSON (test: request.Test): Promise { + return test.then(res => { + if (res.body instanceof Buffer) { + return JSON.parse(new TextDecoder().decode(res.body)) + } + + return res.body + }) +} + +function unwrapTextOrDecode (test: request.Test): Promise { + return test.then(res => res.text || new TextDecoder().decode(res.body)) +} + // --------------------------------------------------------------------------- export { @@ -134,6 +148,8 @@ export { makeRawRequest, makeActivityPubGetRequest, unwrapBody, + unwrapTextOrDecode, + unwrapBodyOrDecodeToJSON, unwrapText } @@ -169,7 +185,7 @@ function buildFields (req: request.Test, fields: { [ fieldName: string ]: any }, if (fields[key] === undefined) continue if (Array.isArray(fields[key]) && fields[key].length === 0) { - req.field(key, null) + req.field(key, []) continue }