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

Function makeClient

packages/effect/src/unstable/httpapi/HttpApiClient.ts:258–471  ·  view source on GitHub ↗
(
  api: HttpApi.HttpApi<ApiId, Groups>,
  options: {
    readonly httpClient: HttpClient.HttpClient.With<E, R>
    readonly predicate?: Predicate.Predicate<{
      readonly endpoint: HttpApiEndpoint.Top
      readonly group: HttpApiGroup.Top
    }>
    readonly onGroup?: (options: {
      readonly group: HttpApiGroup.Top
      readonly mergedAnnotations: Context.Context<never>
    }) => void
    readonly onEndpoint: (options: {
      readonly group: HttpApiGroup.Top
      readonly endpoint: HttpApiEndpoint.Top
      readonly mergedAnnotations: Context.Context<never>
      readonly middleware: ReadonlySet<HttpApiMiddleware.AnyService>
      readonly successes: ReadonlyMap<number, readonly [Schema.Top, ...Array<Schema.Top>]>
      readonly errors: ReadonlyMap<number, readonly [Schema.Top, ...Array<Schema.Top>]>
      readonly endpointFn: Function
    }) => void
    readonly transformResponse?:
      | ((effect: Effect.Effect<unknown, unknown, unknown>) => Effect.Effect<unknown, unknown, unknown>)
      | undefined
    readonly baseUrl?: URL | string | undefined
  }
)

Source from the content-addressed store, hash-verified

256
257/** @internal */
258export const makeClient = <ApiId extends string, Groups extends HttpApiGroup.Constraint, E, R>(
259 api: HttpApi.HttpApi<ApiId, Groups>,
260 options: {
261 readonly httpClient: HttpClient.HttpClient.With<E, R>
262 readonly predicate?: Predicate.Predicate<{
263 readonly endpoint: HttpApiEndpoint.Top
264 readonly group: HttpApiGroup.Top
265 }>
266 readonly onGroup?: (options: {
267 readonly group: HttpApiGroup.Top
268 readonly mergedAnnotations: Context.Context<never>
269 }) => void
270 readonly onEndpoint: (options: {
271 readonly group: HttpApiGroup.Top
272 readonly endpoint: HttpApiEndpoint.Top
273 readonly mergedAnnotations: Context.Context<never>
274 readonly middleware: ReadonlySet<HttpApiMiddleware.AnyService>
275 readonly successes: ReadonlyMap<number, readonly [Schema.Top, ...Array<Schema.Top>]>
276 readonly errors: ReadonlyMap<number, readonly [Schema.Top, ...Array<Schema.Top>]>
277 readonly endpointFn: Function
278 }) => void
279 readonly transformResponse?:
280 | ((effect: Effect.Effect<unknown, unknown, unknown>) => Effect.Effect<unknown, unknown, unknown>)
281 | undefined
282 readonly baseUrl?: URL | string | undefined
283 }
284): Effect.Effect<void> =>
285 Effect.gen(function*() {
286 const services = yield* Effect.context()
287
288 const httpClient = options.httpClient.pipe(
289 options?.baseUrl === undefined
290 ? identity
291 : HttpClient.mapRequest(
292 HttpClientRequest.prependUrl(options.baseUrl.toString())
293 )
294 )
295
296 function executeMiddleware(
297 group: HttpApiGroup.Top,
298 endpoint: HttpApiEndpoint.Top,
299 request: HttpClientRequest.HttpClientRequest,
300 middlewareKeys: ReadonlyArray<string>,
301 index: number
302 ): Effect.Effect<HttpClientResponse.HttpClientResponse, HttpClientError.HttpClientError> {
303 if (index === -1) {
304 return httpClient.execute(request) as unknown as Effect.Effect<
305 HttpClientResponse.HttpClientResponse,
306 HttpClientError.HttpClientError
307 >
308 }
309 const middleware = services.mapUnsafe.get(middlewareKeys[index]) as
310 | HttpApiMiddleware.HttpApiMiddlewareClient<any, any, any>
311 | undefined
312 if (middleware === undefined) {
313 return executeMiddleware(group, endpoint, request, middlewareKeys, index - 1)
314 }
315 return middleware({

Callers 3

makeWithFunction · 0.70
groupFunction · 0.70
endpointFunction · 0.70

Calls 3

contextMethod · 0.80
pipeMethod · 0.65
toStringMethod · 0.65

Tested by

no test coverage detected