MCPcopy
hub / github.com/Effect-TS/effect / onEndpoint

Function onEndpoint

packages/platform/src/HttpApiClient.ts:158–242  ·  view source on GitHub ↗
(onEndpointOptions)

Source from the content-addressed store, hash-verified

156 options.onGroup?.(onGroupOptions)
157 },
158 onEndpoint(onEndpointOptions) {
159 const { endpoint, errors, successes } = onEndpointOptions
160 const makeUrl = compilePath(endpoint.path)
161 const decodeMap: Record<
162 number | "orElse",
163 (response: HttpClientResponse.HttpClientResponse) => Effect.Effect<any, any>
164 > = { orElse: statusOrElse }
165 const decodeResponse = HttpClientResponse.matchStatus(decodeMap)
166 errors.forEach(({ ast }, status) => {
167 if (ast._tag === "None") {
168 decodeMap[status] = statusCodeError
169 return
170 }
171 const decode = schemaToResponse(ast.value)
172 decodeMap[status] = (response) => Effect.flatMap(decode(response), Effect.fail)
173 })
174 successes.forEach(({ ast }, status) => {
175 decodeMap[status] = ast._tag === "None" ? responseAsVoid : schemaToResponse(ast.value)
176 })
177 const encodePath = endpoint.pathSchema.pipe(
178 Option.map(Schema.encodeUnknown)
179 )
180 const encodePayloadBody = endpoint.payloadSchema.pipe(
181 Option.map((schema) => {
182 if (HttpMethod.hasBody(endpoint.method)) {
183 return Schema.encodeUnknown(payloadSchemaBody(schema as any))
184 }
185 return Schema.encodeUnknown(schema)
186 })
187 )
188 const encodeHeaders = endpoint.headersSchema.pipe(
189 Option.map(Schema.encodeUnknown)
190 )
191 const encodeUrlParams = endpoint.urlParamsSchema.pipe(
192 Option.map(Schema.encodeUnknown)
193 )
194 const endpointFn = Effect.fnUntraced(function*(request: {
195 readonly path: any
196 readonly urlParams: any
197 readonly payload: any
198 readonly headers: any
199 readonly withResponse?: boolean
200 }) {
201 let httpRequest = HttpClientRequest.make(endpoint.method)(endpoint.path)
202 if (request && request.path) {
203 const encodedPathParams = encodePath._tag === "Some"
204 ? yield* encodePath.value(request.path)
205 : request.path
206 httpRequest = HttpClientRequest.setUrl(httpRequest, makeUrl(encodedPathParams))
207 }
208 if (request && request.payload instanceof FormData) {
209 httpRequest = HttpClientRequest.bodyFormData(httpRequest, request.payload)
210 } else if (encodePayloadBody._tag === "Some") {
211 if (HttpMethod.hasBody(endpoint.method)) {
212 const body = (yield* encodePayloadBody.value(request.payload)) as HttpBody.HttpBody
213 httpRequest = HttpClientRequest.setBody(httpRequest, body)
214 } else {
215 const urlParams = (yield* encodePayloadBody.value(request.payload)) as Record<string, string>

Callers

nothing calls this directly

Calls 13

compilePathFunction · 0.85
payloadSchemaBodyFunction · 0.85
makeUrlFunction · 0.85
setUrlParamsMethod · 0.80
setHeadersMethod · 0.80
mergeMethod · 0.80
schemaToResponseFunction · 0.70
decodeFunction · 0.70
pipeMethod · 0.65
mapMethod · 0.65
makeMethod · 0.65
valueMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…