@orpc/contract 0.0.0-next.e0f01a5 → 0.0.0-next.e27e0c1
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 +22 -22
- package/dist/index.d.mts +220 -148
- package/dist/index.d.ts +220 -148
- package/dist/index.mjs +110 -36
- package/dist/plugins/index.d.mts +43 -0
- package/dist/plugins/index.d.ts +43 -0
- package/dist/plugins/index.mjs +81 -0
- package/dist/shared/contract.CvRxURhn.d.mts +254 -0
- package/dist/shared/contract.CvRxURhn.d.ts +254 -0
- package/dist/shared/contract.D_dZrO__.mjs +53 -0
- package/package.json +12 -6
package/dist/index.mjs
CHANGED
|
@@ -1,41 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i as isContractProcedure, C as ContractProcedure, m as mergeErrorMap, V as ValidationError } from './shared/contract.D_dZrO__.mjs';
|
|
2
|
+
export { v as validateORPCError } from './shared/contract.D_dZrO__.mjs';
|
|
3
|
+
import { mapEventIterator, ORPCError } from '@orpc/client';
|
|
2
4
|
export { ORPCError } from '@orpc/client';
|
|
3
|
-
import { isAsyncIteratorObject } from '@orpc/shared';
|
|
4
|
-
|
|
5
|
-
class ValidationError extends Error {
|
|
6
|
-
issues;
|
|
7
|
-
constructor(options) {
|
|
8
|
-
super(options.message, options);
|
|
9
|
-
this.issues = options.issues;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
function mergeErrorMap(errorMap1, errorMap2) {
|
|
13
|
-
return { ...errorMap1, ...errorMap2 };
|
|
14
|
-
}
|
|
5
|
+
import { isAsyncIteratorObject, get, isTypescriptObject, isPropertyKey } from '@orpc/shared';
|
|
6
|
+
export { AsyncIteratorClass } from '@orpc/shared';
|
|
15
7
|
|
|
16
8
|
function mergeMeta(meta1, meta2) {
|
|
17
9
|
return { ...meta1, ...meta2 };
|
|
18
10
|
}
|
|
19
11
|
|
|
20
|
-
class ContractProcedure {
|
|
21
|
-
"~orpc";
|
|
22
|
-
constructor(def) {
|
|
23
|
-
if (def.route?.successStatus && isORPCErrorStatus(def.route.successStatus)) {
|
|
24
|
-
throw new Error("[ContractProcedure] Invalid successStatus.");
|
|
25
|
-
}
|
|
26
|
-
if (Object.values(def.errorMap).some((val) => val && val.status && !isORPCErrorStatus(val.status))) {
|
|
27
|
-
throw new Error("[ContractProcedure] Invalid error status code.");
|
|
28
|
-
}
|
|
29
|
-
this["~orpc"] = def;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
function isContractProcedure(item) {
|
|
33
|
-
if (item instanceof ContractProcedure) {
|
|
34
|
-
return true;
|
|
35
|
-
}
|
|
36
|
-
return (typeof item === "object" || typeof item === "function") && item !== null && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "errorMap" in item["~orpc"] && "route" in item["~orpc"] && "meta" in item["~orpc"];
|
|
37
|
-
}
|
|
38
|
-
|
|
39
12
|
function mergeRoute(a, b) {
|
|
40
13
|
return { ...a, ...b };
|
|
41
14
|
}
|
|
@@ -100,6 +73,23 @@ function enhanceContractRouter(router, options) {
|
|
|
100
73
|
}
|
|
101
74
|
return enhanced;
|
|
102
75
|
}
|
|
76
|
+
function minifyContractRouter(router) {
|
|
77
|
+
if (isContractProcedure(router)) {
|
|
78
|
+
const procedure = {
|
|
79
|
+
"~orpc": {
|
|
80
|
+
errorMap: {},
|
|
81
|
+
meta: router["~orpc"].meta,
|
|
82
|
+
route: router["~orpc"].route
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
return procedure;
|
|
86
|
+
}
|
|
87
|
+
const json = {};
|
|
88
|
+
for (const key in router) {
|
|
89
|
+
json[key] = minifyContractRouter(router[key]);
|
|
90
|
+
}
|
|
91
|
+
return json;
|
|
92
|
+
}
|
|
103
93
|
|
|
104
94
|
class ContractBuilder extends ContractProcedure {
|
|
105
95
|
constructor(def) {
|
|
@@ -108,7 +98,9 @@ class ContractBuilder extends ContractProcedure {
|
|
|
108
98
|
this["~orpc"].tags = def.tags;
|
|
109
99
|
}
|
|
110
100
|
/**
|
|
111
|
-
*
|
|
101
|
+
* Sets or overrides the initial meta.
|
|
102
|
+
*
|
|
103
|
+
* @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
|
|
112
104
|
*/
|
|
113
105
|
$meta(initialMeta) {
|
|
114
106
|
return new ContractBuilder({
|
|
@@ -117,7 +109,11 @@ class ContractBuilder extends ContractProcedure {
|
|
|
117
109
|
});
|
|
118
110
|
}
|
|
119
111
|
/**
|
|
120
|
-
*
|
|
112
|
+
* Sets or overrides the initial route.
|
|
113
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
114
|
+
*
|
|
115
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
|
116
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
|
121
117
|
*/
|
|
122
118
|
$route(initialRoute) {
|
|
123
119
|
return new ContractBuilder({
|
|
@@ -125,48 +121,97 @@ class ContractBuilder extends ContractProcedure {
|
|
|
125
121
|
route: initialRoute
|
|
126
122
|
});
|
|
127
123
|
}
|
|
124
|
+
/**
|
|
125
|
+
* Adds type-safe custom errors to the contract.
|
|
126
|
+
* The provided errors are spared-merged with any existing errors in the contract.
|
|
127
|
+
*
|
|
128
|
+
* @see {@link https://orpc.unnoq.com/docs/error-handling#type%E2%80%90safe-error-handling Type-Safe Error Handling Docs}
|
|
129
|
+
*/
|
|
128
130
|
errors(errors) {
|
|
129
131
|
return new ContractBuilder({
|
|
130
132
|
...this["~orpc"],
|
|
131
133
|
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
132
134
|
});
|
|
133
135
|
}
|
|
136
|
+
/**
|
|
137
|
+
* Sets or updates the metadata for the contract.
|
|
138
|
+
* The provided metadata is spared-merged with any existing metadata in the contract.
|
|
139
|
+
*
|
|
140
|
+
* @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
|
|
141
|
+
*/
|
|
134
142
|
meta(meta) {
|
|
135
143
|
return new ContractBuilder({
|
|
136
144
|
...this["~orpc"],
|
|
137
145
|
meta: mergeMeta(this["~orpc"].meta, meta)
|
|
138
146
|
});
|
|
139
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* Sets or updates the route definition for the contract.
|
|
150
|
+
* The provided route is spared-merged with any existing route in the contract.
|
|
151
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
152
|
+
*
|
|
153
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
|
154
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
|
155
|
+
*/
|
|
140
156
|
route(route) {
|
|
141
157
|
return new ContractBuilder({
|
|
142
158
|
...this["~orpc"],
|
|
143
159
|
route: mergeRoute(this["~orpc"].route, route)
|
|
144
160
|
});
|
|
145
161
|
}
|
|
162
|
+
/**
|
|
163
|
+
* Defines the input validation schema for the contract.
|
|
164
|
+
*
|
|
165
|
+
* @see {@link https://orpc.unnoq.com/docs/procedure#input-output-validation Input Validation Docs}
|
|
166
|
+
*/
|
|
146
167
|
input(schema) {
|
|
147
168
|
return new ContractBuilder({
|
|
148
169
|
...this["~orpc"],
|
|
149
170
|
inputSchema: schema
|
|
150
171
|
});
|
|
151
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* Defines the output validation schema for the contract.
|
|
175
|
+
*
|
|
176
|
+
* @see {@link https://orpc.unnoq.com/docs/procedure#input-output-validation Output Validation Docs}
|
|
177
|
+
*/
|
|
152
178
|
output(schema) {
|
|
153
179
|
return new ContractBuilder({
|
|
154
180
|
...this["~orpc"],
|
|
155
181
|
outputSchema: schema
|
|
156
182
|
});
|
|
157
183
|
}
|
|
184
|
+
/**
|
|
185
|
+
* Prefixes all procedures in the contract router.
|
|
186
|
+
* The provided prefix is post-appended to any existing router prefix.
|
|
187
|
+
*
|
|
188
|
+
* @note This option does not affect procedures that do not define a path in their route definition.
|
|
189
|
+
*
|
|
190
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/routing#route-prefixes OpenAPI Route Prefixes Docs}
|
|
191
|
+
*/
|
|
158
192
|
prefix(prefix) {
|
|
159
193
|
return new ContractBuilder({
|
|
160
194
|
...this["~orpc"],
|
|
161
195
|
prefix: mergePrefix(this["~orpc"].prefix, prefix)
|
|
162
196
|
});
|
|
163
197
|
}
|
|
198
|
+
/**
|
|
199
|
+
* Adds tags to all procedures in the contract router.
|
|
200
|
+
* This helpful when you want to group procedures together in the OpenAPI specification.
|
|
201
|
+
*
|
|
202
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
|
|
203
|
+
*/
|
|
164
204
|
tag(...tags) {
|
|
165
205
|
return new ContractBuilder({
|
|
166
206
|
...this["~orpc"],
|
|
167
207
|
tags: mergeTags(this["~orpc"].tags, tags)
|
|
168
208
|
});
|
|
169
209
|
}
|
|
210
|
+
/**
|
|
211
|
+
* Applies all of the previously defined options to the specified contract router.
|
|
212
|
+
*
|
|
213
|
+
* @see {@link https://orpc.unnoq.com/docs/router#extending-router Extending Router Docs}
|
|
214
|
+
*/
|
|
170
215
|
router(router) {
|
|
171
216
|
return enhanceContractRouter(router, this["~orpc"]);
|
|
172
217
|
}
|
|
@@ -214,7 +259,8 @@ function eventIterator(yields, returns) {
|
|
|
214
259
|
message: "Event iterator validation failed",
|
|
215
260
|
cause: new ValidationError({
|
|
216
261
|
issues: result.issues,
|
|
217
|
-
message: "Event iterator validation failed"
|
|
262
|
+
message: "Event iterator validation failed",
|
|
263
|
+
data: value
|
|
218
264
|
})
|
|
219
265
|
});
|
|
220
266
|
}
|
|
@@ -234,6 +280,19 @@ function getEventIteratorSchemaDetails(schema) {
|
|
|
234
280
|
return schema["~standard"][EVENT_ITERATOR_DETAILS_SYMBOL];
|
|
235
281
|
}
|
|
236
282
|
|
|
283
|
+
function inferRPCMethodFromContractRouter(contract) {
|
|
284
|
+
return (_, path) => {
|
|
285
|
+
const procedure = get(contract, path);
|
|
286
|
+
if (!isContractProcedure(procedure)) {
|
|
287
|
+
throw new Error(
|
|
288
|
+
`[inferRPCMethodFromContractRouter] No valid procedure found at path "${path.join(".")}". This may happen when the contract router is not properly configured.`
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
const method = fallbackContractConfig("defaultMethod", procedure["~orpc"].route.method);
|
|
292
|
+
return method === "HEAD" ? "GET" : method;
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
237
296
|
function type(...[map]) {
|
|
238
297
|
return {
|
|
239
298
|
"~standard": {
|
|
@@ -249,4 +308,19 @@ function type(...[map]) {
|
|
|
249
308
|
};
|
|
250
309
|
}
|
|
251
310
|
|
|
252
|
-
|
|
311
|
+
function isSchemaIssue(issue) {
|
|
312
|
+
if (!isTypescriptObject(issue) || typeof issue.message !== "string") {
|
|
313
|
+
return false;
|
|
314
|
+
}
|
|
315
|
+
if (issue.path !== void 0) {
|
|
316
|
+
if (!Array.isArray(issue.path)) {
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
if (!issue.path.every((segment) => isPropertyKey(segment) || isTypescriptObject(segment) && isPropertyKey(segment.key))) {
|
|
320
|
+
return false;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
export { ContractBuilder, ContractProcedure, ValidationError, enhanceContractRouter, enhanceRoute, eventIterator, fallbackContractConfig, getContractRouter, getEventIteratorSchemaDetails, inferRPCMethodFromContractRouter, isContractProcedure, isSchemaIssue, mergeErrorMap, mergeMeta, mergePrefix, mergeRoute, mergeTags, minifyContractRouter, oc, prefixRoute, type, unshiftTagRoute };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ClientContext } from '@orpc/client';
|
|
2
|
+
import { StandardLinkPlugin, StandardLinkOptions } from '@orpc/client/standard';
|
|
3
|
+
import { A as AnyContractRouter } from '../shared/contract.CvRxURhn.mjs';
|
|
4
|
+
import '@orpc/shared';
|
|
5
|
+
import '@standard-schema/spec';
|
|
6
|
+
import 'openapi-types';
|
|
7
|
+
|
|
8
|
+
declare class RequestValidationPluginError extends Error {
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A link plugin that validates client requests against your contract schema,
|
|
12
|
+
* ensuring that data sent to your server matches the expected types defined in your contract.
|
|
13
|
+
*
|
|
14
|
+
* @throws {ORPCError} with code `BAD_REQUEST` (same as server side) if input doesn't match the expected schema
|
|
15
|
+
* @see {@link https://orpc.unnoq.com/docs/plugins/request-validation Request Validation Plugin Docs}
|
|
16
|
+
*/
|
|
17
|
+
declare class RequestValidationPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
18
|
+
private readonly contract;
|
|
19
|
+
constructor(contract: AnyContractRouter);
|
|
20
|
+
init(options: StandardLinkOptions<T>): void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A link plugin that validates server responses against your contract schema,
|
|
25
|
+
* ensuring that data returned from your server matches the expected types defined in your contract.
|
|
26
|
+
*
|
|
27
|
+
* - Throws `ValidationError` if output doesn't match the expected schema
|
|
28
|
+
* - Converts mismatched defined errors to normal `ORPCError` instances
|
|
29
|
+
*
|
|
30
|
+
* @see {@link https://orpc.unnoq.com/docs/plugins/response-validation Response Validation Plugin Docs}
|
|
31
|
+
*/
|
|
32
|
+
declare class ResponseValidationPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
33
|
+
private readonly contract;
|
|
34
|
+
constructor(contract: AnyContractRouter);
|
|
35
|
+
/**
|
|
36
|
+
* run before (validate after) retry plugin, because validation failed can't be retried
|
|
37
|
+
* run before (validate after) durable iterator plugin, because we expect durable iterator to validation (if user use it)
|
|
38
|
+
*/
|
|
39
|
+
order: number;
|
|
40
|
+
init(options: StandardLinkOptions<T>): void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { RequestValidationPlugin, RequestValidationPluginError, ResponseValidationPlugin };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ClientContext } from '@orpc/client';
|
|
2
|
+
import { StandardLinkPlugin, StandardLinkOptions } from '@orpc/client/standard';
|
|
3
|
+
import { A as AnyContractRouter } from '../shared/contract.CvRxURhn.js';
|
|
4
|
+
import '@orpc/shared';
|
|
5
|
+
import '@standard-schema/spec';
|
|
6
|
+
import 'openapi-types';
|
|
7
|
+
|
|
8
|
+
declare class RequestValidationPluginError extends Error {
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A link plugin that validates client requests against your contract schema,
|
|
12
|
+
* ensuring that data sent to your server matches the expected types defined in your contract.
|
|
13
|
+
*
|
|
14
|
+
* @throws {ORPCError} with code `BAD_REQUEST` (same as server side) if input doesn't match the expected schema
|
|
15
|
+
* @see {@link https://orpc.unnoq.com/docs/plugins/request-validation Request Validation Plugin Docs}
|
|
16
|
+
*/
|
|
17
|
+
declare class RequestValidationPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
18
|
+
private readonly contract;
|
|
19
|
+
constructor(contract: AnyContractRouter);
|
|
20
|
+
init(options: StandardLinkOptions<T>): void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A link plugin that validates server responses against your contract schema,
|
|
25
|
+
* ensuring that data returned from your server matches the expected types defined in your contract.
|
|
26
|
+
*
|
|
27
|
+
* - Throws `ValidationError` if output doesn't match the expected schema
|
|
28
|
+
* - Converts mismatched defined errors to normal `ORPCError` instances
|
|
29
|
+
*
|
|
30
|
+
* @see {@link https://orpc.unnoq.com/docs/plugins/response-validation Response Validation Plugin Docs}
|
|
31
|
+
*/
|
|
32
|
+
declare class ResponseValidationPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
33
|
+
private readonly contract;
|
|
34
|
+
constructor(contract: AnyContractRouter);
|
|
35
|
+
/**
|
|
36
|
+
* run before (validate after) retry plugin, because validation failed can't be retried
|
|
37
|
+
* run before (validate after) durable iterator plugin, because we expect durable iterator to validation (if user use it)
|
|
38
|
+
*/
|
|
39
|
+
order: number;
|
|
40
|
+
init(options: StandardLinkOptions<T>): void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { RequestValidationPlugin, RequestValidationPluginError, ResponseValidationPlugin };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { ORPCError } from '@orpc/client';
|
|
2
|
+
import { get } from '@orpc/shared';
|
|
3
|
+
import { i as isContractProcedure, V as ValidationError, v as validateORPCError } from '../shared/contract.D_dZrO__.mjs';
|
|
4
|
+
|
|
5
|
+
class RequestValidationPluginError extends Error {
|
|
6
|
+
}
|
|
7
|
+
class RequestValidationPlugin {
|
|
8
|
+
constructor(contract) {
|
|
9
|
+
this.contract = contract;
|
|
10
|
+
}
|
|
11
|
+
init(options) {
|
|
12
|
+
options.interceptors ??= [];
|
|
13
|
+
options.interceptors.push(async ({ next, path, input }) => {
|
|
14
|
+
const procedure = get(this.contract, path);
|
|
15
|
+
if (!isContractProcedure(procedure)) {
|
|
16
|
+
throw new RequestValidationPluginError(`No valid procedure found at path "${path.join(".")}", this may happen when the contract router is not properly configured.`);
|
|
17
|
+
}
|
|
18
|
+
const inputSchema = procedure["~orpc"].inputSchema;
|
|
19
|
+
if (inputSchema) {
|
|
20
|
+
const result = await inputSchema["~standard"].validate(input);
|
|
21
|
+
if (result.issues) {
|
|
22
|
+
throw new ORPCError("BAD_REQUEST", {
|
|
23
|
+
message: "Input validation failed",
|
|
24
|
+
data: {
|
|
25
|
+
issues: result.issues
|
|
26
|
+
},
|
|
27
|
+
cause: new ValidationError({
|
|
28
|
+
message: "Input validation failed",
|
|
29
|
+
issues: result.issues,
|
|
30
|
+
data: input
|
|
31
|
+
})
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return await next();
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
class ResponseValidationPlugin {
|
|
41
|
+
constructor(contract) {
|
|
42
|
+
this.contract = contract;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* run before (validate after) retry plugin, because validation failed can't be retried
|
|
46
|
+
* run before (validate after) durable iterator plugin, because we expect durable iterator to validation (if user use it)
|
|
47
|
+
*/
|
|
48
|
+
order = 12e5;
|
|
49
|
+
init(options) {
|
|
50
|
+
options.interceptors ??= [];
|
|
51
|
+
options.interceptors.push(async ({ next, path }) => {
|
|
52
|
+
const procedure = get(this.contract, path);
|
|
53
|
+
if (!isContractProcedure(procedure)) {
|
|
54
|
+
throw new Error(`[ResponseValidationPlugin] no valid procedure found at path "${path.join(".")}", this may happen when the contract router is not properly configured.`);
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
const output = await next();
|
|
58
|
+
const outputSchema = procedure["~orpc"].outputSchema;
|
|
59
|
+
if (!outputSchema) {
|
|
60
|
+
return output;
|
|
61
|
+
}
|
|
62
|
+
const result = await outputSchema["~standard"].validate(output);
|
|
63
|
+
if (result.issues) {
|
|
64
|
+
throw new ValidationError({
|
|
65
|
+
message: "Server response output does not match expected schema",
|
|
66
|
+
issues: result.issues,
|
|
67
|
+
data: output
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return result.value;
|
|
71
|
+
} catch (e) {
|
|
72
|
+
if (e instanceof ORPCError) {
|
|
73
|
+
throw await validateORPCError(procedure["~orpc"].errorMap, e);
|
|
74
|
+
}
|
|
75
|
+
throw e;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export { RequestValidationPlugin, RequestValidationPluginError, ResponseValidationPlugin };
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import { ORPCErrorCode, ORPCError, HTTPMethod, HTTPPath } from '@orpc/client';
|
|
2
|
+
import { Promisable, IsEqual, ThrowableError } from '@orpc/shared';
|
|
3
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
4
|
+
import { OpenAPIV3_1 } from 'openapi-types';
|
|
5
|
+
|
|
6
|
+
type Schema<TInput, TOutput> = StandardSchemaV1<TInput, TOutput>;
|
|
7
|
+
type AnySchema = Schema<any, any>;
|
|
8
|
+
type SchemaIssue = StandardSchemaV1.Issue;
|
|
9
|
+
type InferSchemaInput<T extends AnySchema> = T extends StandardSchemaV1<infer UInput, any> ? UInput : never;
|
|
10
|
+
type InferSchemaOutput<T extends AnySchema> = T extends StandardSchemaV1<any, infer UOutput> ? UOutput : never;
|
|
11
|
+
type TypeRest<TInput, TOutput> = [map: (input: TInput) => Promisable<TOutput>] | (IsEqual<TInput, TOutput> extends true ? [] : never);
|
|
12
|
+
/**
|
|
13
|
+
* The schema for things can be trust without validation.
|
|
14
|
+
* If the TInput and TOutput are different, you need pass a map function.
|
|
15
|
+
*
|
|
16
|
+
* @see {@link https://orpc.unnoq.com/docs/procedure#type-utility Type Utility Docs}
|
|
17
|
+
*/
|
|
18
|
+
declare function type<TInput, TOutput = TInput>(...[map]: TypeRest<TInput, TOutput>): Schema<TInput, TOutput>;
|
|
19
|
+
|
|
20
|
+
interface ValidationErrorOptions extends ErrorOptions {
|
|
21
|
+
message: string;
|
|
22
|
+
issues: readonly SchemaIssue[];
|
|
23
|
+
/**
|
|
24
|
+
* @todo require this field in v2
|
|
25
|
+
*/
|
|
26
|
+
data?: unknown;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* This errors usually used for ORPCError.cause when the error is a validation error.
|
|
30
|
+
*
|
|
31
|
+
* @see {@link https://orpc.unnoq.com/docs/advanced/validation-errors Validation Errors Docs}
|
|
32
|
+
*/
|
|
33
|
+
declare class ValidationError extends Error {
|
|
34
|
+
readonly issues: readonly SchemaIssue[];
|
|
35
|
+
readonly data: unknown;
|
|
36
|
+
constructor(options: ValidationErrorOptions);
|
|
37
|
+
}
|
|
38
|
+
interface ErrorMapItem<TDataSchema extends AnySchema> {
|
|
39
|
+
status?: number;
|
|
40
|
+
message?: string;
|
|
41
|
+
data?: TDataSchema;
|
|
42
|
+
}
|
|
43
|
+
type ErrorMap = {
|
|
44
|
+
[key in ORPCErrorCode]?: ErrorMapItem<AnySchema>;
|
|
45
|
+
};
|
|
46
|
+
type MergedErrorMap<T1 extends ErrorMap, T2 extends ErrorMap> = Omit<T1, keyof T2> & T2;
|
|
47
|
+
declare function mergeErrorMap<T1 extends ErrorMap, T2 extends ErrorMap>(errorMap1: T1, errorMap2: T2): MergedErrorMap<T1, T2>;
|
|
48
|
+
type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = {
|
|
49
|
+
[K in keyof TErrorMap]: K extends string ? TErrorMap[K] extends ErrorMapItem<infer TDataSchema extends Schema<unknown, unknown>> ? ORPCError<K, InferSchemaOutput<TDataSchema>> : never : never;
|
|
50
|
+
}[keyof TErrorMap];
|
|
51
|
+
type ErrorFromErrorMap<TErrorMap extends ErrorMap> = ORPCErrorFromErrorMap<TErrorMap> | ThrowableError;
|
|
52
|
+
declare function validateORPCError(map: ErrorMap, error: ORPCError<any, any>): Promise<ORPCError<string, unknown>>;
|
|
53
|
+
|
|
54
|
+
type Meta = Record<string, any>;
|
|
55
|
+
declare function mergeMeta<T extends Meta>(meta1: T, meta2: T): T;
|
|
56
|
+
|
|
57
|
+
type InputStructure = 'compact' | 'detailed';
|
|
58
|
+
type OutputStructure = 'compact' | 'detailed';
|
|
59
|
+
interface Route {
|
|
60
|
+
/**
|
|
61
|
+
* The HTTP method of the procedure.
|
|
62
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
63
|
+
*
|
|
64
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
|
65
|
+
*/
|
|
66
|
+
method?: HTTPMethod;
|
|
67
|
+
/**
|
|
68
|
+
* The HTTP path of the procedure.
|
|
69
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
70
|
+
*
|
|
71
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
|
72
|
+
*/
|
|
73
|
+
path?: HTTPPath;
|
|
74
|
+
/**
|
|
75
|
+
* The operation ID of the endpoint.
|
|
76
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
77
|
+
*
|
|
78
|
+
* @default Concatenation of router segments
|
|
79
|
+
*/
|
|
80
|
+
operationId?: string;
|
|
81
|
+
/**
|
|
82
|
+
* The summary of the procedure.
|
|
83
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
84
|
+
*
|
|
85
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
|
|
86
|
+
*/
|
|
87
|
+
summary?: string;
|
|
88
|
+
/**
|
|
89
|
+
* The description of the procedure.
|
|
90
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
91
|
+
*
|
|
92
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
|
|
93
|
+
*/
|
|
94
|
+
description?: string;
|
|
95
|
+
/**
|
|
96
|
+
* Marks the procedure as deprecated.
|
|
97
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
98
|
+
*
|
|
99
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
|
|
100
|
+
*/
|
|
101
|
+
deprecated?: boolean;
|
|
102
|
+
/**
|
|
103
|
+
* The tags of the procedure.
|
|
104
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
105
|
+
*
|
|
106
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
|
|
107
|
+
*/
|
|
108
|
+
tags?: readonly string[];
|
|
109
|
+
/**
|
|
110
|
+
* The status code of the response when the procedure is successful.
|
|
111
|
+
* The status code must be in the 200-399 range.
|
|
112
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
113
|
+
*
|
|
114
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
|
115
|
+
* @default 200
|
|
116
|
+
*/
|
|
117
|
+
successStatus?: number;
|
|
118
|
+
/**
|
|
119
|
+
* The description of the response when the procedure is successful.
|
|
120
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
121
|
+
*
|
|
122
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
|
|
123
|
+
* @default 'OK'
|
|
124
|
+
*/
|
|
125
|
+
successDescription?: string;
|
|
126
|
+
/**
|
|
127
|
+
* Determines how the input should be structured based on `params`, `query`, `headers`, and `body`.
|
|
128
|
+
*
|
|
129
|
+
* @option 'compact'
|
|
130
|
+
* Combines `params` and either `query` or `body` (depending on the HTTP method) into a single object.
|
|
131
|
+
*
|
|
132
|
+
* @option 'detailed'
|
|
133
|
+
* Keeps each part of the request (`params`, `query`, `headers`, and `body`) as separate fields in the input object.
|
|
134
|
+
*
|
|
135
|
+
* Example:
|
|
136
|
+
* ```ts
|
|
137
|
+
* const input = {
|
|
138
|
+
* params: { id: 1 },
|
|
139
|
+
* query: { search: 'hello' },
|
|
140
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
141
|
+
* body: { name: 'John' },
|
|
142
|
+
* }
|
|
143
|
+
* ```
|
|
144
|
+
*
|
|
145
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
|
146
|
+
* @default 'compact'
|
|
147
|
+
*/
|
|
148
|
+
inputStructure?: InputStructure;
|
|
149
|
+
/**
|
|
150
|
+
* Determines how the response should be structured based on the output.
|
|
151
|
+
*
|
|
152
|
+
* @option 'compact'
|
|
153
|
+
* The output data is directly returned as the response body.
|
|
154
|
+
*
|
|
155
|
+
* @option 'detailed'
|
|
156
|
+
* Return an object with optional properties:
|
|
157
|
+
* - `status`: The response status (must be in 200-399 range) if not set fallback to `successStatus`.
|
|
158
|
+
* - `headers`: Custom headers to merge with the response headers (`Record<string, string | string[] | undefined>`)
|
|
159
|
+
* - `body`: The response body.
|
|
160
|
+
*
|
|
161
|
+
* Example:
|
|
162
|
+
* ```ts
|
|
163
|
+
* const output = {
|
|
164
|
+
* status: 201,
|
|
165
|
+
* headers: { 'x-custom-header': 'value' },
|
|
166
|
+
* body: { message: 'Hello, world!' },
|
|
167
|
+
* };
|
|
168
|
+
* ```
|
|
169
|
+
*
|
|
170
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
|
171
|
+
* @default 'compact'
|
|
172
|
+
*/
|
|
173
|
+
outputStructure?: OutputStructure;
|
|
174
|
+
/**
|
|
175
|
+
* Override entire auto-generated OpenAPI Operation Object Specification.
|
|
176
|
+
*
|
|
177
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata Operation Metadata Docs}
|
|
178
|
+
*/
|
|
179
|
+
spec?: OpenAPIV3_1.OperationObject | ((current: OpenAPIV3_1.OperationObject) => OpenAPIV3_1.OperationObject);
|
|
180
|
+
}
|
|
181
|
+
declare function mergeRoute(a: Route, b: Route): Route;
|
|
182
|
+
declare function prefixRoute(route: Route, prefix: HTTPPath): Route;
|
|
183
|
+
declare function unshiftTagRoute(route: Route, tags: readonly string[]): Route;
|
|
184
|
+
declare function mergePrefix(a: HTTPPath | undefined, b: HTTPPath): HTTPPath;
|
|
185
|
+
declare function mergeTags(a: readonly string[] | undefined, b: readonly string[]): readonly string[];
|
|
186
|
+
interface EnhanceRouteOptions {
|
|
187
|
+
prefix?: HTTPPath;
|
|
188
|
+
tags?: readonly string[];
|
|
189
|
+
}
|
|
190
|
+
declare function enhanceRoute(route: Route, options: EnhanceRouteOptions): Route;
|
|
191
|
+
|
|
192
|
+
interface ContractProcedureDef<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
193
|
+
meta: TMeta;
|
|
194
|
+
route: Route;
|
|
195
|
+
inputSchema?: TInputSchema;
|
|
196
|
+
outputSchema?: TOutputSchema;
|
|
197
|
+
errorMap: TErrorMap;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* This class represents a contract procedure.
|
|
201
|
+
*
|
|
202
|
+
* @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#procedure-contract Contract Procedure Docs}
|
|
203
|
+
*/
|
|
204
|
+
declare class ContractProcedure<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
205
|
+
/**
|
|
206
|
+
* This property holds the defined options for the contract procedure.
|
|
207
|
+
*/
|
|
208
|
+
'~orpc': ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
209
|
+
constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>);
|
|
210
|
+
}
|
|
211
|
+
type AnyContractProcedure = ContractProcedure<any, any, any, any>;
|
|
212
|
+
declare function isContractProcedure(item: unknown): item is AnyContractProcedure;
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Represents a contract router, which defines a hierarchical structure of contract procedures.
|
|
216
|
+
*
|
|
217
|
+
* @info A contract procedure is a contract router too.
|
|
218
|
+
* @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
|
|
219
|
+
*/
|
|
220
|
+
type ContractRouter<TMeta extends Meta> = ContractProcedure<any, any, any, TMeta> | {
|
|
221
|
+
[k: string]: ContractRouter<TMeta>;
|
|
222
|
+
};
|
|
223
|
+
type AnyContractRouter = ContractRouter<any>;
|
|
224
|
+
/**
|
|
225
|
+
* Infer all inputs of the contract router.
|
|
226
|
+
*
|
|
227
|
+
* @info A contract procedure is a contract router too.
|
|
228
|
+
* @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#utilities Contract Utilities Docs}
|
|
229
|
+
*/
|
|
230
|
+
type InferContractRouterInputs<T extends AnyContractRouter> = T extends ContractProcedure<infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
|
|
231
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterInputs<T[K]> : never;
|
|
232
|
+
};
|
|
233
|
+
/**
|
|
234
|
+
* Infer all outputs of the contract router.
|
|
235
|
+
*
|
|
236
|
+
* @info A contract procedure is a contract router too.
|
|
237
|
+
* @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#utilities Contract Utilities Docs}
|
|
238
|
+
*/
|
|
239
|
+
type InferContractRouterOutputs<T extends AnyContractRouter> = T extends ContractProcedure<any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
|
|
240
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterOutputs<T[K]> : never;
|
|
241
|
+
};
|
|
242
|
+
/**
|
|
243
|
+
* Infer all errors of the contract router.
|
|
244
|
+
*
|
|
245
|
+
* @info A contract procedure is a contract router too.
|
|
246
|
+
* @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#utilities Contract Utilities Docs}
|
|
247
|
+
*/
|
|
248
|
+
type InferContractRouterErrorMap<T extends AnyContractRouter> = T extends ContractProcedure<any, any, infer UErrorMap, any> ? UErrorMap : {
|
|
249
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterErrorMap<T[K]> : never;
|
|
250
|
+
}[keyof T];
|
|
251
|
+
type InferContractRouterMeta<T extends AnyContractRouter> = T extends ContractRouter<infer UMeta> ? UMeta : never;
|
|
252
|
+
|
|
253
|
+
export { ContractProcedure as C, type as D, ValidationError as j, mergeErrorMap as m, mergeMeta as n, isContractProcedure as p, mergeRoute as q, prefixRoute as r, mergePrefix as s, mergeTags as t, unshiftTagRoute as u, validateORPCError as v, enhanceRoute as w };
|
|
254
|
+
export type { AnyContractRouter as A, InferContractRouterMeta as B, ErrorMap as E, InputStructure as I, MergedErrorMap as M, OutputStructure as O, Route as R, Schema as S, TypeRest as T, ValidationErrorOptions as V, EnhanceRouteOptions as a, AnySchema as b, Meta as c, ContractRouter as d, ContractProcedureDef as e, InferSchemaInput as f, InferSchemaOutput as g, ErrorFromErrorMap as h, SchemaIssue as i, ErrorMapItem as k, ORPCErrorFromErrorMap as l, AnyContractProcedure as o, InferContractRouterInputs as x, InferContractRouterOutputs as y, InferContractRouterErrorMap as z };
|