diff --git a/packages/node/src/__tests__/typedef-tests.ts b/packages/node/src/__tests__/typedef-tests.ts index c7d414b9c..45ef98f55 100644 --- a/packages/node/src/__tests__/typedef-tests.ts +++ b/packages/node/src/__tests__/typedef-tests.ts @@ -1,10 +1,12 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ import { Analytics, Context, Plugin, UserTraits, GroupTraits, - AnalyticsHTTPClientDELETE, + HTTPClient, + FetchHTTPClient, } from '../' /** @@ -21,19 +23,28 @@ export default { analytics.VERSION = 'foo' }, - 'httpClient setting should be compatible with standard fetch and node-fetch interface': + 'httpClient setting should be compatible with standard fetch and node-fetch interface, as well as functions': () => { - const nodeFetchClient: AnalyticsHTTPClientDELETE = { - send: require('node-fetch'), - } - new Analytics({ writeKey: 'foo', httpClient: nodeFetchClient }) - - const standardFetchClient: AnalyticsHTTPClientDELETE = { - send: fetch, - } - new Analytics({ writeKey: 'foo', httpClient: standardFetchClient }) + new Analytics({ writeKey: 'foo', httpClient: require('node-fetch') }) + new Analytics({ writeKey: 'foo', httpClient: globalThis.fetch }) }, + 'Analytics should accept an entire HTTP Client': () => { + class CustomClient implements HTTPClient { + makeRequest = () => Promise.resolve({} as Response) + } + + new Analytics({ + writeKey: 'foo', + httpClient: new CustomClient(), + }) + + new Analytics({ + writeKey: 'foo', + httpClient: new FetchHTTPClient(globalThis.fetch), + }) + }, + 'track/id/pg/screen/grp calls should require either userId or anonymousId': () => { const analytics = new Analytics({ writeKey: 'abc' }) diff --git a/packages/node/src/index.ts b/packages/node/src/index.ts index a4212fee1..ca2a91172 100644 --- a/packages/node/src/index.ts +++ b/packages/node/src/index.ts @@ -1,11 +1,14 @@ export { Analytics } from './app/analytics-node' export { Context } from './app/context' -export type { +export { + HTTPClient, FetchHTTPClient, FetchHTTPClientOptions, - HTTPFetchFn, HTTPFetchClientResponse, + HTTPFetchFn, + HTTPRequestOptions, } from './lib/http-client' + export type { Plugin, GroupTraits, diff --git a/packages/node/src/lib/http-client.ts b/packages/node/src/lib/http-client.ts index 1e0ea7f85..6bdd0a82a 100644 --- a/packages/node/src/lib/http-client.ts +++ b/packages/node/src/lib/http-client.ts @@ -4,11 +4,6 @@ export interface HTTPFetchClientResponse { ok: boolean status: number statusText: string - /** - * Using string, but this is also response type - * "basic" | "cors" | "default" | "error" | "opaque" | "opaqueredirect"; - */ - // type: string } /** @@ -21,10 +16,6 @@ export interface HTTPFetchFn { ): Promise } -export interface HTTPClient { - makeRequest(_options: HTTPRequestOptions): Promise -} - export interface HTTPRequestOptions { url: string method: string @@ -40,6 +31,10 @@ export interface FetchHTTPClientOptions { signal?: any // AbortSignal type does not play nicely with node-fetch } +export interface HTTPClient { + makeRequest(_options: HTTPRequestOptions): Promise +} + export class FetchHTTPClient implements HTTPClient { private _fetch: HTTPFetchFn constructor(fetchFn: HTTPFetchFn | typeof globalThis.fetch) {