Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions __tests__/typescript-stress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ import { flattenErrorType, ProcedureErrorSchemaType } from '../router/errors';
import { ReadableImpl } from '../router/streams';
import { createMockTransportNetwork } from '../testUtil/fixtures/mockTransport';

enum TestErrorCodes {
ERROR_ONE = 'ERROR_ONE',
ERROR_TWO = 'ERROR_TWO',
ERROR_THREE = 'ERROR_THREE',
}

const requestData = Type.Union([
Type.Object({ a: Type.Number() }),
Type.Object({ c: Type.String() }),
Expand Down Expand Up @@ -479,6 +485,53 @@ describe('Procedure error schema', () => {
);
});

test('object with enum code', () => {
acceptErrorSchema(
Type.Object({
code: Type.Enum(TestErrorCodes),
message: Type.String(),
}),
);
});

test('union containing enum-based error schemas', () => {
acceptErrorSchema(
Type.Union([
Type.Object({
code: Type.Enum(TestErrorCodes),
message: Type.String(),
}),
Type.Object({
code: Type.Literal('OTHER_ERROR'),
message: Type.String(),
}),
]),
);
});

test('flattenErrorType with enum-based error schemas', () => {
acceptErrorSchema(
flattenErrorType(
Type.Union([
Type.Object({
code: Type.Enum(TestErrorCodes),
message: Type.String(),
}),
Type.Union([
Type.Object({
code: Type.Literal('ERROR_4'),
message: Type.String(),
}),
Type.Object({
code: Type.Literal('ERROR_5'),
message: Type.String(),
}),
]),
]),
),
);
});

test('union of union', () => {
acceptErrorSchema(
flattenErrorType(
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@replit/river",
"description": "It's like tRPC but... with JSON Schema Support, duplex streaming and support for service multiplexing. Transport agnostic!",
"version": "0.209.8",
"version": "0.210.0",
"type": "module",
"exports": {
".": {
Expand Down
11 changes: 8 additions & 3 deletions router/errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Kind,
Static,
TEnum,
TLiteral,
TNever,
TObject,
Expand Down Expand Up @@ -32,13 +33,15 @@ export const CANCEL_CODE = 'CANCEL';

type TLiteralString = TLiteral<string>;

type TEnumString = TEnum<Record<string, string>>;

export type BaseErrorSchemaType =
| TObject<{
code: TLiteralString;
code: TLiteralString | TEnumString;
message: TLiteralString | TString;
}>
| TObject<{
code: TLiteralString;
code: TLiteralString | TEnumString;
message: TLiteralString | TString;
extras: TSchema;
}>;
Expand Down Expand Up @@ -139,7 +142,9 @@ function isUnion(schema: TSchema): schema is TUnion {
type Flatten<T> = T extends BaseErrorSchemaType
? T
: T extends TUnion<Array<infer U extends TSchema>>
? Flatten<U>
? U extends BaseErrorSchemaType
? TUnion<Array<U>>
: Flatten<U>
: unknown;

/**
Expand Down
Loading