Skip to content

Commit 4bd45c3

Browse files
committed
fix: start tests, update errors
Signed-off-by: tunnckoCore <5038030+tunnckoCore@users.noreply.github.com>
1 parent d18a350 commit 4bd45c3

File tree

5 files changed

+369
-28
lines changed

5 files changed

+369
-28
lines changed

new-src/errors.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ export type ValidationError<ErrorKindNames = never> = Prettify<
1616
>;
1717
export type InternalError = Prettify<ReturnType<typeof createInternalError>>;
1818
export type DefinedError<T> = Prettify<{ readonly kind: string } & T>;
19-
export type ZagoraError<T> =
20-
| ValidationError
21-
| InternalError
22-
| DefinedError<any>;
19+
export type ZagoraError<T> = ValidationError | InternalError | DefinedError<T>;
2320

2421
export function isValidationError(val: any): val is ValidationError {
2522
return Boolean(
@@ -31,22 +28,29 @@ export function isValidationError(val: any): val is ValidationError {
3128
typeof val.message === "string",
3229
);
3330
}
31+
3432
export function isInternalError(val: any): val is InternalError {
3533
return Boolean(
3634
val &&
3735
val.kind === "UNKNOWN_ERROR" &&
3836
val.message &&
3937
typeof val.message === "string" &&
40-
val.cause,
38+
"cause" in val &&
39+
"stack" in val,
4140
);
4241
}
42+
4343
export function isDefinedError<T>(val: any): val is DefinedError<T> {
44-
return Boolean(
45-
val &&
46-
val.kind &&
47-
typeof val.kind === "string" &&
48-
val.kind.length > 0 &&
49-
val.kind === val.kind.toUpperCase(),
44+
return (
45+
Boolean(
46+
val &&
47+
val.kind &&
48+
typeof val.kind === "string" &&
49+
val.kind.length > 0 &&
50+
val.kind === val.kind.toUpperCase(),
51+
) &&
52+
!isValidationError(val) &&
53+
!isInternalError(val)
5054
);
5155
}
5256

@@ -112,12 +116,22 @@ export type ErrorHelpers<
112116
}
113117
: never;
114118

119+
export type ErrorsMapPlain<TErrorsMap extends Record<string, AnySchema>> = {
120+
[K in keyof TErrorsMap]: { kind: K } & InferSchemaOutput<TErrorsMap[K]>;
121+
};
122+
115123
export type ErrorsMapResolved<
116124
TErrorsMap extends Record<string, AnySchema> | undefined,
117125
> = TErrorsMap extends Record<string, AnySchema>
118-
? Readonly<{ [K in keyof TErrorsMap]: InferSchemaOutput<TErrorsMap[K]> }>
126+
? ErrorsMapPlain<TErrorsMap>
119127
: undefined;
120128

129+
// export type ErrorsMapResolved<
130+
// TErrorsMap extends Record<string, AnySchema> | undefined,
131+
// > = TErrorsMap extends Record<string, AnySchema>
132+
// ? { [K in keyof TErrorsMap]: { kind: K } & InferSchemaOutput<TErrorsMap[K]> }
133+
// : undefined;
134+
121135
export type ResolveErrorKindNames<TErrorsMap> = TErrorsMap extends Record<
122136
string,
123137
AnySchema

new-src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export class Zagora<
3131
TIsHandlerAsync,
3232
THandlerFn,
3333
TContext extends any | undefined = undefined,
34-
TInputSchema extends AnySchema = never,
35-
TOutputSchema extends AnySchema = never,
34+
TInputSchema extends AnySchema = AnySchema,
35+
TOutputSchema extends AnySchema = AnySchema,
3636
TErrorsMap extends Record<string, AnySchema> | undefined = undefined,
3737
> {
3838
constructor(

new-src/types.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import type { StandardSchemaV1 } from "@standard-schema/spec";
2-
import type { ErrorHelpers, InternalError, ValidationError } from "./errors";
2+
import type {
3+
ErrorHelpers,
4+
ErrorsMapPlain,
5+
ErrorsMapResolved,
6+
InternalError,
7+
ValidationError,
8+
} from "./errors";
39

410
export type Schema<I, O = I> = StandardSchemaV1<I, O>;
511

@@ -32,6 +38,13 @@ export type Prettify<T> = {
3238
export type IsOptional<T> = undefined extends T ? true : false;
3339
export type IsPromise<T> = T extends Promise<any> ? true : false;
3440

41+
// export type DefinedErrorsUnion<TErrorsMap> = TErrorsMap extends Record<
42+
// string,
43+
// AnySchema
44+
// >
45+
// ? Prettify<Readonly<ErrorsMapPlain<TErrorsMap>[keyof TErrorsMap]>>
46+
// : never;
47+
3548
export type ZagoraResult<
3649
TOutput,
3750
TErrorsMap extends Record<string, AnySchema> | undefined,
@@ -43,20 +56,22 @@ export type ZagoraResult<
4356
data: TOutput;
4457
readonly error: undefined;
4558
}
46-
: {
47-
readonly ok: false;
48-
readonly isTypedError: IsTypedError;
49-
readonly error: TErrorsMap extends Record<string, any>
50-
?
51-
| Prettify<Readonly<TErrorsMap[keyof TErrorsMap]>>
52-
| InternalError
53-
| ValidationError<keyof TErrorsMap>
54-
: InternalError | ValidationError<never>;
55-
};
59+
: TErrorsMap extends Record<string, any>
60+
? {
61+
readonly ok: false;
62+
readonly isTypedError: IsTypedError;
63+
readonly error:
64+
| Prettify<Readonly<TErrorsMap[keyof TErrorsMap]>>
65+
| ValidationError<keyof TErrorsMap>
66+
| InternalError;
67+
}
68+
: {
69+
readonly ok: false;
70+
readonly isTypedError: IsTypedError;
71+
readonly error: InternalError | ValidationError<never>;
72+
};
5673

5774
export interface ZagoraDef<
58-
// TIsHandlerAsync,
59-
// THandlerFn,
6075
TContext,
6176
TInputSchema extends AnySchema | undefined,
6277
TOutputSchema extends AnySchema | undefined,

new-src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export function validateError<TKindNames>(
135135
const EXECUTION_CACHE = new Map();
136136

137137
export function executeHandler(fn: any, args: any[]) {
138-
const key = fn.toString();
138+
const key = JSON.stringify({ key: fn.toString(), args });
139139
if (EXECUTION_CACHE.has(key)) {
140140
return EXECUTION_CACHE.get(key);
141141
}

0 commit comments

Comments
 (0)