@tanstack/start-client-core 1.20.3-alpha.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.
- package/LICENSE +21 -0
- package/README.md +12 -0
- package/dist/cjs/createIsomorphicFn.cjs +7 -0
- package/dist/cjs/createIsomorphicFn.cjs.map +1 -0
- package/dist/cjs/createIsomorphicFn.d.cts +12 -0
- package/dist/cjs/createMiddleware.cjs +37 -0
- package/dist/cjs/createMiddleware.cjs.map +1 -0
- package/dist/cjs/createMiddleware.d.cts +175 -0
- package/dist/cjs/createServerFn.cjs +378 -0
- package/dist/cjs/createServerFn.cjs.map +1 -0
- package/dist/cjs/createServerFn.d.cts +159 -0
- package/dist/cjs/envOnly.cjs +7 -0
- package/dist/cjs/envOnly.cjs.map +1 -0
- package/dist/cjs/envOnly.d.cts +4 -0
- package/dist/cjs/headers.cjs +30 -0
- package/dist/cjs/headers.cjs.map +1 -0
- package/dist/cjs/headers.d.cts +5 -0
- package/dist/cjs/index.cjs +33 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +11 -0
- package/dist/cjs/json.cjs +14 -0
- package/dist/cjs/json.cjs.map +1 -0
- package/dist/cjs/json.d.cts +2 -0
- package/dist/cjs/registerGlobalMiddleware.cjs +9 -0
- package/dist/cjs/registerGlobalMiddleware.cjs.map +1 -0
- package/dist/cjs/registerGlobalMiddleware.d.cts +5 -0
- package/dist/cjs/serializer.cjs +152 -0
- package/dist/cjs/serializer.cjs.map +1 -0
- package/dist/cjs/serializer.d.cts +2 -0
- package/dist/cjs/ssr-client.cjs +130 -0
- package/dist/cjs/ssr-client.cjs.map +1 -0
- package/dist/cjs/ssr-client.d.cts +64 -0
- package/dist/cjs/tests/createIsomorphicFn.test-d.d.cts +1 -0
- package/dist/cjs/tests/createServerFn.test-d.d.cts +1 -0
- package/dist/cjs/tests/createServerMiddleware.test-d.d.cts +1 -0
- package/dist/cjs/tests/envOnly.test-d.d.cts +1 -0
- package/dist/cjs/tests/json.test.d.cts +1 -0
- package/dist/cjs/tests/transformer.test.d.cts +1 -0
- package/dist/esm/createIsomorphicFn.d.ts +12 -0
- package/dist/esm/createIsomorphicFn.js +7 -0
- package/dist/esm/createIsomorphicFn.js.map +1 -0
- package/dist/esm/createMiddleware.d.ts +175 -0
- package/dist/esm/createMiddleware.js +37 -0
- package/dist/esm/createMiddleware.js.map +1 -0
- package/dist/esm/createServerFn.d.ts +159 -0
- package/dist/esm/createServerFn.js +356 -0
- package/dist/esm/createServerFn.js.map +1 -0
- package/dist/esm/envOnly.d.ts +4 -0
- package/dist/esm/envOnly.js +7 -0
- package/dist/esm/envOnly.js.map +1 -0
- package/dist/esm/headers.d.ts +5 -0
- package/dist/esm/headers.js +30 -0
- package/dist/esm/headers.js.map +1 -0
- package/dist/esm/index.d.ts +11 -0
- package/dist/esm/index.js +30 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/json.d.ts +2 -0
- package/dist/esm/json.js +14 -0
- package/dist/esm/json.js.map +1 -0
- package/dist/esm/registerGlobalMiddleware.d.ts +5 -0
- package/dist/esm/registerGlobalMiddleware.js +9 -0
- package/dist/esm/registerGlobalMiddleware.js.map +1 -0
- package/dist/esm/serializer.d.ts +2 -0
- package/dist/esm/serializer.js +152 -0
- package/dist/esm/serializer.js.map +1 -0
- package/dist/esm/ssr-client.d.ts +64 -0
- package/dist/esm/ssr-client.js +130 -0
- package/dist/esm/ssr-client.js.map +1 -0
- package/dist/esm/tests/createIsomorphicFn.test-d.d.ts +1 -0
- package/dist/esm/tests/createServerFn.test-d.d.ts +1 -0
- package/dist/esm/tests/createServerMiddleware.test-d.d.ts +1 -0
- package/dist/esm/tests/envOnly.test-d.d.ts +1 -0
- package/dist/esm/tests/json.test.d.ts +1 -0
- package/dist/esm/tests/transformer.test.d.ts +1 -0
- package/package.json +56 -0
- package/src/createIsomorphicFn.ts +36 -0
- package/src/createMiddleware.ts +706 -0
- package/src/createServerFn.ts +1004 -0
- package/src/envOnly.ts +9 -0
- package/src/headers.ts +50 -0
- package/src/index.tsx +88 -0
- package/src/json.ts +15 -0
- package/src/registerGlobalMiddleware.ts +9 -0
- package/src/serializer.ts +177 -0
- package/src/ssr-client.tsx +243 -0
- package/src/tests/createIsomorphicFn.test-d.ts +72 -0
- package/src/tests/createServerFn.test-d.ts +519 -0
- package/src/tests/createServerMiddleware.test-d.ts +736 -0
- package/src/tests/envOnly.test-d.ts +34 -0
- package/src/tests/json.test.ts +37 -0
- package/src/tests/transformer.test.tsx +147 -0
|
@@ -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
|
+
})
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { startSerializer as tf } from '../serializer'
|
|
4
|
+
|
|
5
|
+
describe('transformer.stringify', () => {
|
|
6
|
+
it('should stringify dates', () => {
|
|
7
|
+
const date = new Date('2021-08-19T20:00:00.000Z')
|
|
8
|
+
expect(tf.stringify(date)).toMatchInlineSnapshot(`
|
|
9
|
+
"{"$date":"2021-08-19T20:00:00.000Z"}"
|
|
10
|
+
`)
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('should stringify undefined', () => {
|
|
14
|
+
expect(tf.stringify(undefined)).toMatchInlineSnapshot(`"{"$undefined":0}"`)
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('should stringify object foo="bar"', () => {
|
|
18
|
+
expect(tf.stringify({ foo: 'bar' })).toMatchInlineSnapshot(`
|
|
19
|
+
"{"foo":"bar"}"
|
|
20
|
+
`)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('should stringify object foo=undefined', () => {
|
|
24
|
+
expect(tf.stringify({ foo: undefined })).toMatchInlineSnapshot(
|
|
25
|
+
`"{"foo":{"$undefined":0}}"`,
|
|
26
|
+
)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('should stringify object foo=Date', () => {
|
|
30
|
+
const date = new Date('2021-08-19T20:00:00.000Z')
|
|
31
|
+
expect(tf.stringify({ foo: date })).toMatchInlineSnapshot(`
|
|
32
|
+
"{"foo":{"$date":"2021-08-19T20:00:00.000Z"}}"
|
|
33
|
+
`)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('should stringify empty FormData', () => {
|
|
37
|
+
const formData = new FormData()
|
|
38
|
+
expect(tf.stringify(formData)).toMatchInlineSnapshot(`"{"$formData":{}}"`)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('should stringify FormData with key-value pairs of foo="bar",name="Sean"', () => {
|
|
42
|
+
const formData = new FormData()
|
|
43
|
+
formData.append('foo', 'bar')
|
|
44
|
+
formData.append('name', 'Sean')
|
|
45
|
+
expect(tf.stringify(formData)).toMatchInlineSnapshot(
|
|
46
|
+
`"{"$formData":{"foo":"bar","name":"Sean"}}"`,
|
|
47
|
+
)
|
|
48
|
+
})
|
|
49
|
+
it('should stringify FormData with multiple values for the same key', () => {
|
|
50
|
+
const formData = new FormData()
|
|
51
|
+
formData.append('foo', 'bar')
|
|
52
|
+
formData.append('foo', 'baz')
|
|
53
|
+
expect(tf.stringify(formData)).toMatchInlineSnapshot(
|
|
54
|
+
`"{"$formData":{"foo":["bar","baz"]}}"`,
|
|
55
|
+
)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('should stringify bigint', () => {
|
|
59
|
+
const bigint = BigInt('9007199254740992')
|
|
60
|
+
expect(tf.stringify(bigint)).toMatchInlineSnapshot(
|
|
61
|
+
`"{"$bigint":"9007199254740992"}"`,
|
|
62
|
+
)
|
|
63
|
+
})
|
|
64
|
+
it('should stringify object foo=bigint', () => {
|
|
65
|
+
const bigint = BigInt('9007199254740992')
|
|
66
|
+
expect(tf.stringify({ foo: bigint })).toMatchInlineSnapshot(
|
|
67
|
+
`"{"foo":{"$bigint":"9007199254740992"}}"`,
|
|
68
|
+
)
|
|
69
|
+
})
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
describe('transformer.parse', () => {
|
|
73
|
+
it('should parse dates', () => {
|
|
74
|
+
const date = new Date('2021-08-19T20:00:00.000Z')
|
|
75
|
+
const str = tf.stringify(date)
|
|
76
|
+
expect(tf.parse(str)).toEqual(date)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('should parse undefined', () => {
|
|
80
|
+
const str = tf.stringify(undefined)
|
|
81
|
+
expect(tf.parse(str)).toBeUndefined()
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
it('should parse object foo="bar"', () => {
|
|
85
|
+
const obj = { foo: 'bar' }
|
|
86
|
+
const str = tf.stringify(obj)
|
|
87
|
+
expect(tf.parse(str)).toEqual(obj)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('should parse object foo=undefined', () => {
|
|
91
|
+
const obj = { foo: undefined }
|
|
92
|
+
const str = tf.stringify(obj)
|
|
93
|
+
expect(tf.parse(str)).toEqual(obj)
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it('should parse object foo=Date', () => {
|
|
97
|
+
const date = new Date('2021-08-19T20:00:00.000Z')
|
|
98
|
+
const obj = { foo: date }
|
|
99
|
+
const str = tf.stringify(obj)
|
|
100
|
+
expect(tf.parse(str)).toEqual(obj)
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
it('should parse empty FormData', () => {
|
|
104
|
+
const formData = new FormData()
|
|
105
|
+
const str = tf.stringify(formData)
|
|
106
|
+
const parsed = tf.parse(str) as FormData
|
|
107
|
+
expect(parsed).toBeInstanceOf(FormData)
|
|
108
|
+
expect([...parsed.entries()]).toEqual([])
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
it('should parse FormData with key-value pairs of foo="bar",name="Sean"', () => {
|
|
112
|
+
const formData = new FormData()
|
|
113
|
+
formData.append('foo', 'bar')
|
|
114
|
+
formData.append('name', 'Sean')
|
|
115
|
+
const str = tf.stringify(formData)
|
|
116
|
+
const parsed = tf.parse(str) as FormData
|
|
117
|
+
expect(parsed).toBeInstanceOf(FormData)
|
|
118
|
+
expect([...parsed.entries()]).toEqual([
|
|
119
|
+
['foo', 'bar'],
|
|
120
|
+
['name', 'Sean'],
|
|
121
|
+
])
|
|
122
|
+
})
|
|
123
|
+
it('should parse FormData with multiple values for the same key', () => {
|
|
124
|
+
const formData = new FormData()
|
|
125
|
+
formData.append('foo', 'bar')
|
|
126
|
+
formData.append('foo', 'baz')
|
|
127
|
+
const str = tf.stringify(formData)
|
|
128
|
+
const parsed = tf.parse(str) as FormData
|
|
129
|
+
expect(parsed).toBeInstanceOf(FormData)
|
|
130
|
+
expect([...parsed.entries()]).toEqual([
|
|
131
|
+
['foo', 'bar'],
|
|
132
|
+
['foo', 'baz'],
|
|
133
|
+
])
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
it('should parse bigint', () => {
|
|
137
|
+
const bigint = BigInt('9007199254740992')
|
|
138
|
+
const str = tf.stringify(bigint)
|
|
139
|
+
expect(tf.parse(str)).toEqual(bigint)
|
|
140
|
+
})
|
|
141
|
+
it('should parse object foo=bigint', () => {
|
|
142
|
+
const bigint = BigInt('9007199254740992')
|
|
143
|
+
const obj = { foo: bigint }
|
|
144
|
+
const str = tf.stringify(obj)
|
|
145
|
+
expect(tf.parse(str)).toEqual(obj)
|
|
146
|
+
})
|
|
147
|
+
})
|