Comments (3)
Yeah, makes sense.
I believe all that is needed is to update HTTPError
here:
Line 5 in 36d0bd3
… to use the KyResponse
type instead of Response
:
Line 1 in 36d0bd3
from ky.
I think so, but to be sure, can you provide a full usage example?
from ky.
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.
from ky.
Related Issues (20)
- Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'headers') HOT 2
- beforeError hook isn't called on 500 responses HOT 4
- how to send binary data? HOT 1
- Error: unsupported BodyInit type
- retry not working? HOT 1
- Allow extend() to append to prefixUrl HOT 5
- Refresh token with ky: 2 calls instead of 1 HOT 2
- [TypeError: signal.throwIfAborted is not a function (it is undefined)] in React Native context HOT 5
- feat: consider cookiejar support HOT 4
- Set TimeoutError#cause to be Request or URL HOT 2
- New request in afterResponse hook with formData content HOT 1
- How to download files from external host
- Allow for logging network errors and JSON parse errors HOT 5
- retry and beforeRetry not triggered HOT 7
- Use `X-RateLimit-Reset` header to determine retry delay HOT 9
- Consider extracting the HTTPError logic to an external module HOT 4
- Add generic for HTTPError to type the response HOT 2
- Aggregated error on server side. HOT 1
- [Question] Does it support HTTP/2 ? (compraison with Got) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ky.