adding generic error output type to App type

Hey all, I am using the typed RPC feature of hono and got it working, except for the errors I have an .onError that consstruct a json from error thrown and I would like that error type to be unioned with each of the output type of all path Is there a way to do this ? Like is there some type generic magic I could add to my exported App type to add an extra type for all outputs Something like export type App = AddErrorType<typeof app, {success: false, errors: {message: string, code: number}[]}> ?
1 Reply
ronan(wighawag)
ronan(wighawag)OP4w ago
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>;
but maybe hono should handle it automatically somehow ? 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;
wanted to have the error happen only when response is not ok, but did not managed
Want results from more Discord servers?
Add your server