Joydip Roy
Joydip Roy
HHono
Created by Benno on 8/28/2024 in #help
Enforcing strict types for Hono's c.json() responses?
5 replies
HHono
Created by FBI_kingmohbil on 8/24/2024 in #help
how can I specify the return type of the response of API response
You can't apply any custom type for custom middleware. Here is the reason, Unlike the previous solution, the router has the 'HandlerInterface' type, H<E2, P, I, R>. Here R is HandlerResponse<any> which uses TypedResponse<>. That's why a custom response can be attached. But the middleware return typed MiddlewareHandler<E, P, I> where E stands for Environment, P stands for Path (always string), and I stands for Input which can be further typed for inand out If you see the MiddlewareHandler it doesn't have R so it can't be extended or can't be applied any custom type for return.
9 replies
HHono
Created by ceifa on 8/21/2024 in #help
[RPC] is it possible to send a Blob on the body or I really need to use formdata in this case?
No description
2 replies
HHono
Created by FBI_kingmohbil on 8/24/2024 in #help
how can I specify the return type of the response of API response
Got it, in that case, you can do following
import { Hono, TypedResponse } from 'hono'

interface Response<T> {
message: string;
data: T;
}

const app = new Hono()

app.get('/', (c): TypedResponse<Response<string>, 200> => {
return c.json({
message: 'success',
data: 'Hello Hono!'
})
})
import { Hono, TypedResponse } from 'hono'

interface Response<T> {
message: string;
data: T;
}

const app = new Hono()

app.get('/', (c): TypedResponse<Response<string>, 200> => {
return c.json({
message: 'success',
data: 'Hello Hono!'
})
})
9 replies
HHono
Created by ./xehbit.sh on 8/22/2024 in #help
c.env.incoming already piped with an await?
No description
2 replies
HHono
Created by bombillazo on 8/22/2024 in #help
Cannot load `hono` from JSR
This is looks like internet connection issue
2 replies
HHono
Created by jamesfoley on 8/23/2024 in #help
Help Understanding Cache Middleware
How does Hono handle caching? Hono uses the Cache-Control header to determine how to cache requests and responses. This header is included in the communication between your server and client (browser) and specifies how long a response should be stored and reused. By default, Hono doesn't automatically cache requests or responses on the server side. Caching is something you, the developer, need to explicitly enable using the cache middleware. What is the cache middleware helper for? The cache middleware is a tool that helps you implement caching logic in your Hono application. It doesn't directly cache data on the client side, but it allows you to set Cache-Control headers in your responses, which can then be used by the client (browser) to cache content. Can I access cached information on the server? Hono doesn't currently provide a built-in way to directly access cached information on the server. However, you can set up caching with expiry times and let the browser handle caching for you. How does serveStatic handle caching? The serveStatic method itself doesn't implement caching by default. However, you can use the cache middleware in conjunction with serveStatic to set appropriate Cache-Control headers for static assets like HTML, CSS, images, and JavaScript files. Do I need to create cache headers for static assets? Yes, if you want to control how static assets are cached on both the server and client, you'll need to create Cache-Control headers using the cache middleware. This allows you to specify how long these assets should be stored and reused before prefetching from the server. In summary: - Hono relies on Cache-Control headers for caching. - Caching is not enabled by default; you need the cache middleware. - The middleware helps set cache headers, not directly cache data on clients. - You cannot directly access cached data on the server (yet). - Use cache middleware with serveStatic to control the caching of static assets.
3 replies
HHono
Created by FBI_kingmohbil on 8/24/2024 in #help
how can I specify the return type of the response of API response
type JSONRespondReturn<
T extends JSONValue | SimplifyDeepArray<unknown> | InvalidJSONValue,
U extends StatusCode
> = Response &
TypedResponse<
SimplifyDeepArray<T> extends JSONValue
? JSONValue extends SimplifyDeepArray<T>
? never
: JSONParsed<T>
: never,
U,
'json'
>
type JSONRespondReturn<
T extends JSONValue | SimplifyDeepArray<unknown> | InvalidJSONValue,
U extends StatusCode
> = Response &
TypedResponse<
SimplifyDeepArray<T> extends JSONValue
? JSONValue extends SimplifyDeepArray<T>
? never
: JSONParsed<T>
: never,
U,
'json'
>
- This type is designed to automatically infer and define the data types used within Hono applications. - It eliminates the need for manual type declarations, making development more efficient and reducing the risk of type-related errors. - Hono analyzes the structure and content of your data to determine its underlying types. - This includes identifying objects, arrays, primitives (like strings, numbers, booleans), and more complex structures. - Hono infers the appropriate TypeScript types for your data based on the analysis. For example, if your data contains an object with certain properties and values, Hono might infer an interface type that describes those properties and their types.
9 replies