Adrià Vilanova MartÃnez | 9c418ab | 2024-12-05 15:34:40 +0100 | [diff] [blame] | 1 | export default class FetchBody { |
2 | constructor(private body: RequestInit['body'] | undefined) {} | ||||
3 | |||||
4 | async getJSONRequestBody() { | ||||
5 | if (!this.body) return undefined; | ||||
6 | |||||
7 | // Using Response is a hack, but it works and converts a possibly long code | ||||
8 | // into a one-liner :D | ||||
9 | // Source of inspiration: https://stackoverflow.com/a/72718732 | ||||
10 | return await new Response(this.body).json(); | ||||
11 | } | ||||
12 | } |