| 256 | |
| 257 | /** @internal */ |
| 258 | export 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({ |