@effect/platform 0.69.14 → 0.69.16

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.
@@ -5,7 +5,7 @@ import * as Chunk from "effect/Chunk"
5
5
  import * as Context from "effect/Context"
6
6
  import * as Effect from "effect/Effect"
7
7
  import * as Encoding from "effect/Encoding"
8
- import * as FiberRef from "effect/FiberRef"
8
+ import * as Fiber from "effect/Fiber"
9
9
  import { identity } from "effect/Function"
10
10
  import { globalValue } from "effect/GlobalValue"
11
11
  import * as HashMap from "effect/HashMap"
@@ -163,17 +163,13 @@ export const toWebHandler = <LA, LE>(
163
163
  options?.memoMap
164
164
  )
165
165
  let handlerCached: ((request: Request) => Promise<Response>) | undefined
166
- const handlerPromise = httpApp.pipe(
167
- Effect.bindTo("httpApp"),
168
- Effect.bind("runtime", () => runtime.runtimeEffect),
169
- Effect.map(({ httpApp, runtime }) =>
170
- HttpApp.toWebHandlerRuntime(runtime)(options?.middleware ? options.middleware(httpApp as any) as any : httpApp)
171
- ),
172
- Effect.tap((handler) => {
173
- handlerCached = handler
174
- }),
175
- runtime.runPromise
176
- )
166
+ const handlerPromise = Effect.gen(function*() {
167
+ const app = yield* httpApp
168
+ const rt = yield* runtime.runtimeEffect
169
+ const handler = HttpApp.toWebHandlerRuntime(rt)(options?.middleware ? options.middleware(app as any) as any : app)
170
+ handlerCached = handler
171
+ return handler
172
+ }).pipe(runtime.runPromise)
177
173
  function handler(request: Request): Promise<Response> {
178
174
  if (handlerCached !== undefined) {
179
175
  return handlerCached(request)
@@ -570,52 +566,35 @@ const handlerToRoute = (
570
566
  endpoint.path,
571
567
  applyMiddleware(
572
568
  middleware,
573
- Effect.withFiberRuntime((fiber) => {
574
- const context = fiber.getFiberRef(FiberRef.currentContext)
575
- const request = Context.unsafeGet(context, HttpServerRequest.HttpServerRequest)
569
+ Effect.gen(function*() {
570
+ const fiber = Option.getOrThrow(Fiber.getCurrentFiber())
571
+ const context = fiber.currentContext
572
+ const httpRequest = Context.unsafeGet(context, HttpServerRequest.HttpServerRequest)
576
573
  const routeContext = Context.unsafeGet(context, HttpRouter.RouteContext)
577
574
  const urlParams = Context.unsafeGet(context, HttpServerRequest.ParsedSearchParams)
578
- return (
579
- decodePath._tag === "Some"
580
- ? decodePath.value(routeContext.params)
581
- : Effect.succeed(routeContext.params)
582
- ).pipe(
583
- Effect.bindTo("pathParams"),
584
- decodePayload._tag === "Some"
585
- ? Effect.bind(
586
- "payload",
587
- (_) =>
588
- Effect.flatMap(
589
- requestPayload(request, urlParams, isMultipart),
590
- decodePayload.value
591
- )
592
- ) as typeof identity
593
- : identity,
594
- decodeHeaders._tag === "Some"
595
- ? Effect.bind("headers", (_) => decodeHeaders.value(request.headers)) as typeof identity
596
- : identity,
597
- decodeUrlParams._tag === "Some"
598
- ? Effect.bind("urlParams", (_) => decodeUrlParams.value(urlParams)) as typeof identity
599
- : identity,
600
- Effect.flatMap((input) => {
601
- const request: any = { path: input.pathParams }
602
- if ("payload" in input) {
603
- request.payload = input.payload
604
- }
605
- if ("headers" in input) {
606
- request.headers = input.headers
607
- }
608
- if ("urlParams" in input) {
609
- request.urlParams = input.urlParams
610
- }
611
- return handler(request)
612
- }),
613
- isFullResponse ?
614
- identity as (_: any) => Effect.Effect<HttpServerResponse.HttpServerResponse> :
615
- Effect.flatMap(encodeSuccess),
616
- Effect.catchIf(ParseResult.isParseError, HttpApiDecodeError.refailParseError)
617
- )
618
- })
575
+ const request: any = {}
576
+ if (decodePath._tag === "Some") {
577
+ request.path = yield* decodePath.value(routeContext.params)
578
+ }
579
+ if (decodePayload._tag === "Some") {
580
+ request.payload = yield* Effect.flatMap(
581
+ requestPayload(httpRequest, urlParams, isMultipart),
582
+ decodePayload.value
583
+ )
584
+ }
585
+ if (decodeHeaders._tag === "Some") {
586
+ request.headers = yield* decodeHeaders.value(httpRequest.headers)
587
+ }
588
+ if (decodeUrlParams._tag === "Some") {
589
+ request.urlParams = yield* decodeUrlParams.value(urlParams)
590
+ }
591
+ const response = isFullResponse
592
+ ? yield* handler(request)
593
+ : yield* Effect.flatMap(handler(request), encodeSuccess)
594
+ return response as HttpServerResponse.HttpServerResponse
595
+ }).pipe(
596
+ Effect.catchIf(ParseResult.isParseError, HttpApiDecodeError.refailParseError)
597
+ )
619
598
  )
620
599
  )
621
600
  }
@@ -152,51 +152,41 @@ export const make = <Groups extends HttpApiGroup.Any, ApiError, ApiR>(
152
152
  readonly payload: any
153
153
  readonly headers: any
154
154
  readonly withResponse?: boolean
155
- }) => {
156
- const url = request && request.path ? makeUrl(request.path) : endpoint.path
157
- const baseRequest = HttpClientRequest.make(endpoint.method)(url)
158
- return (isMultipart ?
159
- Effect.succeed(baseRequest.pipe(
160
- HttpClientRequest.bodyFormData(request.payload)
161
- ))
162
- : encodePayload._tag === "Some"
163
- ? encodePayload.value(request.payload).pipe(
164
- Effect.flatMap((payload) =>
165
- HttpMethod.hasBody(endpoint.method)
166
- ? HttpClientRequest.bodyJson(baseRequest, payload)
167
- : Effect.succeed(HttpClientRequest.setUrlParams(baseRequest, payload as any))
168
- ),
169
- Effect.orDie
155
+ }) =>
156
+ Effect.gen(function*() {
157
+ let httpRequest = HttpClientRequest.make(endpoint.method)(
158
+ request && request.path ? makeUrl(request.path) : endpoint.path
170
159
  )
171
- : Effect.succeed(baseRequest)).pipe(
172
- encodeHeaders._tag === "Some"
173
- ? Effect.flatMap((httpRequest) =>
174
- encodeHeaders.value(request.headers).pipe(
175
- Effect.orDie,
176
- Effect.map((headers) => HttpClientRequest.setHeaders(httpRequest, headers as any))
177
- )
178
- )
179
- : identity,
180
- encodeUrlParams._tag === "Some"
181
- ? Effect.flatMap((httpRequest) =>
182
- encodeUrlParams.value(request.urlParams).pipe(
183
- Effect.orDie,
184
- Effect.map((params) => HttpClientRequest.appendUrlParams(httpRequest, params as any))
185
- )
186
- )
187
- : identity,
188
- Effect.flatMap(httpClient.execute),
189
- Effect.flatMap((response) => {
190
- const value = options?.transformResponse === undefined
191
- ? decodeResponse(response)
192
- : options.transformResponse(decodeResponse(response))
193
- return request?.withResponse === true ? Effect.map(value, (value) => [value, response]) : value
194
- }),
195
- Effect.scoped,
196
- Effect.catchIf(ParseResult.isParseError, Effect.die),
197
- Effect.mapInputContext((input) => Context.merge(context, input))
198
- )
199
- }
160
+ if (isMultipart) {
161
+ httpRequest = HttpClientRequest.bodyFormData(httpRequest, request.payload)
162
+ } else if (encodePayload._tag === "Some") {
163
+ const payload = yield* encodePayload.value(request.payload)
164
+ httpRequest = HttpMethod.hasBody(endpoint.method)
165
+ ? yield* Effect.orDie(HttpClientRequest.bodyJson(httpRequest, payload))
166
+ : HttpClientRequest.setUrlParams(httpRequest, payload as any)
167
+ }
168
+ if (encodeHeaders._tag === "Some") {
169
+ httpRequest = HttpClientRequest.setHeaders(
170
+ httpRequest,
171
+ (yield* encodeHeaders.value(request.headers)) as any
172
+ )
173
+ }
174
+ if (encodeUrlParams._tag === "Some") {
175
+ httpRequest = HttpClientRequest.appendUrlParams(
176
+ httpRequest,
177
+ (yield* encodeUrlParams.value(request.urlParams)) as any
178
+ )
179
+ }
180
+ const response = yield* httpClient.execute(httpRequest)
181
+ const value = yield* (options?.transformResponse === undefined
182
+ ? decodeResponse(response)
183
+ : options.transformResponse(decodeResponse(response)))
184
+ return request?.withResponse === true ? [value, response] : value
185
+ }).pipe(
186
+ Effect.scoped,
187
+ Effect.catchIf(ParseResult.isParseError, Effect.die),
188
+ Effect.mapInputContext((input) => Context.merge(context, input))
189
+ )
200
190
  }
201
191
  })
202
192
  return client as any