Skip to content

Commit

Permalink
Fix readable losing error types (#265)
Browse files Browse the repository at this point in the history
## Why

Readable was losing error types

## What changed

Removed type annotation from `ReadableBrokenError` and replaced it with
`satisfies`, since it seems like even with `as const` it follows the
generalized annotation.

## Versioning

- [ ] Breaking protocol change
- [x] Breaking ts/js API change

<!-- Kind reminder to add tests and updated documentation if needed -->
  • Loading branch information
masad-frost authored Sep 2, 2024
1 parent 458c827 commit 998bc75
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
27 changes: 27 additions & 0 deletions __tests__/typescript-stress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
createServerHandshakeOptions,
} from '../router/handshake';
import { flattenErrorType, ProcedureErrorSchemaType } from '../router/errors';
import { ReadableImpl } from '../router/streams';

const requestData = Type.Union([
Type.Object({ a: Type.Number() }),
Expand Down Expand Up @@ -607,3 +608,29 @@ describe('Procedure error schema', () => {
});
});
});

describe('Readable types', () => {
// Skip, we're only testing types
test.skip('should maintain result types', async () => {
function acceptsErrors(_code: 'SOME_ERROR' | 'READABLE_BROKEN') {
// pass
}

function acceptsSuccess(_success: 'SUCCESS') {
// pass
}

const readable = new ReadableImpl<
'SUCCESS',
{ code: 'SOME_ERROR'; message: string }
>();
for await (const value of readable) {
if (value.ok) {
acceptsSuccess(value.payload);
continue;
}

acceptsErrors(value.payload.code);
}
});
});
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.200.2",
"version": "0.200.3",
"type": "module",
"exports": {
".": {
Expand Down
4 changes: 2 additions & 2 deletions router/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Static } from '@sinclair/typebox';
import { Err, Result } from './result';
import { BaseErrorSchemaType } from './errors';

export const ReadableBrokenError: Static<BaseErrorSchemaType> = {
export const ReadableBrokenError = {
code: 'READABLE_BROKEN',
message: 'Readable was broken before it is fully consumed',
} as const;
} as const satisfies Static<BaseErrorSchemaType>;

/**
* Similar to {@link Result} but with an extra error to handle cases where {@link Readable.break} is called
Expand Down

0 comments on commit 998bc75

Please sign in to comment.