@orpc/contract 0.0.0-next.9b3a030 → 0.0.0-next.a2e4a58
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 +167 -44
- package/dist/src/builder.d.ts +24 -11
- package/dist/src/client.d.ts +2 -1
- package/dist/src/config.d.ts +7 -33
- package/dist/src/error-map.d.ts +44 -2
- package/dist/src/index.d.ts +7 -1
- package/dist/src/procedure-builder-with-input.d.ts +19 -0
- package/dist/src/procedure-builder-with-output.d.ts +19 -0
- package/dist/src/procedure-builder.d.ts +15 -0
- package/dist/src/procedure-client.d.ts +6 -0
- package/dist/src/procedure-decorated.d.ts +3 -5
- package/dist/src/router-builder.d.ts +12 -9
- package/dist/src/router-client.d.ts +7 -0
- package/dist/src/router.d.ts +8 -7
- package/dist/src/schema-utils.d.ts +5 -0
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -27,6 +27,15 @@ var DecoratedContractProcedure = class _DecoratedContractProcedure extends Contr
|
|
|
27
27
|
}
|
|
28
28
|
return new _DecoratedContractProcedure(procedure["~orpc"]);
|
|
29
29
|
}
|
|
30
|
+
errors(errors) {
|
|
31
|
+
return new _DecoratedContractProcedure({
|
|
32
|
+
...this["~orpc"],
|
|
33
|
+
errorMap: {
|
|
34
|
+
...this["~orpc"].errorMap,
|
|
35
|
+
...errors
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
30
39
|
route(route) {
|
|
31
40
|
return new _DecoratedContractProcedure({
|
|
32
41
|
...this["~orpc"],
|
|
@@ -59,24 +68,92 @@ var DecoratedContractProcedure = class _DecoratedContractProcedure extends Contr
|
|
|
59
68
|
}
|
|
60
69
|
});
|
|
61
70
|
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// src/procedure-builder-with-input.ts
|
|
74
|
+
var ContractProcedureBuilderWithInput = class _ContractProcedureBuilderWithInput extends ContractProcedure {
|
|
75
|
+
errors(errors) {
|
|
76
|
+
const decorated = DecoratedContractProcedure.decorate(this).errors(errors);
|
|
77
|
+
return new _ContractProcedureBuilderWithInput(decorated["~orpc"]);
|
|
78
|
+
}
|
|
79
|
+
route(route) {
|
|
80
|
+
const decorated = DecoratedContractProcedure.decorate(this).route(route);
|
|
81
|
+
return new _ContractProcedureBuilderWithInput(decorated["~orpc"]);
|
|
82
|
+
}
|
|
83
|
+
prefix(prefix) {
|
|
84
|
+
const decorated = DecoratedContractProcedure.decorate(this).prefix(prefix);
|
|
85
|
+
return new _ContractProcedureBuilderWithInput(decorated["~orpc"]);
|
|
86
|
+
}
|
|
87
|
+
unshiftTag(...tags) {
|
|
88
|
+
const decorated = DecoratedContractProcedure.decorate(this).unshiftTag(...tags);
|
|
89
|
+
return new _ContractProcedureBuilderWithInput(decorated["~orpc"]);
|
|
90
|
+
}
|
|
91
|
+
output(schema, example) {
|
|
92
|
+
return new DecoratedContractProcedure({
|
|
93
|
+
...this["~orpc"],
|
|
94
|
+
OutputSchema: schema,
|
|
95
|
+
outputExample: example
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// src/procedure-builder-with-output.ts
|
|
101
|
+
var ContractProcedureBuilderWithOutput = class _ContractProcedureBuilderWithOutput extends ContractProcedure {
|
|
102
|
+
errors(errors) {
|
|
103
|
+
const decorated = DecoratedContractProcedure.decorate(this).errors(errors);
|
|
104
|
+
return new _ContractProcedureBuilderWithOutput(decorated["~orpc"]);
|
|
105
|
+
}
|
|
106
|
+
route(route) {
|
|
107
|
+
const decorated = DecoratedContractProcedure.decorate(this).route(route);
|
|
108
|
+
return new _ContractProcedureBuilderWithOutput(decorated["~orpc"]);
|
|
109
|
+
}
|
|
110
|
+
prefix(prefix) {
|
|
111
|
+
const decorated = DecoratedContractProcedure.decorate(this).prefix(prefix);
|
|
112
|
+
return new _ContractProcedureBuilderWithOutput(decorated["~orpc"]);
|
|
113
|
+
}
|
|
114
|
+
unshiftTag(...tags) {
|
|
115
|
+
const decorated = DecoratedContractProcedure.decorate(this).unshiftTag(...tags);
|
|
116
|
+
return new _ContractProcedureBuilderWithOutput(decorated["~orpc"]);
|
|
117
|
+
}
|
|
62
118
|
input(schema, example) {
|
|
63
|
-
return new
|
|
119
|
+
return new DecoratedContractProcedure({
|
|
64
120
|
...this["~orpc"],
|
|
65
121
|
InputSchema: schema,
|
|
66
122
|
inputExample: example
|
|
67
123
|
});
|
|
68
124
|
}
|
|
69
|
-
|
|
70
|
-
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// src/procedure-builder.ts
|
|
128
|
+
var ContractProcedureBuilder = class _ContractProcedureBuilder extends ContractProcedure {
|
|
129
|
+
errors(errors) {
|
|
130
|
+
const decorated = DecoratedContractProcedure.decorate(this).errors(errors);
|
|
131
|
+
return new _ContractProcedureBuilder(decorated["~orpc"]);
|
|
132
|
+
}
|
|
133
|
+
route(route) {
|
|
134
|
+
const decorated = DecoratedContractProcedure.decorate(this).route(route);
|
|
135
|
+
return new _ContractProcedureBuilder(decorated["~orpc"]);
|
|
136
|
+
}
|
|
137
|
+
prefix(prefix) {
|
|
138
|
+
const decorated = DecoratedContractProcedure.decorate(this).prefix(prefix);
|
|
139
|
+
return new _ContractProcedureBuilder(decorated["~orpc"]);
|
|
140
|
+
}
|
|
141
|
+
unshiftTag(...tags) {
|
|
142
|
+
const decorated = DecoratedContractProcedure.decorate(this).unshiftTag(...tags);
|
|
143
|
+
return new _ContractProcedureBuilder(decorated["~orpc"]);
|
|
144
|
+
}
|
|
145
|
+
input(schema, example) {
|
|
146
|
+
return new ContractProcedureBuilderWithInput({
|
|
71
147
|
...this["~orpc"],
|
|
72
|
-
|
|
73
|
-
|
|
148
|
+
InputSchema: schema,
|
|
149
|
+
inputExample: example
|
|
74
150
|
});
|
|
75
151
|
}
|
|
76
|
-
|
|
77
|
-
return new
|
|
152
|
+
output(schema, example) {
|
|
153
|
+
return new ContractProcedureBuilderWithOutput({
|
|
78
154
|
...this["~orpc"],
|
|
79
|
-
|
|
155
|
+
OutputSchema: schema,
|
|
156
|
+
outputExample: example
|
|
80
157
|
});
|
|
81
158
|
}
|
|
82
159
|
};
|
|
@@ -100,6 +177,15 @@ var ContractRouterBuilder = class _ContractRouterBuilder {
|
|
|
100
177
|
tags: [...this["~orpc"].tags ?? [], ...tags]
|
|
101
178
|
});
|
|
102
179
|
}
|
|
180
|
+
errors(errors) {
|
|
181
|
+
return new _ContractRouterBuilder({
|
|
182
|
+
...this["~orpc"],
|
|
183
|
+
errorMap: {
|
|
184
|
+
...this["~orpc"].errorMap,
|
|
185
|
+
...errors
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}
|
|
103
189
|
router(router) {
|
|
104
190
|
if (isContractProcedure(router)) {
|
|
105
191
|
let decorated = DecoratedContractProcedure.decorate(router);
|
|
@@ -109,6 +195,7 @@ var ContractRouterBuilder = class _ContractRouterBuilder {
|
|
|
109
195
|
if (this["~orpc"].prefix) {
|
|
110
196
|
decorated = decorated.prefix(this["~orpc"].prefix);
|
|
111
197
|
}
|
|
198
|
+
decorated = decorated.errors(this["~orpc"].errorMap);
|
|
112
199
|
return decorated;
|
|
113
200
|
}
|
|
114
201
|
const adapted = {};
|
|
@@ -120,50 +207,73 @@ var ContractRouterBuilder = class _ContractRouterBuilder {
|
|
|
120
207
|
};
|
|
121
208
|
|
|
122
209
|
// src/builder.ts
|
|
123
|
-
var ContractBuilder = class {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
210
|
+
var ContractBuilder = class _ContractBuilder extends ContractProcedure {
|
|
211
|
+
constructor(def) {
|
|
212
|
+
super(def);
|
|
213
|
+
}
|
|
214
|
+
config(config) {
|
|
215
|
+
return new _ContractBuilder({
|
|
216
|
+
...this["~orpc"],
|
|
217
|
+
config: {
|
|
218
|
+
...this["~orpc"].config,
|
|
219
|
+
...config
|
|
220
|
+
}
|
|
127
221
|
});
|
|
128
222
|
}
|
|
129
|
-
|
|
130
|
-
return new
|
|
131
|
-
|
|
223
|
+
errors(errors) {
|
|
224
|
+
return new _ContractBuilder({
|
|
225
|
+
...this["~orpc"],
|
|
226
|
+
errorMap: {
|
|
227
|
+
...this["~orpc"].errorMap,
|
|
228
|
+
...errors
|
|
229
|
+
}
|
|
132
230
|
});
|
|
133
231
|
}
|
|
134
232
|
route(route) {
|
|
135
|
-
return new
|
|
136
|
-
route
|
|
233
|
+
return new ContractProcedureBuilder({
|
|
234
|
+
route: {
|
|
235
|
+
...this["~orpc"].config.initialRoute,
|
|
236
|
+
...route
|
|
237
|
+
},
|
|
137
238
|
InputSchema: void 0,
|
|
138
239
|
OutputSchema: void 0,
|
|
139
|
-
errorMap:
|
|
240
|
+
errorMap: this["~orpc"].errorMap
|
|
140
241
|
});
|
|
141
242
|
}
|
|
142
243
|
input(schema, example) {
|
|
143
|
-
return new
|
|
244
|
+
return new ContractProcedureBuilderWithInput({
|
|
245
|
+
route: this["~orpc"].config.initialRoute,
|
|
144
246
|
InputSchema: schema,
|
|
145
247
|
inputExample: example,
|
|
146
248
|
OutputSchema: void 0,
|
|
147
|
-
errorMap:
|
|
249
|
+
errorMap: this["~orpc"].errorMap
|
|
148
250
|
});
|
|
149
251
|
}
|
|
150
252
|
output(schema, example) {
|
|
151
|
-
return new
|
|
253
|
+
return new ContractProcedureBuilderWithOutput({
|
|
254
|
+
route: this["~orpc"].config.initialRoute,
|
|
152
255
|
OutputSchema: schema,
|
|
153
256
|
outputExample: example,
|
|
154
257
|
InputSchema: void 0,
|
|
155
|
-
errorMap:
|
|
258
|
+
errorMap: this["~orpc"].errorMap
|
|
156
259
|
});
|
|
157
260
|
}
|
|
158
|
-
|
|
159
|
-
return new
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
261
|
+
prefix(prefix) {
|
|
262
|
+
return new ContractRouterBuilder({
|
|
263
|
+
prefix,
|
|
264
|
+
errorMap: this["~orpc"].errorMap
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
tag(...tags) {
|
|
268
|
+
return new ContractRouterBuilder({
|
|
269
|
+
tags,
|
|
270
|
+
errorMap: this["~orpc"].errorMap
|
|
163
271
|
});
|
|
164
272
|
}
|
|
165
273
|
router(router) {
|
|
166
|
-
return
|
|
274
|
+
return new ContractRouterBuilder({
|
|
275
|
+
errorMap: this["~orpc"].errorMap
|
|
276
|
+
}).router(router);
|
|
167
277
|
}
|
|
168
278
|
};
|
|
169
279
|
|
|
@@ -330,20 +440,9 @@ var DEFAULT_CONFIG = {
|
|
|
330
440
|
defaultInputStructure: "compact",
|
|
331
441
|
defaultOutputStructure: "compact"
|
|
332
442
|
};
|
|
333
|
-
|
|
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) {
|
|
443
|
+
function fallbackContractConfig(key, value) {
|
|
341
444
|
if (value === void 0) {
|
|
342
|
-
|
|
343
|
-
if (fallback === void 0) {
|
|
344
|
-
return DEFAULT_CONFIG[key];
|
|
345
|
-
}
|
|
346
|
-
return fallback;
|
|
445
|
+
return DEFAULT_CONFIG[key];
|
|
347
446
|
}
|
|
348
447
|
return value;
|
|
349
448
|
}
|
|
@@ -357,24 +456,48 @@ var ValidationError = class extends Error {
|
|
|
357
456
|
}
|
|
358
457
|
};
|
|
359
458
|
|
|
459
|
+
// src/schema-utils.ts
|
|
460
|
+
function type(...[map]) {
|
|
461
|
+
return {
|
|
462
|
+
"~standard": {
|
|
463
|
+
vendor: "custom",
|
|
464
|
+
version: 1,
|
|
465
|
+
async validate(value) {
|
|
466
|
+
if (map) {
|
|
467
|
+
return { value: await map(value) };
|
|
468
|
+
}
|
|
469
|
+
return { value };
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
|
|
360
475
|
// src/index.ts
|
|
361
|
-
var oc = new ContractBuilder(
|
|
476
|
+
var oc = new ContractBuilder({
|
|
477
|
+
errorMap: {},
|
|
478
|
+
InputSchema: void 0,
|
|
479
|
+
OutputSchema: void 0,
|
|
480
|
+
config: {}
|
|
481
|
+
});
|
|
362
482
|
export {
|
|
363
483
|
COMMON_ORPC_ERROR_DEFS,
|
|
364
484
|
ContractBuilder,
|
|
365
485
|
ContractProcedure,
|
|
486
|
+
ContractProcedureBuilder,
|
|
487
|
+
ContractProcedureBuilderWithInput,
|
|
488
|
+
ContractProcedureBuilderWithOutput,
|
|
366
489
|
ContractRouterBuilder,
|
|
367
490
|
DecoratedContractProcedure,
|
|
368
491
|
ORPCError,
|
|
369
492
|
ValidationError,
|
|
370
|
-
|
|
493
|
+
fallbackContractConfig,
|
|
371
494
|
fallbackORPCErrorMessage,
|
|
372
495
|
fallbackORPCErrorStatus,
|
|
373
|
-
fallbackToGlobalConfig,
|
|
374
496
|
isContractProcedure,
|
|
375
497
|
isDefinedError,
|
|
376
498
|
oc,
|
|
377
499
|
safe,
|
|
500
|
+
type,
|
|
378
501
|
validateORPCError
|
|
379
502
|
};
|
|
380
503
|
//# sourceMappingURL=index.js.map
|
package/dist/src/builder.d.ts
CHANGED
|
@@ -1,16 +1,29 @@
|
|
|
1
|
-
import type { ErrorMap } from './error-map';
|
|
2
|
-
import type { RouteOptions } from './procedure';
|
|
1
|
+
import type { ErrorMap, ErrorMapGuard, ErrorMapSuggestions, StrictErrorMap } from './error-map';
|
|
2
|
+
import type { ContractProcedureDef, RouteOptions } from './procedure';
|
|
3
3
|
import type { ContractRouter } from './router';
|
|
4
|
+
import type { AdaptedContractRouter } from './router-builder';
|
|
4
5
|
import type { HTTPPath, Schema, SchemaInput, SchemaOutput } from './types';
|
|
5
|
-
import {
|
|
6
|
+
import { ContractProcedure } from './procedure';
|
|
7
|
+
import { ContractProcedureBuilder } from './procedure-builder';
|
|
8
|
+
import { ContractProcedureBuilderWithInput } from './procedure-builder-with-input';
|
|
9
|
+
import { ContractProcedureBuilderWithOutput } from './procedure-builder-with-output';
|
|
6
10
|
import { ContractRouterBuilder } from './router-builder';
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
export interface ContractBuilderConfig {
|
|
12
|
+
initialRoute?: RouteOptions;
|
|
13
|
+
}
|
|
14
|
+
export interface ContractBuilderDef<TErrorMap extends ErrorMap> extends ContractProcedureDef<undefined, undefined, TErrorMap> {
|
|
15
|
+
config: ContractBuilderConfig;
|
|
16
|
+
}
|
|
17
|
+
export declare class ContractBuilder<TErrorMap extends ErrorMap> extends ContractProcedure<undefined, undefined, TErrorMap> {
|
|
18
|
+
'~orpc': ContractBuilderDef<TErrorMap>;
|
|
19
|
+
constructor(def: ContractBuilderDef<TErrorMap>);
|
|
20
|
+
config(config: ContractBuilderConfig): ContractBuilder<TErrorMap>;
|
|
21
|
+
errors<const U extends ErrorMap & ErrorMapGuard<TErrorMap> & ErrorMapSuggestions>(errors: U): ContractBuilder<U & TErrorMap>;
|
|
22
|
+
route(route: RouteOptions): ContractProcedureBuilder<TErrorMap>;
|
|
23
|
+
input<U extends Schema>(schema: U, example?: SchemaInput<U>): ContractProcedureBuilderWithInput<U, TErrorMap>;
|
|
24
|
+
output<U extends Schema>(schema: U, example?: SchemaOutput<U>): ContractProcedureBuilderWithOutput<U, TErrorMap>;
|
|
25
|
+
prefix(prefix: HTTPPath): ContractRouterBuilder<TErrorMap>;
|
|
26
|
+
tag(...tags: string[]): ContractRouterBuilder<TErrorMap>;
|
|
27
|
+
router<T extends ContractRouter<ErrorMap & Partial<StrictErrorMap<TErrorMap>>>>(router: T): AdaptedContractRouter<T, TErrorMap>;
|
|
15
28
|
}
|
|
16
29
|
//# sourceMappingURL=builder.d.ts.map
|
package/dist/src/client.d.ts
CHANGED
|
@@ -6,11 +6,12 @@ export type ClientOptions<TClientContext> = {
|
|
|
6
6
|
} : {
|
|
7
7
|
context: TClientContext;
|
|
8
8
|
});
|
|
9
|
+
export type ClientRest<TClientContext, TInput> = [input: TInput, options: ClientOptions<TClientContext>] | (undefined extends TInput & TClientContext ? [] : never) | (undefined extends TClientContext ? [input: TInput] : never);
|
|
9
10
|
export type ClientPromiseResult<TOutput, TError extends Error> = Promise<TOutput> & {
|
|
10
11
|
__typeError?: TError;
|
|
11
12
|
};
|
|
12
13
|
export interface Client<TClientContext, TInput, TOutput, TError extends Error> {
|
|
13
|
-
(...
|
|
14
|
+
(...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
|
|
14
15
|
}
|
|
15
16
|
export type NestedClient<TClientContext> = Client<TClientContext, any, any, any> | {
|
|
16
17
|
[k: string]: NestedClient<TClientContext>;
|
package/dist/src/config.d.ts
CHANGED
|
@@ -1,36 +1,10 @@
|
|
|
1
1
|
import type { HTTPMethod, InputStructure } from './types';
|
|
2
|
-
export interface
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
* @default 200
|
|
10
|
-
*/
|
|
11
|
-
defaultSuccessStatus?: number;
|
|
12
|
-
/**
|
|
13
|
-
*
|
|
14
|
-
* @default 'OK'
|
|
15
|
-
*/
|
|
16
|
-
defaultSuccessDescription?: string;
|
|
17
|
-
/**
|
|
18
|
-
*
|
|
19
|
-
* @default 'compact'
|
|
20
|
-
*/
|
|
21
|
-
defaultInputStructure?: InputStructure;
|
|
22
|
-
/**
|
|
23
|
-
*
|
|
24
|
-
* @default 'compact'
|
|
25
|
-
*/
|
|
26
|
-
defaultOutputStructure?: InputStructure;
|
|
2
|
+
export interface ContractConfig {
|
|
3
|
+
defaultMethod: HTTPMethod;
|
|
4
|
+
defaultSuccessStatus: number;
|
|
5
|
+
defaultSuccessDescription: string;
|
|
6
|
+
defaultInputStructure: InputStructure;
|
|
7
|
+
defaultOutputStructure: InputStructure;
|
|
27
8
|
}
|
|
28
|
-
|
|
29
|
-
* Set the global configuration, this configuration can effect entire project
|
|
30
|
-
*/
|
|
31
|
-
export declare function configGlobal(config: ORPCConfig): void;
|
|
32
|
-
/**
|
|
33
|
-
* Fallback the value to the global config if it is undefined
|
|
34
|
-
*/
|
|
35
|
-
export declare function fallbackToGlobalConfig<T extends keyof ORPCConfig>(key: T, value: ORPCConfig[T]): Exclude<ORPCConfig[T], undefined>;
|
|
9
|
+
export declare function fallbackContractConfig<T extends keyof ContractConfig>(key: T, value: ContractConfig[T] | undefined): ContractConfig[T];
|
|
36
10
|
//# sourceMappingURL=config.d.ts.map
|
package/dist/src/error-map.d.ts
CHANGED
|
@@ -3,14 +3,56 @@ import type { Schema } from './types';
|
|
|
3
3
|
export type ErrorMapItem<TDataSchema extends Schema> = {
|
|
4
4
|
/**
|
|
5
5
|
*
|
|
6
|
-
* @default
|
|
6
|
+
* @default 500
|
|
7
7
|
*/
|
|
8
8
|
status?: number;
|
|
9
9
|
message?: string;
|
|
10
10
|
description?: string;
|
|
11
11
|
data?: TDataSchema;
|
|
12
12
|
};
|
|
13
|
-
export
|
|
13
|
+
export interface ErrorMap {
|
|
14
|
+
[k: string]: ErrorMapItem<Schema>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* const U extends ErrorMap & ErrorMapGuard<TErrorMap> & ErrorMapSuggestions
|
|
18
|
+
*
|
|
19
|
+
* Purpose:
|
|
20
|
+
* - Helps `U` suggest `CommonORPCErrorCode` to the user when typing.
|
|
21
|
+
*
|
|
22
|
+
* Why not replace `ErrorMap` with `ErrorMapSuggestions`?
|
|
23
|
+
* - `ErrorMapSuggestions` has a drawback: it allows `undefined` values for items.
|
|
24
|
+
* - `ErrorMapGuard<TErrorMap>` uses `Partial`, which can introduce `undefined` values.
|
|
25
|
+
*
|
|
26
|
+
* This could lead to unintended behavior where `undefined` values override `TErrorMap`,
|
|
27
|
+
* potentially resulting in a `never` type after merging.
|
|
28
|
+
*
|
|
29
|
+
* Recommendation:
|
|
30
|
+
* - Use `ErrorMapSuggestions` to assist users in typing correctly but do not replace `ErrorMap`.
|
|
31
|
+
* - Ensure `ErrorMapGuard<TErrorMap>` is adjusted to prevent `undefined` values.
|
|
32
|
+
*/
|
|
33
|
+
export type ErrorMapSuggestions = {
|
|
14
34
|
[key in CommonORPCErrorCode | (string & {})]?: ErrorMapItem<Schema>;
|
|
15
35
|
};
|
|
36
|
+
/**
|
|
37
|
+
* `U` extends `ErrorMap` & `ErrorMapGuard<TErrorMap>`
|
|
38
|
+
*
|
|
39
|
+
* `ErrorMapGuard` is a utility type that ensures `U` cannot redefine the structure of `TErrorMap`.
|
|
40
|
+
* It achieves this by setting each key in `TErrorMap` to `never`, effectively preventing any redefinition.
|
|
41
|
+
*
|
|
42
|
+
* Why not just use `Partial<TErrorMap>`?
|
|
43
|
+
* - Allowing users to redefine existing error map items would require using `StrictErrorMap`.
|
|
44
|
+
* - However, I prefer not to use `StrictErrorMap` frequently, due to perceived performance concerns,
|
|
45
|
+
* though this has not been benchmarked and is based on personal preference.
|
|
46
|
+
*
|
|
47
|
+
*/
|
|
48
|
+
export type ErrorMapGuard<TErrorMap extends ErrorMap> = {
|
|
49
|
+
[K in keyof TErrorMap]?: never;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Since `undefined` has a specific meaning (it use default value),
|
|
53
|
+
* we ensure all additional properties in each item of the ErrorMap are explicitly set to `undefined`.
|
|
54
|
+
*/
|
|
55
|
+
export type StrictErrorMap<T extends ErrorMap> = {
|
|
56
|
+
[K in keyof T]: T[K] & Partial<Record<Exclude<keyof ErrorMapItem<any>, keyof T[K]>, undefined>>;
|
|
57
|
+
};
|
|
16
58
|
//# sourceMappingURL=error-map.d.ts.map
|
package/dist/src/index.d.ts
CHANGED
|
@@ -8,9 +8,15 @@ export * from './error';
|
|
|
8
8
|
export * from './error-map';
|
|
9
9
|
export * from './error-orpc';
|
|
10
10
|
export * from './procedure';
|
|
11
|
+
export * from './procedure-builder';
|
|
12
|
+
export * from './procedure-builder-with-input';
|
|
13
|
+
export * from './procedure-builder-with-output';
|
|
14
|
+
export * from './procedure-client';
|
|
11
15
|
export * from './procedure-decorated';
|
|
12
16
|
export * from './router';
|
|
13
17
|
export * from './router-builder';
|
|
18
|
+
export * from './router-client';
|
|
19
|
+
export * from './schema-utils';
|
|
14
20
|
export * from './types';
|
|
15
|
-
export declare const oc: ContractBuilder
|
|
21
|
+
export declare const oc: ContractBuilder<Record<never, never>>;
|
|
16
22
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ErrorMap, ErrorMapGuard, ErrorMapSuggestions } from './error-map';
|
|
2
|
+
import type { RouteOptions } from './procedure';
|
|
3
|
+
import type { HTTPPath, Schema, SchemaOutput } from './types';
|
|
4
|
+
import { ContractProcedure } from './procedure';
|
|
5
|
+
import { DecoratedContractProcedure } from './procedure-decorated';
|
|
6
|
+
/**
|
|
7
|
+
* `ContractProcedureBuilderWithInput` is a branch of `ContractProcedureBuilder` which it has input schema.
|
|
8
|
+
*
|
|
9
|
+
* Why?
|
|
10
|
+
* - prevents override input schema after .input
|
|
11
|
+
*/
|
|
12
|
+
export declare class ContractProcedureBuilderWithInput<TInputSchema extends Schema, TErrorMap extends ErrorMap> extends ContractProcedure<TInputSchema, undefined, TErrorMap> {
|
|
13
|
+
errors<const U extends ErrorMap & ErrorMapGuard<TErrorMap> & ErrorMapSuggestions>(errors: U): ContractProcedureBuilderWithInput<TInputSchema, TErrorMap & U>;
|
|
14
|
+
route(route: RouteOptions): ContractProcedureBuilderWithInput<TInputSchema, TErrorMap>;
|
|
15
|
+
prefix(prefix: HTTPPath): ContractProcedureBuilderWithInput<TInputSchema, TErrorMap>;
|
|
16
|
+
unshiftTag(...tags: string[]): ContractProcedureBuilderWithInput<TInputSchema, TErrorMap>;
|
|
17
|
+
output<U extends Schema>(schema: U, example?: SchemaOutput<U>): DecoratedContractProcedure<TInputSchema, U, TErrorMap>;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=procedure-builder-with-input.d.ts.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ErrorMap, ErrorMapGuard, ErrorMapSuggestions } from './error-map';
|
|
2
|
+
import type { RouteOptions } from './procedure';
|
|
3
|
+
import type { HTTPPath, Schema, SchemaInput } from './types';
|
|
4
|
+
import { ContractProcedure } from './procedure';
|
|
5
|
+
import { DecoratedContractProcedure } from './procedure-decorated';
|
|
6
|
+
/**
|
|
7
|
+
* `ContractProcedureBuilderWithOutput` is a branch of `ContractProcedureBuilder` which it has output schema.
|
|
8
|
+
*
|
|
9
|
+
* Why?
|
|
10
|
+
* - prevents override output schema after .output
|
|
11
|
+
*/
|
|
12
|
+
export declare class ContractProcedureBuilderWithOutput<TOutputSchema extends Schema, TErrorMap extends ErrorMap> extends ContractProcedure<undefined, TOutputSchema, TErrorMap> {
|
|
13
|
+
errors<const U extends ErrorMap & ErrorMapGuard<TErrorMap> & ErrorMapSuggestions>(errors: U): ContractProcedureBuilderWithOutput<TOutputSchema, TErrorMap & U>;
|
|
14
|
+
route(route: RouteOptions): ContractProcedureBuilderWithOutput<TOutputSchema, TErrorMap>;
|
|
15
|
+
prefix(prefix: HTTPPath): ContractProcedureBuilderWithOutput<TOutputSchema, TErrorMap>;
|
|
16
|
+
unshiftTag(...tags: string[]): ContractProcedureBuilderWithOutput<TOutputSchema, TErrorMap>;
|
|
17
|
+
input<U extends Schema>(schema: U, example?: SchemaInput<U>): DecoratedContractProcedure<U, TOutputSchema, TErrorMap>;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=procedure-builder-with-output.d.ts.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ErrorMap, ErrorMapGuard, ErrorMapSuggestions } from './error-map';
|
|
2
|
+
import type { RouteOptions } from './procedure';
|
|
3
|
+
import type { HTTPPath, Schema, SchemaInput, SchemaOutput } from './types';
|
|
4
|
+
import { ContractProcedure } from './procedure';
|
|
5
|
+
import { ContractProcedureBuilderWithInput } from './procedure-builder-with-input';
|
|
6
|
+
import { ContractProcedureBuilderWithOutput } from './procedure-builder-with-output';
|
|
7
|
+
export declare class ContractProcedureBuilder<TErrorMap extends ErrorMap> extends ContractProcedure<undefined, undefined, TErrorMap> {
|
|
8
|
+
errors<const U extends ErrorMap & ErrorMapGuard<TErrorMap> & ErrorMapSuggestions>(errors: U): ContractProcedureBuilder<TErrorMap & U>;
|
|
9
|
+
route(route: RouteOptions): ContractProcedureBuilder<TErrorMap>;
|
|
10
|
+
prefix(prefix: HTTPPath): ContractProcedureBuilder<TErrorMap>;
|
|
11
|
+
unshiftTag(...tags: string[]): ContractProcedureBuilder<TErrorMap>;
|
|
12
|
+
input<U extends Schema>(schema: U, example?: SchemaInput<U>): ContractProcedureBuilderWithInput<U, TErrorMap>;
|
|
13
|
+
output<U extends Schema>(schema: U, example?: SchemaOutput<U>): ContractProcedureBuilderWithOutput<U, TErrorMap>;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=procedure-builder.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,14 +1,12 @@
|
|
|
1
|
-
import type { ErrorMap } from './error-map';
|
|
1
|
+
import type { ErrorMap, ErrorMapGuard, ErrorMapSuggestions } from './error-map';
|
|
2
2
|
import type { RouteOptions } from './procedure';
|
|
3
|
-
import type { HTTPPath, Schema
|
|
3
|
+
import type { HTTPPath, Schema } from './types';
|
|
4
4
|
import { ContractProcedure } from './procedure';
|
|
5
5
|
export declare class DecoratedContractProcedure<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap> {
|
|
6
6
|
static decorate<UInputSchema extends Schema, UOutputSchema extends Schema, TErrorMap extends ErrorMap>(procedure: ContractProcedure<UInputSchema, UOutputSchema, TErrorMap>): DecoratedContractProcedure<UInputSchema, UOutputSchema, TErrorMap>;
|
|
7
|
+
errors<const U extends ErrorMap & ErrorMapGuard<TErrorMap> & ErrorMapSuggestions>(errors: U): DecoratedContractProcedure<TInputSchema, TOutputSchema, TErrorMap & U>;
|
|
7
8
|
route(route: RouteOptions): DecoratedContractProcedure<TInputSchema, TOutputSchema, TErrorMap>;
|
|
8
9
|
prefix(prefix: HTTPPath): DecoratedContractProcedure<TInputSchema, TOutputSchema, TErrorMap>;
|
|
9
10
|
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>;
|
|
13
11
|
}
|
|
14
12
|
//# sourceMappingURL=procedure-decorated.d.ts.map
|
|
@@ -1,20 +1,23 @@
|
|
|
1
|
+
import type { ErrorMap, ErrorMapGuard, ErrorMapSuggestions, StrictErrorMap } from './error-map';
|
|
1
2
|
import type { ContractProcedure } from './procedure';
|
|
2
3
|
import type { ContractRouter } from './router';
|
|
3
4
|
import type { HTTPPath } from './types';
|
|
4
5
|
import { DecoratedContractProcedure } from './procedure-decorated';
|
|
5
|
-
export type AdaptedContractRouter<TContract extends ContractRouter> = {
|
|
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;
|
|
6
|
+
export type AdaptedContractRouter<TContract extends ContractRouter<any>, TErrorMapExtra extends ErrorMap> = {
|
|
7
|
+
[K in keyof TContract]: TContract[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrors> ? DecoratedContractProcedure<UInputSchema, UOutputSchema, UErrors & TErrorMapExtra> : TContract[K] extends ContractRouter<any> ? AdaptedContractRouter<TContract[K], TErrorMapExtra> : never;
|
|
7
8
|
};
|
|
8
|
-
export interface ContractRouterBuilderDef {
|
|
9
|
+
export interface ContractRouterBuilderDef<TErrorMap extends ErrorMap> {
|
|
9
10
|
prefix?: HTTPPath;
|
|
10
11
|
tags?: string[];
|
|
12
|
+
errorMap: TErrorMap;
|
|
11
13
|
}
|
|
12
|
-
export declare class ContractRouterBuilder {
|
|
14
|
+
export declare class ContractRouterBuilder<TErrorMap extends ErrorMap> {
|
|
13
15
|
'~type': "ContractProcedure";
|
|
14
|
-
'~orpc': ContractRouterBuilderDef
|
|
15
|
-
constructor(def: ContractRouterBuilderDef);
|
|
16
|
-
prefix(prefix: HTTPPath): ContractRouterBuilder
|
|
17
|
-
tag(...tags: string[]): ContractRouterBuilder
|
|
18
|
-
|
|
16
|
+
'~orpc': ContractRouterBuilderDef<TErrorMap>;
|
|
17
|
+
constructor(def: ContractRouterBuilderDef<TErrorMap>);
|
|
18
|
+
prefix(prefix: HTTPPath): ContractRouterBuilder<TErrorMap>;
|
|
19
|
+
tag(...tags: string[]): ContractRouterBuilder<TErrorMap>;
|
|
20
|
+
errors<const U extends ErrorMap & ErrorMapGuard<TErrorMap> & ErrorMapSuggestions>(errors: U): ContractRouterBuilder<U & TErrorMap>;
|
|
21
|
+
router<T extends ContractRouter<ErrorMap & Partial<StrictErrorMap<TErrorMap>>>>(router: T): AdaptedContractRouter<T, TErrorMap>;
|
|
19
22
|
}
|
|
20
23
|
//# sourceMappingURL=router-builder.d.ts.map
|
|
@@ -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<any>, TClientContext> = TRouter extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap> ? ContractProcedureClient<TClientContext, UInputSchema, UOutputSchema, UErrorMap> : {
|
|
5
|
+
[K in keyof TRouter]: TRouter[K] extends ContractRouter<any> ? ContractRouterClient<TRouter[K], TClientContext> : never;
|
|
6
|
+
};
|
|
7
|
+
//# sourceMappingURL=router-client.d.ts.map
|
package/dist/src/router.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ErrorMap } from './error-map';
|
|
2
|
+
import type { ContractProcedure } from './procedure';
|
|
2
3
|
import type { SchemaInput, SchemaOutput } from './types';
|
|
3
|
-
export type ContractRouter =
|
|
4
|
-
[k: string]: ContractRouter
|
|
4
|
+
export type ContractRouter<T extends ErrorMap> = ContractProcedure<any, any, T> | {
|
|
5
|
+
[k: string]: ContractRouter<T>;
|
|
5
6
|
};
|
|
6
|
-
export type InferContractRouterInputs<T extends ContractRouter
|
|
7
|
-
[K in keyof T]: T[K] extends ContractRouter ? InferContractRouterInputs<T[K]> : never;
|
|
7
|
+
export type InferContractRouterInputs<T extends ContractRouter<any>> = T extends ContractProcedure<infer UInputSchema, any, any> ? SchemaInput<UInputSchema> : {
|
|
8
|
+
[K in keyof T]: T[K] extends ContractRouter<any> ? InferContractRouterInputs<T[K]> : never;
|
|
8
9
|
};
|
|
9
|
-
export type InferContractRouterOutputs<T extends ContractRouter
|
|
10
|
-
[K in keyof T]: T[K] extends ContractRouter ? InferContractRouterOutputs<T[K]> : never;
|
|
10
|
+
export type InferContractRouterOutputs<T extends ContractRouter<any>> = T extends ContractProcedure<any, infer UOutputSchema, any> ? SchemaOutput<UOutputSchema> : {
|
|
11
|
+
[K in keyof T]: T[K] extends ContractRouter<any> ? InferContractRouterOutputs<T[K]> : never;
|
|
11
12
|
};
|
|
12
13
|
//# sourceMappingURL=router.d.ts.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { IsEqual, Promisable } from '@orpc/shared';
|
|
2
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
3
|
+
export type TypeRest<TInput, TOutput> = [map: (input: TInput) => Promisable<TOutput>] | (IsEqual<TInput, TOutput> extends true ? [] : never);
|
|
4
|
+
export declare function type<TInput, TOutput = TInput>(...[map]: TypeRest<TInput, TOutput>): StandardSchemaV1<TInput, TOutput>;
|
|
5
|
+
//# sourceMappingURL=schema-utils.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.0.0-next.
|
|
4
|
+
"version": "0.0.0-next.a2e4a58",
|
|
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.0.0-next.
|
|
33
|
+
"@orpc/shared": "0.0.0-next.a2e4a58"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"arktype": "2.0.0-rc.26",
|