( yields: Schema<TYieldIn, TYieldOut>, returns?: Schema<TReturnIn, TReturnOut>, )
| 17 | * @see {@link https://orpc.dev/docs/event-iterator#validate-event-iterator Validate Event Iterator Docs} |
| 18 | */ |
| 19 | export function eventIterator<TYieldIn, TYieldOut, TReturnIn = unknown, TReturnOut = unknown>( |
| 20 | yields: Schema<TYieldIn, TYieldOut>, |
| 21 | returns?: Schema<TReturnIn, TReturnOut>, |
| 22 | ): Schema<AsyncIteratorObject<TYieldIn, TReturnIn, void>, AsyncIteratorClass<TYieldOut, TReturnOut, void>> { |
| 23 | return { |
| 24 | '~standard': { |
| 25 | [EVENT_ITERATOR_DETAILS_SYMBOL as any]: { yields, returns } satisfies EventIteratorSchemaDetails, |
| 26 | vendor: 'orpc', |
| 27 | version: 1, |
| 28 | validate(iterator) { |
| 29 | if (!isAsyncIteratorObject(iterator)) { |
| 30 | return { issues: [{ message: 'Expect event iterator', path: [] }] } |
| 31 | } |
| 32 | |
| 33 | const mapped = mapEventIterator(iterator, { |
| 34 | async value(value, done) { |
| 35 | const schema = done ? returns : yields |
| 36 | |
| 37 | if (!schema) { |
| 38 | return value |
| 39 | } |
| 40 | |
| 41 | const result = await schema['~standard'].validate(value) |
| 42 | |
| 43 | if (result.issues) { |
| 44 | throw new ORPCError('EVENT_ITERATOR_VALIDATION_FAILED', { |
| 45 | message: 'Event iterator validation failed', |
| 46 | cause: new ValidationError({ |
| 47 | issues: result.issues, |
| 48 | message: 'Event iterator validation failed', |
| 49 | data: value, |
| 50 | }), |
| 51 | }) |
| 52 | } |
| 53 | |
| 54 | return result.value |
| 55 | }, |
| 56 | error: async error => error, |
| 57 | }) |
| 58 | |
| 59 | return { value: mapped } |
| 60 | }, |
| 61 | }, |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | export function getEventIteratorSchemaDetails(schema: AnySchema | undefined): undefined | EventIteratorSchemaDetails { |
| 66 | if (schema === undefined) { |
no outgoing calls
no test coverage detected