@orpc/contract 2.0.0-beta.5 → 2.0.0-beta.51

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 (46) hide show
  1. package/README.md +139 -102
  2. package/dist/builder-variants.d.ts +103 -0
  3. package/dist/builder.d.ts +78 -0
  4. package/dist/builder.js +108 -0
  5. package/dist/client-factory.d.ts +24 -0
  6. package/dist/client-factory.js +38 -0
  7. package/dist/error-factory.d.ts +77 -0
  8. package/dist/error-factory.js +110 -0
  9. package/dist/error-utils.d.ts +5 -0
  10. package/dist/error-utils.js +37 -0
  11. package/dist/error.d.ts +41 -0
  12. package/dist/error.js +22 -0
  13. package/dist/index.d.ts +20 -182
  14. package/dist/index.js +12 -0
  15. package/dist/meta-built-in.d.ts +30 -0
  16. package/dist/meta-built-in.js +45 -0
  17. package/dist/meta-utils.d.ts +55 -0
  18. package/dist/meta-utils.js +70 -0
  19. package/dist/meta.d.ts +53 -0
  20. package/dist/meta.js +11 -0
  21. package/dist/plugins/index.d.ts +3 -38
  22. package/dist/plugins/index.js +2 -0
  23. package/dist/plugins/request-validation.d.ts +28 -0
  24. package/dist/plugins/request-validation.js +50 -0
  25. package/dist/plugins/response-validation.d.ts +15 -0
  26. package/dist/plugins/response-validation.js +67 -0
  27. package/dist/procedure-client.d.ts +5 -0
  28. package/dist/procedure.d.ts +28 -0
  29. package/dist/procedure.js +17 -0
  30. package/dist/router-client.d.ts +11 -0
  31. package/dist/router-utils.d.ts +24 -0
  32. package/dist/router-utils.js +59 -0
  33. package/dist/router.d.ts +47 -0
  34. package/dist/schema-built-in.d.ts +18 -0
  35. package/dist/schema-built-in.js +69 -0
  36. package/dist/schema-utils.d.ts +20 -0
  37. package/dist/schema-utils.js +36 -0
  38. package/dist/schema.d.ts +25 -0
  39. package/package.json +34 -24
  40. package/dist/index.d.mts +0 -182
  41. package/dist/index.mjs +0 -193
  42. package/dist/plugins/index.d.mts +0 -38
  43. package/dist/plugins/index.mjs +0 -90
  44. package/dist/shared/contract.CW-2wl1i.mjs +0 -180
  45. package/dist/shared/contract.Do92aRJ4.d.mts +0 -126
  46. package/dist/shared/contract.Do92aRJ4.d.ts +0 -126
@@ -0,0 +1,67 @@
1
+ import { ValidationError } from "../error.js";
2
+ import { reconcileORPCError } from "../error-utils.js";
3
+ import { getProcedureContractOrThrow } from "../router-utils.js";
4
+ import { isAsyncIteratorObject, override, toArray } from "@orpc/shared";
5
+ import { ORPCError, wrapAsyncIteratorPreservingEventMeta } from "@orpc/client";
6
+ // src/plugins/response-validation.ts
7
+ /**
8
+ * Validates server responses against contract schemas before your application uses them.
9
+ * This helps ensure the data returned by the server matches the types defined in your contract.
10
+ *
11
+ * @see {@link https://orpc.dev/docs/plugins/response-validation | Response Validation Plugin}
12
+ */
13
+ var ResponseValidationLinkPlugin = class {
14
+ contract;
15
+ name = "~response-validation";
16
+ constructor(contract) {
17
+ this.contract = contract;
18
+ }
19
+ init(options) {
20
+ return {
21
+ ...options,
22
+ interceptors: [
23
+ async ({ next, path }) => {
24
+ const procedure = getProcedureContractOrThrow(this.contract, path);
25
+ const reconcileError = async (error) => {
26
+ if (error instanceof ORPCError) return await reconcileORPCError(procedure["~orpc"].errorMap, error);
27
+ return error;
28
+ };
29
+ try {
30
+ const output = await next();
31
+ if (isAsyncIteratorObject(output))
32
+ /**
33
+ * Errors thrown while consuming the iterator must be reconciled too.
34
+ *
35
+ * @warning
36
+ * Remember use `override` for AsyncIteratorObject to remain other special properties
37
+ */
38
+ return override(output, wrapAsyncIteratorPreservingEventMeta(output, { mapError: reconcileError }));
39
+ return output;
40
+ } catch (error) {
41
+ throw await reconcileError(error);
42
+ }
43
+ },
44
+ ...toArray(options.interceptors),
45
+ async ({ next, path }) => {
46
+ const procedure = getProcedureContractOrThrow(this.contract, path);
47
+ const outputSchemas = toArray(procedure["~orpc"].outputSchemas);
48
+ let output = await next();
49
+ for (let i = outputSchemas.length - 1; i >= 0; i--) {
50
+ const result = await outputSchemas[i]["~standard"].validate(output);
51
+ if (result.issues) throw new ORPCError("INTERNAL_SERVER_ERROR", {
52
+ message: "Output validation failed",
53
+ cause: new ValidationError({
54
+ message: "Output validation failed",
55
+ issues: result.issues,
56
+ invalidData: output
57
+ })
58
+ });
59
+ output = result.value;
60
+ }
61
+ return output;
62
+ }
63
+ ]
64
+ };
65
+ }
66
+ };
67
+ export { ResponseValidationLinkPlugin };
@@ -0,0 +1,5 @@
1
+ import { ErrorMap, ORPCErrorFromErrorMap } from "./error.js";
2
+ import { AnySchema, InferSchemaInput, InferSchemaOutput } from "./schema.js";
3
+ import { ThrowableError } from "@orpc/shared";
4
+ import { Client, ClientContext } from "@orpc/client";
5
+ export type ProcedureContractClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | ThrowableError>;
@@ -0,0 +1,28 @@
1
+ import { ErrorMap } from "./error.js";
2
+ import { AnyMetaPlugin, Meta } from "./meta.js";
3
+ import { AnySchema } from "./schema.js";
4
+ export interface ProcedureContractDefinition<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
5
+ __TInputSchema?: {
6
+ type: TInputSchema;
7
+ };
8
+ __TOutputSchema?: {
9
+ type: TOutputSchema;
10
+ };
11
+ /**
12
+ * Non-serializable should be optional
13
+ */
14
+ inputSchemas?: AnySchema[] | undefined;
15
+ outputSchemas?: AnySchema[] | undefined;
16
+ metaPlugins?: AnyMetaPlugin[] | undefined;
17
+ errorMap: TErrorMap;
18
+ meta: Meta;
19
+ }
20
+ export declare class ProcedureContract<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
21
+ "~orpc": ProcedureContractDefinition<TInputSchema, TOutputSchema, TErrorMap>;
22
+ "constructor"(def: ProcedureContractDefinition<TInputSchema, TOutputSchema, TErrorMap>);
23
+ /**
24
+ * Checks if the given instance satisfies the {@link ProcedureContract} class/interface.
25
+ */
26
+ static [Symbol.hasInstance](instance: unknown): boolean;
27
+ }
28
+ export type AnyProcedureContract = ProcedureContract<any, any, any>;
@@ -0,0 +1,17 @@
1
+ import { isNonPrimitive } from "@orpc/shared";
2
+ // src/procedure.ts
3
+ var ProcedureContract = class ProcedureContract {
4
+ "~orpc";
5
+ "constructor"(def) {
6
+ this["~orpc"] = def;
7
+ }
8
+ /**
9
+ * Checks if the given instance satisfies the {@link ProcedureContract} class/interface.
10
+ */
11
+ static [Symbol.hasInstance](instance) {
12
+ if (Function.prototype[Symbol.hasInstance].call(this, instance)) return true;
13
+ if (this !== ProcedureContract) return false;
14
+ return isNonPrimitive(instance) && isNonPrimitive(instance["~orpc"]) && isNonPrimitive(instance["~orpc"].errorMap) && isNonPrimitive(instance["~orpc"].meta) && (instance["~orpc"].inputSchemas === void 0 || Array.isArray(instance["~orpc"].inputSchemas)) && (instance["~orpc"].outputSchemas === void 0 || Array.isArray(instance["~orpc"].outputSchemas));
15
+ }
16
+ };
17
+ export { ProcedureContract };
@@ -0,0 +1,11 @@
1
+ import { ProcedureContract } from "./procedure.js";
2
+ import { ProcedureContractClient } from "./procedure-client.js";
3
+ import { RouterContract } from "./router.js";
4
+ import { ClientContext } from "@orpc/client";
5
+ /**
6
+ * Client type inferred from a router contract, preserving its shape.
7
+ * Useful for typing a client without importing the server router.
8
+ *
9
+ * @see {@link https://orpc.dev/docs/client/client-side | Client-Side Clients}
10
+ */
11
+ export type RouterContractClient<TRouter extends RouterContract, TClientContext extends ClientContext = object> = TRouter extends ProcedureContract<infer UInputSchema, infer UOutputSchema, infer UErrorMap> ? ProcedureContractClient<TClientContext, UInputSchema, UOutputSchema, UErrorMap> : { [K in keyof TRouter]: TRouter[K] extends RouterContract ? RouterContractClient<TRouter[K], TClientContext> : never; };
@@ -0,0 +1,24 @@
1
+ import { ErrorMap } from "./error.js";
2
+ import { MergedErrorMap } from "./error-utils.js";
3
+ import { AnyMetaPlugin, Meta } from "./meta.js";
4
+ import { AnyProcedureContract, ProcedureContract } from "./procedure.js";
5
+ import { RouterContract } from "./router.js";
6
+ export type AugmentedContractRouter<T extends RouterContract, TErrorMap extends ErrorMap> = AugmentedContractRouterInternal<T, TErrorMap>;
7
+ export type AugmentedContractRouterInternal<T, TErrorMap extends ErrorMap> = T extends ProcedureContract<infer $InputSchema, infer $OutputSchema, infer $Errors> ? ProcedureContract<$InputSchema, $OutputSchema, MergedErrorMap<TErrorMap, $Errors>> : { [K in keyof T]: AugmentedContractRouterInternal<T[K] & {}, TErrorMap>; };
8
+ export interface AugmentContractRouterOptions<TErrorMap extends ErrorMap> {
9
+ meta: Meta;
10
+ metaPlugins?: AnyMetaPlugin[] | undefined;
11
+ errorMap: TErrorMap;
12
+ }
13
+ export declare function getRouterContract(router: RouterContract, path: readonly string[]): RouterContract | undefined;
14
+ export declare function getProcedureContractOrThrow(router: RouterContract, path: readonly string[]): AnyProcedureContract;
15
+ /**
16
+ * Minifies a router contract so it can be safely exported to the client
17
+ * without exposing internal logic.
18
+ *
19
+ * @remarks
20
+ * **Note**: Only the metadata needed by the client is preserved; all other data is stripped out.
21
+ *
22
+ * @see {@link https://orpc.dev/docs/contract/router | Router Contract}
23
+ */
24
+ export declare function minifyRouterContract(router: RouterContract): RouterContract;
@@ -0,0 +1,59 @@
1
+ import { mergeErrorMap } from "./error-utils.js";
2
+ import { resolveMetaPlugins } from "./meta-utils.js";
3
+ import { ProcedureContract } from "./procedure.js";
4
+ import { getOwn, isNonPrimitive, setOwn } from "@orpc/shared";
5
+ // src/router-utils.ts
6
+ /**
7
+ * Add capabilities without changing identity of the router contract
8
+ */
9
+ function augmentContractRouter(router, options) {
10
+ if (router instanceof ProcedureContract) {
11
+ const [meta, metaPlugins] = resolveMetaPlugins(options.meta, options.metaPlugins, router["~orpc"].metaPlugins);
12
+ return new ProcedureContract({
13
+ ...router["~orpc"],
14
+ errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
15
+ meta,
16
+ metaPlugins
17
+ });
18
+ }
19
+ if (!isNonPrimitive(router)) return router;
20
+ const enhanced = {};
21
+ for (const key of Object.keys(router)) setOwn(enhanced, key, augmentContractRouter(router[key], options));
22
+ return enhanced;
23
+ }
24
+ function getRouterContract(router, path) {
25
+ let current = router;
26
+ for (let i = 0; i < path.length; i++) {
27
+ const segment = path[i];
28
+ if (!isNonPrimitive(current)) return;
29
+ if (current instanceof ProcedureContract) return;
30
+ current = getOwn(current, segment);
31
+ }
32
+ if (!isNonPrimitive(current)) return;
33
+ return current;
34
+ }
35
+ function getProcedureContractOrThrow(router, path) {
36
+ const procedure = getRouterContract(router, path);
37
+ if (!(procedure instanceof ProcedureContract)) throw new TypeError(`No valid procedure found at path "${path.join(".")}", this may happen when the router contract is not properly configured.`);
38
+ return procedure;
39
+ }
40
+ /**
41
+ * Minifies a router contract so it can be safely exported to the client
42
+ * without exposing internal logic.
43
+ *
44
+ * @remarks
45
+ * **Note**: Only the metadata needed by the client is preserved; all other data is stripped out.
46
+ *
47
+ * @see {@link https://orpc.dev/docs/contract/router | Router Contract}
48
+ */
49
+ function minifyRouterContract(router) {
50
+ if (router instanceof ProcedureContract) return { "~orpc": {
51
+ errorMap: {},
52
+ meta: router["~orpc"].meta
53
+ } };
54
+ if (!isNonPrimitive(router)) return router;
55
+ const json = {};
56
+ for (const [key, child] of Object.entries(router)) setOwn(json, key, minifyRouterContract(child));
57
+ return json;
58
+ }
59
+ export { augmentContractRouter, getProcedureContractOrThrow, getRouterContract, minifyRouterContract };
@@ -0,0 +1,47 @@
1
+ import { ORPCErrorFromErrorMap } from "./error.js";
2
+ import { AnyProcedureContract, ProcedureContract } from "./procedure.js";
3
+ import { InferSchemaInput, InferSchemaOutput } from "./schema.js";
4
+ import { ThrowableError } from "@orpc/shared";
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> = InferRouterContractInputsInternal<T>;
19
+ export type InferRouterContractInputsInternal<T> = T extends ProcedureContract<infer UInputSchema, any, any> ? InferSchemaInput<UInputSchema> : { [K in keyof T]: InferRouterContractInputsInternal<T[K] & {}>; };
20
+ /**
21
+ * Infer the output types for each procedure-contract, preserving the router-contract shape.
22
+ *
23
+ * @see {@link https://orpc.dev/docs/contract/router#infer-router-contract-outputs | Router Contract - Infer Router Contract Outputs}
24
+ */
25
+ export type InferRouterContractOutputs<T extends RouterContract> = InferRouterContractOutputsInternal<T>;
26
+ export type InferRouterContractOutputsInternal<T> = T extends ProcedureContract<any, infer UOutputSchema, any> ? InferSchemaOutput<UOutputSchema> : { [K in keyof T]: InferRouterContractOutputsInternal<T[K] & {}>; };
27
+ /**
28
+ * Infer the union of error maps defined across the entire router-contract.
29
+ *
30
+ * @see {@link https://orpc.dev/docs/contract/router#infer-router-contract-error-map | Router Contract - Infer Router Contract Error Map}
31
+ */
32
+ export type InferRouterContractErrorMap<T extends RouterContract> = InferRouterContractErrorMapInternal<T>;
33
+ export type InferRouterContractErrorMapInternal<T> = T extends ProcedureContract<any, any, infer UErrorMap> ? UErrorMap : { [K in keyof T]-?: InferRouterContractErrorMapInternal<T[K] & {}>; }[keyof T];
34
+ /**
35
+ * Infer the union of throwable errors for entire router-contract.
36
+ *
37
+ * @see {@link https://orpc.dev/docs/contract/router#infer-router-contract-error | Router Contract - Infer Router Contract Error}
38
+ */
39
+ export type InferRouterContractError<T extends RouterContract> = InferRouterContractErrorInternal<T>;
40
+ export type InferRouterContractErrorInternal<T> = T extends ProcedureContract<any, any, infer UErrorMap> ? ORPCErrorFromErrorMap<UErrorMap> | ThrowableError : { [K in keyof T]-?: InferRouterContractErrorInternal<T[K] & {}>; }[keyof T];
41
+ /**
42
+ * Infer throwable errors for each procedure-contract, preserving the router-contract shape.
43
+ *
44
+ * @see {@link https://orpc.dev/docs/contract/router#infer-router-contract-errors | Router Contract - Infer Router Contract Errors}
45
+ */
46
+ export type InferRouterContractErrors<T extends RouterContract> = InferRouterContractErrorsInternal<T>;
47
+ export type InferRouterContractErrorsInternal<T> = T extends ProcedureContract<any, any, infer UErrorMap> ? ORPCErrorFromErrorMap<UErrorMap> | ThrowableError : { [K in keyof T]: InferRouterContractErrorsInternal<T[K] & {}>; };
@@ -0,0 +1,18 @@
1
+ import { AnySchema, Schema } from "./schema.js";
2
+ import { AsyncIteratorClass } from "@orpc/shared";
3
+ /**
4
+ * Defines a schema for an AsyncIteratorObject, validating each yielded value
5
+ * (and optionally the return value) with the given schemas.
6
+ *
7
+ * @see {@link https://orpc.dev/docs/async-iterator-object | AsyncIteratorObject (SSE)}
8
+ */
9
+ 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>>;
10
+ export interface AsyncIteratorObjectSchemasDetails {
11
+ yieldSchemas: AnySchema[];
12
+ returnSchemas: AnySchema[];
13
+ }
14
+ /**
15
+ * Collects the yield and return schemas of multiple schemas, such as the `.output` schemas of a procedure,
16
+ * or returns `undefined` when there are none or any of them does not describe an AsyncIteratorObject.
17
+ */
18
+ export declare function getAsyncIteratorObjectSchemasDetails(schemas: readonly AnySchema[] | undefined): undefined | AsyncIteratorObjectSchemasDetails;
@@ -0,0 +1,69 @@
1
+ import { ValidationError } from "./error.js";
2
+ import { ORPC_NAME, isAsyncIteratorObject, override } from "@orpc/shared";
3
+ import { ORPCError, wrapAsyncIteratorPreservingEventMeta } from "@orpc/client";
4
+ // src/schema-built-in.ts
5
+ const ASYNC_ITERATOR_OBJECT_SCHEMA_DETAILS_SYMBOL = /* @__PURE__ */ Symbol.for("ORPC_ASYNC_ITERATOR_OBJECT_SCHEMA_DETAILS");
6
+ /**
7
+ * Defines a schema for an AsyncIteratorObject, validating each yielded value
8
+ * (and optionally the return value) with the given schemas.
9
+ *
10
+ * @see {@link https://orpc.dev/docs/async-iterator-object | AsyncIteratorObject (SSE)}
11
+ */
12
+ function asyncIteratorObject(yieldSchema, returnSchema) {
13
+ return { "~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)) return { issues: [{
22
+ message: "Expect AsyncIteratorObject",
23
+ path: []
24
+ }] };
25
+ const mapped = wrapAsyncIteratorPreservingEventMeta(iterator, { async mapResult(result) {
26
+ const schema = result.done ? returnSchema : yieldSchema;
27
+ if (!schema) return result;
28
+ const validated = await schema["~standard"].validate(result.value);
29
+ if (validated.issues) throw new ORPCError("ASYNC_ITERATOR_OBJECT_VALIDATION_FAILED", {
30
+ message: "AsyncIteratorObject validation failed",
31
+ cause: new ValidationError({
32
+ issues: validated.issues,
33
+ message: "AsyncIteratorObject validation failed",
34
+ invalidData: result.value
35
+ })
36
+ });
37
+ return {
38
+ done: result.done,
39
+ value: validated.value
40
+ };
41
+ } });
42
+ return { value: override(iterator, mapped) };
43
+ }
44
+ } };
45
+ }
46
+ function getAsyncIteratorObjectSchemaDetails(schema) {
47
+ if (schema === void 0) return;
48
+ return schema["~standard"][ASYNC_ITERATOR_OBJECT_SCHEMA_DETAILS_SYMBOL];
49
+ }
50
+ /**
51
+ * Collects the yield and return schemas of multiple schemas, such as the `.output` schemas of a procedure,
52
+ * or returns `undefined` when there are none or any of them does not describe an AsyncIteratorObject.
53
+ */
54
+ function getAsyncIteratorObjectSchemasDetails(schemas) {
55
+ if (!schemas || schemas.length === 0) return;
56
+ const yieldSchemas = [];
57
+ const returnSchemas = [];
58
+ for (const schema of schemas) {
59
+ const details = getAsyncIteratorObjectSchemaDetails(schema);
60
+ if (!details) return;
61
+ yieldSchemas.push(details.yieldSchema);
62
+ if (details.returnSchema) returnSchemas.push(details.returnSchema);
63
+ }
64
+ return {
65
+ yieldSchemas,
66
+ returnSchemas
67
+ };
68
+ }
69
+ export { asyncIteratorObject, getAsyncIteratorObjectSchemaDetails, getAsyncIteratorObjectSchemasDetails };
@@ -0,0 +1,20 @@
1
+ import { Schema, SchemaIssue } from "./schema.js";
2
+ import { IsEqual, Promisable } from "@orpc/shared";
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,36 @@
1
+ import { ORPC_NAME, isNonPrimitive, isPropertyKey } from "@orpc/shared";
2
+ // src/schema-utils.ts
3
+ /**
4
+ * Create a schema for things can be trust without validation.
5
+ * You can optionally pass a map function for mapping
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const normal = type<number>()
10
+ * const withMap = type<number, string>(input => input.toString())
11
+ *```
12
+ *
13
+ * @see {@link https://orpc.dev/docs/procedure#type-utility | Procedure - type Utility}
14
+ */
15
+ function type(...[map]) {
16
+ return { "~standard": {
17
+ vendor: ORPC_NAME,
18
+ version: 1,
19
+ async validate(value) {
20
+ if (map) return { value: await map(value) };
21
+ return { value };
22
+ }
23
+ } };
24
+ }
25
+ /**
26
+ * Check if the given issue is following the standard-schema issue format.
27
+ */
28
+ function isSchemaIssue(issue) {
29
+ if (!isNonPrimitive(issue) || typeof issue.message !== "string") return false;
30
+ if (issue.path !== void 0) {
31
+ if (!Array.isArray(issue.path)) return false;
32
+ if (!issue.path.every((segment) => isPropertyKey(segment) || isNonPrimitive(segment) && isPropertyKey(segment.key))) return false;
33
+ }
34
+ return true;
35
+ }
36
+ export { isSchemaIssue, type };
@@ -0,0 +1,25 @@
1
+ import { 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 unknown ? 0 extends 1 & T ? unknown : NonNullable<T["~standard"]["types"]>["input"] : 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 unknown ? 0 extends 1 & T ? unknown : NonNullable<T["~standard"]["types"]>["output"] : 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/package.json CHANGED
@@ -1,46 +1,56 @@
1
1
  {
2
2
  "name": "@orpc/contract",
3
- "type": "module",
4
- "version": "2.0.0-beta.5",
5
- "license": "MIT",
3
+ "version": "2.0.0-beta.51",
4
+ "description": "Define typesafe API contracts as the single source of truth for oRPC, powered by Standard Schema",
5
+ "keywords": [
6
+ "api",
7
+ "contract",
8
+ "contract-first",
9
+ "openapi",
10
+ "orpc",
11
+ "rpc",
12
+ "standard-schema",
13
+ "typesafe",
14
+ "typescript"
15
+ ],
6
16
  "homepage": "https://orpc.dev",
17
+ "license": "MIT",
7
18
  "repository": {
8
19
  "type": "git",
9
20
  "url": "git+https://github.com/middleapi/orpc.git",
10
21
  "directory": "packages/contract"
11
22
  },
12
- "keywords": [
13
- "orpc"
23
+ "funding": [
24
+ "https://github.com/sponsors/dinwwwh",
25
+ "https://opencollective.com/middleapi"
14
26
  ],
27
+ "files": [
28
+ "dist"
29
+ ],
30
+ "type": "module",
15
31
  "sideEffects": false,
16
32
  "exports": {
17
33
  "./package.json": "./package.json",
18
34
  ".": {
19
- "types": "./dist/index.d.mts",
20
- "import": "./dist/index.mjs",
21
- "default": "./dist/index.mjs"
35
+ "types": "./dist/index.d.ts",
36
+ "import": "./dist/index.js",
37
+ "default": "./dist/index.js"
22
38
  },
23
39
  "./plugins": {
24
- "types": "./dist/plugins/index.d.mts",
25
- "import": "./dist/plugins/index.mjs",
26
- "default": "./dist/plugins/index.mjs"
40
+ "types": "./dist/plugins/index.d.ts",
41
+ "import": "./dist/plugins/index.js",
42
+ "default": "./dist/plugins/index.js"
27
43
  }
28
44
  },
29
- "files": [
30
- "dist"
31
- ],
32
45
  "dependencies": {
33
- "@standard-schema/spec": "^1.1.0",
34
- "@orpc/shared": "2.0.0-beta.5",
35
- "@orpc/client": "2.0.0-beta.5"
46
+ "@orpc/client": "2.0.0-beta.51",
47
+ "@orpc/shared": "2.0.0-beta.51",
48
+ "@standard-schema/spec": "^1.1.0"
36
49
  },
37
50
  "devDependencies": {
38
- "arktype": "^2.1.29",
39
- "valibot": "^1.2.0",
40
- "zod": "^4.4.3"
51
+ "arktype": "^2.2.9",
52
+ "valibot": "^1.5.0",
53
+ "zod": "^4.6.7"
41
54
  },
42
- "scripts": {
43
- "build": "unbuild",
44
- "type:check": "tsc -b"
45
- }
55
+ "scripts": {}
46
56
  }