@orpc/contract 2.0.0-beta.47 → 2.0.0-beta.49

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.
Files changed (51) hide show
  1. package/README.md +6 -7
  2. package/dist/builder-variants.d.ts +103 -0
  3. package/dist/builder-variants.js +1 -0
  4. package/dist/builder.d.ts +78 -0
  5. package/dist/builder.js +112 -0
  6. package/dist/client-factory.d.ts +24 -0
  7. package/dist/client-factory.js +38 -0
  8. package/dist/error-factory.d.ts +80 -0
  9. package/dist/error-factory.js +121 -0
  10. package/dist/error-utils.d.ts +5 -0
  11. package/dist/error-utils.js +40 -0
  12. package/dist/error.d.ts +45 -0
  13. package/dist/error.js +20 -0
  14. package/dist/index.d.ts +44 -415
  15. package/dist/index.js +31 -0
  16. package/dist/meta-built-in.d.ts +30 -0
  17. package/dist/meta-built-in.js +54 -0
  18. package/dist/meta-utils.d.ts +58 -0
  19. package/dist/meta-utils.js +76 -0
  20. package/dist/meta.d.ts +53 -0
  21. package/dist/meta.js +13 -0
  22. package/dist/plugins/index.d.ts +8 -43
  23. package/dist/plugins/index.js +12 -0
  24. package/dist/plugins/request-validation.d.ts +28 -0
  25. package/dist/plugins/request-validation.js +55 -0
  26. package/dist/plugins/response-validation.d.ts +15 -0
  27. package/dist/plugins/response-validation.js +72 -0
  28. package/dist/procedure-client.d.ts +5 -0
  29. package/dist/procedure-client.js +1 -0
  30. package/dist/procedure.d.ts +28 -0
  31. package/dist/procedure.js +28 -0
  32. package/dist/router-client.d.ts +13 -0
  33. package/dist/router-client.js +1 -0
  34. package/dist/router-utils.d.ts +30 -0
  35. package/dist/router-utils.js +79 -0
  36. package/dist/router.d.ts +52 -0
  37. package/dist/router.js +1 -0
  38. package/dist/schema-built-in.d.ts +23 -0
  39. package/dist/schema-built-in.js +76 -0
  40. package/dist/schema-utils.d.ts +20 -0
  41. package/dist/schema-utils.js +44 -0
  42. package/dist/schema.d.ts +25 -0
  43. package/dist/schema.js +1 -0
  44. package/package.json +29 -31
  45. package/dist/index.d.mts +0 -433
  46. package/dist/index.mjs +0 -342
  47. package/dist/plugins/index.d.mts +0 -47
  48. package/dist/plugins/index.mjs +0 -91
  49. package/dist/shared/contract.CM62dTQK.d.mts +0 -202
  50. package/dist/shared/contract.CM62dTQK.d.ts +0 -202
  51. package/dist/shared/contract.T7BNVKJ4.mjs +0 -177
@@ -0,0 +1,52 @@
1
+ import type { ThrowableError } from '@orpc/shared';
2
+ import type { ORPCErrorFromErrorMap } from './error.ts';
3
+ import type { AnyProcedureContract, ProcedureContract } from './procedure.ts';
4
+ import type { InferSchemaInput, InferSchemaOutput } from './schema.ts';
5
+ /**
6
+ * A router contract: a single procedure contract or a nested record of them.
7
+ *
8
+ * @see {@link https://orpc.dev/docs/contract/client-factory | Contract Client Factory}
9
+ */
10
+ export type RouterContract = AnyProcedureContract | {
11
+ [k: string]: RouterContract;
12
+ };
13
+ /**
14
+ * Infer the input types for each procedure-contract, preserving the router-contract shape.
15
+ *
16
+ * @see {@link https://orpc.dev/docs/contract/router#infer-router-contract-inputs | Router Contract - Infer Router Contract Inputs}
17
+ */
18
+ export type InferRouterContractInputs<T extends RouterContract> = T extends ProcedureContract<infer UInputSchema, any, any> ? InferSchemaInput<UInputSchema> : {
19
+ [K in keyof T]: T[K] extends RouterContract ? InferRouterContractInputs<T[K]> : never;
20
+ };
21
+ /**
22
+ * Infer the output types for each procedure-contract, preserving the router-contract shape.
23
+ *
24
+ * @see {@link https://orpc.dev/docs/contract/router#infer-router-contract-outputs | Router Contract - Infer Router Contract Outputs}
25
+ */
26
+ export type InferRouterContractOutputs<T extends RouterContract> = T extends ProcedureContract<any, infer UOutputSchema, any> ? InferSchemaOutput<UOutputSchema> : {
27
+ [K in keyof T]: T[K] extends RouterContract ? InferRouterContractOutputs<T[K]> : never;
28
+ };
29
+ /**
30
+ * Infer the union of error maps defined across the entire router-contract.
31
+ *
32
+ * @see {@link https://orpc.dev/docs/contract/router#infer-router-contract-error-map | Router Contract - Infer Router Contract Error Map}
33
+ */
34
+ export type InferRouterContractErrorMap<T extends RouterContract> = T extends ProcedureContract<any, any, infer UErrorMap> ? UErrorMap : {
35
+ [K in keyof T]: T[K] extends RouterContract ? InferRouterContractErrorMap<T[K]> : never;
36
+ }[keyof T];
37
+ /**
38
+ * Infer the union of throwable errors for entire router-contract.
39
+ *
40
+ * @see {@link https://orpc.dev/docs/contract/router#infer-router-contract-error | Router Contract - Infer Router Contract Error}
41
+ */
42
+ export type InferRouterContractError<T extends RouterContract> = T extends ProcedureContract<any, any, infer UErrorMap> ? ORPCErrorFromErrorMap<UErrorMap> | ThrowableError : {
43
+ [K in keyof T]: T[K] extends RouterContract ? InferRouterContractError<T[K]> : never;
44
+ }[keyof T];
45
+ /**
46
+ * Infer throwable errors for each procedure-contract, preserving the router-contract shape.
47
+ *
48
+ * @see {@link https://orpc.dev/docs/contract/router#infer-router-contract-errors | Router Contract - Infer Router Contract Errors}
49
+ */
50
+ export type InferRouterContractErrors<T extends RouterContract> = T extends ProcedureContract<any, any, infer UErrorMap> ? ORPCErrorFromErrorMap<UErrorMap> | ThrowableError : {
51
+ [K in keyof T]: T[K] extends RouterContract ? InferRouterContractErrors<T[K]> : never;
52
+ };
package/dist/router.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,23 @@
1
+ import type { AsyncIteratorClass } from '@orpc/shared';
2
+ import type { AnySchema, Schema } from './schema.ts';
3
+ export interface AsyncIteratorObjectSchemaDetails {
4
+ yieldSchema: AnySchema;
5
+ returnSchema?: AnySchema | undefined;
6
+ }
7
+ /**
8
+ * Defines a schema for an AsyncIteratorObject, validating each yielded value
9
+ * (and optionally the return value) with the given schemas.
10
+ *
11
+ * @see {@link https://orpc.dev/docs/async-iterator-object | AsyncIteratorObject (SSE)}
12
+ */
13
+ export declare function asyncIteratorObject<TYieldIn, TYieldOut, TReturnIn = unknown, TReturnOut = unknown>(yieldSchema: Schema<TYieldIn, TYieldOut>, returnSchema?: Schema<TReturnIn, TReturnOut>): Schema<AsyncIteratorObject<TYieldIn, TReturnIn, void>, AsyncIteratorClass<TYieldOut, TReturnOut, void>>;
14
+ export declare function getAsyncIteratorObjectSchemaDetails(schema: AnySchema | undefined): undefined | AsyncIteratorObjectSchemaDetails;
15
+ export interface AsyncIteratorObjectSchemasDetails {
16
+ yieldSchemas: AnySchema[];
17
+ returnSchemas: AnySchema[];
18
+ }
19
+ /**
20
+ * Collects the yield and return schemas of multiple schemas, such as the `.output` schemas of a procedure,
21
+ * or returns `undefined` when there are none or any of them does not describe an AsyncIteratorObject.
22
+ */
23
+ export declare function getAsyncIteratorObjectSchemasDetails(schemas: readonly AnySchema[] | undefined): undefined | AsyncIteratorObjectSchemasDetails;
@@ -0,0 +1,76 @@
1
+ import { ORPCError, wrapAsyncIteratorPreservingEventMeta } from '@orpc/client';
2
+ import { isAsyncIteratorObject, ORPC_NAME, override } from '@orpc/shared';
3
+ import { ValidationError } from './error.js';
4
+ const ASYNC_ITERATOR_OBJECT_SCHEMA_DETAILS_SYMBOL = /* @__PURE__ */ Symbol.for('ORPC_ASYNC_ITERATOR_OBJECT_SCHEMA_DETAILS');
5
+ /**
6
+ * Defines a schema for an AsyncIteratorObject, validating each yielded value
7
+ * (and optionally the return value) with the given schemas.
8
+ *
9
+ * @see {@link https://orpc.dev/docs/async-iterator-object | AsyncIteratorObject (SSE)}
10
+ */
11
+ export function asyncIteratorObject(yieldSchema, returnSchema) {
12
+ return {
13
+ '~standard': {
14
+ [ASYNC_ITERATOR_OBJECT_SCHEMA_DETAILS_SYMBOL]: {
15
+ yieldSchema,
16
+ returnSchema,
17
+ },
18
+ vendor: ORPC_NAME,
19
+ version: 1,
20
+ validate(iterator) {
21
+ if (!isAsyncIteratorObject(iterator)) {
22
+ return { issues: [{ message: 'Expect AsyncIteratorObject', path: [] }] };
23
+ }
24
+ const mapped = wrapAsyncIteratorPreservingEventMeta(iterator, {
25
+ async mapResult(result) {
26
+ const schema = result.done ? returnSchema : yieldSchema;
27
+ if (!schema) {
28
+ return result;
29
+ }
30
+ const validated = await schema['~standard'].validate(result.value);
31
+ if (validated.issues) {
32
+ throw new ORPCError('ASYNC_ITERATOR_OBJECT_VALIDATION_FAILED', {
33
+ message: 'AsyncIteratorObject validation failed',
34
+ cause: new ValidationError({
35
+ issues: validated.issues,
36
+ message: 'AsyncIteratorObject validation failed',
37
+ invalidData: result.value,
38
+ }),
39
+ });
40
+ }
41
+ return { done: result.done, value: validated.value };
42
+ },
43
+ });
44
+ return { value: override(iterator, mapped) };
45
+ },
46
+ },
47
+ };
48
+ }
49
+ export function getAsyncIteratorObjectSchemaDetails(schema) {
50
+ if (schema === undefined) {
51
+ return undefined;
52
+ }
53
+ return schema['~standard'][ASYNC_ITERATOR_OBJECT_SCHEMA_DETAILS_SYMBOL];
54
+ }
55
+ /**
56
+ * Collects the yield and return schemas of multiple schemas, such as the `.output` schemas of a procedure,
57
+ * or returns `undefined` when there are none or any of them does not describe an AsyncIteratorObject.
58
+ */
59
+ export function getAsyncIteratorObjectSchemasDetails(schemas) {
60
+ if (!schemas || schemas.length === 0) {
61
+ return undefined;
62
+ }
63
+ const yieldSchemas = [];
64
+ const returnSchemas = [];
65
+ for (const schema of schemas) {
66
+ const details = getAsyncIteratorObjectSchemaDetails(schema);
67
+ if (!details) {
68
+ return undefined;
69
+ }
70
+ yieldSchemas.push(details.yieldSchema);
71
+ if (details.returnSchema) {
72
+ returnSchemas.push(details.returnSchema);
73
+ }
74
+ }
75
+ return { yieldSchemas, returnSchemas };
76
+ }
@@ -0,0 +1,20 @@
1
+ import type { IsEqual, Promisable } from '@orpc/shared';
2
+ import type { Schema, SchemaIssue } from './schema.ts';
3
+ export type TypeRest<TInput, TOutput> = [map: (input: TInput) => Promisable<TOutput>] | (IsEqual<TInput, TOutput> extends true ? [] : never);
4
+ /**
5
+ * Create a schema for things can be trust without validation.
6
+ * You can optionally pass a map function for mapping
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * const normal = type<number>()
11
+ * const withMap = type<number, string>(input => input.toString())
12
+ *```
13
+ *
14
+ * @see {@link https://orpc.dev/docs/procedure#type-utility | Procedure - type Utility}
15
+ */
16
+ export declare function type<TInput, TOutput = TInput>(...[map]: TypeRest<TInput, TOutput>): Schema<TInput, TOutput>;
17
+ /**
18
+ * Check if the given issue is following the standard-schema issue format.
19
+ */
20
+ export declare function isSchemaIssue(issue: unknown): issue is SchemaIssue;
@@ -0,0 +1,44 @@
1
+ import { isPropertyKey, isNonPrimitive, ORPC_NAME } from '@orpc/shared';
2
+ /**
3
+ * Create a schema for things can be trust without validation.
4
+ * You can optionally pass a map function for mapping
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * const normal = type<number>()
9
+ * const withMap = type<number, string>(input => input.toString())
10
+ *```
11
+ *
12
+ * @see {@link https://orpc.dev/docs/procedure#type-utility | Procedure - type Utility}
13
+ */
14
+ export function type(...[map]) {
15
+ return {
16
+ '~standard': {
17
+ vendor: ORPC_NAME,
18
+ version: 1,
19
+ async validate(value) {
20
+ if (map) {
21
+ return { value: (await map(value)) };
22
+ }
23
+ return { value: value };
24
+ },
25
+ },
26
+ };
27
+ }
28
+ /**
29
+ * Check if the given issue is following the standard-schema issue format.
30
+ */
31
+ export function isSchemaIssue(issue) {
32
+ if (!isNonPrimitive(issue) || typeof issue.message !== 'string') {
33
+ return false;
34
+ }
35
+ if (issue.path !== undefined) {
36
+ if (!Array.isArray(issue.path)) {
37
+ return false;
38
+ }
39
+ if (!issue.path.every((segment) => isPropertyKey(segment) || (isNonPrimitive(segment) && isPropertyKey(segment.key)))) {
40
+ return false;
41
+ }
42
+ }
43
+ return true;
44
+ }
@@ -0,0 +1,25 @@
1
+ import type { StandardSchemaV1 } from '@standard-schema/spec';
2
+ /**
3
+ * TOutput default = TInput for better readability (shorter) in-case both TInput, TOutput is equal
4
+ */
5
+ export type Schema<TInput, TOutput = TInput> = StandardSchemaV1<TInput, TOutput>;
6
+ /**
7
+ * Any Standard Schema compatible schema, regardless of its input and output types.
8
+ *
9
+ * @see {@link https://orpc.dev/docs/integrations/standard-schema | Standard Schema Integration}
10
+ */
11
+ export type AnySchema = Schema<any>;
12
+ export type SchemaIssue = StandardSchemaV1.Issue;
13
+ /**
14
+ * Infers the input type of a schema.
15
+ *
16
+ * @see {@link https://orpc.dev/docs/metadata | Metadata}
17
+ */
18
+ export type InferSchemaInput<T extends AnySchema> = T extends StandardSchemaV1<infer UInput, any> ? UInput : never;
19
+ /**
20
+ * Infers the output type of a schema.
21
+ *
22
+ * @see {@link https://orpc.dev/docs/metadata | Metadata}
23
+ */
24
+ export type InferSchemaOutput<T extends AnySchema> = T extends StandardSchemaV1<any, infer UOutput> ? UOutput : never;
25
+ export type MergedSchema<T extends AnySchema, U extends AnySchema> = T extends Schema<infer TInput, infer TOutput> ? U extends Schema<infer UInput, infer UOutput> ? Schema<TInput & UInput, TOutput & UOutput> : never : never;
package/dist/schema.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,58 +1,56 @@
1
1
  {
2
2
  "name": "@orpc/contract",
3
- "type": "module",
4
- "version": "2.0.0-beta.47",
3
+ "version": "2.0.0-beta.49",
5
4
  "description": "Define typesafe API contracts as the single source of truth for oRPC, powered by Standard Schema",
6
- "license": "MIT",
7
- "funding": [
8
- "https://github.com/sponsors/dinwwwh",
9
- "https://opencollective.com/middleapi"
5
+ "keywords": [
6
+ "api",
7
+ "contract",
8
+ "contract-first",
9
+ "openapi",
10
+ "orpc",
11
+ "rpc",
12
+ "standard-schema",
13
+ "typesafe",
14
+ "typescript"
10
15
  ],
11
16
  "homepage": "https://orpc.dev",
17
+ "license": "MIT",
12
18
  "repository": {
13
19
  "type": "git",
14
20
  "url": "git+https://github.com/middleapi/orpc.git",
15
21
  "directory": "packages/contract"
16
22
  },
17
- "keywords": [
18
- "orpc",
19
- "rpc",
20
- "api",
21
- "typescript",
22
- "typesafe",
23
- "contract",
24
- "contract-first",
25
- "standard-schema",
26
- "openapi"
23
+ "funding": [
24
+ "https://github.com/sponsors/dinwwwh",
25
+ "https://opencollective.com/middleapi"
26
+ ],
27
+ "files": [
28
+ "dist"
27
29
  ],
30
+ "type": "module",
28
31
  "sideEffects": false,
29
32
  "exports": {
30
33
  "./package.json": "./package.json",
31
34
  ".": {
32
- "types": "./dist/index.d.mts",
33
- "import": "./dist/index.mjs",
34
- "default": "./dist/index.mjs"
35
+ "types": "./dist/index.d.ts",
36
+ "import": "./dist/index.js",
37
+ "default": "./dist/index.js"
35
38
  },
36
39
  "./plugins": {
37
- "types": "./dist/plugins/index.d.mts",
38
- "import": "./dist/plugins/index.mjs",
39
- "default": "./dist/plugins/index.mjs"
40
+ "types": "./dist/plugins/index.d.ts",
41
+ "import": "./dist/plugins/index.js",
42
+ "default": "./dist/plugins/index.js"
40
43
  }
41
44
  },
42
- "files": [
43
- "dist"
44
- ],
45
45
  "dependencies": {
46
- "@orpc/client": "2.0.0-beta.47",
47
- "@orpc/shared": "2.0.0-beta.47",
46
+ "@orpc/client": "2.0.0-beta.49",
47
+ "@orpc/shared": "2.0.0-beta.49",
48
48
  "@standard-schema/spec": "^1.1.0"
49
49
  },
50
50
  "devDependencies": {
51
- "arktype": "^2.2.8",
51
+ "arktype": "^2.2.9",
52
52
  "valibot": "^1.5.0",
53
53
  "zod": "^4.6.7"
54
54
  },
55
- "scripts": {
56
- "type:check": "tsc -b"
57
- }
55
+ "scripts": {}
58
56
  }