@orpc/contract 0.0.0-next.cba521d → 0.0.0-next.cc1a1aa
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 +114 -0
- package/dist/index.d.mts +313 -0
- package/dist/index.d.ts +313 -0
- package/dist/index.mjs +337 -0
- 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.D_dZrO__.mjs +53 -0
- package/dist/shared/contract.TuRtB1Ca.d.mts +254 -0
- package/dist/shared/contract.TuRtB1Ca.d.ts +254 -0
- package/package.json +17 -15
- package/dist/index.js +0 -380
- package/dist/src/builder.d.ts +0 -16
- package/dist/src/client-utils.d.ts +0 -5
- package/dist/src/client.d.ts +0 -19
- package/dist/src/config.d.ts +0 -36
- package/dist/src/error-map.d.ts +0 -16
- package/dist/src/error-orpc.d.ts +0 -109
- package/dist/src/error.d.ts +0 -13
- package/dist/src/index.d.ts +0 -18
- package/dist/src/procedure-client.d.ts +0 -6
- package/dist/src/procedure-decorated.d.ts +0 -14
- package/dist/src/procedure.d.ts +0 -83
- package/dist/src/router-builder.d.ts +0 -20
- package/dist/src/router-client.d.ts +0 -7
- package/dist/src/router.d.ts +0 -12
- package/dist/src/types.d.ts +0 -11
|
@@ -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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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.dev/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 };
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/contract",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.0-next.
|
|
4
|
+
"version": "0.0.0-next.cc1a1aa",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"homepage": "https://orpc.
|
|
6
|
+
"homepage": "https://orpc.dev",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "git+https://github.com/unnoq/orpc.git",
|
|
@@ -15,30 +15,32 @@
|
|
|
15
15
|
],
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
18
|
-
"types": "./dist/
|
|
19
|
-
"import": "./dist/index.
|
|
20
|
-
"default": "./dist/index.
|
|
18
|
+
"types": "./dist/index.d.mts",
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"default": "./dist/index.mjs"
|
|
21
21
|
},
|
|
22
|
-
"
|
|
23
|
-
"types": "./dist/
|
|
22
|
+
"./plugins": {
|
|
23
|
+
"types": "./dist/plugins/index.d.mts",
|
|
24
|
+
"import": "./dist/plugins/index.mjs",
|
|
25
|
+
"default": "./dist/plugins/index.mjs"
|
|
24
26
|
}
|
|
25
27
|
},
|
|
26
28
|
"files": [
|
|
27
|
-
"!**/*.map",
|
|
28
|
-
"!**/*.tsbuildinfo",
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@standard-schema/spec": "1.0.0
|
|
33
|
-
"
|
|
32
|
+
"@standard-schema/spec": "^1.0.0",
|
|
33
|
+
"openapi-types": "^12.1.3",
|
|
34
|
+
"@orpc/client": "0.0.0-next.cc1a1aa",
|
|
35
|
+
"@orpc/shared": "0.0.0-next.cc1a1aa"
|
|
34
36
|
},
|
|
35
37
|
"devDependencies": {
|
|
36
|
-
"arktype": "2.
|
|
37
|
-
"valibot": "1.
|
|
38
|
-
"zod": "
|
|
38
|
+
"arktype": "2.1.27",
|
|
39
|
+
"valibot": "^1.2.0",
|
|
40
|
+
"zod": "^4.1.12"
|
|
39
41
|
},
|
|
40
42
|
"scripts": {
|
|
41
|
-
"build": "
|
|
43
|
+
"build": "unbuild",
|
|
42
44
|
"build:watch": "pnpm run build --watch",
|
|
43
45
|
"type:check": "tsc -b"
|
|
44
46
|
}
|
package/dist/index.js
DELETED
|
@@ -1,380 +0,0 @@
|
|
|
1
|
-
// src/procedure.ts
|
|
2
|
-
var ContractProcedure = class {
|
|
3
|
-
"~type" = "ContractProcedure";
|
|
4
|
-
"~orpc";
|
|
5
|
-
constructor(def) {
|
|
6
|
-
if (def.route?.successStatus && (def.route.successStatus < 200 || def.route?.successStatus > 299)) {
|
|
7
|
-
throw new Error("[ContractProcedure] The successStatus must be between 200 and 299");
|
|
8
|
-
}
|
|
9
|
-
if (Object.values(def.errorMap ?? {}).some((val) => val && val.status && (val.status < 400 || val.status > 599))) {
|
|
10
|
-
throw new Error("[ContractProcedure] The error status code must be in the 400-599 range.");
|
|
11
|
-
}
|
|
12
|
-
this["~orpc"] = def;
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
function isContractProcedure(item) {
|
|
16
|
-
if (item instanceof ContractProcedure) {
|
|
17
|
-
return true;
|
|
18
|
-
}
|
|
19
|
-
return (typeof item === "object" || typeof item === "function") && item !== null && "~type" in item && item["~type"] === "ContractProcedure" && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "InputSchema" in item["~orpc"] && "OutputSchema" in item["~orpc"] && "errorMap" in item["~orpc"];
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// src/procedure-decorated.ts
|
|
23
|
-
var DecoratedContractProcedure = class _DecoratedContractProcedure extends ContractProcedure {
|
|
24
|
-
static decorate(procedure) {
|
|
25
|
-
if (procedure instanceof _DecoratedContractProcedure) {
|
|
26
|
-
return procedure;
|
|
27
|
-
}
|
|
28
|
-
return new _DecoratedContractProcedure(procedure["~orpc"]);
|
|
29
|
-
}
|
|
30
|
-
route(route) {
|
|
31
|
-
return new _DecoratedContractProcedure({
|
|
32
|
-
...this["~orpc"],
|
|
33
|
-
route: {
|
|
34
|
-
...this["~orpc"].route,
|
|
35
|
-
...route
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
prefix(prefix) {
|
|
40
|
-
return new _DecoratedContractProcedure({
|
|
41
|
-
...this["~orpc"],
|
|
42
|
-
...this["~orpc"].route?.path ? {
|
|
43
|
-
route: {
|
|
44
|
-
...this["~orpc"].route,
|
|
45
|
-
path: `${prefix}${this["~orpc"].route.path}`
|
|
46
|
-
}
|
|
47
|
-
} : void 0
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
unshiftTag(...tags) {
|
|
51
|
-
return new _DecoratedContractProcedure({
|
|
52
|
-
...this["~orpc"],
|
|
53
|
-
route: {
|
|
54
|
-
...this["~orpc"].route,
|
|
55
|
-
tags: [
|
|
56
|
-
...tags,
|
|
57
|
-
...this["~orpc"].route?.tags?.filter((tag) => !tags.includes(tag)) ?? []
|
|
58
|
-
]
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
input(schema, example) {
|
|
63
|
-
return new _DecoratedContractProcedure({
|
|
64
|
-
...this["~orpc"],
|
|
65
|
-
InputSchema: schema,
|
|
66
|
-
inputExample: example
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
output(schema, example) {
|
|
70
|
-
return new _DecoratedContractProcedure({
|
|
71
|
-
...this["~orpc"],
|
|
72
|
-
OutputSchema: schema,
|
|
73
|
-
outputExample: example
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
errors(errorMap) {
|
|
77
|
-
return new _DecoratedContractProcedure({
|
|
78
|
-
...this["~orpc"],
|
|
79
|
-
errorMap
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
// src/router-builder.ts
|
|
85
|
-
var ContractRouterBuilder = class _ContractRouterBuilder {
|
|
86
|
-
"~type" = "ContractProcedure";
|
|
87
|
-
"~orpc";
|
|
88
|
-
constructor(def) {
|
|
89
|
-
this["~orpc"] = def;
|
|
90
|
-
}
|
|
91
|
-
prefix(prefix) {
|
|
92
|
-
return new _ContractRouterBuilder({
|
|
93
|
-
...this["~orpc"],
|
|
94
|
-
prefix: `${this["~orpc"].prefix ?? ""}${prefix}`
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
tag(...tags) {
|
|
98
|
-
return new _ContractRouterBuilder({
|
|
99
|
-
...this["~orpc"],
|
|
100
|
-
tags: [...this["~orpc"].tags ?? [], ...tags]
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
router(router) {
|
|
104
|
-
if (isContractProcedure(router)) {
|
|
105
|
-
let decorated = DecoratedContractProcedure.decorate(router);
|
|
106
|
-
if (this["~orpc"].tags) {
|
|
107
|
-
decorated = decorated.unshiftTag(...this["~orpc"].tags);
|
|
108
|
-
}
|
|
109
|
-
if (this["~orpc"].prefix) {
|
|
110
|
-
decorated = decorated.prefix(this["~orpc"].prefix);
|
|
111
|
-
}
|
|
112
|
-
return decorated;
|
|
113
|
-
}
|
|
114
|
-
const adapted = {};
|
|
115
|
-
for (const key in router) {
|
|
116
|
-
adapted[key] = this.router(router[key]);
|
|
117
|
-
}
|
|
118
|
-
return adapted;
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
// src/builder.ts
|
|
123
|
-
var ContractBuilder = class {
|
|
124
|
-
prefix(prefix) {
|
|
125
|
-
return new ContractRouterBuilder({
|
|
126
|
-
prefix
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
tag(...tags) {
|
|
130
|
-
return new ContractRouterBuilder({
|
|
131
|
-
tags
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
route(route) {
|
|
135
|
-
return new DecoratedContractProcedure({
|
|
136
|
-
route,
|
|
137
|
-
InputSchema: void 0,
|
|
138
|
-
OutputSchema: void 0,
|
|
139
|
-
errorMap: void 0
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
input(schema, example) {
|
|
143
|
-
return new DecoratedContractProcedure({
|
|
144
|
-
InputSchema: schema,
|
|
145
|
-
inputExample: example,
|
|
146
|
-
OutputSchema: void 0,
|
|
147
|
-
errorMap: void 0
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
output(schema, example) {
|
|
151
|
-
return new DecoratedContractProcedure({
|
|
152
|
-
OutputSchema: schema,
|
|
153
|
-
outputExample: example,
|
|
154
|
-
InputSchema: void 0,
|
|
155
|
-
errorMap: void 0
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
errors(errorMap) {
|
|
159
|
-
return new DecoratedContractProcedure({
|
|
160
|
-
InputSchema: void 0,
|
|
161
|
-
OutputSchema: void 0,
|
|
162
|
-
errorMap
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
router(router) {
|
|
166
|
-
return router;
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
// src/error-orpc.ts
|
|
171
|
-
import { isPlainObject } from "@orpc/shared";
|
|
172
|
-
var COMMON_ORPC_ERROR_DEFS = {
|
|
173
|
-
BAD_REQUEST: {
|
|
174
|
-
status: 400,
|
|
175
|
-
message: "Bad Request"
|
|
176
|
-
},
|
|
177
|
-
UNAUTHORIZED: {
|
|
178
|
-
status: 401,
|
|
179
|
-
message: "Unauthorized"
|
|
180
|
-
},
|
|
181
|
-
FORBIDDEN: {
|
|
182
|
-
status: 403,
|
|
183
|
-
message: "Forbidden"
|
|
184
|
-
},
|
|
185
|
-
NOT_FOUND: {
|
|
186
|
-
status: 404,
|
|
187
|
-
message: "Not Found"
|
|
188
|
-
},
|
|
189
|
-
METHOD_NOT_SUPPORTED: {
|
|
190
|
-
status: 405,
|
|
191
|
-
message: "Method Not Supported"
|
|
192
|
-
},
|
|
193
|
-
NOT_ACCEPTABLE: {
|
|
194
|
-
status: 406,
|
|
195
|
-
message: "Not Acceptable"
|
|
196
|
-
},
|
|
197
|
-
TIMEOUT: {
|
|
198
|
-
status: 408,
|
|
199
|
-
message: "Request Timeout"
|
|
200
|
-
},
|
|
201
|
-
CONFLICT: {
|
|
202
|
-
status: 409,
|
|
203
|
-
message: "Conflict"
|
|
204
|
-
},
|
|
205
|
-
PRECONDITION_FAILED: {
|
|
206
|
-
status: 412,
|
|
207
|
-
message: "Precondition Failed"
|
|
208
|
-
},
|
|
209
|
-
PAYLOAD_TOO_LARGE: {
|
|
210
|
-
status: 413,
|
|
211
|
-
message: "Payload Too Large"
|
|
212
|
-
},
|
|
213
|
-
UNSUPPORTED_MEDIA_TYPE: {
|
|
214
|
-
status: 415,
|
|
215
|
-
message: "Unsupported Media Type"
|
|
216
|
-
},
|
|
217
|
-
UNPROCESSABLE_CONTENT: {
|
|
218
|
-
status: 422,
|
|
219
|
-
message: "Unprocessable Content"
|
|
220
|
-
},
|
|
221
|
-
TOO_MANY_REQUESTS: {
|
|
222
|
-
status: 429,
|
|
223
|
-
message: "Too Many Requests"
|
|
224
|
-
},
|
|
225
|
-
CLIENT_CLOSED_REQUEST: {
|
|
226
|
-
status: 499,
|
|
227
|
-
message: "Client Closed Request"
|
|
228
|
-
},
|
|
229
|
-
INTERNAL_SERVER_ERROR: {
|
|
230
|
-
status: 500,
|
|
231
|
-
message: "Internal Server Error"
|
|
232
|
-
},
|
|
233
|
-
NOT_IMPLEMENTED: {
|
|
234
|
-
status: 501,
|
|
235
|
-
message: "Not Implemented"
|
|
236
|
-
},
|
|
237
|
-
BAD_GATEWAY: {
|
|
238
|
-
status: 502,
|
|
239
|
-
message: "Bad Gateway"
|
|
240
|
-
},
|
|
241
|
-
SERVICE_UNAVAILABLE: {
|
|
242
|
-
status: 503,
|
|
243
|
-
message: "Service Unavailable"
|
|
244
|
-
},
|
|
245
|
-
GATEWAY_TIMEOUT: {
|
|
246
|
-
status: 504,
|
|
247
|
-
message: "Gateway Timeout"
|
|
248
|
-
}
|
|
249
|
-
};
|
|
250
|
-
function fallbackORPCErrorStatus(code, status) {
|
|
251
|
-
return status ?? COMMON_ORPC_ERROR_DEFS[code]?.status ?? 500;
|
|
252
|
-
}
|
|
253
|
-
function fallbackORPCErrorMessage(code, message) {
|
|
254
|
-
return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
|
|
255
|
-
}
|
|
256
|
-
var ORPCError = class extends Error {
|
|
257
|
-
defined;
|
|
258
|
-
code;
|
|
259
|
-
status;
|
|
260
|
-
data;
|
|
261
|
-
constructor(options) {
|
|
262
|
-
if (options.status && (options.status < 400 || options.status >= 600)) {
|
|
263
|
-
throw new Error("[ORPCError] The error status code must be in the 400-599 range.");
|
|
264
|
-
}
|
|
265
|
-
const message = fallbackORPCErrorMessage(options.code, options.message);
|
|
266
|
-
super(message, options);
|
|
267
|
-
this.code = options.code;
|
|
268
|
-
this.status = fallbackORPCErrorStatus(options.code, options.status);
|
|
269
|
-
this.defined = options.defined ?? false;
|
|
270
|
-
this.data = options.data;
|
|
271
|
-
}
|
|
272
|
-
toJSON() {
|
|
273
|
-
return {
|
|
274
|
-
defined: this.defined,
|
|
275
|
-
code: this.code,
|
|
276
|
-
status: this.status,
|
|
277
|
-
message: this.message,
|
|
278
|
-
data: this.data
|
|
279
|
-
};
|
|
280
|
-
}
|
|
281
|
-
static isValidJSON(json) {
|
|
282
|
-
return isPlainObject(json) && "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && "message" in json && typeof json.message === "string";
|
|
283
|
-
}
|
|
284
|
-
};
|
|
285
|
-
function isDefinedError(error) {
|
|
286
|
-
return error instanceof ORPCError && error.defined;
|
|
287
|
-
}
|
|
288
|
-
async function validateORPCError(map, error) {
|
|
289
|
-
const { code, status, message, data, cause, defined } = error;
|
|
290
|
-
const config = map?.[error.code];
|
|
291
|
-
if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) {
|
|
292
|
-
return defined ? new ORPCError({ defined: false, code, status, message, data, cause }) : error;
|
|
293
|
-
}
|
|
294
|
-
if (!config.data) {
|
|
295
|
-
return defined ? error : new ORPCError({ defined: true, code, status, message, data, cause });
|
|
296
|
-
}
|
|
297
|
-
const validated = await config.data["~standard"].validate(error.data);
|
|
298
|
-
if (validated.issues) {
|
|
299
|
-
return defined ? new ORPCError({ defined: false, code, status, message, data, cause }) : error;
|
|
300
|
-
}
|
|
301
|
-
return new ORPCError({
|
|
302
|
-
defined: true,
|
|
303
|
-
code,
|
|
304
|
-
status,
|
|
305
|
-
message,
|
|
306
|
-
data: validated.value,
|
|
307
|
-
cause
|
|
308
|
-
});
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
// src/client-utils.ts
|
|
312
|
-
async function safe(promise) {
|
|
313
|
-
try {
|
|
314
|
-
const output = await promise;
|
|
315
|
-
return [output, void 0, false];
|
|
316
|
-
} catch (e) {
|
|
317
|
-
const error = e;
|
|
318
|
-
if (isDefinedError(error)) {
|
|
319
|
-
return [void 0, error, true];
|
|
320
|
-
}
|
|
321
|
-
return [void 0, error, false];
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
// src/config.ts
|
|
326
|
-
var DEFAULT_CONFIG = {
|
|
327
|
-
defaultMethod: "POST",
|
|
328
|
-
defaultSuccessStatus: 200,
|
|
329
|
-
defaultSuccessDescription: "OK",
|
|
330
|
-
defaultInputStructure: "compact",
|
|
331
|
-
defaultOutputStructure: "compact"
|
|
332
|
-
};
|
|
333
|
-
var GLOBAL_CONFIG_REF = { value: DEFAULT_CONFIG };
|
|
334
|
-
function configGlobal(config) {
|
|
335
|
-
if (config.defaultSuccessStatus !== void 0 && (config.defaultSuccessStatus < 200 || config.defaultSuccessStatus > 299)) {
|
|
336
|
-
throw new Error("[configGlobal] The defaultSuccessStatus must be between 200 and 299");
|
|
337
|
-
}
|
|
338
|
-
GLOBAL_CONFIG_REF.value = config;
|
|
339
|
-
}
|
|
340
|
-
function fallbackToGlobalConfig(key, value) {
|
|
341
|
-
if (value === void 0) {
|
|
342
|
-
const fallback = GLOBAL_CONFIG_REF.value[key];
|
|
343
|
-
if (fallback === void 0) {
|
|
344
|
-
return DEFAULT_CONFIG[key];
|
|
345
|
-
}
|
|
346
|
-
return fallback;
|
|
347
|
-
}
|
|
348
|
-
return value;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
// src/error.ts
|
|
352
|
-
var ValidationError = class extends Error {
|
|
353
|
-
issues;
|
|
354
|
-
constructor(options) {
|
|
355
|
-
super(options.message, options);
|
|
356
|
-
this.issues = options.issues;
|
|
357
|
-
}
|
|
358
|
-
};
|
|
359
|
-
|
|
360
|
-
// src/index.ts
|
|
361
|
-
var oc = new ContractBuilder();
|
|
362
|
-
export {
|
|
363
|
-
COMMON_ORPC_ERROR_DEFS,
|
|
364
|
-
ContractBuilder,
|
|
365
|
-
ContractProcedure,
|
|
366
|
-
ContractRouterBuilder,
|
|
367
|
-
DecoratedContractProcedure,
|
|
368
|
-
ORPCError,
|
|
369
|
-
ValidationError,
|
|
370
|
-
configGlobal,
|
|
371
|
-
fallbackORPCErrorMessage,
|
|
372
|
-
fallbackORPCErrorStatus,
|
|
373
|
-
fallbackToGlobalConfig,
|
|
374
|
-
isContractProcedure,
|
|
375
|
-
isDefinedError,
|
|
376
|
-
oc,
|
|
377
|
-
safe,
|
|
378
|
-
validateORPCError
|
|
379
|
-
};
|
|
380
|
-
//# sourceMappingURL=index.js.map
|