@orpc/contract 2.0.0-beta.48 → 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.
- package/README.md +4 -4
- package/dist/builder-variants.d.ts +103 -0
- package/dist/builder.d.ts +78 -0
- package/dist/builder.js +112 -0
- package/dist/client-factory.d.ts +24 -0
- package/dist/client-factory.js +38 -0
- package/dist/error-factory.d.ts +80 -0
- package/dist/error-factory.js +121 -0
- package/dist/error-utils.d.ts +5 -0
- package/dist/error-utils.js +40 -0
- package/dist/error.d.ts +45 -0
- package/dist/error.js +20 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.js +31 -0
- package/dist/meta-built-in.d.ts +30 -0
- package/dist/meta-built-in.js +54 -0
- package/dist/meta-utils.d.ts +58 -0
- package/dist/meta-utils.js +76 -0
- package/dist/meta.d.ts +53 -0
- package/dist/meta.js +13 -0
- package/dist/plugins/index.d.ts +12 -0
- package/dist/plugins/index.js +12 -0
- package/dist/plugins/request-validation.d.ts +28 -0
- package/dist/plugins/request-validation.js +55 -0
- package/dist/plugins/response-validation.d.ts +15 -0
- package/dist/plugins/response-validation.js +72 -0
- package/dist/procedure-client.d.ts +5 -0
- package/dist/procedure.d.ts +28 -0
- package/dist/procedure.js +28 -0
- package/dist/router-client.d.ts +13 -0
- package/dist/router-utils.d.ts +30 -0
- package/dist/router-utils.js +79 -0
- package/dist/router.d.ts +52 -0
- package/dist/schema-built-in.d.ts +23 -0
- package/dist/schema-built-in.js +76 -0
- package/dist/schema-utils.d.ts +20 -0
- package/dist/schema-utils.js +44 -0
- package/dist/schema.d.ts +25 -0
- package/package.json +28 -30
- package/dist/builder-variants.d.mts +0 -113
- package/dist/builder.d.mts +0 -78
- package/dist/builder.mjs +0 -71
- package/dist/client-factory.d.mts +0 -24
- package/dist/client-factory.mjs +0 -34
- package/dist/error-factory.d.mts +0 -87
- package/dist/error-factory.mjs +0 -75
- package/dist/error-utils.d.mts +0 -11
- package/dist/error-utils.mjs +0 -43
- package/dist/error.d.mts +0 -41
- package/dist/error.mjs +0 -12
- package/dist/index.d.mts +0 -62
- package/dist/index.mjs +0 -25
- package/dist/meta-built-in.d.mts +0 -38
- package/dist/meta-built-in.mjs +0 -40
- package/dist/meta-utils.d.mts +0 -62
- package/dist/meta-utils.mjs +0 -33
- package/dist/meta.d.mts +0 -61
- package/dist/meta.mjs +0 -11
- package/dist/plugins/index.d.mts +0 -12
- package/dist/plugins/index.mjs +0 -8
- package/dist/plugins/request-validation.d.mts +0 -28
- package/dist/plugins/request-validation.mjs +0 -46
- package/dist/plugins/response-validation.d.mts +0 -15
- package/dist/plugins/response-validation.mjs +0 -53
- package/dist/procedure-client.d.mts +0 -10
- package/dist/procedure.d.mts +0 -36
- package/dist/procedure.mjs +0 -19
- package/dist/router-client.d.mts +0 -14
- package/dist/router-utils.d.mts +0 -34
- package/dist/router-utils.mjs +0 -67
- package/dist/router.d.mts +0 -42
- package/dist/schema-built-in.d.mts +0 -28
- package/dist/schema-built-in.mjs +0 -73
- package/dist/schema-utils.d.mts +0 -26
- package/dist/schema-utils.mjs +0 -29
- package/dist/schema.d.mts +0 -31
- /package/dist/{builder-variants.mjs → builder-variants.js} +0 -0
- /package/dist/{procedure-client.mjs → procedure-client.js} +0 -0
- /package/dist/{router-client.mjs → router-client.js} +0 -0
- /package/dist/{router.mjs → router.js} +0 -0
- /package/dist/{schema.mjs → schema.js} +0 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ErrorMap } from './error.ts';
|
|
2
|
+
import type { AnyMetaPlugin, Meta } from './meta.ts';
|
|
3
|
+
import type { AnySchema } from './schema.ts';
|
|
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,28 @@
|
|
|
1
|
+
import { getConstructor, isNonPrimitive } from '@orpc/shared';
|
|
2
|
+
export class ProcedureContract {
|
|
3
|
+
'~orpc';
|
|
4
|
+
constructor(def) {
|
|
5
|
+
this['~orpc'] = def;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Checks if the given instance satisfies the {@link ProcedureContract} class/interface.
|
|
9
|
+
*/
|
|
10
|
+
static [Symbol.hasInstance](instance) {
|
|
11
|
+
if (this !== ProcedureContract) {
|
|
12
|
+
// fallback to default instanceof check if this is extended class
|
|
13
|
+
return Function.prototype[Symbol.hasInstance].call(this, instance);
|
|
14
|
+
}
|
|
15
|
+
const constructor = getConstructor(instance);
|
|
16
|
+
if (constructor === ProcedureContract) {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
return (isNonPrimitive(instance) &&
|
|
20
|
+
isNonPrimitive(instance['~orpc']) &&
|
|
21
|
+
isNonPrimitive(instance['~orpc'].errorMap) &&
|
|
22
|
+
isNonPrimitive(instance['~orpc'].meta) &&
|
|
23
|
+
(instance['~orpc'].inputSchemas === undefined ||
|
|
24
|
+
Array.isArray(instance['~orpc'].inputSchemas)) &&
|
|
25
|
+
(instance['~orpc'].outputSchemas === undefined ||
|
|
26
|
+
Array.isArray(instance['~orpc'].outputSchemas)));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ClientContext } from '@orpc/client';
|
|
2
|
+
import type { ProcedureContractClient } from './procedure-client.ts';
|
|
3
|
+
import type { ProcedureContract } from './procedure.ts';
|
|
4
|
+
import type { RouterContract } from './router.ts';
|
|
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> : {
|
|
12
|
+
[K in keyof TRouter]: TRouter[K] extends RouterContract ? RouterContractClient<TRouter[K], TClientContext> : never;
|
|
13
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { MergedErrorMap } from './error-utils.ts';
|
|
2
|
+
import type { ErrorMap } from './error.ts';
|
|
3
|
+
import type { AnyMetaPlugin, Meta } from './meta.ts';
|
|
4
|
+
import type { AnyProcedureContract } from './procedure.ts';
|
|
5
|
+
import { ProcedureContract } from './procedure.ts';
|
|
6
|
+
import type { RouterContract } from './router.ts';
|
|
7
|
+
export type AugmentedContractRouter<T extends RouterContract, TErrorMap extends ErrorMap> = T extends ProcedureContract<infer $InputSchema, infer $OutputSchema, infer $Errors> ? ProcedureContract<$InputSchema, $OutputSchema, MergedErrorMap<TErrorMap, $Errors>> : {
|
|
8
|
+
[K in keyof T]: T[K] extends RouterContract ? AugmentedContractRouter<T[K], TErrorMap> : never;
|
|
9
|
+
};
|
|
10
|
+
export interface AugmentContractRouterOptions<TErrorMap extends ErrorMap> {
|
|
11
|
+
meta: Meta;
|
|
12
|
+
metaPlugins?: AnyMetaPlugin[] | undefined;
|
|
13
|
+
errorMap: TErrorMap;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Add capabilities without changing identity of the router contract
|
|
17
|
+
*/
|
|
18
|
+
export declare function augmentContractRouter<T extends RouterContract, TErrorMap extends ErrorMap>(router: T, options: AugmentContractRouterOptions<TErrorMap>): AugmentedContractRouter<T, TErrorMap>;
|
|
19
|
+
export declare function getRouterContract(router: RouterContract, path: readonly string[]): RouterContract | undefined;
|
|
20
|
+
export declare function getProcedureContractOrThrow(router: RouterContract, path: readonly string[]): AnyProcedureContract;
|
|
21
|
+
/**
|
|
22
|
+
* Minifies a router contract so it can be safely exported to the client
|
|
23
|
+
* without exposing internal logic.
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* **Note**: Only the metadata needed by the client is preserved; all other data is stripped out.
|
|
27
|
+
*
|
|
28
|
+
* @see {@link https://orpc.dev/docs/contract/router | Router Contract}
|
|
29
|
+
*/
|
|
30
|
+
export declare function minifyRouterContract(router: RouterContract): RouterContract;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { getOwn, isNonPrimitive, setOwn } from '@orpc/shared';
|
|
2
|
+
import { mergeErrorMap } from './error-utils.js';
|
|
3
|
+
import { resolveMetaPlugins } from './meta-utils.js';
|
|
4
|
+
import { ProcedureContract } from './procedure.js';
|
|
5
|
+
/**
|
|
6
|
+
* Add capabilities without changing identity of the router contract
|
|
7
|
+
*/
|
|
8
|
+
export function augmentContractRouter(router, options) {
|
|
9
|
+
if (router instanceof ProcedureContract) {
|
|
10
|
+
const [meta, metaPlugins] = resolveMetaPlugins(options.meta, options.metaPlugins, router['~orpc'].metaPlugins);
|
|
11
|
+
const enhanced = new ProcedureContract({
|
|
12
|
+
...router['~orpc'],
|
|
13
|
+
errorMap: mergeErrorMap(options.errorMap, router['~orpc'].errorMap),
|
|
14
|
+
meta,
|
|
15
|
+
metaPlugins,
|
|
16
|
+
});
|
|
17
|
+
return enhanced;
|
|
18
|
+
}
|
|
19
|
+
if (!isNonPrimitive(router)) {
|
|
20
|
+
return router;
|
|
21
|
+
}
|
|
22
|
+
const enhanced = {};
|
|
23
|
+
for (const key of Object.keys(router)) {
|
|
24
|
+
setOwn(enhanced, key, augmentContractRouter(router[key], options));
|
|
25
|
+
}
|
|
26
|
+
return enhanced;
|
|
27
|
+
}
|
|
28
|
+
export function getRouterContract(router, path) {
|
|
29
|
+
let current = router;
|
|
30
|
+
for (let i = 0; i < path.length; i++) {
|
|
31
|
+
const segment = path[i];
|
|
32
|
+
if (!isNonPrimitive(current)) {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
if (current instanceof ProcedureContract) {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
current = getOwn(current, segment);
|
|
39
|
+
}
|
|
40
|
+
if (!isNonPrimitive(current)) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
return current;
|
|
44
|
+
}
|
|
45
|
+
export function getProcedureContractOrThrow(router, path) {
|
|
46
|
+
const procedure = getRouterContract(router, path);
|
|
47
|
+
if (!(procedure instanceof ProcedureContract)) {
|
|
48
|
+
throw new TypeError(`No valid procedure found at path "${path.join('.')}", this may happen when the router contract is not properly configured.`);
|
|
49
|
+
}
|
|
50
|
+
return procedure;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Minifies a router contract so it can be safely exported to the client
|
|
54
|
+
* without exposing internal logic.
|
|
55
|
+
*
|
|
56
|
+
* @remarks
|
|
57
|
+
* **Note**: Only the metadata needed by the client is preserved; all other data is stripped out.
|
|
58
|
+
*
|
|
59
|
+
* @see {@link https://orpc.dev/docs/contract/router | Router Contract}
|
|
60
|
+
*/
|
|
61
|
+
export function minifyRouterContract(router) {
|
|
62
|
+
if (router instanceof ProcedureContract) {
|
|
63
|
+
const procedure = {
|
|
64
|
+
'~orpc': {
|
|
65
|
+
errorMap: {},
|
|
66
|
+
meta: router['~orpc'].meta,
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
return procedure;
|
|
70
|
+
}
|
|
71
|
+
if (!isNonPrimitive(router)) {
|
|
72
|
+
return router;
|
|
73
|
+
}
|
|
74
|
+
const json = {};
|
|
75
|
+
for (const [key, child] of Object.entries(router)) {
|
|
76
|
+
setOwn(json, key, minifyRouterContract(child));
|
|
77
|
+
}
|
|
78
|
+
return json;
|
|
79
|
+
}
|
package/dist/router.d.ts
ADDED
|
@@ -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
|
+
};
|
|
@@ -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
|
+
}
|
package/dist/schema.d.ts
ADDED
|
@@ -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/package.json
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/contract",
|
|
3
|
-
"
|
|
4
|
-
"version": "2.0.0-beta.48",
|
|
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
|
-
"
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
"
|
|
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
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
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.
|
|
33
|
-
"import": "./dist/index.
|
|
34
|
-
"default": "./dist/index.
|
|
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.
|
|
38
|
-
"import": "./dist/plugins/index.
|
|
39
|
-
"default": "./dist/plugins/index.
|
|
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
|
-
"@orpc/shared": "2.0.0-beta.
|
|
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": {
|
|
@@ -52,7 +52,5 @@
|
|
|
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
|
}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import type { InitialInputSchema, InitialOutputSchema } from "./builder.mjs";
|
|
2
|
-
import type { ErrorMap } from "./error.mjs";
|
|
3
|
-
import type { MergedErrorMap } from "./error-utils.mjs";
|
|
4
|
-
import type { MetaPlugin } from "./meta.mjs";
|
|
5
|
-
import type { ProcedureContract } from "./procedure.mjs";
|
|
6
|
-
import type { AnySchema, MergedSchema } from "./schema.mjs";
|
|
7
|
-
/**
|
|
8
|
-
* The contract builder variant returned after `.input` is called.
|
|
9
|
-
* It is already a complete procedure contract that can still be refined.
|
|
10
|
-
*
|
|
11
|
-
* @see {@link https://orpc.dev/docs/contract/procedure | Procedure Contract}
|
|
12
|
-
*/
|
|
13
|
-
export interface ProcedureContractBuilderWithInput<
|
|
14
|
-
TInputSchema extends AnySchema,
|
|
15
|
-
TErrorMap extends ErrorMap
|
|
16
|
-
> extends ProcedureContract<TInputSchema, InitialOutputSchema, TErrorMap> {
|
|
17
|
-
/**
|
|
18
|
-
* Applies metadata plugins to contracts built from this builder.
|
|
19
|
-
*
|
|
20
|
-
* @see {@link https://orpc.dev/docs/contract/procedure#metadata | Procedure Contract - Metadata}
|
|
21
|
-
*/
|
|
22
|
-
meta(...plugins: MetaPlugin<TInputSchema, InitialOutputSchema, TErrorMap>[]): ProcedureContractBuilderWithInput<TInputSchema, TErrorMap>;
|
|
23
|
-
/**
|
|
24
|
-
* Defines typesafe errors that implementations of this contract can throw.
|
|
25
|
-
*
|
|
26
|
-
* @see {@link https://orpc.dev/docs/contract/procedure#typesafe-errors | Procedure Contract - Typesafe Errors}
|
|
27
|
-
*/
|
|
28
|
-
errors<T extends ErrorMap>(errors: T): ProcedureContractBuilderWithInput<TInputSchema, MergedErrorMap<TErrorMap, T>>;
|
|
29
|
-
/**
|
|
30
|
-
* Adds an additional input schema, merged with the previously defined one.
|
|
31
|
-
*
|
|
32
|
-
* @see {@link https://orpc.dev/docs/contract/procedure#multiple-schemas | Procedure Contract - Multiple Schemas}
|
|
33
|
-
*/
|
|
34
|
-
input<T extends AnySchema>(schema: T): ProcedureContractBuilderWithInput<MergedSchema<T, TInputSchema>, TErrorMap>;
|
|
35
|
-
/**
|
|
36
|
-
* Defines the output schema used to validate and type the procedure output.
|
|
37
|
-
*
|
|
38
|
-
* @see {@link https://orpc.dev/docs/contract/procedure#inputoutput-validation | Procedure Contract - Input/Output Validation}
|
|
39
|
-
*/
|
|
40
|
-
output<T extends AnySchema>(schema: T): ProcedureContractBuilderWithInputOutput<TInputSchema, T, TErrorMap>;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* The contract builder variant returned after `.output` is called.
|
|
44
|
-
* It is already a complete procedure contract that can still be refined.
|
|
45
|
-
*
|
|
46
|
-
* @see {@link https://orpc.dev/docs/contract/procedure | Procedure Contract}
|
|
47
|
-
*/
|
|
48
|
-
export interface ProcedureContractBuilderWithOutput<
|
|
49
|
-
TOutputSchema extends AnySchema,
|
|
50
|
-
TErrorMap extends ErrorMap
|
|
51
|
-
> extends ProcedureContract<InitialInputSchema, TOutputSchema, TErrorMap> {
|
|
52
|
-
/**
|
|
53
|
-
* Applies metadata plugins to contracts built from this builder.
|
|
54
|
-
*
|
|
55
|
-
* @see {@link https://orpc.dev/docs/contract/procedure#metadata | Procedure Contract - Metadata}
|
|
56
|
-
*/
|
|
57
|
-
meta(...plugins: MetaPlugin<InitialInputSchema, TOutputSchema, TErrorMap>[]): ProcedureContractBuilderWithOutput<TOutputSchema, TErrorMap>;
|
|
58
|
-
/**
|
|
59
|
-
* Defines typesafe errors that implementations of this contract can throw.
|
|
60
|
-
*
|
|
61
|
-
* @see {@link https://orpc.dev/docs/contract/procedure#typesafe-errors | Procedure Contract - Typesafe Errors}
|
|
62
|
-
*/
|
|
63
|
-
errors<T extends ErrorMap>(errors: T): ProcedureContractBuilderWithOutput<TOutputSchema, MergedErrorMap<TErrorMap, T>>;
|
|
64
|
-
/**
|
|
65
|
-
* Defines the input schema used to validate and type the procedure input.
|
|
66
|
-
*
|
|
67
|
-
* @see {@link https://orpc.dev/docs/contract/procedure#inputoutput-validation | Procedure Contract - Input/Output Validation}
|
|
68
|
-
*/
|
|
69
|
-
input<T extends AnySchema>(schema: T): ProcedureContractBuilderWithInputOutput<T, TOutputSchema, TErrorMap>;
|
|
70
|
-
/**
|
|
71
|
-
* Adds an additional output schema, merged with the previously defined one.
|
|
72
|
-
*
|
|
73
|
-
* @see {@link https://orpc.dev/docs/contract/procedure#multiple-schemas | Procedure Contract - Multiple Schemas}
|
|
74
|
-
*/
|
|
75
|
-
output<T extends AnySchema>(schema: T): ProcedureContractBuilderWithOutput<MergedSchema<T, TOutputSchema>, TErrorMap>;
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* The contract builder variant returned after both `.input` and `.output`
|
|
79
|
-
* are called. It is already a complete procedure contract that can still be
|
|
80
|
-
* refined.
|
|
81
|
-
*
|
|
82
|
-
* @see {@link https://orpc.dev/docs/contract/procedure | Procedure Contract}
|
|
83
|
-
*/
|
|
84
|
-
export interface ProcedureContractBuilderWithInputOutput<
|
|
85
|
-
TInputSchema extends AnySchema,
|
|
86
|
-
TOutputSchema extends AnySchema,
|
|
87
|
-
TErrorMap extends ErrorMap
|
|
88
|
-
> extends ProcedureContract<TInputSchema, TOutputSchema, TErrorMap> {
|
|
89
|
-
/**
|
|
90
|
-
* Applies metadata plugins to contracts built from this builder.
|
|
91
|
-
*
|
|
92
|
-
* @see {@link https://orpc.dev/docs/contract/procedure#metadata | Procedure Contract - Metadata}
|
|
93
|
-
*/
|
|
94
|
-
meta(...plugins: MetaPlugin<TInputSchema, TOutputSchema, TErrorMap>[]): ProcedureContractBuilderWithInputOutput<TInputSchema, TOutputSchema, TErrorMap>;
|
|
95
|
-
/**
|
|
96
|
-
* Defines typesafe errors that implementations of this contract can throw.
|
|
97
|
-
*
|
|
98
|
-
* @see {@link https://orpc.dev/docs/contract/procedure#typesafe-errors | Procedure Contract - Typesafe Errors}
|
|
99
|
-
*/
|
|
100
|
-
errors<T extends ErrorMap>(errors: T): ProcedureContractBuilderWithInputOutput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, T>>;
|
|
101
|
-
/**
|
|
102
|
-
* Adds an additional input schema, merged with the previously defined one.
|
|
103
|
-
*
|
|
104
|
-
* @see {@link https://orpc.dev/docs/contract/procedure#multiple-schemas | Procedure Contract - Multiple Schemas}
|
|
105
|
-
*/
|
|
106
|
-
input<T extends AnySchema>(schema: T): ProcedureContractBuilderWithInputOutput<MergedSchema<T, TInputSchema>, TOutputSchema, TErrorMap>;
|
|
107
|
-
/**
|
|
108
|
-
* Adds an additional output schema, merged with the previously defined one.
|
|
109
|
-
*
|
|
110
|
-
* @see {@link https://orpc.dev/docs/contract/procedure#multiple-schemas | Procedure Contract - Multiple Schemas}
|
|
111
|
-
*/
|
|
112
|
-
output<T extends AnySchema>(schema: T): ProcedureContractBuilderWithInputOutput<TInputSchema, MergedSchema<T, TOutputSchema>, TErrorMap>;
|
|
113
|
-
}
|