ky icon indicating copy to clipboard operation
ky copied to clipboard

HTTPError json as unknown/type

Open mfulton26 opened this issue 1 year ago • 3 comments

I like how for requests I can chain .json<Foo>() or .json() (for unknown instead of any).

Can this same functionality be added when grabbing an HTTP response body from an HTTPError instance?

mfulton26 avatar May 01 '24 13:05 mfulton26

I think so, but to be sure, can you provide a full usage example?

sholladay avatar Jun 24 '24 19:06 sholladay

Condensed example

Instead of

try {
  // some ky call
} catch (e) {
  const { msg } = (await e.response.json()) as { msg: string };
  // do something with the parsed error response payload
}

I want to be able to write

try {
  // some ky call
} catch (e) {
  const { msg } = await e.response.json<{ msg: string }>();
  // do something with the parsed error response payload
}

so that the I don't need to wrap await e.response.json() inside parameters and explicitly cast

@@ -1,6 +1,6 @@
 try {
   // some ky call
 } catch (e) {
-  const { msg } = (await e.response.json()) as { msg: string };
+  const { msg } = await e.response.json<{ msg: string }>();
   // do something with the parsed error response payload
 }

Longer example

Instead of

import ky, { BeforeRequestHook, HTTPError } from "npm:ky";

function notFound(): BeforeRequestHook {
  return (request, options) => {
    const response = Response.json({ msg: "Not Found" }, {
      status: 404,
    });
    throw new HTTPError(response, request, options);
  };
}

async function updatePostBody(id: string, body: string) {
  try {
    const url = `https://www.example.com/posts/${encodeURIComponent(id)}`;
    const hooks = { beforeRequest: [notFound()] };
    return await ky.put(url, { json: { id, body }, hooks })
      .json<{ id: number; title: string; body: string; userId: number }>();
  } catch (e) {
    if (!(e instanceof HTTPError)) throw e;
    const { msg } = (await e.response.json()) as { msg: string };
    throw Error(`Update failed: ${msg}`);
  }
}

await updatePostBody(crypto.randomUUID(), "42");

I want to be able to change (await e.response.json()) as { msg: string } to await e.response.json<{ msg: string }>() so that I don't need to wrap await e.response.json() inside parentheses and then cast.

mfulton26 avatar Jun 24 '24 20:06 mfulton26

Yeah, makes sense.

I believe all that is needed is to update HTTPError here:

https://github.com/sindresorhus/ky/blob/36d0bd3ed3a3d3d915536988311fd046fa325e8c/source/errors/HTTPError.ts#L5

… to use the KyResponse type instead of Response:

https://github.com/sindresorhus/ky/blob/36d0bd3ed3a3d3d915536988311fd046fa325e8c/source/types/response.ts#L1

sholladay avatar Jun 24 '24 20:06 sholladay