@trpc/server 11.0.0-alpha-tmp-02-12-response-types-idea.839 → 11.0.0-alpha-tmp-issues-6554.823

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 (34) hide show
  1. package/dist/adapters/aws-lambda/getPlanner.d.ts.map +1 -1
  2. package/dist/adapters/aws-lambda/getPlanner.js +3 -1
  3. package/dist/adapters/aws-lambda/getPlanner.mjs +3 -1
  4. package/dist/adapters/fetch/types.d.ts +2 -8
  5. package/dist/adapters/fetch/types.d.ts.map +1 -1
  6. package/dist/bundle-analysis.json +86 -72
  7. package/dist/unstable-core-do-not-import/clientish/inference.d.ts +1 -1
  8. package/dist/unstable-core-do-not-import/clientish/inference.d.ts.map +1 -1
  9. package/dist/unstable-core-do-not-import/http/resolveResponse.d.ts.map +1 -1
  10. package/dist/unstable-core-do-not-import/http/resolveResponse.js +1 -20
  11. package/dist/unstable-core-do-not-import/http/resolveResponse.mjs +1 -20
  12. package/dist/unstable-core-do-not-import/initTRPC.d.ts.map +1 -1
  13. package/dist/unstable-core-do-not-import/initTRPC.js +0 -1
  14. package/dist/unstable-core-do-not-import/initTRPC.mjs +0 -1
  15. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +2 -4
  16. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +1 -1
  17. package/dist/unstable-core-do-not-import/procedureBuilder.js +3 -9
  18. package/dist/unstable-core-do-not-import/procedureBuilder.mjs +3 -9
  19. package/dist/unstable-core-do-not-import/rootConfig.d.ts +1 -3
  20. package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -1
  21. package/dist/unstable-core-do-not-import/transformer.d.ts +1 -1
  22. package/dist/vendor/cookie-es/set-cookie/split.d.ts +18 -0
  23. package/dist/vendor/cookie-es/set-cookie/split.d.ts.map +1 -0
  24. package/dist/vendor/cookie-es/set-cookie/split.js +79 -0
  25. package/dist/vendor/cookie-es/set-cookie/split.mjs +77 -0
  26. package/package.json +182 -47
  27. package/src/adapters/aws-lambda/getPlanner.ts +4 -4
  28. package/src/adapters/fetch/types.ts +2 -8
  29. package/src/unstable-core-do-not-import/clientish/inference.ts +3 -8
  30. package/src/unstable-core-do-not-import/http/resolveResponse.ts +4 -31
  31. package/src/unstable-core-do-not-import/initTRPC.ts +0 -1
  32. package/src/unstable-core-do-not-import/procedureBuilder.ts +2 -15
  33. package/src/unstable-core-do-not-import/rootConfig.ts +1 -3
  34. package/src/vendor/cookie-es/set-cookie/split.ts +91 -0
@@ -4,6 +4,7 @@ import type {
4
4
  APIGatewayProxyResult,
5
5
  APIGatewayProxyStructuredResultV2,
6
6
  } from 'aws-lambda';
7
+ import { splitSetCookieString } from '../../vendor/cookie-es/set-cookie/split';
7
8
 
8
9
  export type LambdaEvent = APIGatewayProxyEvent | APIGatewayProxyEventV2;
9
10
 
@@ -43,10 +44,9 @@ interface Processor<TEvent extends LambdaEvent> {
43
44
  function getHeadersAndCookiesFromResponse(response: Response) {
44
45
  const headers = Object.fromEntries(response.headers.entries());
45
46
 
46
- const cookies: string[] = response.headers
47
- .getSetCookie()
48
- .flatMap((value) => value.split(','))
49
- .map((cookie) => cookie.trim());
47
+ const cookies: string[] = splitSetCookieString(
48
+ response.headers.getSetCookie(),
49
+ ).map((cookie) => cookie.trim());
50
50
 
51
51
  delete headers['set-cookie'];
52
52
 
@@ -19,17 +19,11 @@ import type {
19
19
  TRPCRequestInfo,
20
20
  } from '../../@trpc/server/http';
21
21
 
22
- export interface FetchCreateContextFnOptions {
22
+ export type FetchCreateContextFnOptions = {
23
23
  req: Request;
24
- /**
25
- * Used for setting headers on the `Response`
26
- */
27
24
  resHeaders: Headers;
28
- /**
29
- * Info about the request
30
- */
31
25
  info: TRPCRequestInfo;
32
- }
26
+ };
33
27
 
34
28
  export type FetchCreateContextFn<TRouter extends AnyRouter> = (
35
29
  opts: FetchCreateContextFnOptions,
@@ -19,14 +19,9 @@ import type { Serialize } from './serialize';
19
19
  export type inferTransformedProcedureOutput<
20
20
  TInferrable extends InferrableClientTypes,
21
21
  TProcedure extends AnyProcedure,
22
- > =
23
- inferProcedureOutput<TProcedure> extends infer $Output
24
- ? $Output extends Response
25
- ? $Output
26
- : inferClientTypes<TInferrable>['transformer'] extends false
27
- ? Serialize<$Output>
28
- : $Output
29
- : inferProcedureOutput<TProcedure>;
22
+ > = inferClientTypes<TInferrable>['transformer'] extends false
23
+ ? Serialize<inferProcedureOutput<TProcedure>>
24
+ : inferProcedureOutput<TProcedure>;
30
25
  /** @internal */
31
26
 
32
27
  export type inferTransformedSubscriptionOutput<
@@ -351,14 +351,6 @@ export async function resolveResponse<TRouter extends AnyRouter>(
351
351
  type: proc._def.type,
352
352
  signal: opts.req.signal,
353
353
  });
354
-
355
- if (info.isBatchCall && data instanceof Response) {
356
- throw new TRPCError({
357
- code: 'BAD_REQUEST',
358
- message: 'This procedure cannot be batched - use httpLink instead',
359
- });
360
- }
361
-
362
354
  return [undefined, { data }];
363
355
  } catch (cause) {
364
356
  const error = getTRPCErrorFromUnknown(cause);
@@ -379,27 +371,9 @@ export async function resolveResponse<TRouter extends AnyRouter>(
379
371
 
380
372
  // ----------- response handlers -----------
381
373
  if (!info.isBatchCall) {
382
- const call = info.calls[0]!;
374
+ const [call] = info.calls;
383
375
  const [error, result] = await rpcCalls[0]!;
384
376
 
385
- if (!error && result?.data instanceof Response) {
386
- const response = result.data;
387
- if (!config.experimental?.outputResponse) {
388
- throw new Error(
389
- 'You need to activate outputResponse in the root config to use this feature - e.g. `initTRPC.create({ experimental: { outputResponse: true } })`',
390
- );
391
- }
392
-
393
- const headers = new Headers(response.headers);
394
- headers.set('trpc-response-output', '1');
395
-
396
- return new Response(response.body, {
397
- status: response.status,
398
- statusText: response.statusText,
399
- headers,
400
- });
401
- }
402
-
403
377
  switch (info.type) {
404
378
  case 'unknown':
405
379
  case 'mutation':
@@ -420,8 +394,8 @@ export async function resolveResponse<TRouter extends AnyRouter>(
420
394
  config,
421
395
  ctx: ctxManager.valueOrUndefined(),
422
396
  error,
423
- input: call.result(),
424
- path: call.path,
397
+ input: call!.result(),
398
+ path: call!.path,
425
399
  type: info.type,
426
400
  }),
427
401
  }
@@ -435,7 +409,6 @@ export async function resolveResponse<TRouter extends AnyRouter>(
435
409
  headers,
436
410
  untransformedJSON: [res],
437
411
  });
438
-
439
412
  return new Response(
440
413
  JSON.stringify(transformTRPCResponse(config, res)),
441
414
  {
@@ -464,7 +437,7 @@ export async function resolveResponse<TRouter extends AnyRouter>(
464
437
  return errorToAsyncIterable(
465
438
  new TRPCError({
466
439
  message: `Subscription ${
467
- call.path
440
+ call!.path
468
441
  } did not return an observable or a AsyncGenerator`,
469
442
  code: 'INTERNAL_SERVER_ERROR',
470
443
  }),
@@ -113,7 +113,6 @@ class TRPCBuilder<TContext extends object, TMeta extends object> {
113
113
  * @see https://trpc.io/docs/v11/server/procedures
114
114
  */
115
115
  procedure: createBuilder<$Root['ctx'], $Root['meta']>({
116
- _config: config,
117
116
  meta: opts?.defaultMeta,
118
117
  }),
119
118
  /**
@@ -23,7 +23,6 @@ import type {
23
23
  QueryProcedure,
24
24
  SubscriptionProcedure,
25
25
  } from './procedure';
26
- import type { RootConfig } from './rootConfig';
27
26
  import type { inferTrackedOutput } from './stream/tracked';
28
27
  import type {
29
28
  GetRawInputFn,
@@ -68,7 +67,6 @@ export type CallerOverride<TContext> = (opts: {
68
67
  _def: AnyProcedure['_def'];
69
68
  }) => Promise<unknown>;
70
69
  type ProcedureBuilderDef<TMeta> = {
71
- _config: Partial<Pick<RootConfig<any>, 'experimental'>>;
72
70
  procedure: true;
73
71
  inputs: Parser[];
74
72
  output?: Parser;
@@ -424,7 +422,7 @@ function createNewBuilder(
424
422
  def1: AnyProcedureBuilderDef,
425
423
  def2: Partial<AnyProcedureBuilderDef>,
426
424
  ): AnyProcedureBuilder {
427
- const { middlewares = [], inputs, meta, _config, ...rest } = def2;
425
+ const { middlewares = [], inputs, meta, ...rest } = def2;
428
426
 
429
427
  // TODO: maybe have a fn here to warn about calls
430
428
  return createBuilder({
@@ -432,22 +430,11 @@ function createNewBuilder(
432
430
  inputs: [...def1.inputs, ...(inputs ?? [])],
433
431
  middlewares: [...def1.middlewares, ...middlewares],
434
432
  meta: def1.meta && meta ? { ...def1.meta, ...meta } : (meta ?? def1.meta),
435
- _config: !_config
436
- ? def1._config
437
- : {
438
- experimental: {
439
- ...def1._config.experimental,
440
- ..._config.experimental,
441
- },
442
- },
443
433
  });
444
434
  }
445
435
 
446
- type SetRequired<TType, TKey extends keyof TType> = TType &
447
- Required<Pick<TType, TKey>>;
448
-
449
436
  export function createBuilder<TContext, TMeta>(
450
- initDef: SetRequired<Partial<ProcedureBuilderDef<TMeta>>, '_config'>,
437
+ initDef: Partial<AnyProcedureBuilderDef> = {},
451
438
  ): ProcedureBuilder<
452
439
  TContext,
453
440
  TMeta,
@@ -86,9 +86,7 @@ export interface RootConfig<TTypes extends RootTypes> {
86
86
  * @see https://trpc.io/docs/client/links/httpBatchStreamLink
87
87
  */
88
88
  jsonl?: Pick<JSONLProducerOptions, 'pingMs'>;
89
- experimental?: {
90
- outputResponse?: boolean;
91
- };
89
+ experimental?: {};
92
90
  }
93
91
 
94
92
  /**
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Based on https://github.com/unjs/cookie-es/tree/v1.2.2
3
+ * MIT License
4
+ *
5
+ * Cookie-es copyright (c) Pooya Parsa <pooya@pi0.io>
6
+ * Set-Cookie parsing based on https://github.com/nfriedly/set-cookie-parser
7
+ * Copyright (c) 2015 Nathan Friedly <nathan@nfriedly.com> (http://nfriedly.com/)
8
+ *
9
+ * @see https://github.com/unjs/cookie-es/blob/main/src/set-cookie/split.ts
10
+ */
11
+
12
+ /**
13
+ * Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas
14
+ * that are within a single set-cookie field-value, such as in the Expires portion.
15
+ *
16
+ * See https://tools.ietf.org/html/rfc2616#section-4.2
17
+ */
18
+ export function splitSetCookieString(
19
+ cookiesString: string | string[],
20
+ ): string[] {
21
+ if (Array.isArray(cookiesString)) {
22
+ return cookiesString.flatMap((c) => splitSetCookieString(c));
23
+ }
24
+
25
+ if (typeof cookiesString !== "string") {
26
+ return [];
27
+ }
28
+
29
+ const cookiesStrings: string[] = [];
30
+ let pos: number = 0;
31
+ let start: number;
32
+ let ch: string;
33
+ let lastComma: number;
34
+ let nextStart: number;
35
+ let cookiesSeparatorFound: boolean;
36
+
37
+ const skipWhitespace = () => {
38
+ while (pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))) {
39
+ pos += 1;
40
+ }
41
+ return pos < cookiesString.length;
42
+ };
43
+
44
+ const notSpecialChar = () => {
45
+ ch = cookiesString.charAt(pos);
46
+ return ch !== "=" && ch !== ";" && ch !== ",";
47
+ };
48
+
49
+ while (pos < cookiesString.length) {
50
+ start = pos;
51
+ cookiesSeparatorFound = false;
52
+
53
+ while (skipWhitespace()) {
54
+ ch = cookiesString.charAt(pos);
55
+ if (ch === ",") {
56
+ // ',' is a cookie separator if we have later first '=', not ';' or ','
57
+ lastComma = pos;
58
+ pos += 1;
59
+
60
+ skipWhitespace();
61
+ nextStart = pos;
62
+
63
+ while (pos < cookiesString.length && notSpecialChar()) {
64
+ pos += 1;
65
+ }
66
+
67
+ // currently special character
68
+ if (pos < cookiesString.length && cookiesString.charAt(pos) === "=") {
69
+ // we found cookies separator
70
+ cookiesSeparatorFound = true;
71
+ // pos is inside the next cookie, so back up and return it.
72
+ pos = nextStart;
73
+ cookiesStrings.push(cookiesString.slice(start, lastComma));
74
+ start = pos;
75
+ } else {
76
+ // in param ',' or param separator ';',
77
+ // we continue from that comma
78
+ pos = lastComma + 1;
79
+ }
80
+ } else {
81
+ pos += 1;
82
+ }
83
+ }
84
+
85
+ if (!cookiesSeparatorFound || pos >= cookiesString.length) {
86
+ cookiesStrings.push(cookiesString.slice(start));
87
+ }
88
+ }
89
+
90
+ return cookiesStrings;
91
+ }