| 168 | */ |
| 169 | export const Service = |
| 170 | <Self>() => |
| 171 | <const Id extends string, ApiId extends string, Groups extends HttpApiGroup.Constraint>( |
| 172 | id: Id, |
| 173 | options: { |
| 174 | readonly api: HttpApi.HttpApi<ApiId, Groups> |
| 175 | readonly httpClient: |
| 176 | | Layer.Layer< |
| 177 | | HttpApiGroup.ClientServices<Groups> |
| 178 | | HttpClient.HttpClient |
| 179 | > |
| 180 | | ((get: Atom.AtomContext) => Layer.Layer< |
| 181 | | HttpApiGroup.ClientServices<Groups> |
| 182 | | HttpClient.HttpClient |
| 183 | >) |
| 184 | readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined |
| 185 | readonly transformResponse?: |
| 186 | | ((effect: Effect.Effect<unknown, unknown, unknown>) => Effect.Effect<unknown, unknown, unknown>) |
| 187 | | undefined |
| 188 | readonly baseUrl?: URL | string | undefined |
| 189 | readonly runtime?: Atom.RuntimeFactory | undefined |
| 190 | } |
| 191 | ): AtomHttpApiClient<Self, Id, Groups> => { |
| 192 | const self: Mutable<AtomHttpApiClient<Self, Id, Groups>> = Context.Service< |
| 193 | Self, |
| 194 | HttpApiClient.Client<Groups, never, never> |
| 195 | >()(id) as any |
| 196 | |
| 197 | const layer = Layer.effect( |
| 198 | self, |
| 199 | HttpApiClient.make(options.api, options) |
| 200 | ) |
| 201 | const runtimeFactory = options.runtime ?? Atom.runtime |
| 202 | self.runtime = runtimeFactory( |
| 203 | typeof options.httpClient === "function" ? |
| 204 | (get) => |
| 205 | Layer.provide( |
| 206 | layer, |
| 207 | (options.httpClient as (get: Atom.AtomContext) => Layer.Layer< |
| 208 | | HttpApiGroup.ClientServices<Groups> |
| 209 | | HttpClient.HttpClient |
| 210 | >)(get) |
| 211 | ) as Layer.Layer<Self> : |
| 212 | Layer.provide(layer, options.httpClient) as Layer.Layer<Self> |
| 213 | ) |
| 214 | |
| 215 | const catchErrors = Effect.catch((e: unknown) => |
| 216 | Schema.isSchemaError(e) || HttpClientError.isHttpClientError(e) ? Effect.die(e) : Effect.fail(e) |
| 217 | ) |
| 218 | const groups = options.api.groups as unknown as Record<string, HttpApiGroup.Top> |
| 219 | |
| 220 | const mutationFamily = Atom.family(({ endpoint, group, responseMode }: MutationKey) => { |
| 221 | const atom = self.runtime.fn<{ |
| 222 | params: any |
| 223 | query: any |
| 224 | headers: any |
| 225 | payload: any |
| 226 | reactivityKeys?: ReadonlyArray<unknown> | ReadonlyRecord<string, ReadonlyArray<unknown>> | undefined |
| 227 | }>()( |