(
options?: { messages?: Record<string, string> | Record<string, MessageFormatElement[]>; toasts: any[] }
)
| 273 | } |
| 274 | |
| 275 | export const useClient = ( |
| 276 | options?: { messages?: Record<string, string> | Record<string, MessageFormatElement[]>; toasts: any[] } |
| 277 | ) => { |
| 278 | const FakeIntlLayer = fakeIntlLayer(options?.messages) |
| 279 | const FakeToastLayer = fakeToastLayer(options?.toasts) |
| 280 | const CommanderLayer = Commander.Default.pipe(Layer.provide([FakeIntlLayer, FakeToastLayer])) |
| 281 | const WithToastLayer = WithToast.Default.pipe(Layer.provide(FakeToastLayer)) |
| 282 | const api = ApiClientFactory.layer({ url: "bogus", headers: Option.none() }).pipe( |
| 283 | Layer.provide(FetchHttpClient.layer) |
| 284 | ) |
| 285 | const layers = Layer.mergeAll(CommanderLayer, WithToastLayer, FakeToastLayer, FakeIntlLayer, api) |
| 286 | |
| 287 | const clientFor_ = ApiClientFactory.makeFor(Layer.empty) |
| 288 | const rawClient = makeClient(() => ManagedRuntime.make(layers), clientFor_, Layer.empty) |
| 289 | |
| 290 | // Provide a Vue injection context so composition-API hooks called during client |
| 291 | // initialisation work outside a component setup() function. |
| 292 | const vueApp = createApp({}) |
| 293 | |
| 294 | const origClientFor = rawClient.clientFor |
| 295 | const clientFor: typeof origClientFor = function(m, ...args) { |
| 296 | const proxy = origClientFor(m, ...args) |
| 297 | // Warm up lazy mutation-hook initialisation inside the Vue injection context. |
| 298 | // After the first property access, useMutation() is cached and subsequent |
| 299 | // accesses outside the context succeed. |
| 300 | const firstPropertyName = Object.keys(m)[0] |
| 301 | if (firstPropertyName !== undefined) { |
| 302 | vueApp.runWithContext(() => { |
| 303 | void (proxy as Record<string, unknown>)[firstPropertyName] |
| 304 | }) |
| 305 | } |
| 306 | return proxy |
| 307 | } |
| 308 | |
| 309 | return { ...rawClient, clientFor } |
| 310 | } |
no test coverage detected
searching dependent graphs…