@orpc/contract 0.30.0 → 0.32.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
@@ -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,27 +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 _DecoratedContractProcedure({
119
+ return new DecoratedContractProcedure({
64
120
  ...this["~orpc"],
65
121
  InputSchema: schema,
66
122
  inputExample: example
67
123
  });
68
124
  }
69
- output(schema, example) {
70
- return new _DecoratedContractProcedure({
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
- OutputSchema: schema,
73
- outputExample: example
148
+ InputSchema: schema,
149
+ inputExample: example
74
150
  });
75
151
  }
76
- errors(errors) {
77
- return new _DecoratedContractProcedure({
152
+ output(schema, example) {
153
+ return new ContractProcedureBuilderWithOutput({
78
154
  ...this["~orpc"],
79
- errorMap: {
80
- ...this["~orpc"].errorMap,
81
- ...errors
82
- }
155
+ OutputSchema: schema,
156
+ outputExample: example
83
157
  });
84
158
  }
85
159
  };
@@ -133,11 +207,18 @@ var ContractRouterBuilder = class _ContractRouterBuilder {
133
207
  };
134
208
 
135
209
  // src/builder.ts
136
- var ContractBuilder = class _ContractBuilder {
137
- "~type" = "ContractBuilder";
138
- "~orpc";
210
+ var ContractBuilder = class _ContractBuilder extends ContractProcedure {
139
211
  constructor(def) {
140
- this["~orpc"] = def;
212
+ super(def);
213
+ }
214
+ config(config) {
215
+ return new _ContractBuilder({
216
+ ...this["~orpc"],
217
+ config: {
218
+ ...this["~orpc"].config,
219
+ ...config
220
+ }
221
+ });
141
222
  }
142
223
  errors(errors) {
143
224
  return new _ContractBuilder({
@@ -148,28 +229,20 @@ var ContractBuilder = class _ContractBuilder {
148
229
  }
149
230
  });
150
231
  }
151
- prefix(prefix) {
152
- return new ContractRouterBuilder({
153
- prefix,
154
- errorMap: this["~orpc"].errorMap
155
- });
156
- }
157
- tag(...tags) {
158
- return new ContractRouterBuilder({
159
- tags,
160
- errorMap: this["~orpc"].errorMap
161
- });
162
- }
163
232
  route(route) {
164
- return new DecoratedContractProcedure({
165
- route,
233
+ return new ContractProcedureBuilder({
234
+ route: {
235
+ ...this["~orpc"].config.initialRoute,
236
+ ...route
237
+ },
166
238
  InputSchema: void 0,
167
239
  OutputSchema: void 0,
168
240
  errorMap: this["~orpc"].errorMap
169
241
  });
170
242
  }
171
243
  input(schema, example) {
172
- return new DecoratedContractProcedure({
244
+ return new ContractProcedureBuilderWithInput({
245
+ route: this["~orpc"].config.initialRoute,
173
246
  InputSchema: schema,
174
247
  inputExample: example,
175
248
  OutputSchema: void 0,
@@ -177,13 +250,26 @@ var ContractBuilder = class _ContractBuilder {
177
250
  });
178
251
  }
179
252
  output(schema, example) {
180
- return new DecoratedContractProcedure({
253
+ return new ContractProcedureBuilderWithOutput({
254
+ route: this["~orpc"].config.initialRoute,
181
255
  OutputSchema: schema,
182
256
  outputExample: example,
183
257
  InputSchema: void 0,
184
258
  errorMap: this["~orpc"].errorMap
185
259
  });
186
260
  }
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
271
+ });
272
+ }
187
273
  router(router) {
188
274
  return new ContractRouterBuilder({
189
275
  errorMap: this["~orpc"].errorMap
@@ -354,20 +440,9 @@ var DEFAULT_CONFIG = {
354
440
  defaultInputStructure: "compact",
355
441
  defaultOutputStructure: "compact"
356
442
  };
357
- var GLOBAL_CONFIG_REF = { value: DEFAULT_CONFIG };
358
- function configGlobal(config) {
359
- if (config.defaultSuccessStatus !== void 0 && (config.defaultSuccessStatus < 200 || config.defaultSuccessStatus > 299)) {
360
- throw new Error("[configGlobal] The defaultSuccessStatus must be between 200 and 299");
361
- }
362
- GLOBAL_CONFIG_REF.value = config;
363
- }
364
- function fallbackToGlobalConfig(key, value) {
443
+ function fallbackContractConfig(key, value) {
365
444
  if (value === void 0) {
366
- const fallback = GLOBAL_CONFIG_REF.value[key];
367
- if (fallback === void 0) {
368
- return DEFAULT_CONFIG[key];
369
- }
370
- return fallback;
445
+ return DEFAULT_CONFIG[key];
371
446
  }
372
447
  return value;
373
448
  }
@@ -381,26 +456,48 @@ var ValidationError = class extends Error {
381
456
  }
382
457
  };
383
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
+
384
475
  // src/index.ts
385
476
  var oc = new ContractBuilder({
386
- errorMap: {}
477
+ errorMap: {},
478
+ InputSchema: void 0,
479
+ OutputSchema: void 0,
480
+ config: {}
387
481
  });
388
482
  export {
389
483
  COMMON_ORPC_ERROR_DEFS,
390
484
  ContractBuilder,
391
485
  ContractProcedure,
486
+ ContractProcedureBuilder,
487
+ ContractProcedureBuilderWithInput,
488
+ ContractProcedureBuilderWithOutput,
392
489
  ContractRouterBuilder,
393
490
  DecoratedContractProcedure,
394
491
  ORPCError,
395
492
  ValidationError,
396
- configGlobal,
493
+ fallbackContractConfig,
397
494
  fallbackORPCErrorMessage,
398
495
  fallbackORPCErrorStatus,
399
- fallbackToGlobalConfig,
400
496
  isContractProcedure,
401
497
  isDefinedError,
402
498
  oc,
403
499
  safe,
500
+ type,
404
501
  validateORPCError
405
502
  };
406
503
  //# sourceMappingURL=index.js.map
@@ -1,23 +1,29 @@
1
1
  import type { ErrorMap, ErrorMapGuard, ErrorMapSuggestions, StrictErrorMap } from './error-map';
2
- import type { RouteOptions } from './procedure';
2
+ import type { ContractProcedureDef, RouteOptions } from './procedure';
3
3
  import type { ContractRouter } from './router';
4
4
  import type { AdaptedContractRouter } from './router-builder';
5
5
  import type { HTTPPath, Schema, SchemaInput, SchemaOutput } from './types';
6
- import { DecoratedContractProcedure } from './procedure-decorated';
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';
7
10
  import { ContractRouterBuilder } from './router-builder';
8
- export type ContractBuilderDef<TErrorMap extends ErrorMap> = {
9
- errorMap: TErrorMap;
10
- };
11
- export declare class ContractBuilder<TErrorMap extends ErrorMap> {
12
- '~type': "ContractBuilder";
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> {
13
18
  '~orpc': ContractBuilderDef<TErrorMap>;
14
19
  constructor(def: ContractBuilderDef<TErrorMap>);
20
+ config(config: ContractBuilderConfig): ContractBuilder<TErrorMap>;
15
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>;
16
25
  prefix(prefix: HTTPPath): ContractRouterBuilder<TErrorMap>;
17
26
  tag(...tags: string[]): ContractRouterBuilder<TErrorMap>;
18
- route(route: RouteOptions): DecoratedContractProcedure<undefined, undefined, TErrorMap>;
19
- input<U extends Schema>(schema: U, example?: SchemaInput<U>): DecoratedContractProcedure<U, undefined, TErrorMap>;
20
- output<U extends Schema>(schema: U, example?: SchemaOutput<U>): DecoratedContractProcedure<undefined, U, TErrorMap>;
21
27
  router<T extends ContractRouter<ErrorMap & Partial<StrictErrorMap<TErrorMap>>>>(router: T): AdaptedContractRouter<T, TErrorMap>;
22
28
  }
23
29
  //# sourceMappingURL=builder.d.ts.map
@@ -1,36 +1,10 @@
1
1
  import type { HTTPMethod, InputStructure } from './types';
2
- export interface ORPCConfig {
3
- /**
4
- * @default 'POST'
5
- */
6
- defaultMethod?: HTTPMethod;
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
@@ -8,11 +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';
11
14
  export * from './procedure-client';
12
15
  export * from './procedure-decorated';
13
16
  export * from './router';
14
17
  export * from './router-builder';
15
18
  export * from './router-client';
19
+ export * from './schema-utils';
16
20
  export * from './types';
17
21
  export declare const oc: ContractBuilder<Record<never, never>>;
18
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
@@ -1,14 +1,12 @@
1
1
  import type { ErrorMap, ErrorMapGuard, ErrorMapSuggestions } from './error-map';
2
2
  import type { RouteOptions } from './procedure';
3
- import type { HTTPPath, Schema, SchemaInput, SchemaOutput } from './types';
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 & ErrorMapGuard<TErrorMap> & ErrorMapSuggestions>(errors: U): DecoratedContractProcedure<TInputSchema, TOutputSchema, TErrorMap & U>;
13
11
  }
14
12
  //# sourceMappingURL=procedure-decorated.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.30.0",
4
+ "version": "0.32.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.30.0"
33
+ "@orpc/shared": "0.32.0"
34
34
  },
35
35
  "devDependencies": {
36
36
  "arktype": "2.0.0-rc.26",