MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / makeApiClient

Function makeApiClient

apps/cli/src/main.ts:1073–1102  ·  view source on GitHub ↗
(connection: ExecutorServerConnection, target: ServerTarget = {})

Source from the content-addressed store, hash-verified

1071// ---------------------------------------------------------------------------
1072
1073const makeApiClient = (connection: ExecutorServerConnection, target: ServerTarget = {}) =>
1074 Effect.gen(function* () {
1075 const headers = yield* resolveExecutorServerRequestHeaders(connection, process.env).pipe(
1076 Effect.mapError(toError),
1077 );
1078 return yield* HttpApiClient.make(ExecutorApi, {
1079 baseUrl: connection.apiBaseUrl,
1080 ...(Object.keys(headers).length > 0
1081 ? {
1082 transformClient: HttpClient.mapRequest((request) =>
1083 HttpClientRequest.setHeaders(request, headers),
1084 ),
1085 }
1086 : {}),
1087 // A 401 on an endpoint that doesn't model it is a sign-in problem: rewrite
1088 // the transport-level error into the login hint. Without this the client
1089 // fails decoding the unexpected status and prints the opaque
1090 // `Decode error (401 GET .../api/tools)`. Declared 401s (typed API errors)
1091 // decode before this catch and pass through untouched.
1092 transformResponse: (effect) =>
1093 Effect.catchIf(
1094 effect,
1095 (cause) => HttpClientError.isHttpClientError(cause) && cause.response?.status === 401,
1096 () =>
1097 Effect.fail(
1098 new Error(describeUnauthorizedCliServer({ connection, cliPrefix, target })),
1099 ),
1100 ),
1101 });
1102 }).pipe(Effect.provide(FetchHttpClient.layer));
1103
1104// ---------------------------------------------------------------------------
1105// Foreground session

Tested by

no test coverage detected