@orpc/contract 0.32.0 → 0.34.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 CHANGED
@@ -1,12 +1,21 @@
1
+ // src/error-map.ts
2
+ function mergeErrorMap(errorMap1, errorMap2) {
3
+ return { ...errorMap1, ...errorMap2 };
4
+ }
5
+
6
+ // src/meta.ts
7
+ function mergeMeta(meta1, meta2) {
8
+ return { ...meta1, ...meta2 };
9
+ }
10
+
1
11
  // src/procedure.ts
2
12
  var ContractProcedure = class {
3
- "~type" = "ContractProcedure";
4
13
  "~orpc";
5
14
  constructor(def) {
6
15
  if (def.route?.successStatus && (def.route.successStatus < 200 || def.route?.successStatus > 299)) {
7
16
  throw new Error("[ContractProcedure] The successStatus must be between 200 and 299");
8
17
  }
9
- if (Object.values(def.errorMap ?? {}).some((val) => val && val.status && (val.status < 400 || val.status > 599))) {
18
+ if (Object.values(def.errorMap).some((val) => val && val.status && (val.status < 400 || val.status > 599))) {
10
19
  throw new Error("[ContractProcedure] The error status code must be in the 400-599 range.");
11
20
  }
12
21
  this["~orpc"] = def;
@@ -16,266 +25,140 @@ function isContractProcedure(item) {
16
25
  if (item instanceof ContractProcedure) {
17
26
  return true;
18
27
  }
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"];
28
+ return (typeof item === "object" || typeof item === "function") && item !== null && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "inputSchema" in item["~orpc"] && "outputSchema" in item["~orpc"] && "errorMap" in item["~orpc"] && "route" in item["~orpc"] && "meta" in item["~orpc"];
20
29
  }
21
30
 
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
- errors(errors) {
31
- return new _DecoratedContractProcedure({
32
- ...this["~orpc"],
33
- errorMap: {
34
- ...this["~orpc"].errorMap,
35
- ...errors
36
- }
37
- });
38
- }
39
- route(route) {
40
- return new _DecoratedContractProcedure({
41
- ...this["~orpc"],
42
- route: {
43
- ...this["~orpc"].route,
44
- ...route
45
- }
46
- });
47
- }
48
- prefix(prefix) {
49
- return new _DecoratedContractProcedure({
50
- ...this["~orpc"],
51
- ...this["~orpc"].route?.path ? {
52
- route: {
53
- ...this["~orpc"].route,
54
- path: `${prefix}${this["~orpc"].route.path}`
55
- }
56
- } : void 0
57
- });
58
- }
59
- unshiftTag(...tags) {
60
- return new _DecoratedContractProcedure({
61
- ...this["~orpc"],
62
- route: {
63
- ...this["~orpc"].route,
64
- tags: [
65
- ...tags,
66
- ...this["~orpc"].route?.tags?.filter((tag) => !tags.includes(tag)) ?? []
67
- ]
68
- }
69
- });
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"]);
31
+ // src/route.ts
32
+ function mergeRoute(a, b) {
33
+ return { ...a, ...b };
34
+ }
35
+ function prefixRoute(route, prefix) {
36
+ if (!route.path) {
37
+ return route;
86
38
  }
87
- unshiftTag(...tags) {
88
- const decorated = DecoratedContractProcedure.decorate(this).unshiftTag(...tags);
89
- return new _ContractProcedureBuilderWithInput(decorated["~orpc"]);
39
+ return {
40
+ ...route,
41
+ path: `${prefix}${route.path}`
42
+ };
43
+ }
44
+ function unshiftTagRoute(route, tags) {
45
+ return {
46
+ ...route,
47
+ tags: [...tags, ...route.tags ?? []]
48
+ };
49
+ }
50
+ function mergePrefix(a, b) {
51
+ return a ? `${a}${b}` : b;
52
+ }
53
+ function mergeTags(a, b) {
54
+ return a ? [...a, ...b] : b;
55
+ }
56
+ function adaptRoute(route, options) {
57
+ let router = route;
58
+ if (options.prefix) {
59
+ router = prefixRoute(router, options.prefix);
90
60
  }
91
- output(schema, example) {
92
- return new DecoratedContractProcedure({
93
- ...this["~orpc"],
94
- OutputSchema: schema,
95
- outputExample: example
96
- });
61
+ if (options.tags) {
62
+ router = unshiftTagRoute(router, options.tags);
97
63
  }
98
- };
64
+ return router;
65
+ }
99
66
 
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
- }
118
- input(schema, example) {
119
- return new DecoratedContractProcedure({
120
- ...this["~orpc"],
121
- InputSchema: schema,
122
- inputExample: example
67
+ // src/router.ts
68
+ function adaptContractRouter(contract, options) {
69
+ if (isContractProcedure(contract)) {
70
+ const adapted2 = new ContractProcedure({
71
+ ...contract["~orpc"],
72
+ errorMap: mergeErrorMap(options.errorMap, contract["~orpc"].errorMap),
73
+ route: adaptRoute(contract["~orpc"].route, options)
123
74
  });
75
+ return adapted2;
124
76
  }
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"]);
77
+ const adapted = {};
78
+ for (const key in contract) {
79
+ adapted[key] = adaptContractRouter(contract[key], options);
136
80
  }
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({
147
- ...this["~orpc"],
148
- InputSchema: schema,
149
- inputExample: example
150
- });
151
- }
152
- output(schema, example) {
153
- return new ContractProcedureBuilderWithOutput({
154
- ...this["~orpc"],
155
- OutputSchema: schema,
156
- outputExample: example
157
- });
158
- }
159
- };
81
+ return adapted;
82
+ }
160
83
 
161
- // src/router-builder.ts
162
- var ContractRouterBuilder = class _ContractRouterBuilder {
163
- "~type" = "ContractProcedure";
164
- "~orpc";
84
+ // src/builder.ts
85
+ var ContractBuilder = class _ContractBuilder extends ContractProcedure {
165
86
  constructor(def) {
166
- this["~orpc"] = def;
87
+ super(def);
88
+ this["~orpc"].prefix = def.prefix;
89
+ this["~orpc"].tags = def.tags;
167
90
  }
168
- prefix(prefix) {
169
- return new _ContractRouterBuilder({
91
+ /**
92
+ * Reset initial meta
93
+ */
94
+ $meta(initialMeta) {
95
+ return new _ContractBuilder({
170
96
  ...this["~orpc"],
171
- prefix: `${this["~orpc"].prefix ?? ""}${prefix}`
97
+ meta: initialMeta
172
98
  });
173
99
  }
174
- tag(...tags) {
175
- return new _ContractRouterBuilder({
100
+ /**
101
+ * Reset initial route
102
+ */
103
+ $route(initialRoute) {
104
+ return new _ContractBuilder({
176
105
  ...this["~orpc"],
177
- tags: [...this["~orpc"].tags ?? [], ...tags]
106
+ route: initialRoute
178
107
  });
179
108
  }
180
109
  errors(errors) {
181
- return new _ContractRouterBuilder({
182
- ...this["~orpc"],
183
- errorMap: {
184
- ...this["~orpc"].errorMap,
185
- ...errors
186
- }
187
- });
188
- }
189
- router(router) {
190
- if (isContractProcedure(router)) {
191
- let decorated = DecoratedContractProcedure.decorate(router);
192
- if (this["~orpc"].tags) {
193
- decorated = decorated.unshiftTag(...this["~orpc"].tags);
194
- }
195
- if (this["~orpc"].prefix) {
196
- decorated = decorated.prefix(this["~orpc"].prefix);
197
- }
198
- decorated = decorated.errors(this["~orpc"].errorMap);
199
- return decorated;
200
- }
201
- const adapted = {};
202
- for (const key in router) {
203
- adapted[key] = this.router(router[key]);
204
- }
205
- return adapted;
206
- }
207
- };
208
-
209
- // src/builder.ts
210
- var ContractBuilder = class _ContractBuilder extends ContractProcedure {
211
- constructor(def) {
212
- super(def);
213
- }
214
- config(config) {
215
110
  return new _ContractBuilder({
216
111
  ...this["~orpc"],
217
- config: {
218
- ...this["~orpc"].config,
219
- ...config
220
- }
112
+ errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
221
113
  });
222
114
  }
223
- errors(errors) {
115
+ meta(meta) {
224
116
  return new _ContractBuilder({
225
117
  ...this["~orpc"],
226
- errorMap: {
227
- ...this["~orpc"].errorMap,
228
- ...errors
229
- }
118
+ meta: mergeMeta(this["~orpc"].meta, meta)
230
119
  });
231
120
  }
232
121
  route(route) {
233
- return new ContractProcedureBuilder({
234
- route: {
235
- ...this["~orpc"].config.initialRoute,
236
- ...route
237
- },
238
- InputSchema: void 0,
239
- OutputSchema: void 0,
240
- errorMap: this["~orpc"].errorMap
122
+ return new _ContractBuilder({
123
+ ...this["~orpc"],
124
+ route: mergeRoute(this["~orpc"].route, route)
241
125
  });
242
126
  }
243
- input(schema, example) {
244
- return new ContractProcedureBuilderWithInput({
245
- route: this["~orpc"].config.initialRoute,
246
- InputSchema: schema,
247
- inputExample: example,
248
- OutputSchema: void 0,
249
- errorMap: this["~orpc"].errorMap
127
+ input(schema) {
128
+ return new _ContractBuilder({
129
+ ...this["~orpc"],
130
+ inputSchema: schema
250
131
  });
251
132
  }
252
- output(schema, example) {
253
- return new ContractProcedureBuilderWithOutput({
254
- route: this["~orpc"].config.initialRoute,
255
- OutputSchema: schema,
256
- outputExample: example,
257
- InputSchema: void 0,
258
- errorMap: this["~orpc"].errorMap
133
+ output(schema) {
134
+ return new _ContractBuilder({
135
+ ...this["~orpc"],
136
+ outputSchema: schema
259
137
  });
260
138
  }
261
139
  prefix(prefix) {
262
- return new ContractRouterBuilder({
263
- prefix,
264
- errorMap: this["~orpc"].errorMap
140
+ return new _ContractBuilder({
141
+ ...this["~orpc"],
142
+ prefix: mergePrefix(this["~orpc"].prefix, prefix)
265
143
  });
266
144
  }
267
145
  tag(...tags) {
268
- return new ContractRouterBuilder({
269
- tags,
270
- errorMap: this["~orpc"].errorMap
146
+ return new _ContractBuilder({
147
+ ...this["~orpc"],
148
+ tags: mergeTags(this["~orpc"].tags, tags)
271
149
  });
272
150
  }
273
151
  router(router) {
274
- return new ContractRouterBuilder({
275
- errorMap: this["~orpc"].errorMap
276
- }).router(router);
152
+ return adaptContractRouter(router, this["~orpc"]);
277
153
  }
278
154
  };
155
+ var oc = new ContractBuilder({
156
+ errorMap: {},
157
+ inputSchema: void 0,
158
+ outputSchema: void 0,
159
+ route: {},
160
+ meta: {}
161
+ });
279
162
 
280
163
  // src/error-orpc.ts
281
164
  import { isPlainObject } from "@orpc/shared";
@@ -363,21 +246,21 @@ function fallbackORPCErrorStatus(code, status) {
363
246
  function fallbackORPCErrorMessage(code, message) {
364
247
  return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
365
248
  }
366
- var ORPCError = class extends Error {
249
+ var ORPCError = class _ORPCError extends Error {
367
250
  defined;
368
251
  code;
369
252
  status;
370
253
  data;
371
- constructor(options) {
372
- if (options.status && (options.status < 400 || options.status >= 600)) {
254
+ constructor(code, ...[options]) {
255
+ if (options?.status && (options.status < 400 || options.status >= 600)) {
373
256
  throw new Error("[ORPCError] The error status code must be in the 400-599 range.");
374
257
  }
375
- const message = fallbackORPCErrorMessage(options.code, options.message);
258
+ const message = fallbackORPCErrorMessage(code, options?.message);
376
259
  super(message, options);
377
- this.code = options.code;
378
- this.status = fallbackORPCErrorStatus(options.code, options.status);
379
- this.defined = options.defined ?? false;
380
- this.data = options.data;
260
+ this.code = code;
261
+ this.status = fallbackORPCErrorStatus(code, options?.status);
262
+ this.defined = options?.defined ?? false;
263
+ this.data = options?.data;
381
264
  }
382
265
  toJSON() {
383
266
  return {
@@ -388,34 +271,53 @@ var ORPCError = class extends Error {
388
271
  data: this.data
389
272
  };
390
273
  }
274
+ static fromJSON(json) {
275
+ return new _ORPCError(json.code, json);
276
+ }
391
277
  static isValidJSON(json) {
392
278
  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";
393
279
  }
394
280
  };
281
+
282
+ // src/error-utils.ts
395
283
  function isDefinedError(error) {
396
284
  return error instanceof ORPCError && error.defined;
397
285
  }
286
+ function createORPCErrorConstructorMap(errors) {
287
+ const proxy = new Proxy(errors, {
288
+ get(target, code) {
289
+ if (typeof code !== "string") {
290
+ return Reflect.get(target, code);
291
+ }
292
+ const item = (...[options]) => {
293
+ const config = errors[code];
294
+ return new ORPCError(code, {
295
+ defined: Boolean(config),
296
+ status: config?.status,
297
+ message: options?.message ?? config?.message,
298
+ data: options?.data,
299
+ cause: options?.cause
300
+ });
301
+ };
302
+ return item;
303
+ }
304
+ });
305
+ return proxy;
306
+ }
398
307
  async function validateORPCError(map, error) {
399
308
  const { code, status, message, data, cause, defined } = error;
400
309
  const config = map?.[error.code];
401
310
  if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) {
402
- return defined ? new ORPCError({ defined: false, code, status, message, data, cause }) : error;
311
+ return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
403
312
  }
404
313
  if (!config.data) {
405
- return defined ? error : new ORPCError({ defined: true, code, status, message, data, cause });
314
+ return defined ? error : new ORPCError(code, { defined: true, status, message, data, cause });
406
315
  }
407
316
  const validated = await config.data["~standard"].validate(error.data);
408
317
  if (validated.issues) {
409
- return defined ? new ORPCError({ defined: false, code, status, message, data, cause }) : error;
318
+ return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
410
319
  }
411
- return new ORPCError({
412
- defined: true,
413
- code,
414
- status,
415
- message,
416
- data: validated.value,
417
- cause
418
- });
320
+ return new ORPCError(code, { defined: true, status, message, data: validated.value, cause });
419
321
  }
420
322
 
421
323
  // src/client-utils.ts
@@ -456,7 +358,7 @@ var ValidationError = class extends Error {
456
358
  }
457
359
  };
458
360
 
459
- // src/schema-utils.ts
361
+ // src/schema.ts
460
362
  function type(...[map]) {
461
363
  return {
462
364
  "~standard": {
@@ -471,33 +373,30 @@ function type(...[map]) {
471
373
  }
472
374
  };
473
375
  }
474
-
475
- // src/index.ts
476
- var oc = new ContractBuilder({
477
- errorMap: {},
478
- InputSchema: void 0,
479
- OutputSchema: void 0,
480
- config: {}
481
- });
482
376
  export {
483
377
  COMMON_ORPC_ERROR_DEFS,
484
378
  ContractBuilder,
485
379
  ContractProcedure,
486
- ContractProcedureBuilder,
487
- ContractProcedureBuilderWithInput,
488
- ContractProcedureBuilderWithOutput,
489
- ContractRouterBuilder,
490
- DecoratedContractProcedure,
491
380
  ORPCError,
492
381
  ValidationError,
382
+ adaptContractRouter,
383
+ adaptRoute,
384
+ createORPCErrorConstructorMap,
493
385
  fallbackContractConfig,
494
386
  fallbackORPCErrorMessage,
495
387
  fallbackORPCErrorStatus,
496
388
  isContractProcedure,
497
389
  isDefinedError,
390
+ mergeErrorMap,
391
+ mergeMeta,
392
+ mergePrefix,
393
+ mergeRoute,
394
+ mergeTags,
498
395
  oc,
396
+ prefixRoute,
499
397
  safe,
500
398
  type,
399
+ unshiftTagRoute,
501
400
  validateORPCError
502
401
  };
503
402
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,38 @@
1
+ import type { ErrorMap, MergedErrorMap } from './error-map';
2
+ import type { Meta } from './meta';
3
+ import type { ContractProcedure } from './procedure';
4
+ import type { HTTPPath, Route } from './route';
5
+ import type { AdaptContractRouterOptions, AdaptedContractRouter, ContractRouter } from './router';
6
+ import type { Schema } from './schema';
7
+ export interface ContractProcedureBuilder<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
8
+ errors<U extends ErrorMap>(errors: U): ContractProcedureBuilder<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
9
+ meta(meta: TMeta): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
10
+ route(route: Route): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
11
+ input<U extends Schema>(schema: U): ContractProcedureBuilderWithInput<U, TOutputSchema, TErrorMap, TMeta>;
12
+ output<U extends Schema>(schema: U): ContractProcedureBuilderWithOutput<TInputSchema, U, TErrorMap, TMeta>;
13
+ }
14
+ export interface ContractProcedureBuilderWithInput<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
15
+ errors<U extends ErrorMap>(errors: U): ContractProcedureBuilderWithInput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
16
+ meta(meta: TMeta): ContractProcedureBuilderWithInput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
17
+ route(route: Route): ContractProcedureBuilderWithInput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
18
+ output<U extends Schema>(schema: U): ContractProcedureBuilderWithInputOutput<TInputSchema, U, TErrorMap, TMeta>;
19
+ }
20
+ export interface ContractProcedureBuilderWithOutput<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
21
+ errors<U extends ErrorMap>(errors: U): ContractProcedureBuilderWithOutput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
22
+ meta(meta: TMeta): ContractProcedureBuilderWithOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
23
+ route(route: Route): ContractProcedureBuilderWithOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
24
+ input<U extends Schema>(schema: U): ContractProcedureBuilderWithInputOutput<U, TOutputSchema, TErrorMap, TMeta>;
25
+ }
26
+ export interface ContractProcedureBuilderWithInputOutput<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
27
+ errors<U extends ErrorMap>(errors: U): ContractProcedureBuilderWithInputOutput<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
28
+ meta(meta: TMeta): ContractProcedureBuilderWithInputOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
29
+ route(route: Route): ContractProcedureBuilderWithInputOutput<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
30
+ }
31
+ export interface ContractRouterBuilder<TErrorMap extends ErrorMap, TMeta extends Meta> {
32
+ '~orpc': AdaptContractRouterOptions<TErrorMap>;
33
+ 'errors'<U extends ErrorMap>(errors: U): ContractRouterBuilder<MergedErrorMap<TErrorMap, U>, TMeta>;
34
+ 'prefix'(prefix: HTTPPath): ContractRouterBuilder<TErrorMap, TMeta>;
35
+ 'tag'(...tags: string[]): ContractRouterBuilder<TErrorMap, TMeta>;
36
+ 'router'<T extends ContractRouter<TMeta>>(router: T): AdaptedContractRouter<T, TErrorMap>;
37
+ }
38
+ //# sourceMappingURL=builder-variants.d.ts.map
@@ -1,29 +1,32 @@
1
- import type { ErrorMap, ErrorMapGuard, ErrorMapSuggestions, StrictErrorMap } from './error-map';
2
- import type { ContractProcedureDef, RouteOptions } from './procedure';
3
- import type { ContractRouter } from './router';
4
- import type { AdaptedContractRouter } from './router-builder';
5
- import type { HTTPPath, Schema, SchemaInput, SchemaOutput } from './types';
1
+ import type { ContractProcedureBuilder, ContractProcedureBuilderWithInput, ContractProcedureBuilderWithOutput, ContractRouterBuilder } from './builder-variants';
2
+ import type { ContractProcedureDef } from './procedure';
3
+ import type { AdaptContractRouterOptions, AdaptedContractRouter, ContractRouter } from './router';
4
+ import type { Schema } from './schema';
5
+ import { type ErrorMap, type MergedErrorMap } from './error-map';
6
+ import { type Meta } from './meta';
6
7
  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';
10
- import { ContractRouterBuilder } from './router-builder';
11
- export interface ContractBuilderConfig {
12
- initialRoute?: RouteOptions;
8
+ import { type HTTPPath, type Route } from './route';
9
+ export interface ContractBuilderDef<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>, AdaptContractRouterOptions<TErrorMap> {
13
10
  }
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>;
11
+ export declare class ContractBuilder<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
12
+ '~orpc': ContractBuilderDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
13
+ constructor(def: ContractBuilderDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>);
14
+ /**
15
+ * Reset initial meta
16
+ */
17
+ $meta<U extends Meta>(initialMeta: U): ContractBuilder<TInputSchema, TOutputSchema, TErrorMap, U>;
18
+ /**
19
+ * Reset initial route
20
+ */
21
+ $route(initialRoute: Route): ContractBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
22
+ errors<U extends ErrorMap>(errors: U): ContractBuilder<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>;
23
+ meta(meta: TMeta): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
24
+ route(route: Route): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
25
+ input<U extends Schema>(schema: U): ContractProcedureBuilderWithInput<U, TOutputSchema, TErrorMap, TMeta>;
26
+ output<U extends Schema>(schema: U): ContractProcedureBuilderWithOutput<TInputSchema, U, TErrorMap, TMeta>;
27
+ prefix(prefix: HTTPPath): ContractRouterBuilder<TErrorMap, TMeta>;
28
+ tag(...tags: string[]): ContractRouterBuilder<TErrorMap, TMeta>;
29
+ router<T extends ContractRouter<TMeta>>(router: T): AdaptedContractRouter<T, TErrorMap>;
28
30
  }
31
+ export declare const oc: ContractBuilder<undefined, undefined, {}, {}>;
29
32
  //# sourceMappingURL=builder.d.ts.map
@@ -1,5 +1,5 @@
1
1
  import type { ClientPromiseResult } from './client';
2
- import { type ORPCError } from './error-orpc';
2
+ import type { ORPCError } from './error-orpc';
3
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
4
  export declare function safe<TOutput, TError extends Error>(promise: ClientPromiseResult<TOutput, TError>): Promise<SafeResult<TOutput, TError>>;
5
5
  //# sourceMappingURL=client-utils.d.ts.map
@@ -8,7 +8,9 @@ export type ClientOptions<TClientContext> = {
8
8
  });
9
9
  export type ClientRest<TClientContext, TInput> = [input: TInput, options: ClientOptions<TClientContext>] | (undefined extends TInput & TClientContext ? [] : never) | (undefined extends TClientContext ? [input: TInput] : never);
10
10
  export type ClientPromiseResult<TOutput, TError extends Error> = Promise<TOutput> & {
11
- __typeError?: TError;
11
+ __error?: {
12
+ type: TError;
13
+ };
12
14
  };
13
15
  export interface Client<TClientContext, TInput, TOutput, TError extends Error> {
14
16
  (...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
@@ -1,4 +1,4 @@
1
- import type { HTTPMethod, InputStructure } from './types';
1
+ import type { HTTPMethod, InputStructure } from './route';
2
2
  export interface ContractConfig {
3
3
  defaultMethod: HTTPMethod;
4
4
  defaultSuccessStatus: number;
@@ -1,58 +1,14 @@
1
- import type { CommonORPCErrorCode } from './error-orpc';
2
- import type { Schema } from './types';
1
+ import type { ORPCErrorCode } from './error-orpc';
2
+ import type { Schema } from './schema';
3
3
  export type ErrorMapItem<TDataSchema extends Schema> = {
4
- /**
5
- *
6
- * @default 500
7
- */
8
4
  status?: number;
9
5
  message?: string;
10
6
  description?: string;
11
7
  data?: TDataSchema;
12
8
  };
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 = {
34
- [key in CommonORPCErrorCode | (string & {})]?: ErrorMapItem<Schema>;
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>>;
9
+ export type ErrorMap = {
10
+ [key in ORPCErrorCode]?: ErrorMapItem<Schema>;
57
11
  };
12
+ export type MergedErrorMap<T1 extends ErrorMap, T2 extends ErrorMap> = Omit<T1, keyof T2> & T2;
13
+ export declare function mergeErrorMap<T1 extends ErrorMap, T2 extends ErrorMap>(errorMap1: T1, errorMap2: T2): MergedErrorMap<T1, T2>;
58
14
  //# sourceMappingURL=error-map.d.ts.map
@@ -1,5 +1,5 @@
1
1
  import type { ErrorMap, ErrorMapItem } from './error-map';
2
- import type { SchemaOutput } from './types';
2
+ import type { SchemaOutput } from './schema';
3
3
  export type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = {
4
4
  [K in keyof TErrorMap]: K extends string ? TErrorMap[K] extends ErrorMapItem<infer TDataSchema> ? ORPCError<K, SchemaOutput<TDataSchema>> : never : never;
5
5
  }[keyof TErrorMap];
@@ -82,9 +82,11 @@ export declare const COMMON_ORPC_ERROR_DEFS: {
82
82
  };
83
83
  };
84
84
  export type CommonORPCErrorCode = keyof typeof COMMON_ORPC_ERROR_DEFS;
85
- export type ORPCErrorOptions<TCode extends string, TData> = ErrorOptions & {
85
+ export type ORPCErrorCode = CommonORPCErrorCode | (string & {});
86
+ export declare function fallbackORPCErrorStatus(code: ORPCErrorCode, status: number | undefined): number;
87
+ export declare function fallbackORPCErrorMessage(code: ORPCErrorCode, message: string | undefined): string;
88
+ export type ORPCErrorOptions<TData> = ErrorOptions & {
86
89
  defined?: boolean;
87
- code: TCode;
88
90
  status?: number;
89
91
  message?: string;
90
92
  } & (undefined extends TData ? {
@@ -92,18 +94,16 @@ export type ORPCErrorOptions<TCode extends string, TData> = ErrorOptions & {
92
94
  } : {
93
95
  data: TData;
94
96
  });
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 {
97
+ export type ORPCErrorOptionsRest<TData> = [options: ORPCErrorOptions<TData>] | (undefined extends TData ? [] : never);
98
+ export declare class ORPCError<TCode extends ORPCErrorCode, TData> extends Error {
98
99
  readonly defined: boolean;
99
100
  readonly code: TCode;
100
101
  readonly status: number;
101
102
  readonly data: TData;
102
- constructor(options: ORPCErrorOptions<TCode, TData>);
103
+ constructor(code: TCode, ...[options]: ORPCErrorOptionsRest<TData>);
103
104
  toJSON(): ORPCErrorJSON<TCode, TData>;
104
- static isValidJSON(json: unknown): json is ORPCErrorJSON<string, unknown>;
105
+ static fromJSON<TCode extends ORPCErrorCode, TData>(json: ORPCErrorJSON<TCode, TData>): ORPCError<TCode, TData>;
106
+ static isValidJSON(json: unknown): json is ORPCErrorJSON<ORPCErrorCode, unknown>;
105
107
  }
106
108
  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
109
  //# sourceMappingURL=error-orpc.d.ts.map
@@ -0,0 +1,14 @@
1
+ import type { ErrorMap, ErrorMapItem } from './error-map';
2
+ import type { ORPCErrorCode, ORPCErrorOptions } from './error-orpc';
3
+ import type { SchemaInput } from './schema';
4
+ import { ORPCError } from './error-orpc';
5
+ export declare function isDefinedError<T>(error: T): error is Extract<T, ORPCError<any, any>>;
6
+ export type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
7
+ export type ORPCErrorConstructorMapItemRest<TData> = [options: ORPCErrorConstructorMapItemOptions<TData>] | (undefined extends TData ? [] : never);
8
+ export type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: ORPCErrorConstructorMapItemRest<TInData>) => ORPCError<TCode, TInData>;
9
+ export type ORPCErrorConstructorMap<T extends ErrorMap> = {
10
+ [K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, SchemaInput<UInputSchema>> : never : never;
11
+ };
12
+ export declare function createORPCErrorConstructorMap<T extends ErrorMap>(errors: T): ORPCErrorConstructorMap<T>;
13
+ export declare function validateORPCError(map: ErrorMap, error: ORPCError<any, any>): Promise<ORPCError<string, unknown>>;
14
+ //# sourceMappingURL=error-utils.d.ts.map
@@ -1,22 +1,19 @@
1
1
  /** unnoq */
2
- import { ContractBuilder } from './builder';
3
2
  export * from './builder';
3
+ export * from './builder-variants';
4
4
  export * from './client';
5
5
  export * from './client-utils';
6
6
  export * from './config';
7
7
  export * from './error';
8
8
  export * from './error-map';
9
9
  export * from './error-orpc';
10
+ export * from './error-utils';
11
+ export * from './meta';
10
12
  export * from './procedure';
11
- export * from './procedure-builder';
12
- export * from './procedure-builder-with-input';
13
- export * from './procedure-builder-with-output';
14
13
  export * from './procedure-client';
15
- export * from './procedure-decorated';
14
+ export * from './route';
16
15
  export * from './router';
17
- export * from './router-builder';
18
16
  export * from './router-client';
19
- export * from './schema-utils';
17
+ export * from './schema';
20
18
  export * from './types';
21
- export declare const oc: ContractBuilder<Record<never, never>>;
22
19
  //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,3 @@
1
+ export type Meta = Record<string, any>;
2
+ export declare function mergeMeta<T extends Meta>(meta1: T, meta2: T): T;
3
+ //# sourceMappingURL=meta.d.ts.map
@@ -1,6 +1,6 @@
1
1
  import type { Client } from './client';
2
2
  import type { ErrorFromErrorMap } from './error';
3
3
  import type { ErrorMap } from './error-map';
4
- import type { Schema, SchemaInput, SchemaOutput } from './types';
4
+ import type { Schema, SchemaInput, SchemaOutput } from './schema';
5
5
  export type ContractProcedureClient<TClientContext, TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> = Client<TClientContext, SchemaInput<TInputSchema>, SchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
6
6
  //# sourceMappingURL=procedure-client.d.ts.map
@@ -1,83 +1,18 @@
1
1
  import type { ErrorMap } from './error-map';
2
- import type { HTTPMethod, HTTPPath, InputStructure, OutputStructure, Schema, SchemaOutput } from './types';
3
- export interface RouteOptions {
4
- method?: HTTPMethod;
5
- path?: HTTPPath;
6
- summary?: string;
7
- description?: string;
8
- deprecated?: boolean;
9
- tags?: readonly string[];
10
- /**
11
- * The status code of the response when the procedure is successful.
12
- *
13
- * @default 200
14
- */
15
- successStatus?: number;
16
- /**
17
- * The description of the response when the procedure is successful.
18
- *
19
- * @default 'OK'
20
- */
21
- successDescription?: string;
22
- /**
23
- * Determines how the input should be structured based on `params`, `query`, `headers`, and `body`.
24
- *
25
- * @option 'compact'
26
- * Combines `params` and either `query` or `body` (depending on the HTTP method) into a single object.
27
- *
28
- * @option 'detailed'
29
- * Keeps each part of the request (`params`, `query`, `headers`, and `body`) as separate fields in the input object.
30
- *
31
- * Example:
32
- * ```ts
33
- * const input = {
34
- * params: { id: 1 },
35
- * query: { search: 'hello' },
36
- * headers: { 'Content-Type': 'application/json' },
37
- * body: { name: 'John' },
38
- * }
39
- * ```
40
- *
41
- * @default 'compact'
42
- */
43
- inputStructure?: InputStructure;
44
- /**
45
- * Determines how the response should be structured based on the output.
46
- *
47
- * @option 'compact'
48
- * Includes only the body data, encoded directly in the response.
49
- *
50
- * @option 'detailed'
51
- * Separates the output into `headers` and `body` fields.
52
- * - `headers`: Custom headers to merge with the response headers.
53
- * - `body`: The response data.
54
- *
55
- * Example:
56
- * ```ts
57
- * const output = {
58
- * headers: { 'x-custom-header': 'value' },
59
- * body: { message: 'Hello, world!' },
60
- * };
61
- * ```
62
- *
63
- * @default 'compact'
64
- */
65
- outputStructure?: OutputStructure;
66
- }
67
- export interface ContractProcedureDef<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> {
68
- route?: RouteOptions;
69
- InputSchema: TInputSchema;
70
- inputExample?: SchemaOutput<TInputSchema>;
71
- OutputSchema: TOutputSchema;
72
- outputExample?: SchemaOutput<TOutputSchema>;
2
+ import type { Meta } from './meta';
3
+ import type { Route } from './route';
4
+ import type { Schema } from './schema';
5
+ export interface ContractProcedureDef<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> {
6
+ meta: TMeta;
7
+ route: Route;
8
+ inputSchema: TInputSchema;
9
+ outputSchema: TOutputSchema;
73
10
  errorMap: TErrorMap;
74
11
  }
75
- export declare class ContractProcedure<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> {
76
- '~type': "ContractProcedure";
77
- '~orpc': ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap>;
78
- constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap>);
12
+ export declare class ContractProcedure<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> {
13
+ '~orpc': ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
14
+ constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>);
79
15
  }
80
- export type ANY_CONTRACT_PROCEDURE = ContractProcedure<any, any, any>;
81
- export type WELL_CONTRACT_PROCEDURE = ContractProcedure<Schema, Schema, ErrorMap>;
82
- export declare function isContractProcedure(item: unknown): item is ANY_CONTRACT_PROCEDURE;
16
+ export type AnyContractProcedure = ContractProcedure<any, any, any, any>;
17
+ export declare function isContractProcedure(item: unknown): item is AnyContractProcedure;
83
18
  //# sourceMappingURL=procedure.d.ts.map
@@ -0,0 +1,79 @@
1
+ export type HTTPPath = `/${string}`;
2
+ export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
3
+ export type InputStructure = 'compact' | 'detailed';
4
+ export type OutputStructure = 'compact' | 'detailed';
5
+ export interface Route {
6
+ method?: HTTPMethod;
7
+ path?: HTTPPath;
8
+ summary?: string;
9
+ description?: string;
10
+ deprecated?: boolean;
11
+ tags?: readonly string[];
12
+ /**
13
+ * The status code of the response when the procedure is successful.
14
+ *
15
+ * @default 200
16
+ */
17
+ successStatus?: number;
18
+ /**
19
+ * The description of the response when the procedure is successful.
20
+ *
21
+ * @default 'OK'
22
+ */
23
+ successDescription?: string;
24
+ /**
25
+ * Determines how the input should be structured based on `params`, `query`, `headers`, and `body`.
26
+ *
27
+ * @option 'compact'
28
+ * Combines `params` and either `query` or `body` (depending on the HTTP method) into a single object.
29
+ *
30
+ * @option 'detailed'
31
+ * Keeps each part of the request (`params`, `query`, `headers`, and `body`) as separate fields in the input object.
32
+ *
33
+ * Example:
34
+ * ```ts
35
+ * const input = {
36
+ * params: { id: 1 },
37
+ * query: { search: 'hello' },
38
+ * headers: { 'Content-Type': 'application/json' },
39
+ * body: { name: 'John' },
40
+ * }
41
+ * ```
42
+ *
43
+ * @default 'compact'
44
+ */
45
+ inputStructure?: InputStructure;
46
+ /**
47
+ * Determines how the response should be structured based on the output.
48
+ *
49
+ * @option 'compact'
50
+ * Includes only the body data, encoded directly in the response.
51
+ *
52
+ * @option 'detailed'
53
+ * Separates the output into `headers` and `body` fields.
54
+ * - `headers`: Custom headers to merge with the response headers.
55
+ * - `body`: The response data.
56
+ *
57
+ * Example:
58
+ * ```ts
59
+ * const output = {
60
+ * headers: { 'x-custom-header': 'value' },
61
+ * body: { message: 'Hello, world!' },
62
+ * };
63
+ * ```
64
+ *
65
+ * @default 'compact'
66
+ */
67
+ outputStructure?: OutputStructure;
68
+ }
69
+ export declare function mergeRoute(a: Route, b: Route): Route;
70
+ export declare function prefixRoute(route: Route, prefix: HTTPPath): Route;
71
+ export declare function unshiftTagRoute(route: Route, tags: readonly string[]): Route;
72
+ export declare function mergePrefix(a: HTTPPath | undefined, b: HTTPPath): HTTPPath;
73
+ export declare function mergeTags(a: readonly string[] | undefined, b: readonly string[]): readonly string[];
74
+ export interface AdaptRouteOptions {
75
+ prefix?: HTTPPath;
76
+ tags?: readonly string[];
77
+ }
78
+ export declare function adaptRoute(route: Route, options: AdaptRouteOptions): Route;
79
+ //# sourceMappingURL=route.d.ts.map
@@ -1,7 +1,7 @@
1
1
  import type { ContractProcedure } from './procedure';
2
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;
3
+ import type { AnyContractRouter } from './router';
4
+ export type ContractRouterClient<TRouter extends AnyContractRouter, TClientContext> = TRouter extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, any> ? ContractProcedureClient<TClientContext, UInputSchema, UOutputSchema, UErrorMap> : {
5
+ [K in keyof TRouter]: TRouter[K] extends AnyContractRouter ? ContractRouterClient<TRouter[K], TClientContext> : never;
6
6
  };
7
7
  //# sourceMappingURL=router-client.d.ts.map
@@ -1,13 +1,29 @@
1
- import type { ErrorMap } from './error-map';
2
- import type { ContractProcedure } from './procedure';
3
- import type { SchemaInput, SchemaOutput } from './types';
4
- export type ContractRouter<T extends ErrorMap> = ContractProcedure<any, any, T> | {
5
- [k: string]: ContractRouter<T>;
1
+ import type { Meta } from './meta';
2
+ import type { SchemaInput, SchemaOutput } from './schema';
3
+ import { type ErrorMap, type MergedErrorMap } from './error-map';
4
+ import { ContractProcedure } from './procedure';
5
+ import { type HTTPPath } from './route';
6
+ export type ContractRouter<TMeta extends Meta> = ContractProcedure<any, any, any, TMeta> | {
7
+ [k: string]: ContractRouter<TMeta>;
6
8
  };
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;
9
+ export type AnyContractRouter = ContractRouter<any>;
10
+ export type AdaptedContractRouter<TContract extends AnyContractRouter, TErrorMap extends ErrorMap> = {
11
+ [K in keyof TContract]: TContract[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrors, infer UMeta> ? ContractProcedure<UInputSchema, UOutputSchema, MergedErrorMap<TErrorMap, UErrors>, UMeta> : TContract[K] extends AnyContractRouter ? AdaptedContractRouter<TContract[K], TErrorMap> : never;
9
12
  };
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;
13
+ export interface AdaptContractRouterOptions<TErrorMap extends ErrorMap> {
14
+ errorMap: TErrorMap;
15
+ prefix?: HTTPPath;
16
+ tags?: readonly string[];
17
+ }
18
+ export declare function adaptContractRouter<TRouter extends ContractRouter<any>, TErrorMap extends ErrorMap>(contract: TRouter, options: AdaptContractRouterOptions<TErrorMap>): AdaptedContractRouter<TRouter, TErrorMap>;
19
+ export type InferContractRouterInputs<T extends AnyContractRouter> = T extends ContractProcedure<infer UInputSchema, any, any, any> ? SchemaInput<UInputSchema> : {
20
+ [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterInputs<T[K]> : never;
12
21
  };
22
+ export type InferContractRouterOutputs<T extends AnyContractRouter> = T extends ContractProcedure<any, infer UOutputSchema, any, any> ? SchemaOutput<UOutputSchema> : {
23
+ [K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterOutputs<T[K]> : never;
24
+ };
25
+ export type ContractRouterToErrorMap<T extends AnyContractRouter> = T extends ContractProcedure<any, any, infer UErrorMap, any> ? UErrorMap : {
26
+ [K in keyof T]: T[K] extends AnyContractRouter ? ContractRouterToErrorMap<T[K]> : never;
27
+ }[keyof T];
28
+ export type ContractRouterToMeta<T extends AnyContractRouter> = T extends ContractRouter<infer UMeta> ? UMeta : never;
13
29
  //# sourceMappingURL=router.d.ts.map
@@ -0,0 +1,8 @@
1
+ import type { IsEqual, Promisable } from '@orpc/shared';
2
+ import type { StandardSchemaV1 } from '@standard-schema/spec';
3
+ export type Schema = StandardSchemaV1 | undefined;
4
+ export type SchemaInput<TSchema extends Schema, TFallback = unknown> = TSchema extends undefined ? TFallback : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferInput<TSchema> : TFallback;
5
+ export type SchemaOutput<TSchema extends Schema, TFallback = unknown> = TSchema extends undefined ? TFallback : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TSchema> : TFallback;
6
+ export type TypeRest<TInput, TOutput> = [map: (input: TInput) => Promisable<TOutput>] | (IsEqual<TInput, TOutput> extends true ? [] : never);
7
+ export declare function type<TInput, TOutput = TInput>(...[map]: TypeRest<TInput, TOutput>): StandardSchemaV1<TInput, TOutput>;
8
+ //# sourceMappingURL=schema.d.ts.map
@@ -1,11 +1,3 @@
1
1
  import type { FindGlobalInstanceType } from '@orpc/shared';
2
- import type { StandardSchemaV1 } from '@standard-schema/spec';
3
- export type HTTPPath = `/${string}`;
4
- export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
5
- export type InputStructure = 'compact' | 'detailed';
6
- export type OutputStructure = 'compact' | 'detailed';
7
- export type Schema = StandardSchemaV1 | undefined;
8
- export type SchemaInput<TSchema extends Schema, TFallback = unknown> = TSchema extends undefined ? TFallback : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferInput<TSchema> : TFallback;
9
- export type SchemaOutput<TSchema extends Schema, TFallback = unknown> = TSchema extends undefined ? TFallback : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TSchema> : TFallback;
10
2
  export type AbortSignal = FindGlobalInstanceType<'AbortSignal'>;
11
3
  //# 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.32.0",
4
+ "version": "0.34.0",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -29,8 +29,8 @@
29
29
  "dist"
30
30
  ],
31
31
  "dependencies": {
32
- "@standard-schema/spec": "1.0.0-beta.4",
33
- "@orpc/shared": "0.32.0"
32
+ "@standard-schema/spec": "1.0.0-rc.0",
33
+ "@orpc/shared": "0.34.0"
34
34
  },
35
35
  "devDependencies": {
36
36
  "arktype": "2.0.0-rc.26",
@@ -1,19 +0,0 @@
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
@@ -1,19 +0,0 @@
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
@@ -1,15 +0,0 @@
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
@@ -1,12 +0,0 @@
1
- import type { ErrorMap, ErrorMapGuard, ErrorMapSuggestions } from './error-map';
2
- import type { RouteOptions } from './procedure';
3
- import type { HTTPPath, Schema } from './types';
4
- import { ContractProcedure } from './procedure';
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
- errors<const U extends ErrorMap & ErrorMapGuard<TErrorMap> & ErrorMapSuggestions>(errors: U): DecoratedContractProcedure<TInputSchema, TOutputSchema, TErrorMap & U>;
8
- route(route: RouteOptions): DecoratedContractProcedure<TInputSchema, TOutputSchema, TErrorMap>;
9
- prefix(prefix: HTTPPath): DecoratedContractProcedure<TInputSchema, TOutputSchema, TErrorMap>;
10
- unshiftTag(...tags: string[]): DecoratedContractProcedure<TInputSchema, TOutputSchema, TErrorMap>;
11
- }
12
- //# sourceMappingURL=procedure-decorated.d.ts.map
@@ -1,23 +0,0 @@
1
- import type { ErrorMap, ErrorMapGuard, ErrorMapSuggestions, StrictErrorMap } from './error-map';
2
- import type { ContractProcedure } from './procedure';
3
- import type { ContractRouter } from './router';
4
- import type { HTTPPath } from './types';
5
- import { DecoratedContractProcedure } from './procedure-decorated';
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;
8
- };
9
- export interface ContractRouterBuilderDef<TErrorMap extends ErrorMap> {
10
- prefix?: HTTPPath;
11
- tags?: string[];
12
- errorMap: TErrorMap;
13
- }
14
- export declare class ContractRouterBuilder<TErrorMap extends ErrorMap> {
15
- '~type': "ContractProcedure";
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>;
22
- }
23
- //# sourceMappingURL=router-builder.d.ts.map
@@ -1,5 +0,0 @@
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