ky icon indicating copy to clipboard operation
ky copied to clipboard

Content-Type detection

Open fregante opened this issue 10 months ago • 3 comments

I'm manually doing content detection:

const response = await ky
  .post(tokenURL, {
    body: formData,
  })

switch (response.headers.get("Content-Type")) {
  case "application/json": {
    const parsed: AuthData = await response.json();
    // etc
  }

  case "application/x-www-form-urlencoded": {
    const parsed = await response.formData();
    // etc
  }

  default: {
    throw new TypeError(
      "Error getting OAuth2 token: unexpected response format",
    );
  }
}

Would it make sense to have an .auto() parser that deals with the content type automatically? JSON, FormData, Blob, etc. I think that's what Axios does for example (but it returns unknown as a type)

fregante avatar Mar 25 '24 04:03 fregante