@beepbox.net/gofetch-client
v0.3.4
Published
Typesafe Eden treaty client for the gofetch API
Readme
@beepbox.net/gofetch-client
Typesafe Eden treaty client for the gofetch API.
Install
bun add @beepbox.net/gofetch-clientQuick start
import { createClient } from '@beepbox.net/gofetch-client'
const api = createClient()
// defaults to https://gofetch.blaqat.netAPI docs
Live OpenAPI reference: https://gofetch.blaqat.net/docs
Friendly fetcher
cuter wrapper over the normal client for better dev experience
import { createFetcher } from '@beepbox.net/gofetch-client'
// `createFetcher` accepts the same options as `createClient` (`baseUrl`, `token`).
const client = createFetcher()
const { data } = await client.util.resolveShortUrl('https://tinyurl.com/buwnpvpd')
console.log(data?.modName)await client.beep.list({ songType: 'Original', limit: 20 })
await client.beep.get(id)
await client.event.removeHost(eventId, userId)
await client.user.me({ headers: { Authorization: `Bearer ${token}` } })Auth
Browser apps should use the returnTo redirect flow:
- Send the user to
buildLoginUrl({ returnTo: 'http://localhost:3000/callback' }) - Add a
/callbackroute that reads the hash fragment withparseOAuthCallback() - Store the JWT and clear the hash with
clearOAuthCallbackHash()
import {
buildLoginUrl,
clearOAuthCallbackHash,
createClient,
parseOAuthCallback,
} from '@beepbox.net/gofetch-client'
// Login button
window.location.href = buildLoginUrl({
returnTo: `${window.location.origin}/callback`,
})
// On /callback
const result = parseOAuthCallback()
if (result?.ok) {
localStorage.setItem('bds-admin-token', result.accessToken)
clearOAuthCallbackHash()
}
const api = createClient({ token: result?.ok ? result.accessToken : undefined })
const { data: me } = await api.v1.user.me.get()The gofetch server must allow your portal origin via OAUTH_RETURN_ALLOWLIST. Without returnTo, GET /v0/auth/discord/callback still returns JSON for API testing.
Per-request headers also work:
await api.v1.beep.post(body, {
headers: { Authorization: `Bearer ${token}` },
})Test server (NODE_ENV=test): use x-test-api-key with a seeded user id instead of a JWT:
const api = createClient({ baseUrl: 'http://localhost:3001' })
await api.v1.user.me.get({
headers: { 'x-test-api-key': 'seed-user-admin' },
})List / filter beeps
GET /v1/beep — filter by author, platform, song type, state, etc.:
const { data } = await api.v1.beep.get({
query: {
userId: 'some-user-id',
songType: 'Original',
state: 'Complete',
limit: 20,
expand: ['authors', 'platforms'],
},
})
// data.data → beeps
// data.nextCursor → pagination cursorPublic list works without auth (URLs hidden for guests). Authenticated callers see full fields.
Custom base URL
const api = createClient({
baseUrl: 'https://gofetch.blaqat.net',
token: optionalJwt,
})