import { HttpClient, HttpClientRequest } from '@effect/platform'; import { version } from '@instantdb/version'; import { Context, Data, Effect, Layer, Schema } from 'effect'; import { AuthToken } from '../context/authToken.ts'; import { TimeoutException } from 'effect/Cause'; import { RequestError } from '@effect/platform/HttpClientError'; import { getBaseUrl } from './config.ts'; export { getBaseUrl, getDashUrl } from './config.ts'; export class InstantHttp extends Context.Tag( 'instant-cli/new/lib/http/InstantHttp', )< InstantHttp, HttpClient.HttpClient.With >() {} export class InstantHttpAuthed extends Context.Tag( 'instant-cli/new/lib/http/InstantHttpAuthed', )< InstantHttpAuthed, HttpClient.HttpClient.With >() {} export class InstantHttpError extends Data.TaggedError('InstantHttpError')< { methodAndUrl: string; } & typeof InstantTypicalHttpErrorResponse.Type > {} // Pipe on an http client to set the command header export const withCommand = (command: string) => { return ( client: HttpClient.HttpClient.With< InstantHttpError | TimeoutException | RequestError >, ) => client.pipe( HttpClient.mapRequest((r) => r.pipe(HttpClientRequest.setHeader(`X-Instant-Command`, command)), ), ); }; class InstantTypicalHttpErrorResponse extends Schema.Struct({ message: Schema.String, type: Schema.String.pipe(Schema.optional), hint: Schema.Record({ key: Schema.String, value: Schema.Any }).pipe( Schema.optional, ), }) {} export const InstantHttpLive = Layer.effect( InstantHttp, Effect.gen(function* () { const client = yield* HttpClient.HttpClient; const baseUrl = yield* getBaseUrl; return client.pipe( HttpClient.mapRequest((r) => r.pipe( HttpClientRequest.prependUrl(baseUrl), HttpClientRequest.setHeader('X-Instant-Source', 'instant-cli'), HttpClientRequest.setHeader('X-Instant-Version', version), ), ), HttpClient.transformResponse((r) => r.pipe(Effect.timeout('5 minutes'))), HttpClient.filterStatusOk, // makes non 2xx http codes error // parses the non 2xx errors into known instant error shape HttpClient.transformResponse((r) => r.pipe( Effect.catchTag('ResponseError', (requestError) => Effect.gen(function* () { const jsonBody = yield* requestError.response.json.pipe( Effect.andThen( Schema.decodeUnknown(InstantTypicalHttpErrorResponse), ), Effect.mapError( (e) => new InstantHttpError({ message: 'Error making request to ' + requestError.methodAndUrl, type: e._tag, methodAndUrl: requestError.methodAndUrl, }), ), ); // The hint.message is usually better error message let message = jsonBody.message; if ( jsonBody.hint?.message && typeof jsonBody.hint.message === 'string' ) { message = jsonBody.hint.message; } return yield* new InstantHttpError({ message: message, methodAndUrl: requestError.methodAndUrl, hint: jsonBody.hint, type: jsonBody.type || 'Unknown type', }); }), ), ), ), ); }), ); export const InstantHttpAuthedLive = Layer.effect( InstantHttpAuthed, Effect.gen(function* () { const http = yield* InstantHttp; const authToken = yield* AuthToken; return http.pipe( HttpClient.mapRequestEffect((r) => authToken.getAuthToken.pipe( Effect.map((token) => r.pipe( HttpClientRequest.setHeader('Authorization', `Bearer ${token}`), ), ), ), ), ); }), );