ronan(wighawag)
ronan(wighawag)
Explore posts from servers
HHono
Created by ronan(wighawag) on 10/29/2024 in #help
any way to have different .notFound depending on parent path ?
anyone ?
2 replies
HHono
Created by ronan(wighawag) on 10/28/2024 in #help
adding generic error output type to App type
wanted to have the error happen only when response is not ok, but did not managed
5 replies
HHono
Created by ronan(wighawag) on 10/28/2024 in #help
adding generic error output type to App type
Got it simplified to
type WithErrorResponse<T extends Endpoint, ErrorType> = T extends {output: infer O}
? O extends string | number | boolean | null | undefined
? Omit<T, 'output'> & {output: O | ErrorType}
: Omit<T, 'output'> & {output: O | ErrorType}
: T;
type AddToAllSchemaOutputs<S extends Schema, ErrorType> = {
[K in keyof S]: {
[M in keyof S[K]]: S[K][M] extends Endpoint ? WithErrorResponse<S[K][M], ErrorType> : never;
};
};
export type AddToAllOutputs<T extends HonoBase<any, any>, ErrorType> =
T extends HonoBase<infer E, infer S> ? HonoBase<E, AddToAllSchemaOutputs<S, ErrorType>> : T;
type WithErrorResponse<T extends Endpoint, ErrorType> = T extends {output: infer O}
? O extends string | number | boolean | null | undefined
? Omit<T, 'output'> & {output: O | ErrorType}
: Omit<T, 'output'> & {output: O | ErrorType}
: T;
type AddToAllSchemaOutputs<S extends Schema, ErrorType> = {
[K in keyof S]: {
[M in keyof S[K]]: S[K][M] extends Endpoint ? WithErrorResponse<S[K][M], ErrorType> : never;
};
};
export type AddToAllOutputs<T extends HonoBase<any, any>, ErrorType> =
T extends HonoBase<infer E, infer S> ? HonoBase<E, AddToAllSchemaOutputs<S, ErrorType>> : T;
5 replies
HHono
Created by ronan(wighawag) on 10/28/2024 in #help
adding generic error output type to App type
but maybe hono should handle it automatically somehow ?
5 replies
HHono
Created by ronan(wighawag) on 10/28/2024 in #help
adding generic error output type to App type
I got something working with
type WithErrorResponse<T, ErrorType> = T extends {output: infer O}
? O extends string | number | boolean | null | undefined
? Omit<T, 'output'> & {output: O | ErrorType}
: Omit<T, 'output'> & {output: O | ErrorType}
: T;

export type AddToAllOutputs<T, ErrorType> =
T extends HonoBase<infer E, infer S>
? HonoBase<
E,
{
[K in keyof S]: S[K] extends {[method: string]: any}
? {
[M in keyof S[K]]: M extends `$${string}` ? WithErrorResponse<S[K][M], ErrorType> : S[K][M];
}
: S[K] extends object
? AddToAllOutputs<S[K], ErrorType>
: S[K];
}
>
: T;
type WithErrorResponse<T, ErrorType> = T extends {output: infer O}
? O extends string | number | boolean | null | undefined
? Omit<T, 'output'> & {output: O | ErrorType}
: Omit<T, 'output'> & {output: O | ErrorType}
: T;

export type AddToAllOutputs<T, ErrorType> =
T extends HonoBase<infer E, infer S>
? HonoBase<
E,
{
[K in keyof S]: S[K] extends {[method: string]: any}
? {
[M in keyof S[K]]: M extends `$${string}` ? WithErrorResponse<S[K][M], ErrorType> : S[K][M];
}
: S[K] extends object
? AddToAllOutputs<S[K], ErrorType>
: S[K];
}
>
: T;
you can then do
type ErrorType = {
success: false;
errors: {message: string; code: number}[];
};
export type App = AddToAllOutputs<MyApp, ErrorType>;
type ErrorType = {
success: false;
errors: {message: string; code: number}[];
};
export type App = AddToAllOutputs<MyApp, ErrorType>;
5 replies