@segment/analytics-next
Version:
Analytics Next (aka Analytics 2.0) is the latest version of Segment’s JavaScript SDK - enabling you to send your data to any tool without having to learn, test, or use a new API every time.
31 lines (26 loc) • 1.05 kB
text/typescript
import { CDNSettings } from '../..'
import { createSuccess } from '../factories'
import { cdnSettingsMinimal } from './cdn-settings'
export const createMockFetchImplementation = (
cdnSettings: Partial<CDNSettings> = cdnSettingsMinimal
) => {
return (...[url, req]: Parameters<typeof fetch>) => {
const reqUrl = url.toString()
const reqMethod = req?.method?.toLowerCase()
if (!req || (reqMethod === 'get' && reqUrl.includes('cdn.segment.com'))) {
// GET https://cdn.segment.com/v1/projects/{writeKey}
return createSuccess({ ...cdnSettingsMinimal, ...cdnSettings })
}
if (reqMethod === 'post' && reqUrl.includes('api.segment.io')) {
// POST https://api.segment.io/v1/{event.type}
return createSuccess({ success: true }, { status: 201 })
}
if (reqMethod === 'post' && reqUrl.endsWith('/m')) {
// POST https://api.segment.io/m
return createSuccess({ success: true })
}
throw new Error(
`no match found for request (url:${url}, req:${JSON.stringify(req)})`
)
}
}