Skip to content

Commit

Permalink
feat: custom fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed Jun 23, 2023
1 parent 2b41169 commit 0c29bbe
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/warm-needles-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@just-web/browser': minor
---

Supports getting `fetch` from optional `FetchGizmo`.
Supports using custom `fetch` from options.
24 changes: 23 additions & 1 deletion plugins/browser/ts/browser_gizmo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -84,3 +84,25 @@ it('provides location', async () => {
testType.equal<typeof app.browser.location, Location>(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({})
})
16 changes: 10 additions & 6 deletions plugins/browser/ts/browser_gizmo.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
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 {
/**
* Prevents the default event handler of `onerror` to be fired.
* @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<
Expand All @@ -31,10 +35,10 @@ export const browserGizmoFn: (options?: BrowserGizmoOptions) => GizmoStatic<
() => () => void
]
> = define((options?: BrowserGizmoOptions) => ({
static: define.require<LogGizmo>(),
async create({ log }) {
static: define.require<LogGizmo>().optional<FetchGizmo>(),
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.
Expand All @@ -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)
Expand Down

0 comments on commit 0c29bbe

Please sign in to comment.