@orpc/contract 0.38.0 → 0.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -271,11 +271,21 @@ var ORPCError = class _ORPCError extends Error {
271
271
  data: this.data
272
272
  };
273
273
  }
274
- static fromJSON(json) {
275
- return new _ORPCError(json.code, json);
274
+ static fromJSON(json, options) {
275
+ return new _ORPCError(json.code, {
276
+ ...options,
277
+ ...json
278
+ });
276
279
  }
277
280
  static isValidJSON(json) {
278
- return isObject(json) && "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && "message" in json && typeof json.message === "string";
281
+ if (!isObject(json)) {
282
+ return false;
283
+ }
284
+ const validKeys = ["defined", "code", "status", "message", "data"];
285
+ if (Object.keys(json).some((k) => !validKeys.includes(k))) {
286
+ return false;
287
+ }
288
+ return "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && "message" in json && typeof json.message === "string";
279
289
  }
280
290
  };
281
291
 
@@ -364,6 +374,82 @@ var ValidationError = class extends Error {
364
374
  }
365
375
  };
366
376
 
377
+ // src/event-iterator.ts
378
+ import { getEventMeta, isAsyncIteratorObject, isEventMetaContainer, withEventMeta } from "@orpc/server-standard";
379
+ function mapEventIterator(iterator, maps) {
380
+ return async function* () {
381
+ try {
382
+ while (true) {
383
+ const { done, value } = await iterator.next();
384
+ let mappedValue = await maps.value(value, done);
385
+ if (mappedValue !== value) {
386
+ const meta = getEventMeta(value);
387
+ if (meta && isEventMetaContainer(mappedValue)) {
388
+ mappedValue = withEventMeta(mappedValue, meta);
389
+ }
390
+ }
391
+ if (done) {
392
+ return mappedValue;
393
+ }
394
+ yield mappedValue;
395
+ }
396
+ } catch (error) {
397
+ let mappedError = await maps.error(error);
398
+ if (mappedError !== error) {
399
+ const meta = getEventMeta(error);
400
+ if (meta && isEventMetaContainer(mappedError)) {
401
+ mappedError = withEventMeta(mappedError, meta);
402
+ }
403
+ }
404
+ throw mappedError;
405
+ } finally {
406
+ await iterator.return?.();
407
+ }
408
+ }();
409
+ }
410
+ var EVENT_ITERATOR_SCHEMA_SYMBOL = Symbol("ORPC_EVENT_ITERATOR_SCHEMA");
411
+ function eventIterator(yields, returns) {
412
+ return {
413
+ "~standard": {
414
+ [EVENT_ITERATOR_SCHEMA_SYMBOL]: { yields, returns },
415
+ vendor: "orpc",
416
+ version: 1,
417
+ validate(iterator) {
418
+ if (!isAsyncIteratorObject(iterator)) {
419
+ return { issues: [{ message: "Expect event source iterator", path: [] }] };
420
+ }
421
+ const mapped = mapEventIterator(iterator, {
422
+ async value(value, done) {
423
+ const schema = done ? returns : yields;
424
+ if (!schema) {
425
+ return value;
426
+ }
427
+ const result = await schema["~standard"].validate(value);
428
+ if (result.issues) {
429
+ throw new ORPCError("EVENT_ITERATOR_VALIDATION_FAILED", {
430
+ message: "Event source iterator validation failed",
431
+ cause: new ValidationError({
432
+ issues: result.issues,
433
+ message: "Event source iterator validation failed"
434
+ })
435
+ });
436
+ }
437
+ return result.value;
438
+ },
439
+ error: async (error) => error
440
+ });
441
+ return { value: mapped };
442
+ }
443
+ }
444
+ };
445
+ }
446
+ function getEventIteratorSchemaDetails(schema) {
447
+ if (schema === void 0) {
448
+ return void 0;
449
+ }
450
+ return schema["~standard"][EVENT_ITERATOR_SCHEMA_SYMBOL];
451
+ }
452
+
367
453
  // src/schema.ts
368
454
  function type(...[map]) {
369
455
  return {
@@ -388,11 +474,14 @@ export {
388
474
  adaptContractRouter,
389
475
  adaptRoute,
390
476
  createORPCErrorConstructorMap,
477
+ eventIterator,
391
478
  fallbackContractConfig,
392
479
  fallbackORPCErrorMessage,
393
480
  fallbackORPCErrorStatus,
481
+ getEventIteratorSchemaDetails,
394
482
  isContractProcedure,
395
483
  isDefinedError,
484
+ mapEventIterator,
396
485
  mergeErrorMap,
397
486
  mergeMeta,
398
487
  mergePrefix,
@@ -1,6 +1,7 @@
1
1
  export type ClientContext = Record<string, any>;
2
2
  export type ClientOptions<TClientContext extends ClientContext> = {
3
3
  signal?: AbortSignal;
4
+ lastEventId?: string | undefined;
4
5
  } & (Record<never, never> extends TClientContext ? {
5
6
  context?: TClientContext;
6
7
  } : {
@@ -102,7 +102,7 @@ export declare class ORPCError<TCode extends ORPCErrorCode, TData> extends Error
102
102
  readonly data: TData;
103
103
  constructor(code: TCode, ...[options]: MaybeOptionalOptions<ORPCErrorOptions<TData>>);
104
104
  toJSON(): ORPCErrorJSON<TCode, TData>;
105
- static fromJSON<TCode extends ORPCErrorCode, TData>(json: ORPCErrorJSON<TCode, TData>): ORPCError<TCode, TData>;
105
+ static fromJSON<TCode extends ORPCErrorCode, TData>(json: ORPCErrorJSON<TCode, TData>, options?: ErrorOptions): ORPCError<TCode, TData>;
106
106
  static isValidJSON(json: unknown): json is ORPCErrorJSON<ORPCErrorCode, unknown>;
107
107
  }
108
108
  export type ORPCErrorJSON<TCode extends string, TData> = Pick<ORPCError<TCode, TData>, 'defined' | 'code' | 'status' | 'message' | 'data'>;
@@ -0,0 +1,12 @@
1
+ import type { StandardSchemaV1 } from '@standard-schema/spec';
2
+ import type { Schema } from './schema';
3
+ export declare function mapEventIterator<TYield, TReturn, TNext, TMap = TYield | TReturn>(iterator: AsyncIterator<TYield, TReturn, TNext>, maps: {
4
+ value: (value: NoInfer<TYield | TReturn>, done: boolean | undefined) => Promise<TMap>;
5
+ error: (error: unknown) => Promise<unknown>;
6
+ }): AsyncGenerator<TMap, TMap, TNext>;
7
+ export declare function eventIterator<TYieldIn, TYieldOut, TReturnIn = unknown, TReturnOut = unknown>(yields: StandardSchemaV1<TYieldIn, TYieldOut>, returns?: StandardSchemaV1<TReturnIn, TReturnOut>): StandardSchemaV1<AsyncIteratorObject<TYieldIn, TReturnIn, void>, AsyncIteratorObject<TYieldOut, TReturnOut, void>>;
8
+ export declare function getEventIteratorSchemaDetails(schema: Schema): undefined | {
9
+ yields: Schema;
10
+ returns: Schema;
11
+ };
12
+ //# sourceMappingURL=event-iterator.d.ts.map
@@ -8,6 +8,7 @@ export * from './error';
8
8
  export * from './error-map';
9
9
  export * from './error-orpc';
10
10
  export * from './error-utils';
11
+ export * from './event-iterator';
11
12
  export * from './meta';
12
13
  export * from './procedure';
13
14
  export * from './procedure-client';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/contract",
3
3
  "type": "module",
4
- "version": "0.38.0",
4
+ "version": "0.40.0",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -29,8 +29,9 @@
29
29
  "dist"
30
30
  ],
31
31
  "dependencies": {
32
- "@standard-schema/spec": "1.0.0",
33
- "@orpc/shared": "0.38.0"
32
+ "@orpc/server-standard": "^0.4.0",
33
+ "@standard-schema/spec": "^1.0.0",
34
+ "@orpc/shared": "0.40.0"
34
35
  },
35
36
  "devDependencies": {
36
37
  "arktype": "2.0.0-rc.26",