@trpc/server 11.0.0-next-beta.234 → 11.0.0-next-beta.236

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.
Files changed (39) hide show
  1. package/dist/adapters/node-http/content-type/form-data/index.d.ts +3 -5
  2. package/dist/adapters/node-http/content-type/form-data/index.d.ts.map +1 -1
  3. package/dist/bundle-analysis.json +67 -66
  4. package/dist/index.js +1 -0
  5. package/dist/index.mjs +1 -0
  6. package/dist/unstable-core-do-not-import/TRPCInferrable.d.ts +2 -2
  7. package/dist/unstable-core-do-not-import/TRPCInferrable.d.ts.map +1 -1
  8. package/dist/unstable-core-do-not-import/index.d.ts +4 -3
  9. package/dist/unstable-core-do-not-import/index.d.ts.map +1 -1
  10. package/dist/unstable-core-do-not-import/index.js +2 -0
  11. package/dist/unstable-core-do-not-import/index.mjs +1 -0
  12. package/dist/unstable-core-do-not-import/initTRPC.d.ts +34 -43
  13. package/dist/unstable-core-do-not-import/initTRPC.d.ts.map +1 -1
  14. package/dist/unstable-core-do-not-import/initTRPC.js +17 -26
  15. package/dist/unstable-core-do-not-import/initTRPC.mjs +17 -26
  16. package/dist/unstable-core-do-not-import/procedure.d.ts +3 -4
  17. package/dist/unstable-core-do-not-import/procedure.d.ts.map +1 -1
  18. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +2 -3
  19. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +1 -1
  20. package/dist/unstable-core-do-not-import/rootConfig.d.ts +10 -12
  21. package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -1
  22. package/dist/unstable-core-do-not-import/router.d.ts.map +1 -1
  23. package/dist/unstable-core-do-not-import/transformer.d.ts +1 -7
  24. package/dist/unstable-core-do-not-import/transformer.d.ts.map +1 -1
  25. package/dist/unstable-core-do-not-import/transformer.js +0 -1
  26. package/dist/unstable-core-do-not-import/transformer.mjs +0 -1
  27. package/dist/unstable-core-do-not-import/types.d.ts +6 -0
  28. package/dist/unstable-core-do-not-import/types.d.ts.map +1 -1
  29. package/package.json +2 -2
  30. package/src/adapters/node-http/content-type/form-data/index.ts +10 -10
  31. package/src/unstable-core-do-not-import/TRPCInferrable.ts +7 -3
  32. package/src/unstable-core-do-not-import/index.ts +3 -2
  33. package/src/unstable-core-do-not-import/initTRPC.ts +59 -101
  34. package/src/unstable-core-do-not-import/procedure.ts +3 -4
  35. package/src/unstable-core-do-not-import/procedureBuilder.ts +10 -6
  36. package/src/unstable-core-do-not-import/rootConfig.ts +10 -14
  37. package/src/unstable-core-do-not-import/router.ts +1 -2
  38. package/src/unstable-core-do-not-import/transformer.ts +1 -9
  39. package/src/unstable-core-do-not-import/types.ts +6 -0
@@ -7,142 +7,94 @@ import {
7
7
  } from './error/formatter';
8
8
  import { createMiddlewareFactory } from './middleware';
9
9
  import { createBuilder } from './procedureBuilder';
10
- import {
11
- isServerDefault,
12
- type CreateRootConfigTypes,
13
- type RootConfig,
14
- type RootConfigTypes,
15
- type RuntimeConfig,
16
- } from './rootConfig';
10
+ import { isServerDefault, type RootConfig } from './rootConfig';
17
11
  import {
18
12
  createCallerFactory,
19
13
  createRouterFactory,
20
14
  mergeRouters,
21
15
  } from './router';
22
- import {
23
- defaultTransformer,
24
- getDataTransformer,
25
- type DataTransformerOptions,
26
- type DefaultDataTransformer,
27
- } from './transformer';
28
- import type {
29
- Overwrite,
30
- PickFirstDefined,
31
- Unwrap,
32
- ValidateShape,
33
- } from './types';
34
-
35
- type PartialRootConfigTypes = Partial<RootConfigTypes>;
36
-
37
- type CreateRootConfigTypesFromPartial<TTypes extends PartialRootConfigTypes> =
38
- CreateRootConfigTypes<{
39
- ctx: TTypes['ctx'] extends RootConfigTypes['ctx'] ? TTypes['ctx'] : object;
40
- meta: TTypes['meta'] extends RootConfigTypes['meta']
41
- ? TTypes['meta']
42
- : object;
43
- errorShape: TTypes['errorShape'];
44
- transformer: DataTransformerOptions;
45
- }>;
46
-
47
- /**
48
- * TODO: This can be improved:
49
- * - We should be able to chain `.meta()`/`.context()` only once
50
- * - Simplify typings
51
- * - Doesn't need to be a class but it doesn't really hurt either
52
- */
16
+ import type { DataTransformerOptions } from './transformer';
17
+ import { defaultTransformer, getDataTransformer } from './transformer';
18
+ import type { PickFirstDefined, Unwrap, ValidateShape } from './types';
19
+
20
+ interface RuntimeConfigOptions<TContext extends object, TMeta extends object>
21
+ extends Partial<
22
+ Omit<
23
+ RootConfig<{
24
+ ctx: TContext;
25
+ meta: TMeta;
26
+ errorShape: any;
27
+ transformer: any;
28
+ }>,
29
+ '$types' | 'transformer'
30
+ >
31
+ > {
32
+ /**
33
+ * Use a data transformer
34
+ * @link https://trpc.io/docs/v11/data-transformers
35
+ */
36
+ transformer?: DataTransformerOptions;
37
+ }
53
38
 
54
- class TRPCBuilder<TParams extends PartialRootConfigTypes = object> {
39
+ class TRPCBuilder<TContext extends object, TMeta extends object> {
55
40
  /**
56
41
  * Add a context shape as a generic to the root object
57
42
  * @link https://trpc.io/docs/v11/server/context
58
43
  */
59
- context<
60
- TNewContext extends
61
- | RootConfigTypes['ctx']
62
- | ((...args: unknown[]) => RootConfigTypes['ctx']),
63
- >() {
64
- type NextParams = Overwrite<TParams, { ctx: Unwrap<TNewContext> }>;
65
-
66
- return new TRPCBuilder<NextParams>();
44
+ context<TNewContext extends object | ((...args: unknown[]) => object)>() {
45
+ return new TRPCBuilder<Unwrap<TNewContext>, TMeta>();
67
46
  }
68
47
 
69
48
  /**
70
49
  * Add a meta shape as a generic to the root object
71
50
  * @link https://trpc.io/docs/v11/quickstart
72
51
  */
73
- meta<TNewMeta extends RootConfigTypes['meta']>() {
74
- type NextParams = Overwrite<TParams, { meta: TNewMeta }>;
75
-
76
- return new TRPCBuilder<NextParams>();
52
+ meta<TNewMeta extends object>() {
53
+ return new TRPCBuilder<TContext, TNewMeta>();
77
54
  }
78
55
 
79
56
  /**
80
57
  * Create the root object
81
58
  * @link https://trpc.io/docs/v11/server/routers#initialize-trpc
82
59
  */
83
- create<
84
- TOptions extends Partial<
85
- RuntimeConfig<CreateRootConfigTypesFromPartial<TParams>>
86
- >,
87
- >(
88
- options?:
89
- | ValidateShape<
90
- TOptions,
91
- Partial<RuntimeConfig<CreateRootConfigTypesFromPartial<TParams>>>
92
- >
60
+ create<TOptions extends RuntimeConfigOptions<TContext, TMeta>>(
61
+ opts?:
62
+ | ValidateShape<TOptions, RuntimeConfigOptions<TContext, TMeta>>
93
63
  | undefined,
94
64
  ) {
95
- return createTRPCInner<TParams>()<TOptions>(options);
96
- }
97
- }
98
-
99
- /**
100
- * Builder to initialize the tRPC root object - use this exactly once per backend
101
- * @link https://trpc.io/docs/v11/quickstart
102
- */
103
- export const initTRPC = new TRPCBuilder();
104
-
105
- function createTRPCInner<TParams extends PartialRootConfigTypes>() {
106
- type $Generics = CreateRootConfigTypesFromPartial<TParams>;
107
-
108
- type $Context = $Generics['ctx'];
109
- type $Meta = $Generics['meta'];
110
- type $Runtime = Partial<RuntimeConfig<$Generics>>;
111
-
112
- return function initTRPCInner<TOptions extends $Runtime>(
113
- runtime?: ValidateShape<TOptions, $Runtime>,
114
- ) {
115
- type $Formatter = PickFirstDefined<
116
- TOptions['errorFormatter'],
117
- ErrorFormatter<$Context, DefaultErrorShape>
65
+ type $Transformer = undefined extends TOptions['transformer']
66
+ ? false
67
+ : true;
68
+ type $ErrorShape = ErrorFormatterShape<
69
+ PickFirstDefined<
70
+ TOptions['errorFormatter'],
71
+ ErrorFormatter<TContext, DefaultErrorShape>
72
+ >
118
73
  >;
119
- type $Transformer = TOptions['transformer'] extends DataTransformerOptions
120
- ? TOptions['transformer']
121
- : DefaultDataTransformer;
122
- type $ErrorShape = ErrorFormatterShape<$Formatter>;
123
74
 
124
75
  type $Config = RootConfig<{
125
- ctx: $Context;
126
- meta: $Meta;
76
+ ctx: TContext;
77
+ meta: TMeta;
127
78
  errorShape: $ErrorShape;
128
79
  transformer: $Transformer;
129
80
  }>;
130
81
 
131
- const errorFormatter = runtime?.errorFormatter ?? defaultFormatter;
82
+ const errorFormatter = opts?.errorFormatter ?? defaultFormatter;
132
83
  const transformer = getDataTransformer(
133
- runtime?.transformer ?? defaultTransformer,
134
- ) as $Transformer;
84
+ opts?.transformer ?? defaultTransformer,
85
+ );
135
86
 
136
87
  const config: $Config = {
137
88
  transformer,
138
89
  isDev:
139
- runtime?.isDev ??
90
+ opts?.isDev ??
140
91
  // eslint-disable-next-line @typescript-eslint/dot-notation
141
92
  globalThis.process?.env?.['NODE_ENV'] !== 'production',
142
- allowOutsideOfServer: runtime?.allowOutsideOfServer ?? false,
93
+ allowOutsideOfServer: opts?.allowOutsideOfServer ?? false,
143
94
  errorFormatter,
144
- isServer: runtime?.isServer ?? isServerDefault,
95
+ isServer: opts?.isServer ?? isServerDefault,
145
96
  /**
97
+ * These are just types, they can't be used at runtime
146
98
  * @internal
147
99
  */
148
100
  $types: createFlatProxy((key) => {
@@ -154,9 +106,9 @@ function createTRPCInner<TParams extends PartialRootConfigTypes>() {
154
106
 
155
107
  {
156
108
  // Server check
157
- const isServer: boolean = runtime?.isServer ?? isServerDefault;
109
+ const isServer: boolean = opts?.isServer ?? isServerDefault;
158
110
 
159
- if (!isServer && runtime?.allowOutsideOfServer !== true) {
111
+ if (!isServer && opts?.allowOutsideOfServer !== true) {
160
112
  throw new Error(
161
113
  `You're trying to use @trpc/server in a non-server environment. This is not supported by default.`,
162
114
  );
@@ -164,7 +116,7 @@ function createTRPCInner<TParams extends PartialRootConfigTypes>() {
164
116
  }
165
117
  return {
166
118
  /**
167
- * These are just types, they can't be used
119
+ * Your router config
168
120
  * @internal
169
121
  */
170
122
  _config: config,
@@ -176,7 +128,7 @@ function createTRPCInner<TParams extends PartialRootConfigTypes>() {
176
128
  $Config['$types']['ctx'],
177
129
  $Config['$types']['meta']
178
130
  >({
179
- meta: runtime?.defaultMeta,
131
+ meta: opts?.defaultMeta,
180
132
  }),
181
133
  /**
182
134
  * Create reusable middlewares
@@ -202,5 +154,11 @@ function createTRPCInner<TParams extends PartialRootConfigTypes>() {
202
154
  */
203
155
  createCallerFactory: createCallerFactory<$Config>(),
204
156
  };
205
- };
157
+ }
206
158
  }
159
+
160
+ /**
161
+ * Builder to initialize the tRPC root object - use this exactly once per backend
162
+ * @link https://trpc.io/docs/v11/quickstart
163
+ */
164
+ export const initTRPC = new TRPCBuilder();
@@ -1,8 +1,7 @@
1
1
  import type { inferObservableValue } from '../observable';
2
2
  import type { ProcedureCallOptions } from './procedureBuilder';
3
3
  import type { Serialize } from './serialize';
4
- import type { DefaultDataTransformer } from './transformer';
5
- import type { inferConfig, TRPCInferrable } from './TRPCInferrable';
4
+ import type { inferConfigTypes, TRPCInferrable } from './TRPCInferrable';
6
5
 
7
6
  export const procedureTypes = ['query', 'mutation', 'subscription'] as const;
8
7
  /**
@@ -72,7 +71,7 @@ export type AnyProcedure = Procedure<ProcedureType, any>;
72
71
  export type inferTransformedProcedureOutput<
73
72
  TInferrable extends TRPCInferrable,
74
73
  TProcedure extends AnyProcedure,
75
- > = inferConfig<TInferrable>['transformer'] extends DefaultDataTransformer
74
+ > = inferConfigTypes<TInferrable>['transformer'] extends false
76
75
  ? Serialize<TProcedure['_def']['_output_out']>
77
76
  : TProcedure['_def']['_output_out'];
78
77
 
@@ -80,7 +79,7 @@ export type inferTransformedProcedureOutput<
80
79
  export type inferTransformedSubscriptionOutput<
81
80
  TInferrable extends TRPCInferrable,
82
81
  TProcedure extends AnyProcedure,
83
- > = inferConfig<TInferrable>['transformer'] extends DefaultDataTransformer
82
+ > = inferConfigTypes<TInferrable>['transformer'] extends false
84
83
  ? Serialize<inferObservableValue<TProcedure['_def']['_output_out']>>
85
84
  : inferObservableValue<TProcedure['_def']['_output_out']>;
86
85
 
@@ -22,15 +22,19 @@ import type {
22
22
  QueryProcedure,
23
23
  SubscriptionProcedure,
24
24
  } from './procedure';
25
- import type { GetRawInputFn, MaybePromise, Overwrite, Simplify } from './types';
25
+ import type {
26
+ GetRawInputFn,
27
+ MaybePromise,
28
+ Overwrite,
29
+ Simplify,
30
+ TypeError,
31
+ } from './types';
26
32
  import { mergeWithoutOverrides } from './utils';
27
33
 
28
34
  type IntersectIfDefined<TType, TWith> = TType extends UnsetMarker
29
35
  ? TWith
30
36
  : Simplify<TType & TWith>;
31
37
 
32
- type ErrorMessage<TMessage extends string> = TMessage;
33
-
34
38
  /** @internal */
35
39
  export const unsetMarker = Symbol('unsetMarker');
36
40
  type UnsetMarker = typeof unsetMarker;
@@ -98,10 +102,10 @@ export interface ProcedureBuilder<
98
102
  ? undefined extends inferParser<$Parser>['out'] // if current is optional the previous must be too
99
103
  ? undefined extends TInputOut
100
104
  ? $Parser
101
- : ErrorMessage<'Cannot chain an optional parser to a required parser'>
105
+ : TypeError<'Cannot chain an optional parser to a required parser'>
102
106
  : $Parser
103
- : ErrorMessage<'All input parsers did not resolve to an object'>
104
- : ErrorMessage<'All input parsers did not resolve to an object'>,
107
+ : TypeError<'All input parsers did not resolve to an object'>
108
+ : TypeError<'All input parsers did not resolve to an object'>,
105
109
  ): ProcedureBuilder<
106
110
  TContext,
107
111
  TMeta,
@@ -1,3 +1,4 @@
1
+ import type { CombinedDataTransformer } from '.';
1
2
  import type { ErrorFormatter } from './error/formatter';
2
3
  import type { TRPCErrorShape } from './rpc';
3
4
 
@@ -9,7 +10,7 @@ export interface RootConfigTypes {
9
10
  ctx: object;
10
11
  meta: object;
11
12
  errorShape: unknown;
12
- transformer: unknown;
13
+ transformer: boolean;
13
14
  }
14
15
 
15
16
  /**
@@ -24,15 +25,20 @@ export const isServerDefault: boolean =
24
25
  !!globalThis.process?.env?.['VITEST_WORKER_ID'];
25
26
 
26
27
  /**
27
- * The runtime config that are used and actually represents real values underneath
28
+ * The tRPC root config
28
29
  * @internal
29
30
  */
30
- export interface RuntimeConfig<TTypes extends RootConfigTypes> {
31
+ export interface RootConfig<TTypes extends RootConfigTypes> {
32
+ /**
33
+ * The types that are used in the config
34
+ * @internal
35
+ */
36
+ $types: TTypes;
31
37
  /**
32
38
  * Use a data transformer
33
39
  * @link https://trpc.io/docs/v11/data-transformers
34
40
  */
35
- transformer: TTypes['transformer'];
41
+ transformer: CombinedDataTransformer;
36
42
  /**
37
43
  * Use custom error formatting
38
44
  * @link https://trpc.io/docs/v11/error-formatting
@@ -69,16 +75,6 @@ export interface RuntimeConfig<TTypes extends RootConfigTypes> {
69
75
  export type CreateRootConfigTypes<TGenerics extends RootConfigTypes> =
70
76
  TGenerics;
71
77
 
72
- /**
73
- * The config that is resolved after `initTRPC.create()` has been called
74
- * Combination of `InitTOptions` + `InitGenerics`
75
- * @internal
76
- */
77
- export interface RootConfig<TGenerics extends RootConfigTypes>
78
- extends RuntimeConfig<TGenerics> {
79
- $types: TGenerics;
80
- }
81
-
82
78
  /**
83
79
  * @internal
84
80
  */
@@ -8,7 +8,6 @@ import type {
8
8
  } from './procedure';
9
9
  import type { ProcedureCallOptions } from './procedureBuilder';
10
10
  import type { AnyRootConfig } from './rootConfig';
11
- import type { CombinedDataTransformer } from './transformer';
12
11
  import { defaultTransformer } from './transformer';
13
12
  import type { MaybePromise } from './types';
14
13
  import { mergeWithoutOverrides, omitPrototype } from './utils';
@@ -353,7 +352,7 @@ export function mergeRouters<TRouters extends AnyRouter[]>(
353
352
  return current._def._config.transformer;
354
353
  }
355
354
  return prev;
356
- }, defaultTransformer as CombinedDataTransformer);
355
+ }, defaultTransformer);
357
356
 
358
357
  const router = createRouterFactory({
359
358
  errorFormatter,
@@ -79,15 +79,7 @@ export function getDataTransformer(
79
79
  /**
80
80
  * @internal
81
81
  */
82
- export type DefaultDataTransformer = CombinedDataTransformer & {
83
- _default: true;
84
- };
85
-
86
- /**
87
- * @internal
88
- */
89
- export const defaultTransformer: DefaultDataTransformer = {
90
- _default: true,
82
+ export const defaultTransformer: CombinedDataTransformer = {
91
83
  input: { serialize: (obj) => obj, deserialize: (obj) => obj },
92
84
  output: { serialize: (obj) => obj, deserialize: (obj) => obj },
93
85
  };
@@ -149,3 +149,9 @@ export type ProtectedIntersection<TType, TWith> = keyof TType &
149
149
  * Returns the raw input type of a procedure
150
150
  */
151
151
  export type GetRawInputFn = () => Promise<unknown>;
152
+
153
+ const ERROR_SYMBOL = Symbol('TypeError');
154
+ export type TypeErrorSymbol = typeof ERROR_SYMBOL;
155
+ export type TypeError<TMessage extends string> = TMessage & {
156
+ _: TypeErrorSymbol;
157
+ };