effect-start 0.18.0 → 0.20.0
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/README.md +3 -3
- package/dist/Development.d.ts +8 -3
- package/dist/Development.js +14 -7
- package/dist/Effectify.d.ts +212 -0
- package/dist/Effectify.js +19 -0
- package/dist/FilePathPattern.d.ts +29 -0
- package/dist/FilePathPattern.js +86 -0
- package/dist/FileRouter.d.ts +39 -41
- package/dist/FileRouter.js +104 -158
- package/dist/FileRouterCodegen.d.ts +7 -8
- package/dist/FileRouterCodegen.js +97 -66
- package/dist/PlatformError.d.ts +46 -0
- package/dist/PlatformError.js +43 -0
- package/dist/PlatformRuntime.d.ts +27 -0
- package/dist/PlatformRuntime.js +51 -0
- package/dist/Route.d.ts +6 -2
- package/dist/Route.js +22 -0
- package/dist/RouteBody.d.ts +1 -1
- package/dist/RouteHttp.d.ts +1 -1
- package/dist/RouteHttp.js +12 -19
- package/dist/RouteMount.d.ts +2 -1
- package/dist/Start.d.ts +33 -6
- package/dist/Start.js +31 -13
- package/dist/Unique.d.ts +50 -0
- package/dist/Unique.js +187 -0
- package/dist/bun/BunHttpServer.js +5 -6
- package/dist/bun/BunPlatformHttpServer.d.ts +10 -0
- package/dist/bun/BunPlatformHttpServer.js +53 -0
- package/dist/bun/BunRoute.d.ts +4 -6
- package/dist/bun/BunRoute.js +10 -18
- package/dist/bun/BunRuntime.d.ts +2 -1
- package/dist/bun/BunRuntime.js +10 -5
- package/dist/bun/BunServer.d.ts +33 -0
- package/dist/bun/BunServer.js +133 -0
- package/dist/bun/BunServerRequest.d.ts +60 -0
- package/dist/bun/BunServerRequest.js +252 -0
- package/dist/bun/index.d.ts +1 -1
- package/dist/bun/index.js +1 -1
- package/dist/datastar/actions/fetch.d.ts +30 -0
- package/dist/datastar/actions/fetch.js +411 -0
- package/dist/datastar/actions/peek.d.ts +1 -0
- package/dist/datastar/actions/peek.js +14 -0
- package/dist/datastar/actions/setAll.d.ts +1 -0
- package/dist/datastar/actions/setAll.js +13 -0
- package/dist/datastar/actions/toggleAll.d.ts +1 -0
- package/dist/datastar/actions/toggleAll.js +13 -0
- package/dist/datastar/attributes/attr.d.ts +1 -0
- package/dist/datastar/attributes/attr.js +49 -0
- package/dist/datastar/attributes/bind.d.ts +1 -0
- package/dist/datastar/attributes/bind.js +183 -0
- package/dist/datastar/attributes/class.d.ts +1 -0
- package/dist/datastar/attributes/class.js +50 -0
- package/dist/datastar/attributes/computed.d.ts +1 -0
- package/dist/datastar/attributes/computed.js +27 -0
- package/dist/datastar/attributes/effect.d.ts +1 -0
- package/dist/datastar/attributes/effect.js +10 -0
- package/dist/datastar/attributes/indicator.d.ts +1 -0
- package/dist/datastar/attributes/indicator.js +32 -0
- package/dist/datastar/attributes/init.d.ts +1 -0
- package/dist/datastar/attributes/init.js +27 -0
- package/dist/datastar/attributes/jsonSignals.d.ts +1 -0
- package/dist/datastar/attributes/jsonSignals.js +31 -0
- package/dist/datastar/attributes/on.d.ts +1 -0
- package/dist/datastar/attributes/on.js +59 -0
- package/dist/datastar/attributes/onIntersect.d.ts +1 -0
- package/dist/datastar/attributes/onIntersect.js +54 -0
- package/dist/datastar/attributes/onInterval.d.ts +1 -0
- package/dist/datastar/attributes/onInterval.js +31 -0
- package/dist/datastar/attributes/onSignalPatch.d.ts +1 -0
- package/dist/datastar/attributes/onSignalPatch.js +44 -0
- package/dist/datastar/attributes/ref.d.ts +1 -0
- package/dist/datastar/attributes/ref.js +11 -0
- package/dist/datastar/attributes/show.d.ts +1 -0
- package/dist/datastar/attributes/show.js +32 -0
- package/dist/datastar/attributes/signals.d.ts +1 -0
- package/dist/datastar/attributes/signals.js +18 -0
- package/dist/datastar/attributes/style.d.ts +1 -0
- package/dist/datastar/attributes/style.js +56 -0
- package/dist/datastar/attributes/text.d.ts +1 -0
- package/dist/datastar/attributes/text.js +27 -0
- package/dist/datastar/engine.d.ts +156 -0
- package/dist/datastar/engine.js +971 -0
- package/dist/datastar/index.d.ts +24 -0
- package/dist/datastar/index.js +24 -0
- package/dist/datastar/load.d.ts +24 -0
- package/dist/datastar/load.js +24 -0
- package/dist/datastar/utils.d.ts +51 -0
- package/dist/datastar/utils.js +205 -0
- package/dist/datastar/watchers/patchElements.d.ts +1 -0
- package/dist/datastar/watchers/patchElements.js +420 -0
- package/dist/datastar/watchers/patchSignals.d.ts +1 -0
- package/dist/datastar/watchers/patchSignals.js +15 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/node/Effectify.d.ts +209 -0
- package/dist/node/Effectify.js +19 -0
- package/dist/node/FileSystem.d.ts +3 -5
- package/dist/node/FileSystem.js +42 -62
- package/dist/node/NodeFileSystem.d.ts +7 -0
- package/dist/node/NodeFileSystem.js +420 -0
- package/dist/node/NodeUtils.d.ts +2 -0
- package/dist/node/NodeUtils.js +20 -0
- package/dist/node/PlatformError.d.ts +46 -0
- package/dist/node/PlatformError.js +43 -0
- package/dist/testing/TestLogger.js +1 -1
- package/dist/x/tailwind/plugin.js +1 -1
- package/package.json +18 -7
- package/src/Development.ts +36 -40
- package/src/Effectify.ts +269 -0
- package/src/FilePathPattern.ts +115 -0
- package/src/FileRouter.ts +178 -255
- package/src/FileRouterCodegen.ts +135 -92
- package/src/PlatformError.ts +117 -0
- package/src/PlatformRuntime.ts +108 -0
- package/src/Route.ts +31 -2
- package/src/RouteBody.ts +1 -1
- package/src/RouteHttp.ts +15 -29
- package/src/RouteMount.ts +1 -1
- package/src/Start.ts +61 -27
- package/src/Unique.ts +232 -0
- package/src/bun/BunPlatformHttpServer.ts +88 -0
- package/src/bun/BunRoute.ts +14 -24
- package/src/bun/BunRuntime.ts +21 -5
- package/src/bun/BunServer.ts +228 -0
- package/src/bun/index.ts +1 -1
- package/src/datastar/README.md +18 -0
- package/src/datastar/actions/fetch.ts +609 -0
- package/src/datastar/actions/peek.ts +17 -0
- package/src/datastar/actions/setAll.ts +20 -0
- package/src/datastar/actions/toggleAll.ts +20 -0
- package/src/datastar/attributes/attr.ts +50 -0
- package/src/datastar/attributes/bind.ts +220 -0
- package/src/datastar/attributes/class.ts +57 -0
- package/src/datastar/attributes/computed.ts +33 -0
- package/src/datastar/attributes/effect.ts +11 -0
- package/src/datastar/attributes/indicator.ts +39 -0
- package/src/datastar/attributes/init.ts +35 -0
- package/src/datastar/attributes/jsonSignals.ts +38 -0
- package/src/datastar/attributes/on.ts +71 -0
- package/src/datastar/attributes/onIntersect.ts +65 -0
- package/src/datastar/attributes/onInterval.ts +39 -0
- package/src/datastar/attributes/onSignalPatch.ts +63 -0
- package/src/datastar/attributes/ref.ts +12 -0
- package/src/datastar/attributes/show.ts +33 -0
- package/src/datastar/attributes/signals.ts +22 -0
- package/src/datastar/attributes/style.ts +63 -0
- package/src/datastar/attributes/text.ts +30 -0
- package/src/datastar/engine.ts +1341 -0
- package/src/datastar/index.ts +25 -0
- package/src/datastar/utils.ts +286 -0
- package/src/datastar/watchers/patchElements.ts +554 -0
- package/src/datastar/watchers/patchSignals.ts +15 -0
- package/src/index.ts +1 -0
- package/src/node/{FileSystem.ts → NodeFileSystem.ts} +59 -97
- package/src/node/{Utils.ts → NodeUtils.ts} +2 -0
- package/src/testing/TestLogger.ts +1 -1
- package/src/x/tailwind/plugin.ts +1 -1
- package/dist/Random.d.ts +0 -5
- package/dist/Random.js +0 -49
- package/src/Commander.test.ts +0 -1639
- package/src/ContentNegotiation.test.ts +0 -603
- package/src/Development.test.ts +0 -119
- package/src/Entity.test.ts +0 -592
- package/src/FileRouterCodegen.todo.ts +0 -1133
- package/src/FileRouterPattern.test.ts +0 -147
- package/src/FileRouterPattern.ts +0 -59
- package/src/FileRouter_files.test.ts +0 -64
- package/src/FileRouter_path.test.ts +0 -145
- package/src/FileRouter_tree.test.ts +0 -132
- package/src/Http.test.ts +0 -319
- package/src/HttpAppExtra.test.ts +0 -103
- package/src/HttpUtils.test.ts +0 -85
- package/src/PathPattern.test.ts +0 -648
- package/src/Random.ts +0 -59
- package/src/RouteBody.test.ts +0 -232
- package/src/RouteHook.test.ts +0 -40
- package/src/RouteHttp.test.ts +0 -2909
- package/src/RouteMount.test.ts +0 -481
- package/src/RouteSchema.test.ts +0 -427
- package/src/RouteSse.test.ts +0 -249
- package/src/RouteTree.test.ts +0 -494
- package/src/RouteTrie.test.ts +0 -322
- package/src/RouterPattern.test.ts +0 -676
- package/src/RouterPattern.ts +0 -416
- package/src/StartApp.ts +0 -47
- package/src/Values.test.ts +0 -263
- package/src/bun/BunBundle.test.ts +0 -268
- package/src/bun/BunBundle_imports.test.ts +0 -48
- package/src/bun/BunHttpServer.test.ts +0 -251
- package/src/bun/BunHttpServer.ts +0 -306
- package/src/bun/BunImportTrackerPlugin.test.ts +0 -77
- package/src/bun/BunRoute.test.ts +0 -162
- package/src/bundler/BundleHttp.test.ts +0 -132
- package/src/effect/HttpRouter.test.ts +0 -548
- package/src/experimental/EncryptedCookies.test.ts +0 -488
- package/src/hyper/HyperHtml.test.ts +0 -209
- package/src/hyper/HyperRoute.test.tsx +0 -197
- package/src/middlewares/BasicAuthMiddleware.test.ts +0 -84
- package/src/testing/TestHttpClient.test.ts +0 -83
- package/src/testing/TestLogger.test.ts +0 -51
- package/src/x/datastar/Datastar.test.ts +0 -266
- package/src/x/tailwind/TailwindPlugin.test.ts +0 -333
- /package/src/bun/{BunHttpServer_web.ts → BunServerRequest.ts} +0 -0
package/src/RouteSchema.test.ts
DELETED
|
@@ -1,427 +0,0 @@
|
|
|
1
|
-
import * as test from "bun:test"
|
|
2
|
-
import {
|
|
3
|
-
Effect,
|
|
4
|
-
Schema,
|
|
5
|
-
} from "effect"
|
|
6
|
-
import * as Http from "./Http.ts"
|
|
7
|
-
import * as Route from "./Route.ts"
|
|
8
|
-
import * as RouteHttp from "./RouteHttp.ts"
|
|
9
|
-
import * as RouteMount from "./RouteMount.ts"
|
|
10
|
-
import * as RouteSchema from "./RouteSchema.ts"
|
|
11
|
-
import * as TestLogger from "./testing/TestLogger.ts"
|
|
12
|
-
|
|
13
|
-
test.describe(`${RouteSchema.schemaHeaders.name}()`, () => {
|
|
14
|
-
test.it(`${RouteSchema.schemaHeaders.name} merges`, () => {
|
|
15
|
-
type ExpectedBindings = {
|
|
16
|
-
hello: string
|
|
17
|
-
"x-custom-header": string
|
|
18
|
-
}
|
|
19
|
-
const route = Route
|
|
20
|
-
.use(
|
|
21
|
-
RouteSchema.schemaHeaders(
|
|
22
|
-
Schema.Struct({
|
|
23
|
-
"hello": Schema.String,
|
|
24
|
-
}),
|
|
25
|
-
),
|
|
26
|
-
)
|
|
27
|
-
.get(
|
|
28
|
-
RouteSchema.schemaHeaders(
|
|
29
|
-
Schema.Struct({
|
|
30
|
-
"x-custom-header": Schema.String,
|
|
31
|
-
}),
|
|
32
|
-
),
|
|
33
|
-
Route.html(function*(ctx) {
|
|
34
|
-
test
|
|
35
|
-
.expectTypeOf(ctx.headers)
|
|
36
|
-
.toExtend<ExpectedBindings>()
|
|
37
|
-
|
|
38
|
-
return `<h1>Hello, world!</h1>`
|
|
39
|
-
}),
|
|
40
|
-
)
|
|
41
|
-
|
|
42
|
-
test
|
|
43
|
-
.expectTypeOf<Route.Route.Context<typeof route>>()
|
|
44
|
-
.toExtend<{
|
|
45
|
-
headers: ExpectedBindings
|
|
46
|
-
}>()
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
test.it("passes bindings and parses value", async () => {
|
|
50
|
-
const headers = {
|
|
51
|
-
"x-hello": "test-value",
|
|
52
|
-
}
|
|
53
|
-
type ExpectedBindings = {
|
|
54
|
-
headers: typeof headers
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const route = RouteMount.get(
|
|
58
|
-
RouteSchema.schemaHeaders(
|
|
59
|
-
Schema.Struct({
|
|
60
|
-
"x-hello": Schema.String,
|
|
61
|
-
}),
|
|
62
|
-
),
|
|
63
|
-
Route.text((context) =>
|
|
64
|
-
Effect.gen(function*() {
|
|
65
|
-
test
|
|
66
|
-
.expectTypeOf(context)
|
|
67
|
-
.toExtend<ExpectedBindings>()
|
|
68
|
-
|
|
69
|
-
test
|
|
70
|
-
.expect(context)
|
|
71
|
-
.toMatchObject({
|
|
72
|
-
headers,
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
return "Hello, World!"
|
|
76
|
-
})
|
|
77
|
-
),
|
|
78
|
-
)
|
|
79
|
-
|
|
80
|
-
test
|
|
81
|
-
.expectTypeOf<Route.Route.Context<typeof route>>()
|
|
82
|
-
.toExtend<ExpectedBindings>()
|
|
83
|
-
})
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
test.describe(`${RouteSchema.schemaCookies.name}()`, () => {
|
|
87
|
-
test.it("merges cookies from schema", () => {
|
|
88
|
-
type ExpectedBindings = {
|
|
89
|
-
session: string
|
|
90
|
-
token: string
|
|
91
|
-
}
|
|
92
|
-
const route = Route
|
|
93
|
-
.use(
|
|
94
|
-
RouteSchema.schemaCookies(
|
|
95
|
-
Schema.Struct({
|
|
96
|
-
"session": Schema.String,
|
|
97
|
-
}),
|
|
98
|
-
),
|
|
99
|
-
)
|
|
100
|
-
.get(
|
|
101
|
-
RouteSchema.schemaCookies(
|
|
102
|
-
Schema.Struct({
|
|
103
|
-
"token": Schema.String,
|
|
104
|
-
}),
|
|
105
|
-
),
|
|
106
|
-
Route.html(function*(ctx) {
|
|
107
|
-
test
|
|
108
|
-
.expectTypeOf(ctx.cookies)
|
|
109
|
-
.toExtend<ExpectedBindings>()
|
|
110
|
-
|
|
111
|
-
return `<h1>Hello, world!</h1>`
|
|
112
|
-
}),
|
|
113
|
-
)
|
|
114
|
-
|
|
115
|
-
test
|
|
116
|
-
.expectTypeOf<Route.Route.Context<typeof route>>()
|
|
117
|
-
.toExtend<{
|
|
118
|
-
cookies: ExpectedBindings
|
|
119
|
-
}>()
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
test.it("parses cookies from request", async () => {
|
|
123
|
-
const handler = RouteHttp.toWebHandler(
|
|
124
|
-
Route.get(
|
|
125
|
-
RouteSchema.schemaCookies(
|
|
126
|
-
Schema.Struct({
|
|
127
|
-
"session": Schema.String,
|
|
128
|
-
}),
|
|
129
|
-
),
|
|
130
|
-
Route.text(function*(ctx) {
|
|
131
|
-
return `session=${ctx.cookies.session}`
|
|
132
|
-
}),
|
|
133
|
-
),
|
|
134
|
-
)
|
|
135
|
-
const response = await Http.fetch(handler, {
|
|
136
|
-
path: "/test",
|
|
137
|
-
headers: {
|
|
138
|
-
"Cookie": "session=abc123",
|
|
139
|
-
},
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
test
|
|
143
|
-
.expect(response.status)
|
|
144
|
-
.toBe(200)
|
|
145
|
-
test
|
|
146
|
-
.expect(await response.text())
|
|
147
|
-
.toBe("session=abc123")
|
|
148
|
-
})
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
test.describe(`${RouteSchema.schemaSearchParams.name}()`, () => {
|
|
152
|
-
test.it("merges search params from schema", () => {
|
|
153
|
-
type ExpectedBindings = {
|
|
154
|
-
page: string
|
|
155
|
-
limit: string
|
|
156
|
-
}
|
|
157
|
-
const route = Route
|
|
158
|
-
.use(
|
|
159
|
-
RouteSchema.schemaSearchParams(
|
|
160
|
-
Schema.Struct({
|
|
161
|
-
"page": Schema.String,
|
|
162
|
-
}),
|
|
163
|
-
),
|
|
164
|
-
)
|
|
165
|
-
.get(
|
|
166
|
-
RouteSchema.schemaSearchParams(
|
|
167
|
-
Schema.Struct({
|
|
168
|
-
"limit": Schema.String,
|
|
169
|
-
}),
|
|
170
|
-
),
|
|
171
|
-
Route.html(function*(ctx) {
|
|
172
|
-
test
|
|
173
|
-
.expectTypeOf(ctx.searchParams)
|
|
174
|
-
.toExtend<ExpectedBindings>()
|
|
175
|
-
|
|
176
|
-
return `<h1>Hello, world!</h1>`
|
|
177
|
-
}),
|
|
178
|
-
)
|
|
179
|
-
|
|
180
|
-
test
|
|
181
|
-
.expectTypeOf<Route.Route.Context<typeof route>>()
|
|
182
|
-
.toExtend<{
|
|
183
|
-
searchParams: ExpectedBindings
|
|
184
|
-
}>()
|
|
185
|
-
})
|
|
186
|
-
|
|
187
|
-
test.it("parses search params from request", async () => {
|
|
188
|
-
const handler = RouteHttp.toWebHandler(
|
|
189
|
-
Route.get(
|
|
190
|
-
RouteSchema.schemaSearchParams(
|
|
191
|
-
Schema.Struct({
|
|
192
|
-
"page": Schema.NumberFromString,
|
|
193
|
-
"limit": Schema.NumberFromString,
|
|
194
|
-
}),
|
|
195
|
-
),
|
|
196
|
-
Route.text(function*(ctx) {
|
|
197
|
-
return `page=${ctx.searchParams.page},limit=${ctx.searchParams.limit}`
|
|
198
|
-
}),
|
|
199
|
-
),
|
|
200
|
-
)
|
|
201
|
-
const response = await Http.fetch(handler, {
|
|
202
|
-
path: "/test?page=2&limit=10",
|
|
203
|
-
})
|
|
204
|
-
|
|
205
|
-
test
|
|
206
|
-
.expect(response.status)
|
|
207
|
-
.toBe(200)
|
|
208
|
-
test
|
|
209
|
-
.expect(await response.text())
|
|
210
|
-
.toBe("page=2,limit=10")
|
|
211
|
-
})
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
test.describe(`${RouteSchema.schemaBodyJson.name}()`, () => {
|
|
215
|
-
test.it("parses JSON body from schema", () => {
|
|
216
|
-
type ExpectedBindings = {
|
|
217
|
-
name: string
|
|
218
|
-
age: number
|
|
219
|
-
}
|
|
220
|
-
const route = Route
|
|
221
|
-
.post(
|
|
222
|
-
RouteSchema.schemaBodyJson(
|
|
223
|
-
Schema.Struct({
|
|
224
|
-
name: Schema.String,
|
|
225
|
-
age: Schema.Number,
|
|
226
|
-
}),
|
|
227
|
-
),
|
|
228
|
-
Route.json(function*(ctx) {
|
|
229
|
-
test
|
|
230
|
-
.expectTypeOf(ctx.body)
|
|
231
|
-
.toExtend<ExpectedBindings>()
|
|
232
|
-
|
|
233
|
-
return { received: ctx.body }
|
|
234
|
-
}),
|
|
235
|
-
)
|
|
236
|
-
|
|
237
|
-
test
|
|
238
|
-
.expectTypeOf<Route.Route.Context<typeof route>>()
|
|
239
|
-
.toExtend<{
|
|
240
|
-
body: ExpectedBindings
|
|
241
|
-
}>()
|
|
242
|
-
})
|
|
243
|
-
|
|
244
|
-
test.it("parses JSON body from request", async () => {
|
|
245
|
-
const handler = RouteHttp.toWebHandler(
|
|
246
|
-
Route.post(
|
|
247
|
-
RouteSchema.schemaBodyJson(
|
|
248
|
-
Schema.Struct({
|
|
249
|
-
name: Schema.String,
|
|
250
|
-
age: Schema.Number,
|
|
251
|
-
}),
|
|
252
|
-
),
|
|
253
|
-
Route.json(function*(ctx) {
|
|
254
|
-
return { name: ctx.body.name, age: ctx.body.age }
|
|
255
|
-
}),
|
|
256
|
-
),
|
|
257
|
-
)
|
|
258
|
-
const response = await Http.fetch(handler, {
|
|
259
|
-
path: "/test",
|
|
260
|
-
method: "POST",
|
|
261
|
-
headers: {
|
|
262
|
-
"Content-Type": "application/json",
|
|
263
|
-
},
|
|
264
|
-
body: JSON.stringify({ name: "Alice", age: 30 }),
|
|
265
|
-
})
|
|
266
|
-
|
|
267
|
-
test
|
|
268
|
-
.expect(response.status)
|
|
269
|
-
.toBe(200)
|
|
270
|
-
test
|
|
271
|
-
.expect(await response.json())
|
|
272
|
-
.toEqual({ name: "Alice", age: 30 })
|
|
273
|
-
})
|
|
274
|
-
|
|
275
|
-
test.it("returns error for invalid JSON body", () =>
|
|
276
|
-
Effect
|
|
277
|
-
.gen(function*() {
|
|
278
|
-
const runtime = yield* Effect.runtime<TestLogger.TestLogger>()
|
|
279
|
-
const handler = RouteHttp.toWebHandlerRuntime(runtime)(
|
|
280
|
-
Route.post(
|
|
281
|
-
RouteSchema.schemaBodyJson(
|
|
282
|
-
Schema.Struct({
|
|
283
|
-
name: Schema.String,
|
|
284
|
-
age: Schema.Number,
|
|
285
|
-
}),
|
|
286
|
-
),
|
|
287
|
-
Route.json(function*(ctx) {
|
|
288
|
-
return ctx.body
|
|
289
|
-
}),
|
|
290
|
-
),
|
|
291
|
-
)
|
|
292
|
-
const response = yield* Effect.promise(() =>
|
|
293
|
-
Http.fetch(handler, {
|
|
294
|
-
path: "/test",
|
|
295
|
-
method: "POST",
|
|
296
|
-
headers: {
|
|
297
|
-
"Content-Type": "application/json",
|
|
298
|
-
},
|
|
299
|
-
body: JSON.stringify({ name: "Alice", age: "not a number" }),
|
|
300
|
-
})
|
|
301
|
-
)
|
|
302
|
-
|
|
303
|
-
test
|
|
304
|
-
.expect(response.status)
|
|
305
|
-
.toBe(400)
|
|
306
|
-
|
|
307
|
-
const messages = yield* TestLogger.messages
|
|
308
|
-
test
|
|
309
|
-
.expect(messages.some((m) => m.includes("ParseError")))
|
|
310
|
-
.toBe(true)
|
|
311
|
-
})
|
|
312
|
-
.pipe(Effect.provide(TestLogger.layer()), Effect.runPromise))
|
|
313
|
-
})
|
|
314
|
-
|
|
315
|
-
test.describe(`${RouteSchema.schemaBodyUrlParams.name}()`, () => {
|
|
316
|
-
test.it("parses URL params body from schema", () => {
|
|
317
|
-
type ExpectedBindings = {
|
|
318
|
-
username: string
|
|
319
|
-
password: string
|
|
320
|
-
}
|
|
321
|
-
const route = Route
|
|
322
|
-
.post(
|
|
323
|
-
RouteSchema.schemaBodyUrlParams(
|
|
324
|
-
Schema.Struct({
|
|
325
|
-
username: Schema.String,
|
|
326
|
-
password: Schema.String,
|
|
327
|
-
}),
|
|
328
|
-
),
|
|
329
|
-
Route.json(function*(ctx) {
|
|
330
|
-
test
|
|
331
|
-
.expectTypeOf(ctx.body)
|
|
332
|
-
.toExtend<ExpectedBindings>()
|
|
333
|
-
|
|
334
|
-
return { received: ctx.body }
|
|
335
|
-
}),
|
|
336
|
-
)
|
|
337
|
-
|
|
338
|
-
test
|
|
339
|
-
.expectTypeOf<Route.Route.Context<typeof route>>()
|
|
340
|
-
.toExtend<{
|
|
341
|
-
body: ExpectedBindings
|
|
342
|
-
}>()
|
|
343
|
-
})
|
|
344
|
-
|
|
345
|
-
test.it("parses URL-encoded body from request", async () => {
|
|
346
|
-
const handler = RouteHttp.toWebHandler(
|
|
347
|
-
Route.post(
|
|
348
|
-
RouteSchema.schemaBodyUrlParams(
|
|
349
|
-
Schema.Struct({
|
|
350
|
-
username: Schema.String,
|
|
351
|
-
password: Schema.String,
|
|
352
|
-
}),
|
|
353
|
-
),
|
|
354
|
-
Route.json(function*(ctx) {
|
|
355
|
-
return { username: ctx.body.username, password: ctx.body.password }
|
|
356
|
-
}),
|
|
357
|
-
),
|
|
358
|
-
)
|
|
359
|
-
const response = await Http.fetch(handler, {
|
|
360
|
-
path: "/test",
|
|
361
|
-
method: "POST",
|
|
362
|
-
headers: {
|
|
363
|
-
"Content-Type": "application/x-www-form-urlencoded",
|
|
364
|
-
},
|
|
365
|
-
body: "username=alice&password=secret123",
|
|
366
|
-
})
|
|
367
|
-
|
|
368
|
-
test
|
|
369
|
-
.expect(response.status)
|
|
370
|
-
.toBe(200)
|
|
371
|
-
test
|
|
372
|
-
.expect(await response.json())
|
|
373
|
-
.toEqual({ username: "alice", password: "secret123" })
|
|
374
|
-
})
|
|
375
|
-
})
|
|
376
|
-
|
|
377
|
-
test.describe(`${RouteSchema.schemaBodyMultipart.name}()`, () => {
|
|
378
|
-
test.it("has correct type signature for multipart body", () => {
|
|
379
|
-
const route = Route
|
|
380
|
-
.post(
|
|
381
|
-
RouteSchema.schemaBodyMultipart(
|
|
382
|
-
Schema.Struct({
|
|
383
|
-
name: Schema.String,
|
|
384
|
-
}),
|
|
385
|
-
),
|
|
386
|
-
Route.json(function*(ctx) {
|
|
387
|
-
test
|
|
388
|
-
.expectTypeOf(ctx.body)
|
|
389
|
-
.toExtend<{ name: string }>()
|
|
390
|
-
|
|
391
|
-
return { name: ctx.body.name }
|
|
392
|
-
}),
|
|
393
|
-
)
|
|
394
|
-
|
|
395
|
-
test
|
|
396
|
-
.expectTypeOf<Route.Route.Context<typeof route>>()
|
|
397
|
-
.toExtend<{
|
|
398
|
-
body: { name: string }
|
|
399
|
-
}>()
|
|
400
|
-
})
|
|
401
|
-
})
|
|
402
|
-
|
|
403
|
-
test.describe(`${RouteSchema.schemaBodyForm.name}()`, () => {
|
|
404
|
-
test.it("has correct type signature for form body", () => {
|
|
405
|
-
const route = Route
|
|
406
|
-
.post(
|
|
407
|
-
RouteSchema.schemaBodyForm(
|
|
408
|
-
Schema.Struct({
|
|
409
|
-
email: Schema.String,
|
|
410
|
-
}),
|
|
411
|
-
),
|
|
412
|
-
Route.json(function*(ctx) {
|
|
413
|
-
test
|
|
414
|
-
.expectTypeOf(ctx.body)
|
|
415
|
-
.toExtend<{ email: string }>()
|
|
416
|
-
|
|
417
|
-
return { email: ctx.body.email }
|
|
418
|
-
}),
|
|
419
|
-
)
|
|
420
|
-
|
|
421
|
-
test
|
|
422
|
-
.expectTypeOf<Route.Route.Context<typeof route>>()
|
|
423
|
-
.toExtend<{
|
|
424
|
-
body: { email: string }
|
|
425
|
-
}>()
|
|
426
|
-
})
|
|
427
|
-
})
|
package/src/RouteSse.test.ts
DELETED
|
@@ -1,249 +0,0 @@
|
|
|
1
|
-
import * as test from "bun:test"
|
|
2
|
-
import * as Context from "effect/Context"
|
|
3
|
-
import * as Effect from "effect/Effect"
|
|
4
|
-
import * as Layer from "effect/Layer"
|
|
5
|
-
import * as Runtime from "effect/Runtime"
|
|
6
|
-
import * as Stream from "effect/Stream"
|
|
7
|
-
import * as Http from "./Http.ts"
|
|
8
|
-
import * as Route from "./Route.ts"
|
|
9
|
-
import * as RouteHttp from "./RouteHttp.ts"
|
|
10
|
-
import * as RouteMount from "./RouteMount.ts"
|
|
11
|
-
|
|
12
|
-
test.describe("Route.sse()", () => {
|
|
13
|
-
test.it("infers format as text", () => {
|
|
14
|
-
RouteMount.get(
|
|
15
|
-
Route.sse(function*(ctx) {
|
|
16
|
-
test
|
|
17
|
-
.expectTypeOf(ctx)
|
|
18
|
-
.toExtend<{
|
|
19
|
-
method: "GET"
|
|
20
|
-
format: "text"
|
|
21
|
-
}>()
|
|
22
|
-
|
|
23
|
-
return Stream.make({ data: "hello" })
|
|
24
|
-
}),
|
|
25
|
-
)
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
test.it("accepts object events with data only", () => {
|
|
29
|
-
RouteMount.get(
|
|
30
|
-
Route.sse(() =>
|
|
31
|
-
Stream.make(
|
|
32
|
-
{ data: "hello" },
|
|
33
|
-
{ data: "world" },
|
|
34
|
-
)
|
|
35
|
-
),
|
|
36
|
-
)
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
test.it("accepts object events with data and event", () => {
|
|
40
|
-
RouteMount.get(
|
|
41
|
-
Route.sse(() =>
|
|
42
|
-
Stream.make(
|
|
43
|
-
{ data: "hello", event: "message" },
|
|
44
|
-
{ data: "world", event: "update" },
|
|
45
|
-
)
|
|
46
|
-
),
|
|
47
|
-
)
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
test.it("accepts events with retry", () => {
|
|
51
|
-
RouteMount.get(
|
|
52
|
-
Route.sse(() =>
|
|
53
|
-
Stream.make(
|
|
54
|
-
{ data: "hello", retry: 3000 },
|
|
55
|
-
)
|
|
56
|
-
),
|
|
57
|
-
)
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
test.it("accepts Effect returning Stream", () => {
|
|
61
|
-
RouteMount.get(
|
|
62
|
-
Route.sse(
|
|
63
|
-
Effect.succeed(Stream.make({ data: "hello" })),
|
|
64
|
-
),
|
|
65
|
-
)
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
test.it("accepts generator returning Stream", () => {
|
|
69
|
-
RouteMount.get(
|
|
70
|
-
Route.sse(function*() {
|
|
71
|
-
const prefix = yield* Effect.succeed("msg: ")
|
|
72
|
-
return Stream.make({ data: `${prefix}hello` })
|
|
73
|
-
}),
|
|
74
|
-
)
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
test.it("formats data events correctly", async () => {
|
|
78
|
-
const handler = RouteHttp.toWebHandler(
|
|
79
|
-
Route.get(
|
|
80
|
-
Route.sse(() => Stream.make({ data: "hello" }, { data: "world" })),
|
|
81
|
-
),
|
|
82
|
-
)
|
|
83
|
-
const response = await Http.fetch(handler, { path: "/events" })
|
|
84
|
-
|
|
85
|
-
test.expect(response.headers.get("content-type")).toBe("text/event-stream")
|
|
86
|
-
test.expect(response.headers.get("cache-control")).toBe("no-cache")
|
|
87
|
-
test.expect(response.headers.get("connection")).toBe("keep-alive")
|
|
88
|
-
|
|
89
|
-
const text = await response.text()
|
|
90
|
-
test.expect(text).toBe("data: hello\n\ndata: world\n\n")
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
test.it("formats events with event field", async () => {
|
|
94
|
-
const handler = RouteHttp.toWebHandler(
|
|
95
|
-
Route.get(
|
|
96
|
-
Route.sse(() => Stream.make({ data: "payload", event: "custom" })),
|
|
97
|
-
),
|
|
98
|
-
)
|
|
99
|
-
const response = await Http.fetch(handler, { path: "/events" })
|
|
100
|
-
|
|
101
|
-
const text = await response.text()
|
|
102
|
-
test.expect(text).toBe("event: custom\ndata: payload\n\n")
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
test.it("formats events with retry", async () => {
|
|
106
|
-
const handler = RouteHttp.toWebHandler(
|
|
107
|
-
Route.get(
|
|
108
|
-
Route.sse(() => Stream.make({ data: "hello", retry: 5000 })),
|
|
109
|
-
),
|
|
110
|
-
)
|
|
111
|
-
const response = await Http.fetch(handler, { path: "/events" })
|
|
112
|
-
|
|
113
|
-
const text = await response.text()
|
|
114
|
-
test.expect(text).toBe("data: hello\nretry: 5000\n\n")
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
test.it("formats multi-line data with multiple data fields", async () => {
|
|
118
|
-
const handler = RouteHttp.toWebHandler(
|
|
119
|
-
Route.get(
|
|
120
|
-
Route.sse(() =>
|
|
121
|
-
Stream.make({
|
|
122
|
-
event: "patch",
|
|
123
|
-
data: "line1\nline2\nline3",
|
|
124
|
-
})
|
|
125
|
-
),
|
|
126
|
-
),
|
|
127
|
-
)
|
|
128
|
-
const response = await Http.fetch(handler, { path: "/events" })
|
|
129
|
-
|
|
130
|
-
const text = await response.text()
|
|
131
|
-
test.expect(text).toBe(
|
|
132
|
-
"event: patch\ndata: line1\ndata: line2\ndata: line3\n\n",
|
|
133
|
-
)
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
test.it("accepts Stream directly", async () => {
|
|
137
|
-
const handler = RouteHttp.toWebHandler(
|
|
138
|
-
Route.get(
|
|
139
|
-
Route.sse(
|
|
140
|
-
Stream.make({ data: "direct" }),
|
|
141
|
-
),
|
|
142
|
-
),
|
|
143
|
-
)
|
|
144
|
-
const response = await Http.fetch(handler, { path: "/events" })
|
|
145
|
-
|
|
146
|
-
const text = await response.text()
|
|
147
|
-
test.expect(text).toBe("data: direct\n\n")
|
|
148
|
-
})
|
|
149
|
-
|
|
150
|
-
test.it("infers error type from Stream", () => {
|
|
151
|
-
class MyError {
|
|
152
|
-
readonly _tag = "MyError"
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const stream: Stream.Stream<{ data: string }, MyError> = Stream.fail(
|
|
156
|
-
new MyError(),
|
|
157
|
-
)
|
|
158
|
-
|
|
159
|
-
const route = Route.get(
|
|
160
|
-
Route.sse(stream),
|
|
161
|
-
)
|
|
162
|
-
|
|
163
|
-
const routes = [...route]
|
|
164
|
-
type RouteError = (typeof routes)[0] extends
|
|
165
|
-
Route.Route.Route<any, any, any, infer E, any> ? E
|
|
166
|
-
: never
|
|
167
|
-
|
|
168
|
-
test.expectTypeOf<RouteError>().toEqualTypeOf<MyError>()
|
|
169
|
-
})
|
|
170
|
-
|
|
171
|
-
test.it("infers context type from Stream", () => {
|
|
172
|
-
class Config extends Context.Tag("Config")<Config, { url: string }>() {}
|
|
173
|
-
|
|
174
|
-
const stream = Stream.fromEffect(
|
|
175
|
-
Effect.map(Config, (cfg) => ({ data: cfg.url })),
|
|
176
|
-
)
|
|
177
|
-
|
|
178
|
-
const route = Route.get(
|
|
179
|
-
Route.sse(stream),
|
|
180
|
-
)
|
|
181
|
-
|
|
182
|
-
const routes = [...route]
|
|
183
|
-
type RouteContext = (typeof routes)[0] extends
|
|
184
|
-
Route.Route.Route<any, any, any, any, infer R> ? R
|
|
185
|
-
: never
|
|
186
|
-
|
|
187
|
-
test.expectTypeOf<RouteContext>().toEqualTypeOf<Config>()
|
|
188
|
-
})
|
|
189
|
-
|
|
190
|
-
test.it("works with context at runtime", async () => {
|
|
191
|
-
class Config extends Context.Tag("Config")<Config, { message: string }>() {}
|
|
192
|
-
|
|
193
|
-
const stream = Stream.fromEffect(
|
|
194
|
-
Effect.map(Config, (cfg) => ({ data: cfg.message })),
|
|
195
|
-
)
|
|
196
|
-
|
|
197
|
-
const route = Route.get(Route.sse(stream))
|
|
198
|
-
const layer = Layer.succeed(Config, { message: "from context" })
|
|
199
|
-
const runtime = Effect.runSync(Layer.toRuntime(layer).pipe(Effect.scoped))
|
|
200
|
-
const handler = RouteHttp.toWebHandlerRuntime(runtime)(route)
|
|
201
|
-
|
|
202
|
-
const response = await Http.fetch(handler, { path: "/events" })
|
|
203
|
-
const text = await response.text()
|
|
204
|
-
|
|
205
|
-
test.expect(text).toBe("data: from context\n\n")
|
|
206
|
-
})
|
|
207
|
-
|
|
208
|
-
test.it("formats tagged struct as event with JSON data", async () => {
|
|
209
|
-
const handler = RouteHttp.toWebHandler(
|
|
210
|
-
Route.get(
|
|
211
|
-
Route.sse(() =>
|
|
212
|
-
Stream.make(
|
|
213
|
-
{ _tag: "UserCreated", id: 123, name: "Alice" },
|
|
214
|
-
{ _tag: "UserUpdated", id: 123, active: true },
|
|
215
|
-
)
|
|
216
|
-
),
|
|
217
|
-
),
|
|
218
|
-
)
|
|
219
|
-
const response = await Http.fetch(handler, { path: "/events" })
|
|
220
|
-
|
|
221
|
-
const text = await response.text()
|
|
222
|
-
test.expect(text).toBe(
|
|
223
|
-
`event: UserCreated\ndata: {"_tag":"UserCreated","id":123,"name":"Alice"}\n\n`
|
|
224
|
-
+ `event: UserUpdated\ndata: {"_tag":"UserUpdated","id":123,"active":true}\n\n`,
|
|
225
|
-
)
|
|
226
|
-
})
|
|
227
|
-
|
|
228
|
-
test.it("handles mixed tagged and regular events", async () => {
|
|
229
|
-
const handler = RouteHttp.toWebHandler(
|
|
230
|
-
Route.get(
|
|
231
|
-
Route.sse(() =>
|
|
232
|
-
Stream.make(
|
|
233
|
-
{ data: "plain message" },
|
|
234
|
-
{ _tag: "Notification", text: "hello" },
|
|
235
|
-
{ data: "another", event: "custom" },
|
|
236
|
-
)
|
|
237
|
-
),
|
|
238
|
-
),
|
|
239
|
-
)
|
|
240
|
-
const response = await Http.fetch(handler, { path: "/events" })
|
|
241
|
-
|
|
242
|
-
const text = await response.text()
|
|
243
|
-
test.expect(text).toBe(
|
|
244
|
-
`data: plain message\n\n`
|
|
245
|
-
+ `event: Notification\ndata: {"_tag":"Notification","text":"hello"}\n\n`
|
|
246
|
-
+ `event: custom\ndata: another\n\n`,
|
|
247
|
-
)
|
|
248
|
-
})
|
|
249
|
-
})
|