@orpc/contract 2.0.0-beta.5 → 2.0.0-beta.50
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/README.md +137 -102
- package/dist/builder-variants.d.ts +103 -0
- package/dist/builder.d.ts +78 -0
- package/dist/builder.js +108 -0
- package/dist/client-factory.d.ts +24 -0
- package/dist/client-factory.js +38 -0
- package/dist/error-factory.d.ts +77 -0
- package/dist/error-factory.js +109 -0
- package/dist/error-utils.d.ts +5 -0
- package/dist/error-utils.js +37 -0
- package/dist/error.d.ts +41 -0
- package/dist/error.js +22 -0
- package/dist/index.d.ts +20 -182
- package/dist/index.js +12 -0
- package/dist/meta-built-in.d.ts +30 -0
- package/dist/meta-built-in.js +45 -0
- package/dist/meta-utils.d.ts +55 -0
- package/dist/meta-utils.js +70 -0
- package/dist/meta.d.ts +53 -0
- package/dist/meta.js +11 -0
- package/dist/plugins/index.d.ts +3 -38
- package/dist/plugins/index.js +2 -0
- package/dist/plugins/request-validation.d.ts +28 -0
- package/dist/plugins/request-validation.js +50 -0
- package/dist/plugins/response-validation.d.ts +15 -0
- package/dist/plugins/response-validation.js +67 -0
- package/dist/procedure-client.d.ts +5 -0
- package/dist/procedure.d.ts +28 -0
- package/dist/procedure.js +17 -0
- package/dist/router-client.d.ts +11 -0
- package/dist/router-utils.d.ts +23 -0
- package/dist/router-utils.js +59 -0
- package/dist/router.d.ts +42 -0
- package/dist/schema-built-in.d.ts +18 -0
- package/dist/schema-built-in.js +69 -0
- package/dist/schema-utils.d.ts +20 -0
- package/dist/schema-utils.js +36 -0
- package/dist/schema.d.ts +25 -0
- package/package.json +34 -24
- package/dist/index.d.mts +0 -182
- package/dist/index.mjs +0 -193
- package/dist/plugins/index.d.mts +0 -38
- package/dist/plugins/index.mjs +0 -90
- package/dist/shared/contract.CW-2wl1i.mjs +0 -180
- package/dist/shared/contract.Do92aRJ4.d.mts +0 -126
- 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 { getConstructor, 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 (this !== ProcedureContract) return Function.prototype[Symbol.hasInstance].call(this, instance);
|
|
13
|
+
if (getConstructor(instance) === ProcedureContract) return true;
|
|
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,23 @@
|
|
|
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> = T extends ProcedureContract<infer $InputSchema, infer $OutputSchema, infer $Errors> ? ProcedureContract<$InputSchema, $OutputSchema, MergedErrorMap<TErrorMap, $Errors>> : { [K in keyof T]: T[K] extends RouterContract ? AugmentedContractRouter<T[K], TErrorMap> : never; };
|
|
7
|
+
export interface AugmentContractRouterOptions<TErrorMap extends ErrorMap> {
|
|
8
|
+
meta: Meta;
|
|
9
|
+
metaPlugins?: AnyMetaPlugin[] | undefined;
|
|
10
|
+
errorMap: TErrorMap;
|
|
11
|
+
}
|
|
12
|
+
export declare function getRouterContract(router: RouterContract, path: readonly string[]): RouterContract | undefined;
|
|
13
|
+
export declare function getProcedureContractOrThrow(router: RouterContract, path: readonly string[]): AnyProcedureContract;
|
|
14
|
+
/**
|
|
15
|
+
* Minifies a router contract so it can be safely exported to the client
|
|
16
|
+
* without exposing internal logic.
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* **Note**: Only the metadata needed by the client is preserved; all other data is stripped out.
|
|
20
|
+
*
|
|
21
|
+
* @see {@link https://orpc.dev/docs/contract/router | Router Contract}
|
|
22
|
+
*/
|
|
23
|
+
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 };
|
package/dist/router.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
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> = T extends ProcedureContract<infer UInputSchema, any, any> ? InferSchemaInput<UInputSchema> : { [K in keyof T]: T[K] extends RouterContract ? InferRouterContractInputs<T[K]> : never; };
|
|
19
|
+
/**
|
|
20
|
+
* Infer the output types for each procedure-contract, preserving the router-contract shape.
|
|
21
|
+
*
|
|
22
|
+
* @see {@link https://orpc.dev/docs/contract/router#infer-router-contract-outputs | Router Contract - Infer Router Contract Outputs}
|
|
23
|
+
*/
|
|
24
|
+
export type InferRouterContractOutputs<T extends RouterContract> = T extends ProcedureContract<any, infer UOutputSchema, any> ? InferSchemaOutput<UOutputSchema> : { [K in keyof T]: T[K] extends RouterContract ? InferRouterContractOutputs<T[K]> : never; };
|
|
25
|
+
/**
|
|
26
|
+
* Infer the union of error maps defined across the entire router-contract.
|
|
27
|
+
*
|
|
28
|
+
* @see {@link https://orpc.dev/docs/contract/router#infer-router-contract-error-map | Router Contract - Infer Router Contract Error Map}
|
|
29
|
+
*/
|
|
30
|
+
export type InferRouterContractErrorMap<T extends RouterContract> = T extends ProcedureContract<any, any, infer UErrorMap> ? UErrorMap : { [K in keyof T]: T[K] extends RouterContract ? InferRouterContractErrorMap<T[K]> : never; }[keyof T];
|
|
31
|
+
/**
|
|
32
|
+
* Infer the union of throwable errors for entire router-contract.
|
|
33
|
+
*
|
|
34
|
+
* @see {@link https://orpc.dev/docs/contract/router#infer-router-contract-error | Router Contract - Infer Router Contract Error}
|
|
35
|
+
*/
|
|
36
|
+
export type InferRouterContractError<T extends RouterContract> = T extends ProcedureContract<any, any, infer UErrorMap> ? ORPCErrorFromErrorMap<UErrorMap> | ThrowableError : { [K in keyof T]: T[K] extends RouterContract ? InferRouterContractError<T[K]> : never; }[keyof T];
|
|
37
|
+
/**
|
|
38
|
+
* Infer throwable errors for each procedure-contract, preserving the router-contract shape.
|
|
39
|
+
*
|
|
40
|
+
* @see {@link https://orpc.dev/docs/contract/router#infer-router-contract-errors | Router Contract - Infer Router Contract Errors}
|
|
41
|
+
*/
|
|
42
|
+
export type InferRouterContractErrors<T extends RouterContract> = T extends ProcedureContract<any, any, infer UErrorMap> ? ORPCErrorFromErrorMap<UErrorMap> | ThrowableError : { [K in keyof T]: T[K] extends RouterContract ? InferRouterContractErrors<T[K]> : never; };
|
|
@@ -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 };
|
package/dist/schema.d.ts
ADDED
|
@@ -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 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/package.json
CHANGED
|
@@ -1,46 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/contract",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "2.0.0-beta.50",
|
|
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
|
-
"
|
|
13
|
-
"
|
|
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.
|
|
20
|
-
"import": "./dist/index.
|
|
21
|
-
"default": "./dist/index.
|
|
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.
|
|
25
|
-
"import": "./dist/plugins/index.
|
|
26
|
-
"default": "./dist/plugins/index.
|
|
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
|
-
"@
|
|
34
|
-
"@orpc/shared": "2.0.0-beta.
|
|
35
|
-
"@
|
|
46
|
+
"@orpc/client": "2.0.0-beta.50",
|
|
47
|
+
"@orpc/shared": "2.0.0-beta.50",
|
|
48
|
+
"@standard-schema/spec": "^1.1.0"
|
|
36
49
|
},
|
|
37
50
|
"devDependencies": {
|
|
38
|
-
"arktype": "^2.
|
|
39
|
-
"valibot": "^1.
|
|
40
|
-
"zod": "^4.
|
|
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
|
}
|