@tanstack/start-client-core 1.114.4 → 1.114.5

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 (62) hide show
  1. package/dist/cjs/createIsomorphicFn.cjs +7 -0
  2. package/dist/cjs/createIsomorphicFn.cjs.map +1 -0
  3. package/dist/cjs/createIsomorphicFn.d.cts +12 -0
  4. package/dist/cjs/createMiddleware.cjs +34 -0
  5. package/dist/cjs/createMiddleware.cjs.map +1 -0
  6. package/dist/cjs/createMiddleware.d.cts +131 -0
  7. package/dist/cjs/createServerFn.cjs +227 -0
  8. package/dist/cjs/createServerFn.cjs.map +1 -0
  9. package/dist/cjs/createServerFn.d.cts +152 -0
  10. package/dist/cjs/envOnly.cjs +7 -0
  11. package/dist/cjs/envOnly.cjs.map +1 -0
  12. package/dist/cjs/envOnly.d.cts +4 -0
  13. package/dist/cjs/index.cjs +22 -0
  14. package/dist/cjs/index.cjs.map +1 -1
  15. package/dist/cjs/index.d.cts +8 -0
  16. package/dist/cjs/json.cjs +14 -0
  17. package/dist/cjs/json.cjs.map +1 -0
  18. package/dist/cjs/json.d.cts +2 -0
  19. package/dist/cjs/registerGlobalMiddleware.cjs +9 -0
  20. package/dist/cjs/registerGlobalMiddleware.cjs.map +1 -0
  21. package/dist/cjs/registerGlobalMiddleware.d.cts +5 -0
  22. package/dist/cjs/tests/createIsomorphicFn.test-d.d.cts +1 -0
  23. package/dist/cjs/tests/createServerMiddleware.test-d.d.cts +1 -0
  24. package/dist/cjs/tests/envOnly.test-d.d.cts +1 -0
  25. package/dist/cjs/tests/json.test.d.cts +1 -0
  26. package/dist/esm/createIsomorphicFn.d.ts +12 -0
  27. package/dist/esm/createIsomorphicFn.js +7 -0
  28. package/dist/esm/createIsomorphicFn.js.map +1 -0
  29. package/dist/esm/createMiddleware.d.ts +131 -0
  30. package/dist/esm/createMiddleware.js +34 -0
  31. package/dist/esm/createMiddleware.js.map +1 -0
  32. package/dist/esm/createServerFn.d.ts +152 -0
  33. package/dist/esm/createServerFn.js +206 -0
  34. package/dist/esm/createServerFn.js.map +1 -0
  35. package/dist/esm/envOnly.d.ts +4 -0
  36. package/dist/esm/envOnly.js +7 -0
  37. package/dist/esm/envOnly.js.map +1 -0
  38. package/dist/esm/index.d.ts +8 -0
  39. package/dist/esm/index.js +19 -0
  40. package/dist/esm/index.js.map +1 -1
  41. package/dist/esm/json.d.ts +2 -0
  42. package/dist/esm/json.js +14 -0
  43. package/dist/esm/json.js.map +1 -0
  44. package/dist/esm/registerGlobalMiddleware.d.ts +5 -0
  45. package/dist/esm/registerGlobalMiddleware.js +9 -0
  46. package/dist/esm/registerGlobalMiddleware.js.map +1 -0
  47. package/dist/esm/tests/createIsomorphicFn.test-d.d.ts +1 -0
  48. package/dist/esm/tests/createServerMiddleware.test-d.d.ts +1 -0
  49. package/dist/esm/tests/envOnly.test-d.d.ts +1 -0
  50. package/dist/esm/tests/json.test.d.ts +1 -0
  51. package/package.json +2 -1
  52. package/src/createIsomorphicFn.ts +36 -0
  53. package/src/createMiddleware.ts +595 -0
  54. package/src/createServerFn.ts +700 -0
  55. package/src/envOnly.ts +9 -0
  56. package/src/index.tsx +73 -0
  57. package/src/json.ts +15 -0
  58. package/src/registerGlobalMiddleware.ts +9 -0
  59. package/src/tests/createIsomorphicFn.test-d.ts +72 -0
  60. package/src/tests/createServerMiddleware.test-d.ts +611 -0
  61. package/src/tests/envOnly.test-d.ts +34 -0
  62. package/src/tests/json.test.ts +37 -0
@@ -0,0 +1,611 @@
1
+ import { expectTypeOf, test } from 'vitest'
2
+ import { createMiddleware } from '../createMiddleware'
3
+ import type { Constrain, Validator } from '@tanstack/router-core'
4
+
5
+ test('createServeMiddleware removes middleware after middleware,', () => {
6
+ const middleware = createMiddleware()
7
+
8
+ expectTypeOf(middleware).toHaveProperty('middleware')
9
+ expectTypeOf(middleware).toHaveProperty('server')
10
+ expectTypeOf(middleware).toHaveProperty('validator')
11
+
12
+ const middlewareAfterMiddleware = middleware.middleware([])
13
+
14
+ expectTypeOf(middlewareAfterMiddleware).toHaveProperty('validator')
15
+ expectTypeOf(middlewareAfterMiddleware).toHaveProperty('server')
16
+ expectTypeOf(middlewareAfterMiddleware).not.toHaveProperty('middleware')
17
+
18
+ const middlewareAfterInput = middleware.validator(() => {})
19
+
20
+ expectTypeOf(middlewareAfterInput).toHaveProperty('server')
21
+ expectTypeOf(middlewareAfterInput).not.toHaveProperty('middleware')
22
+
23
+ const middlewareAfterServer = middleware.server(async (options) => {
24
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
25
+ expectTypeOf(options.data).toEqualTypeOf<undefined>()
26
+ expectTypeOf(options.method).toEqualTypeOf<'GET' | 'POST'>()
27
+
28
+ const result = await options.next({
29
+ context: {
30
+ a: 'a',
31
+ },
32
+ })
33
+
34
+ expectTypeOf(result.context).toEqualTypeOf<{ a: string }>()
35
+
36
+ expectTypeOf(result.sendContext).toEqualTypeOf<undefined>()
37
+
38
+ return result
39
+ })
40
+
41
+ expectTypeOf(middlewareAfterServer).not.toHaveProperty('server')
42
+ expectTypeOf(middlewareAfterServer).not.toHaveProperty('input')
43
+ expectTypeOf(middlewareAfterServer).not.toHaveProperty('middleware')
44
+ })
45
+
46
+ test('createMiddleware merges server context', () => {
47
+ const middleware1 = createMiddleware().server(async (options) => {
48
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
49
+ expectTypeOf(options.data).toEqualTypeOf<undefined>()
50
+ expectTypeOf(options.method).toEqualTypeOf<'GET' | 'POST'>()
51
+
52
+ const result = await options.next({ context: { a: true } })
53
+
54
+ expectTypeOf(result).toEqualTypeOf<{
55
+ 'use functions must return the result of next()': true
56
+ _types: {
57
+ context: {
58
+ a: boolean
59
+ }
60
+ sendContext: undefined
61
+ }
62
+ context: { a: boolean }
63
+ sendContext: undefined
64
+ }>()
65
+
66
+ return result
67
+ })
68
+
69
+ const middleware2 = createMiddleware().server(async (options) => {
70
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
71
+ expectTypeOf(options.data).toEqualTypeOf<undefined>()
72
+ expectTypeOf(options.method).toEqualTypeOf<'GET' | 'POST'>()
73
+
74
+ const result = await options.next({ context: { b: 'test' } })
75
+
76
+ expectTypeOf(result).toEqualTypeOf<{
77
+ 'use functions must return the result of next()': true
78
+ _types: {
79
+ context: {
80
+ b: string
81
+ }
82
+ sendContext: undefined
83
+ }
84
+ context: { b: string }
85
+ sendContext: undefined
86
+ }>()
87
+
88
+ return result
89
+ })
90
+
91
+ const middleware3 = createMiddleware()
92
+ .middleware([middleware1, middleware2])
93
+ .server(async (options) => {
94
+ expectTypeOf(options.context).toEqualTypeOf<{ a: boolean; b: string }>()
95
+
96
+ const result = await options.next({ context: { c: 0 } })
97
+
98
+ expectTypeOf(result).toEqualTypeOf<{
99
+ 'use functions must return the result of next()': true
100
+ _types: {
101
+ context: {
102
+ c: number
103
+ }
104
+ sendContext: undefined
105
+ }
106
+ context: { a: boolean; b: string; c: number }
107
+ sendContext: undefined
108
+ }>()
109
+
110
+ return result
111
+ })
112
+
113
+ createMiddleware()
114
+ .middleware([middleware3])
115
+ .server(async (options) => {
116
+ expectTypeOf(options.context).toEqualTypeOf<{
117
+ a: boolean
118
+ b: string
119
+ c: number
120
+ }>()
121
+
122
+ const result = await options.next({ context: { d: 5 } })
123
+
124
+ expectTypeOf(result).toEqualTypeOf<{
125
+ 'use functions must return the result of next()': true
126
+ _types: {
127
+ context: {
128
+ d: number
129
+ }
130
+ sendContext: undefined
131
+ }
132
+ context: { a: boolean; b: string; c: number; d: number }
133
+ sendContext: undefined
134
+ }>()
135
+
136
+ return result
137
+ })
138
+ })
139
+
140
+ test('createMiddleware merges client context and sends to the server', () => {
141
+ const middleware1 = createMiddleware().client(async (options) => {
142
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
143
+
144
+ const result = await options.next({ context: { a: true } })
145
+
146
+ expectTypeOf(result).toEqualTypeOf<{
147
+ 'use functions must return the result of next()': true
148
+ context: { a: boolean }
149
+ sendContext: undefined
150
+ headers: HeadersInit
151
+ }>()
152
+
153
+ return result
154
+ })
155
+
156
+ const middleware2 = createMiddleware().client(async (options) => {
157
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
158
+
159
+ const result = await options.next({ context: { b: 'test' } })
160
+
161
+ expectTypeOf(result).toEqualTypeOf<{
162
+ 'use functions must return the result of next()': true
163
+ context: { b: string }
164
+ sendContext: undefined
165
+ headers: HeadersInit
166
+ }>()
167
+
168
+ return result
169
+ })
170
+
171
+ const middleware3 = createMiddleware()
172
+ .middleware([middleware1, middleware2])
173
+ .client(async (options) => {
174
+ expectTypeOf(options.context).toEqualTypeOf<{ a: boolean; b: string }>()
175
+
176
+ const result = await options.next({ context: { c: 0 } })
177
+
178
+ expectTypeOf(result).toEqualTypeOf<{
179
+ 'use functions must return the result of next()': true
180
+ context: { a: boolean; b: string; c: number }
181
+ sendContext: undefined
182
+ headers: HeadersInit
183
+ }>()
184
+
185
+ return result
186
+ })
187
+
188
+ const middleware4 = createMiddleware()
189
+ .middleware([middleware3])
190
+ .client(async (options) => {
191
+ expectTypeOf(options.context).toEqualTypeOf<{
192
+ a: boolean
193
+ b: string
194
+ c: number
195
+ }>()
196
+
197
+ const result = await options.next({
198
+ sendContext: { ...options.context, d: 5 },
199
+ })
200
+
201
+ expectTypeOf(result).toEqualTypeOf<{
202
+ 'use functions must return the result of next()': true
203
+ context: { a: boolean; b: string; c: number }
204
+ sendContext: { a: boolean; b: string; c: number; d: number }
205
+ headers: HeadersInit
206
+ }>()
207
+
208
+ return result
209
+ })
210
+
211
+ createMiddleware()
212
+ .middleware([middleware4])
213
+ .server(async (options) => {
214
+ expectTypeOf(options.context).toEqualTypeOf<{
215
+ a: boolean
216
+ b: string
217
+ c: number
218
+ d: number
219
+ }>()
220
+
221
+ const result = await options.next({
222
+ context: {
223
+ e: 'e',
224
+ },
225
+ })
226
+
227
+ expectTypeOf(result).toEqualTypeOf<{
228
+ 'use functions must return the result of next()': true
229
+ _types: {
230
+ context: {
231
+ e: string
232
+ }
233
+ sendContext: undefined
234
+ }
235
+ context: { a: boolean; b: string; c: number; d: number; e: string }
236
+ sendContext: undefined
237
+ }>()
238
+
239
+ return result
240
+ })
241
+ })
242
+
243
+ test('createMiddleware merges input', () => {
244
+ const middleware1 = createMiddleware()
245
+ .validator(() => {
246
+ return {
247
+ a: 'a',
248
+ } as const
249
+ })
250
+ .server(({ data, next }) => {
251
+ expectTypeOf(data).toEqualTypeOf<{ readonly a: 'a' }>()
252
+ return next()
253
+ })
254
+
255
+ const middleware2 = createMiddleware()
256
+ .middleware([middleware1])
257
+ .validator(() => {
258
+ return {
259
+ b: 'b',
260
+ } as const
261
+ })
262
+ .server(({ data, next }) => {
263
+ expectTypeOf(data).toEqualTypeOf<{ readonly a: 'a'; readonly b: 'b' }>
264
+ return next()
265
+ })
266
+
267
+ createMiddleware()
268
+ .middleware([middleware2])
269
+ .validator(() => ({ c: 'c' }) as const)
270
+ .server(({ next, data }) => {
271
+ expectTypeOf(data).toEqualTypeOf<{
272
+ readonly a: 'a'
273
+ readonly b: 'b'
274
+ readonly c: 'c'
275
+ }>
276
+ return next()
277
+ })
278
+ })
279
+
280
+ test('createMiddleware merges server context and client context, sends server context to the client and merges ', () => {
281
+ const middleware1 = createMiddleware()
282
+ .client(async (options) => {
283
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
284
+
285
+ const result = await options.next({
286
+ context: { fromClient1: 'fromClient1' },
287
+ })
288
+
289
+ expectTypeOf(result).toEqualTypeOf<{
290
+ 'use functions must return the result of next()': true
291
+ context: { fromClient1: string }
292
+ sendContext: undefined
293
+ headers: HeadersInit
294
+ }>()
295
+
296
+ return result
297
+ })
298
+ .server(async (options) => {
299
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
300
+
301
+ const result = await options.next({
302
+ context: { fromServer1: 'fromServer1' },
303
+ })
304
+
305
+ expectTypeOf(result).toEqualTypeOf<{
306
+ 'use functions must return the result of next()': true
307
+ _types: {
308
+ context: {
309
+ fromServer1: string
310
+ }
311
+ sendContext: undefined
312
+ }
313
+ context: { fromServer1: string }
314
+ sendContext: undefined
315
+ }>()
316
+
317
+ return result
318
+ })
319
+
320
+ const middleware2 = createMiddleware()
321
+ .client(async (options) => {
322
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
323
+
324
+ const result = await options.next({
325
+ context: { fromClient2: 'fromClient2' },
326
+ })
327
+
328
+ expectTypeOf(result).toEqualTypeOf<{
329
+ 'use functions must return the result of next()': true
330
+ context: { fromClient2: string }
331
+ sendContext: undefined
332
+ headers: HeadersInit
333
+ }>()
334
+
335
+ return result
336
+ })
337
+ .server(async (options) => {
338
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
339
+
340
+ const result = await options.next({
341
+ context: { fromServer2: 'fromServer2' },
342
+ })
343
+
344
+ expectTypeOf(result).toEqualTypeOf<{
345
+ 'use functions must return the result of next()': true
346
+ _types: {
347
+ context: {
348
+ fromServer2: string
349
+ }
350
+ sendContext: undefined
351
+ }
352
+ context: { fromServer2: string }
353
+ sendContext: undefined
354
+ }>()
355
+
356
+ return result
357
+ })
358
+
359
+ const middleware3 = createMiddleware()
360
+ .middleware([middleware1, middleware2])
361
+ .client(async (options) => {
362
+ expectTypeOf(options.context).toEqualTypeOf<{
363
+ fromClient1: string
364
+ fromClient2: string
365
+ }>()
366
+
367
+ const result = await options.next({
368
+ context: { fromClient3: 'fromClient3' },
369
+ })
370
+
371
+ expectTypeOf(result).toEqualTypeOf<{
372
+ 'use functions must return the result of next()': true
373
+ context: {
374
+ fromClient1: string
375
+ fromClient2: string
376
+ fromClient3: string
377
+ }
378
+ sendContext: undefined
379
+ headers: HeadersInit
380
+ }>()
381
+
382
+ return result
383
+ })
384
+ .server(async (options) => {
385
+ expectTypeOf(options.context).toEqualTypeOf<{
386
+ fromServer1: string
387
+ fromServer2: string
388
+ }>()
389
+
390
+ const result = await options.next({
391
+ context: { fromServer3: 'fromServer3' },
392
+ })
393
+
394
+ expectTypeOf(result).toEqualTypeOf<{
395
+ 'use functions must return the result of next()': true
396
+ _types: {
397
+ context: {
398
+ fromServer3: string
399
+ }
400
+ sendContext: undefined
401
+ }
402
+ context: {
403
+ fromServer1: string
404
+ fromServer2: string
405
+ fromServer3: string
406
+ }
407
+ sendContext: undefined
408
+ }>()
409
+
410
+ return result
411
+ })
412
+
413
+ const middleware4 = createMiddleware()
414
+ .middleware([middleware3])
415
+ .client(async (options) => {
416
+ expectTypeOf(options.context).toEqualTypeOf<{
417
+ fromClient1: string
418
+ fromClient2: string
419
+ fromClient3: string
420
+ }>()
421
+
422
+ const result = await options.next({
423
+ context: { fromClient4: 'fromClient4' },
424
+ sendContext: { toServer1: 'toServer1' },
425
+ })
426
+
427
+ expectTypeOf(result).toEqualTypeOf<{
428
+ 'use functions must return the result of next()': true
429
+ context: {
430
+ fromClient1: string
431
+ fromClient2: string
432
+ fromClient3: string
433
+ fromClient4: string
434
+ }
435
+ sendContext: { toServer1: 'toServer1' }
436
+ headers: HeadersInit
437
+ }>()
438
+
439
+ return result
440
+ })
441
+ .server(async (options) => {
442
+ expectTypeOf(options.context).toEqualTypeOf<{
443
+ fromServer1: string
444
+ fromServer2: string
445
+ fromServer3: string
446
+ toServer1: 'toServer1'
447
+ }>()
448
+
449
+ const result = await options.next({
450
+ context: { fromServer4: 'fromServer4' },
451
+ sendContext: { toClient1: 'toClient1' },
452
+ })
453
+
454
+ expectTypeOf(result).toEqualTypeOf<{
455
+ 'use functions must return the result of next()': true
456
+ _types: {
457
+ context: {
458
+ fromServer4: string
459
+ }
460
+ sendContext: {
461
+ toClient1: 'toClient1'
462
+ }
463
+ }
464
+ context: {
465
+ fromServer1: string
466
+ fromServer2: string
467
+ fromServer3: string
468
+ fromServer4: string
469
+ toServer1: 'toServer1'
470
+ }
471
+ sendContext: { toClient1: 'toClient1' }
472
+ }>()
473
+
474
+ return result
475
+ })
476
+
477
+ createMiddleware()
478
+ .middleware([middleware4])
479
+ .client(async (options) => {
480
+ expectTypeOf(options.context).toEqualTypeOf<{
481
+ fromClient1: string
482
+ fromClient2: string
483
+ fromClient3: string
484
+ fromClient4: string
485
+ }>()
486
+
487
+ const result = await options.next({
488
+ context: { fromClient5: 'fromClient5' },
489
+ sendContext: { toServer2: 'toServer2' },
490
+ })
491
+
492
+ expectTypeOf(result).toEqualTypeOf<{
493
+ 'use functions must return the result of next()': true
494
+ context: {
495
+ fromClient1: string
496
+ fromClient2: string
497
+ fromClient3: string
498
+ fromClient4: string
499
+ fromClient5: string
500
+ toClient1: 'toClient1'
501
+ }
502
+ sendContext: { toServer1: 'toServer1'; toServer2: 'toServer2' }
503
+ headers: HeadersInit
504
+ }>()
505
+
506
+ return result
507
+ })
508
+ .server(async (options) => {
509
+ expectTypeOf(options.context).toEqualTypeOf<{
510
+ fromServer1: string
511
+ fromServer2: string
512
+ fromServer3: string
513
+ fromServer4: string
514
+ toServer1: 'toServer1'
515
+ toServer2: 'toServer2'
516
+ }>()
517
+
518
+ const result = await options.next({
519
+ context: { fromServer5: 'fromServer5' },
520
+ sendContext: { toClient2: 'toClient2' },
521
+ })
522
+
523
+ expectTypeOf(result).toEqualTypeOf<{
524
+ 'use functions must return the result of next()': true
525
+ _types: {
526
+ context: {
527
+ fromServer5: string
528
+ }
529
+ sendContext: {
530
+ toClient2: 'toClient2'
531
+ }
532
+ }
533
+ context: {
534
+ fromServer1: string
535
+ fromServer2: string
536
+ fromServer3: string
537
+ fromServer4: string
538
+ fromServer5: string
539
+ toServer1: 'toServer1'
540
+ toServer2: 'toServer2'
541
+ }
542
+ sendContext: { toClient1: 'toClient1'; toClient2: 'toClient2' }
543
+ }>()
544
+
545
+ return result
546
+ })
547
+ })
548
+
549
+ test('createMiddleware sendContext cannot send a function', () => {
550
+ createMiddleware()
551
+ .client(({ next }) => {
552
+ expectTypeOf(next<{ func: () => 'func' }>)
553
+ .parameter(0)
554
+ .exclude<undefined>()
555
+ .toHaveProperty('sendContext')
556
+ .toEqualTypeOf<{ func: 'Function is not serializable' } | undefined>()
557
+
558
+ return next()
559
+ })
560
+ .server(({ next }) => {
561
+ expectTypeOf(next<undefined, { func: () => 'func' }>)
562
+ .parameter(0)
563
+ .exclude<undefined>()
564
+ .toHaveProperty('sendContext')
565
+ .toEqualTypeOf<{ func: 'Function is not serializable' } | undefined>()
566
+
567
+ return next()
568
+ })
569
+ })
570
+
571
+ test('createMiddleware cannot validate function', () => {
572
+ const validator = createMiddleware().validator<
573
+ (input: { func: () => 'string' }) => { output: 'string' }
574
+ >
575
+
576
+ expectTypeOf(validator)
577
+ .parameter(0)
578
+ .toEqualTypeOf<
579
+ Constrain<
580
+ (input: { func: () => 'string' }) => { output: 'string' },
581
+ Validator<{ func: 'Function is not serializable' }, any>
582
+ >
583
+ >()
584
+ })
585
+
586
+ test('createMiddleware can validate Date', () => {
587
+ const validator = createMiddleware().validator<
588
+ (input: Date) => { output: 'string' }
589
+ >
590
+
591
+ expectTypeOf(validator)
592
+ .parameter(0)
593
+ .toEqualTypeOf<
594
+ Constrain<(input: Date) => { output: 'string' }, Validator<Date, any>>
595
+ >()
596
+ })
597
+
598
+ test('createMiddleware can validate FormData', () => {
599
+ const validator = createMiddleware().validator<
600
+ (input: FormData) => { output: 'string' }
601
+ >
602
+
603
+ expectTypeOf(validator)
604
+ .parameter(0)
605
+ .toEqualTypeOf<
606
+ Constrain<
607
+ (input: FormData) => { output: 'string' },
608
+ Validator<FormData, any>
609
+ >
610
+ >()
611
+ })
@@ -0,0 +1,34 @@
1
+ import { expectTypeOf, test } from 'vitest'
2
+ import { clientOnly, serverOnly } from '../envOnly'
3
+
4
+ const inputFn = () => 'output'
5
+
6
+ const genericInputFn = <T>(input: T) => input
7
+
8
+ function overloadedFn(input: string): string
9
+ function overloadedFn(input: number): number
10
+ function overloadedFn(input: any) {
11
+ return input
12
+ }
13
+
14
+ test("clientOnly returns the function it's given", () => {
15
+ const outputFn = clientOnly(inputFn)
16
+ expectTypeOf(outputFn).toEqualTypeOf<typeof inputFn>()
17
+
18
+ const genericOutputFn = clientOnly(genericInputFn)
19
+ expectTypeOf(genericOutputFn).toEqualTypeOf<typeof genericInputFn>()
20
+
21
+ const overloadedOutputFn = clientOnly(overloadedFn)
22
+ expectTypeOf(overloadedOutputFn).toEqualTypeOf<typeof overloadedFn>()
23
+ })
24
+
25
+ test("serverOnly returns the function it's given", () => {
26
+ const outputFn = serverOnly(inputFn)
27
+ expectTypeOf(outputFn).toEqualTypeOf<typeof inputFn>()
28
+
29
+ const genericOutputFn = serverOnly(genericInputFn)
30
+ expectTypeOf(genericOutputFn).toEqualTypeOf<typeof genericInputFn>()
31
+
32
+ const overloadedOutputFn = serverOnly(overloadedFn)
33
+ expectTypeOf(overloadedOutputFn).toEqualTypeOf<typeof overloadedFn>()
34
+ })
@@ -0,0 +1,37 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { json } from '../json'
3
+
4
+ describe('json', () => {
5
+ it('sets the content type to application/json and stringifies the data', async () => {
6
+ const data = { foo: 'bar' }
7
+ const response = json(data)
8
+
9
+ expect(response.headers.get('Content-Type')).toBe('application/json')
10
+
11
+ const responseClone = response.clone()
12
+ await expect(responseClone.text()).resolves.toEqual(JSON.stringify(data))
13
+
14
+ await expect(response.json()).resolves.toEqual(data)
15
+ })
16
+ it("doesn't override the content type if it's already set", () => {
17
+ const response = json(null, { headers: { 'Content-Type': 'text/plain' } })
18
+
19
+ expect(response.headers.get('Content-Type')).toBe('text/plain')
20
+ })
21
+ it('reflects passed status and statusText', () => {
22
+ const response = json(null, { status: 404, statusText: 'Not Found' })
23
+
24
+ expect(response.status).toBe(404)
25
+ expect(response.statusText).toBe('Not Found')
26
+ })
27
+ it.each<[string, HeadersInit]>([
28
+ ['plain object', { 'X-TYPE': 'example' }],
29
+ ['array', [['X-TYPE', 'example']]],
30
+ ['Headers', new Headers({ 'X-TYPE': 'example' })],
31
+ ])('merges headers from %s', (_, headers) => {
32
+ const response = json(null, { headers })
33
+
34
+ expect(response.headers.get('X-TYPE')).toBe('example')
35
+ expect(response.headers.get('Content-Type')).toBe('application/json')
36
+ })
37
+ })