From 0c29bbe93484a84fd9334098cd6ce6461a217b90 Mon Sep 17 00:00:00 2001 From: Homa Wong Date: Fri, 23 Jun 2023 12:39:50 -0700 Subject: [PATCH] feat: custom fetch --- .changeset/warm-needles-ring.md | 6 ++++++ plugins/browser/ts/browser_gizmo.spec.ts | 24 +++++++++++++++++++++++- plugins/browser/ts/browser_gizmo.ts | 16 ++++++++++------ 3 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 .changeset/warm-needles-ring.md diff --git a/.changeset/warm-needles-ring.md b/.changeset/warm-needles-ring.md new file mode 100644 index 00000000..7d6779ed --- /dev/null +++ b/.changeset/warm-needles-ring.md @@ -0,0 +1,6 @@ +--- +'@just-web/browser': minor +--- + +Supports getting `fetch` from optional `FetchGizmo`. +Supports using custom `fetch` from options. diff --git a/plugins/browser/ts/browser_gizmo.spec.ts b/plugins/browser/ts/browser_gizmo.spec.ts index 18756753..b911c01a 100644 --- a/plugins/browser/ts/browser_gizmo.spec.ts +++ b/plugins/browser/ts/browser_gizmo.spec.ts @@ -3,8 +3,8 @@ import { AssertOrder, a, some, startsWith } from 'assertron' import { logLevels } from 'standard-log' import { stub, testType } from 'type-plus' import { ctx } from './browser_gizmo.ctx.js' -import { throwBrowserError } from './testing/index.js' import { browserGizmoFn } from './index.js' +import { throwBrowserError } from './testing/index.js' afterEach(() => { ctx.addEventListener = addEventListener.bind(window) @@ -84,3 +84,25 @@ it('provides location', async () => { testType.equal(true) expect(app.browser.location).toBeDefined() }) + +it('uses fetch from FetchGizmo if provided', async () => { + const app = await justTestApp() + .merge({ fetch: async () => new Response('{}') }) + .with(browserGizmoFn()) + .create() + const r = await app.fetch('abc') + expect(await r.json()).toEqual({}) +}) + +it('uses fetch from options if provided, over from FetchGizmo', async () => { + const app = await justTestApp() + .merge({ + fetch: async () => { + throw 'should not reach' + } + }) + .with(browserGizmoFn({ fetch: async () => new Response('{}') })) + .create() + const r = await app.fetch('abc') + expect(await r.json()).toEqual({}) +}) diff --git a/plugins/browser/ts/browser_gizmo.ts b/plugins/browser/ts/browser_gizmo.ts index ae4a2ea5..0816f018 100644 --- a/plugins/browser/ts/browser_gizmo.ts +++ b/plugins/browser/ts/browser_gizmo.ts @@ -1,11 +1,11 @@ import { define, type DepBuilder, type GizmoStatic, type LogGizmo } from '@just-web/app' -import type { Fetch } from '@just-web/fetch' +import { type Fetch, type FetchGizmo } from '@just-web/fetch' import { ctx } from './browser_gizmo.ctx.js' import { createErrorStore, toReadonlyErrorStore } from './error_store.js' import type { ReadonlyErrorStore } from './error_store.types.js' import { BrowserError } from './errors.js' -export type { Fetch } +export type { Fetch, FetchGizmo } export interface BrowserGizmoOptions { /** @@ -13,6 +13,10 @@ export interface BrowserGizmoOptions { * @see https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror */ preventDefault?: boolean + /** + * Custom fetch instance + */ + fetch?: Fetch } export const browserGizmoFn: (options?: BrowserGizmoOptions) => GizmoStatic< @@ -31,10 +35,10 @@ export const browserGizmoFn: (options?: BrowserGizmoOptions) => GizmoStatic< () => () => void ] > = define((options?: BrowserGizmoOptions) => ({ - static: define.require(), - async create({ log }) { + static: define.require().optional(), + async create(gizmoCtx) { const errors = createErrorStore() - const logger = log.getLogger(`@just-web/browser`) + const logger = gizmoCtx.log.getLogger(`@just-web/browser`) const preventDefault = options?.preventDefault ?? false // Normally, gizmo should not do work during create. @@ -60,7 +64,7 @@ export const browserGizmoFn: (options?: BrowserGizmoOptions) => GizmoStatic< navigator, location }, - fetch + fetch: options?.fetch ?? gizmoCtx.fetch ?? fetch }, // This is no effect right now as `just-web` does not yet support clean up () => () => ctx.removeEventListener('error', listener)