@orpc/contract 0.27.0 → 0.29.0
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/dist/index.js +196 -5
- package/dist/src/builder.d.ts +5 -3
- package/dist/src/client-utils.d.ts +5 -0
- package/dist/src/client.d.ts +19 -0
- package/dist/src/error-map.d.ts +16 -0
- package/dist/src/error-orpc.d.ts +109 -0
- package/dist/src/error.d.ts +13 -0
- package/dist/src/index.d.ts +7 -0
- package/dist/src/procedure-client.d.ts +6 -0
- package/dist/src/procedure-decorated.d.ts +9 -7
- package/dist/src/procedure.d.ts +8 -6
- package/dist/src/router-builder.d.ts +1 -1
- package/dist/src/router-client.d.ts +7 -0
- package/dist/src/router.d.ts +2 -2
- package/dist/src/types.d.ts +2 -0
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -6,6 +6,9 @@ var ContractProcedure = class {
|
|
|
6
6
|
if (def.route?.successStatus && (def.route.successStatus < 200 || def.route?.successStatus > 299)) {
|
|
7
7
|
throw new Error("[ContractProcedure] The successStatus must be between 200 and 299");
|
|
8
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
|
+
}
|
|
9
12
|
this["~orpc"] = def;
|
|
10
13
|
}
|
|
11
14
|
};
|
|
@@ -13,7 +16,7 @@ function isContractProcedure(item) {
|
|
|
13
16
|
if (item instanceof ContractProcedure) {
|
|
14
17
|
return true;
|
|
15
18
|
}
|
|
16
|
-
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"];
|
|
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"];
|
|
17
20
|
}
|
|
18
21
|
|
|
19
22
|
// src/procedure-decorated.ts
|
|
@@ -70,6 +73,12 @@ var DecoratedContractProcedure = class _DecoratedContractProcedure extends Contr
|
|
|
70
73
|
outputExample: example
|
|
71
74
|
});
|
|
72
75
|
}
|
|
76
|
+
errors(errorMap) {
|
|
77
|
+
return new _DecoratedContractProcedure({
|
|
78
|
+
...this["~orpc"],
|
|
79
|
+
errorMap
|
|
80
|
+
});
|
|
81
|
+
}
|
|
73
82
|
};
|
|
74
83
|
|
|
75
84
|
// src/router-builder.ts
|
|
@@ -126,21 +135,31 @@ var ContractBuilder = class {
|
|
|
126
135
|
return new DecoratedContractProcedure({
|
|
127
136
|
route,
|
|
128
137
|
InputSchema: void 0,
|
|
129
|
-
OutputSchema: void 0
|
|
138
|
+
OutputSchema: void 0,
|
|
139
|
+
errorMap: void 0
|
|
130
140
|
});
|
|
131
141
|
}
|
|
132
142
|
input(schema, example) {
|
|
133
143
|
return new DecoratedContractProcedure({
|
|
134
144
|
InputSchema: schema,
|
|
135
145
|
inputExample: example,
|
|
136
|
-
OutputSchema: void 0
|
|
146
|
+
OutputSchema: void 0,
|
|
147
|
+
errorMap: void 0
|
|
137
148
|
});
|
|
138
149
|
}
|
|
139
150
|
output(schema, example) {
|
|
140
151
|
return new DecoratedContractProcedure({
|
|
141
152
|
OutputSchema: schema,
|
|
142
153
|
outputExample: example,
|
|
143
|
-
InputSchema: void 0
|
|
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
|
|
144
163
|
});
|
|
145
164
|
}
|
|
146
165
|
router(router) {
|
|
@@ -148,6 +167,161 @@ var ContractBuilder = class {
|
|
|
148
167
|
}
|
|
149
168
|
};
|
|
150
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
|
+
|
|
151
325
|
// src/config.ts
|
|
152
326
|
var DEFAULT_CONFIG = {
|
|
153
327
|
defaultMethod: "POST",
|
|
@@ -174,16 +348,33 @@ function fallbackToGlobalConfig(key, value) {
|
|
|
174
348
|
return value;
|
|
175
349
|
}
|
|
176
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
|
+
|
|
177
360
|
// src/index.ts
|
|
178
361
|
var oc = new ContractBuilder();
|
|
179
362
|
export {
|
|
363
|
+
COMMON_ORPC_ERROR_DEFS,
|
|
180
364
|
ContractBuilder,
|
|
181
365
|
ContractProcedure,
|
|
182
366
|
ContractRouterBuilder,
|
|
183
367
|
DecoratedContractProcedure,
|
|
368
|
+
ORPCError,
|
|
369
|
+
ValidationError,
|
|
184
370
|
configGlobal,
|
|
371
|
+
fallbackORPCErrorMessage,
|
|
372
|
+
fallbackORPCErrorStatus,
|
|
185
373
|
fallbackToGlobalConfig,
|
|
186
374
|
isContractProcedure,
|
|
187
|
-
|
|
375
|
+
isDefinedError,
|
|
376
|
+
oc,
|
|
377
|
+
safe,
|
|
378
|
+
validateORPCError
|
|
188
379
|
};
|
|
189
380
|
//# sourceMappingURL=index.js.map
|
package/dist/src/builder.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ErrorMap } from './error-map';
|
|
1
2
|
import type { RouteOptions } from './procedure';
|
|
2
3
|
import type { ContractRouter } from './router';
|
|
3
4
|
import type { HTTPPath, Schema, SchemaInput, SchemaOutput } from './types';
|
|
@@ -6,9 +7,10 @@ import { ContractRouterBuilder } from './router-builder';
|
|
|
6
7
|
export declare class ContractBuilder {
|
|
7
8
|
prefix(prefix: HTTPPath): ContractRouterBuilder;
|
|
8
9
|
tag(...tags: string[]): ContractRouterBuilder;
|
|
9
|
-
route(route: RouteOptions): DecoratedContractProcedure<undefined, undefined>;
|
|
10
|
-
input<U extends Schema
|
|
11
|
-
output<U extends Schema
|
|
10
|
+
route(route: RouteOptions): DecoratedContractProcedure<undefined, undefined, undefined>;
|
|
11
|
+
input<U extends Schema>(schema: U, example?: SchemaInput<U>): DecoratedContractProcedure<U, undefined, undefined>;
|
|
12
|
+
output<U extends Schema>(schema: U, example?: SchemaOutput<U>): DecoratedContractProcedure<undefined, U, undefined>;
|
|
13
|
+
errors<const U extends ErrorMap>(errorMap: U): DecoratedContractProcedure<undefined, undefined, U>;
|
|
12
14
|
router<T extends ContractRouter>(router: T): T;
|
|
13
15
|
}
|
|
14
16
|
//# sourceMappingURL=builder.d.ts.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ClientPromiseResult } from './client';
|
|
2
|
+
import { type ORPCError } from './error-orpc';
|
|
3
|
+
export type SafeResult<TOutput, TError extends Error> = [output: TOutput, error: undefined, isDefinedError: false] | [output: undefined, error: TError, isDefinedError: false] | [output: undefined, error: Extract<TError, ORPCError<any, any>>, isDefinedError: true];
|
|
4
|
+
export declare function safe<TOutput, TError extends Error>(promise: ClientPromiseResult<TOutput, TError>): Promise<SafeResult<TOutput, TError>>;
|
|
5
|
+
//# sourceMappingURL=client-utils.d.ts.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AbortSignal } from './types';
|
|
2
|
+
export type ClientOptions<TClientContext> = {
|
|
3
|
+
signal?: AbortSignal;
|
|
4
|
+
} & (undefined extends TClientContext ? {
|
|
5
|
+
context?: TClientContext;
|
|
6
|
+
} : {
|
|
7
|
+
context: TClientContext;
|
|
8
|
+
});
|
|
9
|
+
export type ClientRest<TClientContext, TInput> = [input: TInput, options: ClientOptions<TClientContext>] | (undefined extends TInput & TClientContext ? [] : never) | (undefined extends TClientContext ? [input: TInput] : never);
|
|
10
|
+
export type ClientPromiseResult<TOutput, TError extends Error> = Promise<TOutput> & {
|
|
11
|
+
__typeError?: TError;
|
|
12
|
+
};
|
|
13
|
+
export interface Client<TClientContext, TInput, TOutput, TError extends Error> {
|
|
14
|
+
(...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
|
|
15
|
+
}
|
|
16
|
+
export type NestedClient<TClientContext> = Client<TClientContext, any, any, any> | {
|
|
17
|
+
[k: string]: NestedClient<TClientContext>;
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CommonORPCErrorCode } from './error-orpc';
|
|
2
|
+
import type { Schema } from './types';
|
|
3
|
+
export type ErrorMapItem<TDataSchema extends Schema> = {
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @default 200
|
|
7
|
+
*/
|
|
8
|
+
status?: number;
|
|
9
|
+
message?: string;
|
|
10
|
+
description?: string;
|
|
11
|
+
data?: TDataSchema;
|
|
12
|
+
};
|
|
13
|
+
export type ErrorMap = undefined | {
|
|
14
|
+
[key in CommonORPCErrorCode | (string & {})]?: ErrorMapItem<Schema>;
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=error-map.d.ts.map
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { ErrorMap, ErrorMapItem } from './error-map';
|
|
2
|
+
import type { SchemaOutput } from './types';
|
|
3
|
+
export type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = {
|
|
4
|
+
[K in keyof TErrorMap]: K extends string ? TErrorMap[K] extends ErrorMapItem<infer TDataSchema> ? ORPCError<K, SchemaOutput<TDataSchema>> : never : never;
|
|
5
|
+
}[keyof TErrorMap];
|
|
6
|
+
export declare const COMMON_ORPC_ERROR_DEFS: {
|
|
7
|
+
readonly BAD_REQUEST: {
|
|
8
|
+
readonly status: 400;
|
|
9
|
+
readonly message: "Bad Request";
|
|
10
|
+
};
|
|
11
|
+
readonly UNAUTHORIZED: {
|
|
12
|
+
readonly status: 401;
|
|
13
|
+
readonly message: "Unauthorized";
|
|
14
|
+
};
|
|
15
|
+
readonly FORBIDDEN: {
|
|
16
|
+
readonly status: 403;
|
|
17
|
+
readonly message: "Forbidden";
|
|
18
|
+
};
|
|
19
|
+
readonly NOT_FOUND: {
|
|
20
|
+
readonly status: 404;
|
|
21
|
+
readonly message: "Not Found";
|
|
22
|
+
};
|
|
23
|
+
readonly METHOD_NOT_SUPPORTED: {
|
|
24
|
+
readonly status: 405;
|
|
25
|
+
readonly message: "Method Not Supported";
|
|
26
|
+
};
|
|
27
|
+
readonly NOT_ACCEPTABLE: {
|
|
28
|
+
readonly status: 406;
|
|
29
|
+
readonly message: "Not Acceptable";
|
|
30
|
+
};
|
|
31
|
+
readonly TIMEOUT: {
|
|
32
|
+
readonly status: 408;
|
|
33
|
+
readonly message: "Request Timeout";
|
|
34
|
+
};
|
|
35
|
+
readonly CONFLICT: {
|
|
36
|
+
readonly status: 409;
|
|
37
|
+
readonly message: "Conflict";
|
|
38
|
+
};
|
|
39
|
+
readonly PRECONDITION_FAILED: {
|
|
40
|
+
readonly status: 412;
|
|
41
|
+
readonly message: "Precondition Failed";
|
|
42
|
+
};
|
|
43
|
+
readonly PAYLOAD_TOO_LARGE: {
|
|
44
|
+
readonly status: 413;
|
|
45
|
+
readonly message: "Payload Too Large";
|
|
46
|
+
};
|
|
47
|
+
readonly UNSUPPORTED_MEDIA_TYPE: {
|
|
48
|
+
readonly status: 415;
|
|
49
|
+
readonly message: "Unsupported Media Type";
|
|
50
|
+
};
|
|
51
|
+
readonly UNPROCESSABLE_CONTENT: {
|
|
52
|
+
readonly status: 422;
|
|
53
|
+
readonly message: "Unprocessable Content";
|
|
54
|
+
};
|
|
55
|
+
readonly TOO_MANY_REQUESTS: {
|
|
56
|
+
readonly status: 429;
|
|
57
|
+
readonly message: "Too Many Requests";
|
|
58
|
+
};
|
|
59
|
+
readonly CLIENT_CLOSED_REQUEST: {
|
|
60
|
+
readonly status: 499;
|
|
61
|
+
readonly message: "Client Closed Request";
|
|
62
|
+
};
|
|
63
|
+
readonly INTERNAL_SERVER_ERROR: {
|
|
64
|
+
readonly status: 500;
|
|
65
|
+
readonly message: "Internal Server Error";
|
|
66
|
+
};
|
|
67
|
+
readonly NOT_IMPLEMENTED: {
|
|
68
|
+
readonly status: 501;
|
|
69
|
+
readonly message: "Not Implemented";
|
|
70
|
+
};
|
|
71
|
+
readonly BAD_GATEWAY: {
|
|
72
|
+
readonly status: 502;
|
|
73
|
+
readonly message: "Bad Gateway";
|
|
74
|
+
};
|
|
75
|
+
readonly SERVICE_UNAVAILABLE: {
|
|
76
|
+
readonly status: 503;
|
|
77
|
+
readonly message: "Service Unavailable";
|
|
78
|
+
};
|
|
79
|
+
readonly GATEWAY_TIMEOUT: {
|
|
80
|
+
readonly status: 504;
|
|
81
|
+
readonly message: "Gateway Timeout";
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
export type CommonORPCErrorCode = keyof typeof COMMON_ORPC_ERROR_DEFS;
|
|
85
|
+
export type ORPCErrorOptions<TCode extends string, TData> = ErrorOptions & {
|
|
86
|
+
defined?: boolean;
|
|
87
|
+
code: TCode;
|
|
88
|
+
status?: number;
|
|
89
|
+
message?: string;
|
|
90
|
+
} & (undefined extends TData ? {
|
|
91
|
+
data?: TData;
|
|
92
|
+
} : {
|
|
93
|
+
data: TData;
|
|
94
|
+
});
|
|
95
|
+
export declare function fallbackORPCErrorStatus(code: CommonORPCErrorCode | (string & {}), status: number | undefined): number;
|
|
96
|
+
export declare function fallbackORPCErrorMessage(code: CommonORPCErrorCode | (string & {}), message: string | undefined): string;
|
|
97
|
+
export declare class ORPCError<TCode extends CommonORPCErrorCode | (string & {}), TData> extends Error {
|
|
98
|
+
readonly defined: boolean;
|
|
99
|
+
readonly code: TCode;
|
|
100
|
+
readonly status: number;
|
|
101
|
+
readonly data: TData;
|
|
102
|
+
constructor(options: ORPCErrorOptions<TCode, TData>);
|
|
103
|
+
toJSON(): ORPCErrorJSON<TCode, TData>;
|
|
104
|
+
static isValidJSON(json: unknown): json is ORPCErrorJSON<string, unknown>;
|
|
105
|
+
}
|
|
106
|
+
export type ORPCErrorJSON<TCode extends string, TData> = Pick<ORPCError<TCode, TData>, 'defined' | 'code' | 'status' | 'message' | 'data'>;
|
|
107
|
+
export declare function isDefinedError<T>(error: T): error is Extract<T, ORPCError<any, any>>;
|
|
108
|
+
export declare function validateORPCError(map: ErrorMap, error: ORPCError<any, any>): Promise<ORPCError<string, unknown>>;
|
|
109
|
+
//# sourceMappingURL=error-orpc.d.ts.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import type { ErrorMap } from './error-map';
|
|
3
|
+
import type { ORPCErrorFromErrorMap } from './error-orpc';
|
|
4
|
+
export type ErrorFromErrorMap<TErrorMap extends ErrorMap> = Error | ORPCErrorFromErrorMap<TErrorMap>;
|
|
5
|
+
export interface ValidationErrorOptions extends ErrorOptions {
|
|
6
|
+
message: string;
|
|
7
|
+
issues: readonly StandardSchemaV1.Issue[];
|
|
8
|
+
}
|
|
9
|
+
export declare class ValidationError extends Error {
|
|
10
|
+
readonly issues: readonly StandardSchemaV1.Issue[];
|
|
11
|
+
constructor(options: ValidationErrorOptions);
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=error.d.ts.map
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
/** unnoq */
|
|
2
2
|
import { ContractBuilder } from './builder';
|
|
3
3
|
export * from './builder';
|
|
4
|
+
export * from './client';
|
|
5
|
+
export * from './client-utils';
|
|
4
6
|
export * from './config';
|
|
7
|
+
export * from './error';
|
|
8
|
+
export * from './error-map';
|
|
9
|
+
export * from './error-orpc';
|
|
5
10
|
export * from './procedure';
|
|
11
|
+
export * from './procedure-client';
|
|
6
12
|
export * from './procedure-decorated';
|
|
7
13
|
export * from './router';
|
|
8
14
|
export * from './router-builder';
|
|
15
|
+
export * from './router-client';
|
|
9
16
|
export * from './types';
|
|
10
17
|
export declare const oc: ContractBuilder;
|
|
11
18
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Client } from './client';
|
|
2
|
+
import type { ErrorFromErrorMap } from './error';
|
|
3
|
+
import type { ErrorMap } from './error-map';
|
|
4
|
+
import type { Schema, SchemaInput, SchemaOutput } from './types';
|
|
5
|
+
export type ContractProcedureClient<TClientContext, TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> = Client<TClientContext, SchemaInput<TInputSchema>, SchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
|
|
6
|
+
//# sourceMappingURL=procedure-client.d.ts.map
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
+
import type { ErrorMap } from './error-map';
|
|
1
2
|
import type { RouteOptions } from './procedure';
|
|
2
3
|
import type { HTTPPath, Schema, SchemaInput, SchemaOutput } from './types';
|
|
3
4
|
import { ContractProcedure } from './procedure';
|
|
4
|
-
export declare class DecoratedContractProcedure<TInputSchema extends Schema, TOutputSchema extends Schema> extends ContractProcedure<TInputSchema, TOutputSchema> {
|
|
5
|
-
static decorate<UInputSchema extends Schema
|
|
6
|
-
route(route: RouteOptions): DecoratedContractProcedure<TInputSchema, TOutputSchema>;
|
|
7
|
-
prefix(prefix: HTTPPath): DecoratedContractProcedure<TInputSchema, TOutputSchema>;
|
|
8
|
-
unshiftTag(...tags: string[]): DecoratedContractProcedure<TInputSchema, TOutputSchema>;
|
|
9
|
-
input<U extends Schema
|
|
10
|
-
output<U extends Schema
|
|
5
|
+
export declare class DecoratedContractProcedure<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap> {
|
|
6
|
+
static decorate<UInputSchema extends Schema, UOutputSchema extends Schema, TErrorMap extends ErrorMap>(procedure: ContractProcedure<UInputSchema, UOutputSchema, TErrorMap>): DecoratedContractProcedure<UInputSchema, UOutputSchema, TErrorMap>;
|
|
7
|
+
route(route: RouteOptions): DecoratedContractProcedure<TInputSchema, TOutputSchema, TErrorMap>;
|
|
8
|
+
prefix(prefix: HTTPPath): DecoratedContractProcedure<TInputSchema, TOutputSchema, TErrorMap>;
|
|
9
|
+
unshiftTag(...tags: string[]): DecoratedContractProcedure<TInputSchema, TOutputSchema, TErrorMap>;
|
|
10
|
+
input<U extends Schema>(schema: U, example?: SchemaInput<U>): DecoratedContractProcedure<U, TOutputSchema, TErrorMap>;
|
|
11
|
+
output<U extends Schema>(schema: U, example?: SchemaOutput<U>): DecoratedContractProcedure<TInputSchema, U, TErrorMap>;
|
|
12
|
+
errors<const U extends ErrorMap>(errorMap: U): DecoratedContractProcedure<TInputSchema, TOutputSchema, U>;
|
|
11
13
|
}
|
|
12
14
|
//# sourceMappingURL=procedure-decorated.d.ts.map
|
package/dist/src/procedure.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ErrorMap } from './error-map';
|
|
1
2
|
import type { HTTPMethod, HTTPPath, InputStructure, OutputStructure, Schema, SchemaOutput } from './types';
|
|
2
3
|
export interface RouteOptions {
|
|
3
4
|
method?: HTTPMethod;
|
|
@@ -63,19 +64,20 @@ export interface RouteOptions {
|
|
|
63
64
|
*/
|
|
64
65
|
outputStructure?: OutputStructure;
|
|
65
66
|
}
|
|
66
|
-
export interface ContractProcedureDef<TInputSchema extends Schema, TOutputSchema extends Schema> {
|
|
67
|
+
export interface ContractProcedureDef<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> {
|
|
67
68
|
route?: RouteOptions;
|
|
68
69
|
InputSchema: TInputSchema;
|
|
69
70
|
inputExample?: SchemaOutput<TInputSchema>;
|
|
70
71
|
OutputSchema: TOutputSchema;
|
|
71
72
|
outputExample?: SchemaOutput<TOutputSchema>;
|
|
73
|
+
errorMap: TErrorMap;
|
|
72
74
|
}
|
|
73
|
-
export declare class ContractProcedure<TInputSchema extends Schema, TOutputSchema extends Schema> {
|
|
75
|
+
export declare class ContractProcedure<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> {
|
|
74
76
|
'~type': "ContractProcedure";
|
|
75
|
-
'~orpc': ContractProcedureDef<TInputSchema, TOutputSchema>;
|
|
76
|
-
constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema>);
|
|
77
|
+
'~orpc': ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap>;
|
|
78
|
+
constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap>);
|
|
77
79
|
}
|
|
78
|
-
export type ANY_CONTRACT_PROCEDURE = ContractProcedure<any, any>;
|
|
79
|
-
export type WELL_CONTRACT_PROCEDURE = ContractProcedure<Schema, Schema>;
|
|
80
|
+
export type ANY_CONTRACT_PROCEDURE = ContractProcedure<any, any, any>;
|
|
81
|
+
export type WELL_CONTRACT_PROCEDURE = ContractProcedure<Schema, Schema, ErrorMap>;
|
|
80
82
|
export declare function isContractProcedure(item: unknown): item is ANY_CONTRACT_PROCEDURE;
|
|
81
83
|
//# sourceMappingURL=procedure.d.ts.map
|
|
@@ -3,7 +3,7 @@ import type { ContractRouter } from './router';
|
|
|
3
3
|
import type { HTTPPath } from './types';
|
|
4
4
|
import { DecoratedContractProcedure } from './procedure-decorated';
|
|
5
5
|
export type AdaptedContractRouter<TContract extends ContractRouter> = {
|
|
6
|
-
[K in keyof TContract]: TContract[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema> ? DecoratedContractProcedure<UInputSchema, UOutputSchema> : TContract[K] extends ContractRouter ? AdaptedContractRouter<TContract[K]> : never;
|
|
6
|
+
[K in keyof TContract]: TContract[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrors> ? DecoratedContractProcedure<UInputSchema, UOutputSchema, UErrors> : TContract[K] extends ContractRouter ? AdaptedContractRouter<TContract[K]> : never;
|
|
7
7
|
};
|
|
8
8
|
export interface ContractRouterBuilderDef {
|
|
9
9
|
prefix?: HTTPPath;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ContractProcedure } from './procedure';
|
|
2
|
+
import type { ContractProcedureClient } from './procedure-client';
|
|
3
|
+
import type { ContractRouter } from './router';
|
|
4
|
+
export type ContractRouterClient<TRouter extends ContractRouter, TClientContext> = TRouter extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap> ? ContractProcedureClient<TClientContext, UInputSchema, UOutputSchema, UErrorMap> : {
|
|
5
|
+
[K in keyof TRouter]: TRouter[K] extends ContractRouter ? ContractRouterClient<TRouter[K], TClientContext> : never;
|
|
6
|
+
};
|
|
7
|
+
//# sourceMappingURL=router-client.d.ts.map
|
package/dist/src/router.d.ts
CHANGED
|
@@ -3,10 +3,10 @@ import type { SchemaInput, SchemaOutput } from './types';
|
|
|
3
3
|
export type ContractRouter = ANY_CONTRACT_PROCEDURE | {
|
|
4
4
|
[k: string]: ContractRouter;
|
|
5
5
|
};
|
|
6
|
-
export type InferContractRouterInputs<T extends ContractRouter> = T extends ContractProcedure<infer UInputSchema, any> ? SchemaInput<UInputSchema> : {
|
|
6
|
+
export type InferContractRouterInputs<T extends ContractRouter> = T extends ContractProcedure<infer UInputSchema, any, any> ? SchemaInput<UInputSchema> : {
|
|
7
7
|
[K in keyof T]: T[K] extends ContractRouter ? InferContractRouterInputs<T[K]> : never;
|
|
8
8
|
};
|
|
9
|
-
export type InferContractRouterOutputs<T extends ContractRouter> = T extends ContractProcedure<any, infer UOutputSchema> ? SchemaOutput<UOutputSchema> : {
|
|
9
|
+
export type InferContractRouterOutputs<T extends ContractRouter> = T extends ContractProcedure<any, infer UOutputSchema, any> ? SchemaOutput<UOutputSchema> : {
|
|
10
10
|
[K in keyof T]: T[K] extends ContractRouter ? InferContractRouterOutputs<T[K]> : never;
|
|
11
11
|
};
|
|
12
12
|
//# sourceMappingURL=router.d.ts.map
|
package/dist/src/types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { FindGlobalInstanceType } from '@orpc/shared';
|
|
1
2
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
3
|
export type HTTPPath = `/${string}`;
|
|
3
4
|
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
@@ -6,4 +7,5 @@ export type OutputStructure = 'compact' | 'detailed';
|
|
|
6
7
|
export type Schema = StandardSchemaV1 | undefined;
|
|
7
8
|
export type SchemaInput<TSchema extends Schema, TFallback = unknown> = TSchema extends undefined ? TFallback : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferInput<TSchema> : TFallback;
|
|
8
9
|
export type SchemaOutput<TSchema extends Schema, TFallback = unknown> = TSchema extends undefined ? TFallback : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TSchema> : TFallback;
|
|
10
|
+
export type AbortSignal = FindGlobalInstanceType<'AbortSignal'>;
|
|
9
11
|
//# sourceMappingURL=types.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/contract",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.29.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@standard-schema/spec": "1.0.0-beta.4",
|
|
33
|
-
"@orpc/shared": "0.
|
|
33
|
+
"@orpc/shared": "0.29.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"arktype": "2.0.0-rc.26",
|