@orpc/contract 0.0.0-next.bc564a6 → 0.0.0-next.bf323bf

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
@@ -161,7 +161,7 @@ var oc = new ContractBuilder({
161
161
  });
162
162
 
163
163
  // src/error-orpc.ts
164
- import { isPlainObject } from "@orpc/shared";
164
+ import { isObject } from "@orpc/shared";
165
165
  var COMMON_ORPC_ERROR_DEFS = {
166
166
  BAD_REQUEST: {
167
167
  status: 400,
@@ -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 isPlainObject(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
 
@@ -319,6 +329,12 @@ async function validateORPCError(map, error) {
319
329
  }
320
330
  return new ORPCError(code, { defined: true, status, message, data: validated.value, cause });
321
331
  }
332
+ function toORPCError(error) {
333
+ return error instanceof ORPCError ? error : new ORPCError("INTERNAL_SERVER_ERROR", {
334
+ message: "Internal server error",
335
+ cause: error
336
+ });
337
+ }
322
338
 
323
339
  // src/client-utils.ts
324
340
  async function safe(promise) {
@@ -358,6 +374,82 @@ var ValidationError = class extends Error {
358
374
  }
359
375
  };
360
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
+
361
453
  // src/schema.ts
362
454
  function type(...[map]) {
363
455
  return {
@@ -382,11 +474,14 @@ export {
382
474
  adaptContractRouter,
383
475
  adaptRoute,
384
476
  createORPCErrorConstructorMap,
477
+ eventIterator,
385
478
  fallbackContractConfig,
386
479
  fallbackORPCErrorMessage,
387
480
  fallbackORPCErrorStatus,
481
+ getEventIteratorSchemaDetails,
388
482
  isContractProcedure,
389
483
  isDefinedError,
484
+ mapEventIterator,
390
485
  mergeErrorMap,
391
486
  mergeMeta,
392
487
  mergePrefix,
@@ -395,6 +490,7 @@ export {
395
490
  oc,
396
491
  prefixRoute,
397
492
  safe,
493
+ toORPCError,
398
494
  type,
399
495
  unshiftTagRoute,
400
496
  validateORPCError
@@ -1,21 +1,22 @@
1
- import type { AbortSignal } from './types';
2
- export type ClientOptions<TClientContext> = {
1
+ export type ClientContext = Record<string, any>;
2
+ export type ClientOptions<TClientContext extends ClientContext> = {
3
3
  signal?: AbortSignal;
4
- } & (undefined extends TClientContext ? {
4
+ lastEventId?: string | undefined;
5
+ } & (Record<never, never> extends TClientContext ? {
5
6
  context?: TClientContext;
6
7
  } : {
7
8
  context: TClientContext;
8
9
  });
9
- export type ClientRest<TClientContext, TInput> = [input: TInput, options: ClientOptions<TClientContext>] | (undefined extends TInput & TClientContext ? [] : never) | (undefined extends TClientContext ? [input: TInput] : never);
10
+ export type ClientRest<TClientContext extends ClientContext, TInput> = [input: TInput, options: ClientOptions<TClientContext>] | (Record<never, never> extends TClientContext ? (undefined extends TInput ? [input?: TInput] : [input: TInput]) : never);
10
11
  export type ClientPromiseResult<TOutput, TError extends Error> = Promise<TOutput> & {
11
12
  __error?: {
12
13
  type: TError;
13
14
  };
14
15
  };
15
- export interface Client<TClientContext, TInput, TOutput, TError extends Error> {
16
+ export interface Client<TClientContext extends ClientContext, TInput, TOutput, TError extends Error> {
16
17
  (...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
17
18
  }
18
- export type NestedClient<TClientContext> = Client<TClientContext, any, any, any> | {
19
+ export type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
19
20
  [k: string]: NestedClient<TClientContext>;
20
21
  };
21
22
  //# sourceMappingURL=client.d.ts.map
@@ -1,3 +1,4 @@
1
+ import type { MaybeOptionalOptions } from '@orpc/shared';
1
2
  import type { ErrorMap, ErrorMapItem } from './error-map';
2
3
  import type { SchemaOutput } from './schema';
3
4
  export type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = {
@@ -94,15 +95,14 @@ export type ORPCErrorOptions<TData> = ErrorOptions & {
94
95
  } : {
95
96
  data: TData;
96
97
  });
97
- export type ORPCErrorOptionsRest<TData> = [options: ORPCErrorOptions<TData>] | (undefined extends TData ? [] : never);
98
98
  export declare class ORPCError<TCode extends ORPCErrorCode, TData> extends Error {
99
99
  readonly defined: boolean;
100
100
  readonly code: TCode;
101
101
  readonly status: number;
102
102
  readonly data: TData;
103
- constructor(code: TCode, ...[options]: ORPCErrorOptionsRest<TData>);
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'>;
@@ -1,14 +1,15 @@
1
+ import type { MaybeOptionalOptions } from '@orpc/shared';
1
2
  import type { ErrorMap, ErrorMapItem } from './error-map';
2
3
  import type { ORPCErrorCode, ORPCErrorOptions } from './error-orpc';
3
4
  import type { SchemaInput } from './schema';
4
5
  import { ORPCError } from './error-orpc';
5
6
  export declare function isDefinedError<T>(error: T): error is Extract<T, ORPCError<any, any>>;
6
7
  export type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
7
- export type ORPCErrorConstructorMapItemRest<TData> = [options: ORPCErrorConstructorMapItemOptions<TData>] | (undefined extends TData ? [] : never);
8
- export type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: ORPCErrorConstructorMapItemRest<TInData>) => ORPCError<TCode, TInData>;
8
+ export type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
9
9
  export type ORPCErrorConstructorMap<T extends ErrorMap> = {
10
10
  [K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, SchemaInput<UInputSchema>> : never : never;
11
11
  };
12
12
  export declare function createORPCErrorConstructorMap<T extends ErrorMap>(errors: T): ORPCErrorConstructorMap<T>;
13
13
  export declare function validateORPCError(map: ErrorMap, error: ORPCError<any, any>): Promise<ORPCError<string, unknown>>;
14
+ export declare function toORPCError(error: unknown): ORPCError<any, any>;
14
15
  //# sourceMappingURL=error-utils.d.ts.map
@@ -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';
@@ -15,5 +16,4 @@ export * from './route';
15
16
  export * from './router';
16
17
  export * from './router-client';
17
18
  export * from './schema';
18
- export * from './types';
19
19
  //# sourceMappingURL=index.d.ts.map
@@ -1,6 +1,6 @@
1
- import type { Client } from './client';
1
+ import type { Client, ClientContext } from './client';
2
2
  import type { ErrorFromErrorMap } from './error';
3
3
  import type { ErrorMap } from './error-map';
4
4
  import type { Schema, SchemaInput, SchemaOutput } from './schema';
5
- export type ContractProcedureClient<TClientContext, TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> = Client<TClientContext, SchemaInput<TInputSchema>, SchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
5
+ export type ContractProcedureClient<TClientContext extends ClientContext, TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> = Client<TClientContext, SchemaInput<TInputSchema>, SchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
6
6
  //# sourceMappingURL=procedure-client.d.ts.map
@@ -1,7 +1,8 @@
1
+ import type { ClientContext } from './client';
1
2
  import type { ContractProcedure } from './procedure';
2
3
  import type { ContractProcedureClient } from './procedure-client';
3
4
  import type { AnyContractRouter } from './router';
4
- export type ContractRouterClient<TRouter extends AnyContractRouter, TClientContext> = TRouter extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, any> ? ContractProcedureClient<TClientContext, UInputSchema, UOutputSchema, UErrorMap> : {
5
+ export type ContractRouterClient<TRouter extends AnyContractRouter, TClientContext extends ClientContext> = TRouter extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, any> ? ContractProcedureClient<TClientContext, UInputSchema, UOutputSchema, UErrorMap> : {
5
6
  [K in keyof TRouter]: TRouter[K] extends AnyContractRouter ? ContractRouterClient<TRouter[K], TClientContext> : never;
6
7
  };
7
8
  //# sourceMappingURL=router-client.d.ts.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/contract",
3
3
  "type": "module",
4
- "version": "0.0.0-next.bc564a6",
4
+ "version": "0.0.0-next.bf323bf",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -29,13 +29,14 @@
29
29
  "dist"
30
30
  ],
31
31
  "dependencies": {
32
- "@standard-schema/spec": "1.0.0-rc.0",
33
- "@orpc/shared": "0.0.0-next.bc564a6"
32
+ "@orpc/server-standard": "^0.4.0",
33
+ "@standard-schema/spec": "^1.0.0",
34
+ "@orpc/shared": "0.0.0-next.bf323bf"
34
35
  },
35
36
  "devDependencies": {
36
37
  "arktype": "2.0.0-rc.26",
37
38
  "valibot": "1.0.0-beta.9",
38
- "zod": "3.24.1"
39
+ "zod": "^3.24.1"
39
40
  },
40
41
  "scripts": {
41
42
  "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
@@ -1,3 +0,0 @@
1
- import type { FindGlobalInstanceType } from '@orpc/shared';
2
- export type AbortSignal = FindGlobalInstanceType<'AbortSignal'>;
3
- //# sourceMappingURL=types.d.ts.map