@trpc/client 10.22.0 → 10.23.1

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 (43) hide show
  1. package/dist/{httpUtils-0e44d50d.js → httpUtils-1a07b2e6.js} +18 -8
  2. package/dist/{httpUtils-f08fd191.mjs → httpUtils-8a5c637a.mjs} +18 -8
  3. package/dist/{httpUtils-0c7ec827.js → httpUtils-90a6bccb.js} +18 -7
  4. package/dist/index.js +31 -2
  5. package/dist/index.mjs +34 -5
  6. package/dist/internals/types.d.ts +1 -1
  7. package/dist/internals/types.d.ts.map +1 -1
  8. package/dist/isObject-246cec0f.js +6 -0
  9. package/dist/isObject-3ec9fe00.mjs +6 -0
  10. package/dist/isObject-617ad59e.js +8 -0
  11. package/dist/links/httpBatchLink.js +4 -3
  12. package/dist/links/httpBatchLink.mjs +4 -3
  13. package/dist/links/httpFormDataLink.d.ts +2 -0
  14. package/dist/links/httpFormDataLink.d.ts.map +1 -0
  15. package/dist/links/httpLink.d.ts +5 -2
  16. package/dist/links/httpLink.d.ts.map +1 -1
  17. package/dist/links/httpLink.js +46 -39
  18. package/dist/links/httpLink.mjs +46 -40
  19. package/dist/links/index.d.ts +2 -1
  20. package/dist/links/index.d.ts.map +1 -1
  21. package/dist/links/internals/httpUtils.d.ts +16 -10
  22. package/dist/links/internals/httpUtils.d.ts.map +1 -1
  23. package/dist/links/internals/isObject.d.ts +2 -0
  24. package/dist/links/internals/isObject.d.ts.map +1 -0
  25. package/dist/links/internals/transformResult.d.ts.map +1 -1
  26. package/dist/links/loggerLink.d.ts.map +1 -1
  27. package/dist/links/loggerLink.js +11 -1
  28. package/dist/links/loggerLink.mjs +11 -1
  29. package/dist/links/wsLink.js +2 -1
  30. package/dist/links/wsLink.mjs +2 -1
  31. package/dist/{transformResult-e7a1e69e.js → transformResult-12020457.js} +4 -6
  32. package/dist/{transformResult-6fb67924.mjs → transformResult-403dc1d4.mjs} +2 -4
  33. package/dist/{transformResult-4e205a12.js → transformResult-51c1bc81.js} +2 -4
  34. package/package.json +5 -5
  35. package/src/internals/types.ts +1 -1
  36. package/src/links/httpBatchLink.ts +2 -2
  37. package/src/links/httpFormDataLink.ts +30 -0
  38. package/src/links/httpLink.ts +54 -50
  39. package/src/links/index.ts +3 -1
  40. package/src/links/internals/httpUtils.ts +40 -14
  41. package/src/links/internals/isObject.ts +4 -0
  42. package/src/links/internals/transformResult.ts +1 -5
  43. package/src/links/loggerLink.ts +17 -1
@@ -1,6 +1,6 @@
1
1
  import { ProcedureType } from '@trpc/server';
2
2
  import { TRPCResponse } from '@trpc/server/rpc';
3
- import { AbortControllerEsque, FetchEsque, ResponseEsque } from '../../internals/types';
3
+ import { AbortControllerEsque, FetchEsque, RequestInitEsque, ResponseEsque } from '../../internals/types';
4
4
  import { HTTPHeaders, PromiseAndCancel, TRPCClientRuntime } from '../types';
5
5
  /**
6
6
  * @internal
@@ -35,18 +35,24 @@ declare type GetInputOptions = {
35
35
  } | {
36
36
  input: unknown;
37
37
  });
38
- declare type GetUrlOptions = ResolvedHTTPLinkOptions & GetInputOptions & {
38
+ export declare type HTTPBaseRequestOptions = ResolvedHTTPLinkOptions & GetInputOptions & {
39
39
  type: ProcedureType;
40
40
  path: string;
41
41
  };
42
- export declare function getUrl(opts: GetUrlOptions): string;
43
- declare type GetBodyOptions = {
44
- type: ProcedureType;
45
- } & GetInputOptions;
46
- export declare function getBody(opts: GetBodyOptions): string | undefined;
47
- export declare type HTTPRequestOptions = ResolvedHTTPLinkOptions & GetInputOptions & {
48
- type: ProcedureType;
49
- path: string;
42
+ export declare type GetUrl = (opts: HTTPBaseRequestOptions) => string;
43
+ export declare type GetBody = (opts: HTTPBaseRequestOptions) => RequestInitEsque['body'];
44
+ export declare type ContentOptions = {
45
+ contentTypeHeader?: string;
46
+ getUrl: GetUrl;
47
+ getBody: GetBody;
48
+ };
49
+ export declare const getUrl: GetUrl;
50
+ export declare const getBody: GetBody;
51
+ export declare type Requester = (opts: HTTPBaseRequestOptions & {
52
+ headers: () => HTTPHeaders | Promise<HTTPHeaders>;
53
+ }) => PromiseAndCancel<HTTPResult>;
54
+ export declare const jsonHttpRequester: Requester;
55
+ export declare type HTTPRequestOptions = HTTPBaseRequestOptions & ContentOptions & {
50
56
  headers: () => HTTPHeaders | Promise<HTTPHeaders>;
51
57
  };
52
58
  export declare function httpRequest(opts: HTTPRequestOptions): PromiseAndCancel<HTTPResult>;
@@ -1 +1 @@
1
- {"version":3,"file":"httpUtils.d.ts","sourceRoot":"","sources":["../../../src/links/internals/httpUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAGhD,OAAO,EACL,oBAAoB,EACpB,UAAU,EACV,aAAa,EACd,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAE5E;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,GAAG,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,KAAK,CAAC,EAAE,UAAU,CAAC;IACnB;;OAEG;IACH,eAAe,CAAC,EAAE,oBAAoB,GAAG,IAAI,CAAC;CAC/C;AAED,MAAM,WAAW,uBAAuB;IACtC,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,UAAU,CAAC;IAClB,eAAe,EAAE,oBAAoB,GAAG,IAAI,CAAC;CAC9C;AAED,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,mBAAmB,GACxB,uBAAuB,CAMzB;AAiBD,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,YAAY,CAAC;IACnB,IAAI,EAAE;QACJ,QAAQ,EAAE,aAAa,CAAC;KACzB,CAAC;CACH;AAED,aAAK,eAAe,GAAG;IACrB,OAAO,EAAE,iBAAiB,CAAC;CAC5B,GAAG,CAAC;IAAE,MAAM,EAA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G,sBAAsB,GACrD,cAAc,GAAG;IACf,OAAO,EAAE,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;CACnD,CAAC;AAEJ,wBAAgB,WAAW,CACzB,IAAI,EAAE,kBAAkB,GACvB,gBAAgB,CAAC,UAAU,CAAC,CA4C9B"}
@@ -0,0 +1,2 @@
1
+ export declare function isObject(value: unknown): value is Record<string, unknown>;
2
+ //# sourceMappingURL=isObject.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isObject.d.ts","sourceRoot":"","sources":["../../../src/links/internals/isObject.ts"],"names":[],"mappings":"AAAA,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAGzE"}
@@ -1 +1 @@
1
- {"version":3,"file":"transformResult.d.ts","sourceRoot":"","sources":["../../../src/links/internals/transformResult.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAC3D,OAAO,EACL,YAAY,EACZ,mBAAmB,EAEpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,iBAAiB,EAAE,MAAM,IAAI,CAAC;AAMvC,gBAAgB;AAChB,iBAAS,oBAAoB,CAAC,OAAO,SAAS,SAAS,EAAE,OAAO,EAC9D,QAAQ,EACJ,mBAAmB,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,GACvD,YAAY,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,EACpD,OAAO,EAAE,iBAAiB;;;;;;;;;;;;;;;;;;;;;;EAuB3B;AAOD;;;GAGG;AACH,wBAAgB,eAAe,CAAC,OAAO,SAAS,SAAS,EAAE,OAAO,EAChE,QAAQ,EACJ,mBAAmB,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,GACvD,YAAY,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,EACpD,OAAO,EAAE,iBAAiB,GACzB,UAAU,CAAC,OAAO,oBAAoB,CAAC,CAqBzC"}
1
+ {"version":3,"file":"transformResult.d.ts","sourceRoot":"","sources":["../../../src/links/internals/transformResult.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAC3D,OAAO,EACL,YAAY,EACZ,mBAAmB,EAEpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,iBAAiB,EAAE,MAAM,IAAI,CAAC;AAOvC,gBAAgB;AAChB,iBAAS,oBAAoB,CAAC,OAAO,SAAS,SAAS,EAAE,OAAO,EAC9D,QAAQ,EACJ,mBAAmB,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,GACvD,YAAY,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,EACpD,OAAO,EAAE,iBAAiB;;;;;;;;;;;;;;;;;;;;;;EAuB3B;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,OAAO,SAAS,SAAS,EAAE,OAAO,EAChE,QAAQ,EACJ,mBAAmB,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,GACvD,YAAY,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,EACpD,OAAO,EAAE,iBAAiB,GACzB,UAAU,CAAC,OAAO,oBAAoB,CAAC,CAqBzC"}
@@ -1 +1 @@
1
- {"version":3,"file":"loggerLink.d.ts","sourceRoot":"","sources":["../../src/links/loggerLink.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAE,eAAe,EAAE,MAAM,IAAI,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,uBAAuB,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEvE,aAAK,YAAY,GAAG;IAClB,GAAG,EAAE,CAAC,GA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AAS,SAAS;IAC1D,MAAM,CAAC,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC;IAC/B,OAAO,CAAC,EAAE,SAAS,CAAC,OAAO,CAAC,CAAC;IAC7B;;OAEG;IACH,OAAO,CAAC,EAAE,YAAY,CAAC;CACxB;AAgED,wBAAgB,UAAU,CAAC,OAAO,SAAS,SAAS,GAAG,SAAS,EAC9D,IAAI,GAAE,iBAAiB,CAAC,OAAO,CAAM,GACpC,QAAQ,CAAC,OAAO,CAAC,CA2CnB"}
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var observable = require('@trpc/server/observable');
6
+ var isObject = require('../isObject-617ad59e.js');
6
7
 
7
8
  const palette = {
8
9
  query: [
@@ -18,10 +19,19 @@ const palette = {
18
19
  'd83fbe'
19
20
  ]
20
21
  };
22
+ function isFormData(value) {
23
+ if (typeof FormData === 'undefined') {
24
+ // FormData is not supported
25
+ return false;
26
+ }
27
+ return value instanceof FormData;
28
+ }
21
29
  // maybe this should be moved to it's own package
22
30
  const defaultLogger = (c = console)=>(props)=>{
23
- const { direction , input , type , path , context , id } = props;
31
+ const { direction , type , path , context , id } = props;
24
32
  const [light, dark] = palette[type];
33
+ const rawInput = props.input;
34
+ const input = isObject.isObject(rawInput) && isFormData(rawInput) ? Object.fromEntries(rawInput) : props.input;
25
35
  const css = `
26
36
  background-color: #${direction === 'up' ? light : dark};
27
37
  color: ${direction === 'up' ? 'black' : 'white'};
@@ -1,4 +1,5 @@
1
1
  import { observable, tap } from '@trpc/server/observable';
2
+ import { i as isObject } from '../isObject-3ec9fe00.mjs';
2
3
 
3
4
  const palette = {
4
5
  query: [
@@ -14,10 +15,19 @@ const palette = {
14
15
  'd83fbe'
15
16
  ]
16
17
  };
18
+ function isFormData(value) {
19
+ if (typeof FormData === 'undefined') {
20
+ // FormData is not supported
21
+ return false;
22
+ }
23
+ return value instanceof FormData;
24
+ }
17
25
  // maybe this should be moved to it's own package
18
26
  const defaultLogger = (c = console)=>(props)=>{
19
- const { direction , input , type , path , context , id } = props;
27
+ const { direction , type , path , context , id } = props;
20
28
  const [light, dark] = palette[type];
29
+ const rawInput = props.input;
30
+ const input = isObject(rawInput) && isFormData(rawInput) ? Object.fromEntries(rawInput) : props.input;
21
31
  const css = `
22
32
  background-color: #${direction === 'up' ? light : dark};
23
33
  color: ${direction === 'up' ? 'black' : 'white'};
@@ -3,7 +3,8 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var observable = require('@trpc/server/observable');
6
- var transformResult = require('../transformResult-e7a1e69e.js');
6
+ var transformResult = require('../transformResult-12020457.js');
7
+ require('../isObject-617ad59e.js');
7
8
 
8
9
  /* istanbul ignore next -- @preserve */ const retryDelay = (attemptIndex)=>attemptIndex === 0 ? 0 : Math.min(1000 * 2 ** attemptIndex, 30000);
9
10
 
@@ -1,5 +1,6 @@
1
1
  import { observable } from '@trpc/server/observable';
2
- import { T as TRPCClientError, t as transformResult } from '../transformResult-6fb67924.mjs';
2
+ import { T as TRPCClientError, t as transformResult } from '../transformResult-403dc1d4.mjs';
3
+ import '../isObject-3ec9fe00.mjs';
3
4
 
4
5
  /* istanbul ignore next -- @preserve */ const retryDelay = (attemptIndex)=>attemptIndex === 0 ? 0 : Math.min(1000 * 2 ** attemptIndex, 30000);
5
6
 
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ var isObject = require('./isObject-617ad59e.js');
4
+
3
5
  class TRPCClientError extends Error {
4
6
  static from(cause, opts = {}) {
5
7
  if (!(cause instanceof Error)) {
@@ -60,10 +62,6 @@ class TRPCClientError extends Error {
60
62
  result
61
63
  };
62
64
  }
63
- function isObject(value) {
64
- // check that value is object
65
- return !!value && !Array.isArray(value) && typeof value === 'object';
66
- }
67
65
  /**
68
66
  * Transforms and validates that the result is a valid TRPCResponse
69
67
  * @internal
@@ -76,10 +74,10 @@ function isObject(value) {
76
74
  throw new TRPCClientError('Unable to transform response from server');
77
75
  }
78
76
  // check that output of the transformers is a valid TRPCResponse
79
- if (!result.ok && (!isObject(result.error.error) || typeof result.error.error.code !== 'number')) {
77
+ if (!result.ok && (!isObject.isObject(result.error.error) || typeof result.error.error.code !== 'number')) {
80
78
  throw new TRPCClientError('Badly formatted response from server');
81
79
  }
82
- if (result.ok && !isObject(result.result)) {
80
+ if (result.ok && !isObject.isObject(result.result)) {
83
81
  throw new TRPCClientError('Badly formatted response from server');
84
82
  }
85
83
  return result;
@@ -1,3 +1,5 @@
1
+ import { i as isObject } from './isObject-3ec9fe00.mjs';
2
+
1
3
  class TRPCClientError extends Error {
2
4
  static from(cause, opts = {}) {
3
5
  if (!(cause instanceof Error)) {
@@ -58,10 +60,6 @@ class TRPCClientError extends Error {
58
60
  result
59
61
  };
60
62
  }
61
- function isObject(value) {
62
- // check that value is object
63
- return !!value && !Array.isArray(value) && typeof value === 'object';
64
- }
65
63
  /**
66
64
  * Transforms and validates that the result is a valid TRPCResponse
67
65
  * @internal
@@ -1,3 +1,5 @@
1
+ import { i as isObject } from './isObject-246cec0f.js';
2
+
1
3
  class TRPCClientError extends Error {
2
4
  constructor(message, opts) {
3
5
  const cause = opts?.cause;
@@ -54,10 +56,6 @@ function transformResultInner(response, runtime) {
54
56
  };
55
57
  return { ok: true, result };
56
58
  }
57
- function isObject(value) {
58
- // check that value is object
59
- return !!value && !Array.isArray(value) && typeof value === 'object';
60
- }
61
59
  /**
62
60
  * Transforms and validates that the result is a valid TRPCResponse
63
61
  * @internal
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@trpc/client",
3
- "version": "10.22.0",
4
- "description": "tRPC Client lib",
3
+ "version": "10.23.1",
4
+ "description": "The tRPC client library",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
7
7
  "main": "dist/index.js",
@@ -69,11 +69,11 @@
69
69
  "links"
70
70
  ],
71
71
  "peerDependencies": {
72
- "@trpc/server": "10.22.0"
72
+ "@trpc/server": "10.23.1"
73
73
  },
74
74
  "devDependencies": {
75
75
  "@testing-library/dom": "^9.0.0",
76
- "@trpc/server": "10.22.0",
76
+ "@trpc/server": "10.23.1",
77
77
  "@types/isomorphic-fetch": "^0.0.36",
78
78
  "@types/node": "^18.7.20",
79
79
  "eslint": "^8.30.0",
@@ -88,5 +88,5 @@
88
88
  "publishConfig": {
89
89
  "access": "public"
90
90
  },
91
- "gitHead": "65ca7a2be17de07f35f29e69fdcfddd3eeacfbaf"
91
+ "gitHead": "4edcb4ebd2dd04d530b962086c75b8d4dc93464e"
92
92
  }
@@ -81,7 +81,7 @@ export interface RequestInitEsque {
81
81
  /**
82
82
  * Sets the request's body.
83
83
  */
84
- body?: string | ReadableStream | null;
84
+ body?: string | ReadableStream | FormData | null;
85
85
 
86
86
  /**
87
87
  * Sets the request's associated headers.
@@ -7,7 +7,7 @@ import {
7
7
  HTTPLinkBaseOptions,
8
8
  HTTPResult,
9
9
  getUrl,
10
- httpRequest,
10
+ jsonHttpRequester,
11
11
  resolveHTTPLinkOptions,
12
12
  } from './internals/httpUtils';
13
13
  import { transformResult } from './internals/transformResult';
@@ -58,7 +58,7 @@ export function httpBatchLink<TRouter extends AnyRouter>(
58
58
  const path = batchOps.map((op) => op.path).join(',');
59
59
  const inputs = batchOps.map((op) => op.input);
60
60
 
61
- const { promise, cancel } = httpRequest({
61
+ const { promise, cancel } = jsonHttpRequester({
62
62
  ...resolvedOpts,
63
63
  runtime,
64
64
  type,
@@ -0,0 +1,30 @@
1
+ import { httpLinkFactory } from './httpLink';
2
+ import { GetBody, Requester, httpRequest } from './internals/httpUtils';
3
+
4
+ const getBody: GetBody = (opts) => {
5
+ if (!('input' in opts)) {
6
+ return undefined;
7
+ }
8
+ if (!(opts.input instanceof FormData)) {
9
+ throw new Error('Input is not FormData');
10
+ }
11
+ return opts.input;
12
+ };
13
+
14
+ const formDataRequester: Requester = (opts) => {
15
+ if (opts.type !== 'mutation') {
16
+ // TODO(?) handle formdata queries
17
+ throw new Error('We only handle mutations with formdata');
18
+ }
19
+ return httpRequest({
20
+ ...opts,
21
+ getUrl() {
22
+ return `${opts.url}/${opts.path}`;
23
+ },
24
+ getBody,
25
+ });
26
+ };
27
+
28
+ export const experimental_formDataLink = httpLinkFactory({
29
+ requester: formDataRequester,
30
+ });
@@ -3,7 +3,8 @@ import { observable } from '@trpc/server/observable';
3
3
  import { TRPCClientError } from '../TRPCClientError';
4
4
  import {
5
5
  HTTPLinkBaseOptions,
6
- httpRequest,
6
+ Requester,
7
+ jsonHttpRequester,
7
8
  resolveHTTPLinkOptions,
8
9
  } from './internals/httpUtils';
9
10
  import { transformResult } from './internals/transformResult';
@@ -19,56 +20,59 @@ export interface HTTPLinkOptions extends HTTPLinkBaseOptions {
19
20
  | ((opts: { op: Operation }) => HTTPHeaders | Promise<HTTPHeaders>);
20
21
  }
21
22
 
22
- export function httpLink<TRouter extends AnyRouter>(
23
- opts: HTTPLinkOptions,
24
- ): TRPCLink<TRouter> {
25
- const resolvedOpts = resolveHTTPLinkOptions(opts);
26
- return (runtime) =>
27
- ({ op }) =>
28
- observable((observer) => {
29
- const { path, input, type } = op;
23
+ export function httpLinkFactory(factoryOpts: { requester: Requester }) {
24
+ return <TRouter extends AnyRouter>(
25
+ opts: HTTPLinkOptions,
26
+ ): TRPCLink<TRouter> => {
27
+ const resolvedOpts = resolveHTTPLinkOptions(opts);
30
28
 
31
- const { promise, cancel } = httpRequest({
32
- ...resolvedOpts,
33
- runtime,
34
- type,
35
- path,
36
- input,
37
- headers() {
38
- if (!opts.headers) {
39
- return {};
40
- }
41
- if (typeof opts.headers === 'function') {
42
- return opts.headers({
43
- op,
44
- });
45
- }
46
- return opts.headers;
47
- },
48
- });
49
-
50
- promise
51
- .then((res) => {
52
- const transformed = transformResult(res.json, runtime);
29
+ return (runtime) =>
30
+ ({ op }) =>
31
+ observable((observer) => {
32
+ const { path, input, type } = op;
33
+ const { promise, cancel } = factoryOpts.requester({
34
+ ...resolvedOpts,
35
+ runtime,
36
+ type,
37
+ path,
38
+ input,
39
+ headers() {
40
+ if (!opts.headers) {
41
+ return {};
42
+ }
43
+ if (typeof opts.headers === 'function') {
44
+ return opts.headers({
45
+ op,
46
+ });
47
+ }
48
+ return opts.headers;
49
+ },
50
+ });
51
+ promise
52
+ .then((res) => {
53
+ const transformed = transformResult(res.json, runtime);
53
54
 
54
- if (!transformed.ok) {
55
- observer.error(
56
- TRPCClientError.from(transformed.error, {
57
- meta: res.meta,
58
- }),
59
- );
60
- return;
61
- }
62
- observer.next({
63
- context: res.meta,
64
- result: transformed.result,
65
- });
66
- observer.complete();
67
- })
68
- .catch((cause) => observer.error(TRPCClientError.from(cause)));
55
+ if (!transformed.ok) {
56
+ observer.error(
57
+ TRPCClientError.from(transformed.error, {
58
+ meta: res.meta,
59
+ }),
60
+ );
61
+ return;
62
+ }
63
+ observer.next({
64
+ context: res.meta,
65
+ result: transformed.result,
66
+ });
67
+ observer.complete();
68
+ })
69
+ .catch((cause) => observer.error(TRPCClientError.from(cause)));
69
70
 
70
- return () => {
71
- cancel();
72
- };
73
- });
71
+ return () => {
72
+ cancel();
73
+ };
74
+ });
75
+ };
74
76
  }
77
+
78
+ export const httpLink = httpLinkFactory({ requester: jsonHttpRequester });
@@ -1,9 +1,11 @@
1
+ export * from './types';
2
+
1
3
  export * from './httpBatchLink';
2
4
  export * from './httpLink';
3
5
  export * from './loggerLink';
4
6
  export * from './splitLink';
5
7
  export * from './wsLink';
6
- export * from './types';
8
+ export * from './httpFormDataLink';
7
9
 
8
10
  // These are not public (yet) as we get this functionality from tanstack query
9
11
  // export * from './retryLink';
@@ -5,6 +5,7 @@ import { getAbortController } from '../../internals/getAbortController';
5
5
  import {
6
6
  AbortControllerEsque,
7
7
  FetchEsque,
8
+ RequestInitEsque,
8
9
  ResponseEsque,
9
10
  } from '../../internals/types';
10
11
  import { HTTPHeaders, PromiseAndCancel, TRPCClientRuntime } from '../types';
@@ -74,13 +75,24 @@ function getInput(opts: GetInputOptions) {
74
75
  );
75
76
  }
76
77
 
77
- type GetUrlOptions = ResolvedHTTPLinkOptions &
78
+ export type HTTPBaseRequestOptions = ResolvedHTTPLinkOptions &
78
79
  GetInputOptions & {
79
80
  type: ProcedureType;
80
81
  path: string;
81
82
  };
82
83
 
83
- export function getUrl(opts: GetUrlOptions) {
84
+ export type GetUrl = (opts: HTTPBaseRequestOptions) => string;
85
+ export type GetBody = (
86
+ opts: HTTPBaseRequestOptions,
87
+ ) => RequestInitEsque['body'];
88
+
89
+ export type ContentOptions = {
90
+ contentTypeHeader?: string;
91
+ getUrl: GetUrl;
92
+ getBody: GetBody;
93
+ };
94
+
95
+ export const getUrl: GetUrl = (opts) => {
84
96
  let url = opts.url + '/' + opts.path;
85
97
  const queryParts: string[] = [];
86
98
  if ('inputs' in opts) {
@@ -96,22 +108,33 @@ export function getUrl(opts: GetUrlOptions) {
96
108
  url += '?' + queryParts.join('&');
97
109
  }
98
110
  return url;
99
- }
111
+ };
100
112
 
101
- type GetBodyOptions = { type: ProcedureType } & GetInputOptions;
102
-
103
- export function getBody(opts: GetBodyOptions) {
113
+ export const getBody: GetBody = (opts) => {
104
114
  if (opts.type === 'query') {
105
115
  return undefined;
106
116
  }
107
117
  const input = getInput(opts);
108
118
  return input !== undefined ? JSON.stringify(input) : undefined;
109
- }
119
+ };
110
120
 
111
- export type HTTPRequestOptions = ResolvedHTTPLinkOptions &
112
- GetInputOptions & {
113
- type: ProcedureType;
114
- path: string;
121
+ export type Requester = (
122
+ opts: HTTPBaseRequestOptions & {
123
+ headers: () => HTTPHeaders | Promise<HTTPHeaders>;
124
+ },
125
+ ) => PromiseAndCancel<HTTPResult>;
126
+
127
+ export const jsonHttpRequester: Requester = (opts) => {
128
+ return httpRequest({
129
+ ...opts,
130
+ contentTypeHeader: 'application/json',
131
+ getUrl,
132
+ getBody,
133
+ });
134
+ };
135
+
136
+ export type HTTPRequestOptions = HTTPBaseRequestOptions &
137
+ ContentOptions & {
115
138
  headers: () => HTTPHeaders | Promise<HTTPHeaders>;
116
139
  };
117
140
 
@@ -122,8 +145,8 @@ export function httpRequest(
122
145
  const ac = opts.AbortController ? new opts.AbortController() : null;
123
146
 
124
147
  const promise = new Promise<HTTPResult>((resolve, reject) => {
125
- const url = getUrl(opts);
126
- const body = getBody(opts);
148
+ const url = opts.getUrl(opts);
149
+ const body = opts.getBody(opts);
127
150
 
128
151
  const meta = {} as HTTPResult['meta'];
129
152
  Promise.resolve(opts.headers())
@@ -132,12 +155,15 @@ export function httpRequest(
132
155
  if (type === 'subscription') {
133
156
  throw new Error('Subscriptions should use wsLink');
134
157
  }
158
+
135
159
  return opts.fetch(url, {
136
160
  method: METHOD[type],
137
161
  signal: ac?.signal,
138
162
  body: body,
139
163
  headers: {
140
- 'content-type': 'application/json',
164
+ ...(opts.contentTypeHeader
165
+ ? { 'content-type': opts.contentTypeHeader }
166
+ : {}),
141
167
  ...headers,
142
168
  },
143
169
  });
@@ -0,0 +1,4 @@
1
+ export function isObject(value: unknown): value is Record<string, unknown> {
2
+ // check that value is object
3
+ return !!value && !Array.isArray(value) && typeof value === 'object';
4
+ }
@@ -6,6 +6,7 @@ import {
6
6
  } from '@trpc/server/rpc';
7
7
  import { TRPCClientRuntime } from '..';
8
8
  import { TRPCClientError } from '../../TRPCClientError';
9
+ import { isObject } from './isObject';
9
10
 
10
11
  // FIXME:
11
12
  // - the generics here are probably unnecessary
@@ -40,11 +41,6 @@ function transformResultInner<TRouter extends AnyRouter, TOutput>(
40
41
  return { ok: true, result } as const;
41
42
  }
42
43
 
43
- function isObject(value: unknown): value is Record<string, unknown> {
44
- // check that value is object
45
- return !!value && !Array.isArray(value) && typeof value === 'object';
46
- }
47
-
48
44
  /**
49
45
  * Transforms and validates that the result is a valid TRPCResponse
50
46
  * @internal
@@ -1,6 +1,7 @@
1
1
  import { AnyRouter } from '@trpc/server';
2
2
  import { observable, tap } from '@trpc/server/observable';
3
3
  import { TRPCClientError } from '..';
4
+ import { isObject } from './internals/isObject';
4
5
  import { Operation, OperationResultEnvelope, TRPCLink } from './types';
5
6
 
6
7
  type ConsoleEsque = {
@@ -56,15 +57,30 @@ export interface LoggerLinkOptions<TRouter extends AnyRouter> {
56
57
  console?: ConsoleEsque;
57
58
  }
58
59
 
60
+ function isFormData(value: unknown): value is FormData {
61
+ if (typeof FormData === 'undefined') {
62
+ // FormData is not supported
63
+ return false;
64
+ }
65
+ return value instanceof FormData;
66
+ }
67
+
59
68
  // maybe this should be moved to it's own package
60
69
  const defaultLogger =
61
70
  <TRouter extends AnyRouter>(
62
71
  c: ConsoleEsque = console,
63
72
  ): LoggerLinkFn<TRouter> =>
64
73
  (props) => {
65
- const { direction, input, type, path, context, id } = props;
74
+ const { direction, type, path, context, id } = props;
66
75
  const [light, dark] = palette[type];
67
76
 
77
+ const rawInput = props.input;
78
+
79
+ const input =
80
+ isObject(rawInput) && isFormData(rawInput)
81
+ ? Object.fromEntries(rawInput)
82
+ : props.input;
83
+
68
84
  const css = `
69
85
  background-color: #${direction === 'up' ? light : dark};
70
86
  color: ${direction === 'up' ? 'black' : 'white'};