MCPcopy Create free account
hub / github.com/Effect-TS/effect / onEndpoint

Function onEndpoint

packages/effect/src/unstable/httpapi/HttpApiClient.ts:330–469  ·  view source on GitHub ↗
(onEndpointOptions)

Source from the content-addressed store, hash-verified

328 options.onGroup?.(onGroupOptions)
329 },
330 onEndpoint(onEndpointOptions) {
331 const { group, endpoint, errors, successes } = onEndpointOptions
332 const makeUrl = compilePath(endpoint.path)
333 const decodeMap: Record<
334 number | "orElse",
335 (response: HttpClientResponse.HttpClientResponse) => Effect.Effect<unknown, unknown, unknown>
336 > = { orElse: statusOrElse }
337 const decodeResponse = HttpClientResponse.matchStatus(decodeMap)
338 const errorAlternatives = new Map<number, Array<ResponseAlternative>>()
339 for (const [status, schemas] of errors.entries()) {
340 const grouped = groupSchemasByContentType(schemas)
341 for (const [contentType, schemas] of grouped.entries()) {
342 addResponseAlternative(errorAlternatives, status, contentType, schemasToResponse(schemas))
343 }
344 }
345 for (const [status, alternatives] of errorAlternatives.entries()) {
346 const decode = makeResponseDecoder(alternatives)
347 decodeMap[status] = (response) =>
348 Effect.flatMap(
349 Effect.catchCause(decode(response), (cause) =>
350 Effect.failCause(Cause.combine(
351 Cause.fail(
352 new HttpClientError.HttpClientError({
353 reason: new HttpClientError.StatusCodeError({
354 request: response.request,
355 response
356 })
357 })
358 ),
359 cause
360 ))),
361 Effect.fail
362 )
363 }
364
365 const successAlternatives = new Map<number, Array<ResponseAlternative>>()
366 for (const [status, schemas] of successes.entries()) {
367 const grouped = groupSchemasByContentType(schemas)
368 for (const [contentType, schemas] of grouped.entries()) {
369 addResponseAlternative(successAlternatives, status, contentType, schemasToResponse(schemas))
370 }
371 }
372 for (const streamSuccess of getStreamSuccessSchemas(endpoint)) {
373 const streamSchema = isWithHeadersStreamSuccess(streamSuccess) ? streamSuccess.schema : streamSuccess
374 addResponseAlternative(
375 successAlternatives,
376 HttpApiSchema.getStatusSuccessSchema(streamSuccess),
377 streamSchema.contentType,
378 streamToResponse(streamSuccess)
379 )
380 }
381 for (const [status, alternatives] of successAlternatives.entries()) {
382 decodeMap[status] = makeResponseDecoder(alternatives)
383 }
384
385 // encoders
386 const encodeParams = UndefinedOr.map(endpoint.params, Schema.encodeUnknownEffect)
387

Callers

nothing calls this directly

Calls 15

compilePathFunction · 0.85
addResponseAlternativeFunction · 0.85
schemasToResponseFunction · 0.85
makeResponseDecoderFunction · 0.85
getStreamSuccessSchemasFunction · 0.85
streamToResponseFunction · 0.85
getEncodePayloadSchemaFunction · 0.85
executeMiddlewareFunction · 0.85
encodeSyncMethod · 0.80
decodeFunction · 0.70

Tested by

no test coverage detected