diff --git a/src/api/fetchContent.ts b/src/api/fetchContent.ts index 7fc50504..79a58652 100644 --- a/src/api/fetchContent.ts +++ b/src/api/fetchContent.ts @@ -10,6 +10,8 @@ import {JsonObject, JsonValue} from '../sdk/json'; import {FetchResponse} from '../plug'; import {SlotContent, VersionedSlotId} from '../slot'; +export {FetchResponse} from '../plug'; + type FetchingOptions = { baseEndpointUrl?: string, fallback?: T, diff --git a/src/eap.ts b/src/eap.ts deleted file mode 100644 index f25e0374..00000000 --- a/src/eap.ts +++ /dev/null @@ -1,17 +0,0 @@ -import {VersionedSlotId} from './slot'; -import {JsonObject} from './sdk/json'; -import {LegacyFetchResponse, Plug} from './plug'; - -export interface EapFeatures { - fetch

(this: Plug, slotId: I): Promise>; -} - -interface EapHooks extends EapFeatures{ - initialize(this: Plug): void; -} - -declare global { - interface Window { - croctEap?: Partial; - } -} diff --git a/src/plug.ts b/src/plug.ts index 2e0d9960..1a5a8b35 100644 --- a/src/plug.ts +++ b/src/plug.ts @@ -19,7 +19,6 @@ import {Plugin, PluginArguments, PluginFactory} from './plugin'; import {CDN_URL} from './constants'; import {factory as playgroundPluginFactory} from './playground'; import {factory as previewPluginFactory} from './preview'; -import {EapFeatures} from './eap'; import {VersionedSlotId, SlotContent} from './slot'; import {JsonValue, JsonObject} from './sdk/json'; @@ -37,17 +36,7 @@ export type FetchResponse, }; -/** - * @internal - */ -export type LegacyFetchResponse = FetchResponse & { - /** - * @deprecated Use `content` instead. - */ - payload: SlotContent, -}; - -export interface Plug extends EapFeatures { +export interface Plug { readonly tracker: TrackerFacade; readonly user: UserFacade; readonly session: SessionFacade; @@ -76,7 +65,7 @@ export interface Plug extends EapFeatures { fetch

( slotId: I, options?: FetchOptions - ): Promise>; + ): Promise>; unplug(): Promise; } @@ -268,12 +257,6 @@ export class GlobalPlug implements Plug { ); } - const initializeEap = window.croctEap?.initialize; - - if (typeof initializeEap === 'function') { - initializeEap.call(this); - } - Promise.all(pending) .then(() => { this.initialize(); @@ -377,39 +360,21 @@ export class GlobalPlug implements Plug { .then(result => result === true); } - public get fetch(): Plug['fetch'] { - return this.eap( - 'fetch', - ( - slotId: I, - options: FetchOptions = {}, - ): Promise> => { - const [id, version = 'latest'] = slotId.split('@') as [string, `${number}` | 'latest' | undefined]; - const logger = this.sdk.getLogger(); + public fetch( + slotId: I, + options: FetchOptions = {}, + ): Promise> { + const [id, version = 'latest'] = slotId.split('@') as [string, `${number}` | 'latest' | undefined]; + const logger = this.sdk.getLogger(); - return this.sdk - .contentFetcher - .fetch>(id, version === 'latest' ? options : {...options, version: version}) - .then( - response => ({ - get payload(): SlotContent { - logger.warn( - 'Accessing the "payload" property of the fetch response is deprecated' - + ' and will be removed in a future version. Use the "content" property instead.', - ); - - return response.content; - }, - content: response.content, - }), - ) - .catch(error => { - logger.error(`Failed to fetch content for slot "${id}@${version}": ${formatCause(error)}`); - - throw error; - }); - }, - ); + return this.sdk + .contentFetcher + .fetch>(id, version === 'latest' ? options : {...options, version: version}) + .catch(error => { + logger.error(`Failed to fetch content for slot "${id}@${version}": ${formatCause(error)}`); + + throw error; + }); } public async unplug(): Promise { @@ -459,25 +424,4 @@ export class GlobalPlug implements Plug { logger.info('🔌 Croct has been unplugged.'); } } - - private eap(feature: T, api: EapFeatures[T]): EapFeatures[T] { - const eap = window.croctEap; - const method: EapFeatures[T] | undefined = typeof eap === 'object' ? eap[feature] : undefined; - - if (typeof method !== 'function') { - return api; - } - - return method.bind( - new Proxy(this, { - get: (plug, property): any => { - if (property === feature) { - return api; - } - - return plug[property as keyof GlobalPlug]; - }, - }), - ); - } } diff --git a/test/plug.test.ts b/test/plug.test.ts index 5a6c2667..02dd79df 100644 --- a/test/plug.test.ts +++ b/test/plug.test.ts @@ -3,7 +3,7 @@ import {FetchOptions} from '@croct/sdk/facade/contentFetcherFacade'; import {JsonObject} from '@croct/json'; import {Logger} from '../src/sdk'; import {Plugin, PluginFactory} from '../src/plugin'; -import {GlobalPlug, Plug} from '../src/plug'; +import {GlobalPlug} from '../src/plug'; import {CDN_URL} from '../src/constants'; import {Token} from '../src/sdk/token'; @@ -27,8 +27,6 @@ describe('The Croct plug', () => { delete process.env.NODE_ENV; croct = new GlobalPlug(); - - delete window.croctEap; }); afterEach(async () => { @@ -218,18 +216,6 @@ describe('The Croct plug', () => { expect(initialize).toHaveBeenCalledWith(expect.objectContaining({test: false})); }); - it('should call the EAP initialization hook', () => { - window.croctEap = { - initialize: jest.fn().mockImplementation(function initialize(this: Plug) { - expect(this).toBe(croct); - }), - }; - - croct.plug({appId: APP_ID}); - - expect(window.croctEap.initialize).toHaveBeenCalled(); - }); - it('should log failures initializing plugins', () => { croct.extend('foo', () => { throw new Error('Failure'); @@ -891,7 +877,6 @@ describe('The Croct plug', () => { await expect(croct.fetch(slotId, options)).resolves.toEqual({ content: content, - payload: content, }); expect(fetch).toHaveBeenLastCalledWith('foo', options); @@ -954,7 +939,6 @@ describe('The Croct plug', () => { await expect(croct.fetch(slotId, options)).resolves.toEqual({ content: content, - payload: content, }); expect(fetch).toHaveBeenLastCalledWith('foo', { @@ -986,72 +970,11 @@ describe('The Croct plug', () => { await expect(croct.fetch(slotId, options)).resolves.toEqual({ content: content, - payload: content, }); expect(fetch).toHaveBeenLastCalledWith('foo', options); }); - it('should delegate the fetch to the EAP hook', async () => { - const logger: Logger = { - debug: jest.fn(), - info: jest.fn(), - warn: jest.fn(), - error: jest.fn(), - }; - - const config: SdkFacadeConfiguration = { - appId: APP_ID, - logger: logger, - }; - - const sdkFacade = SdkFacade.init(config); - - const initialize = jest.spyOn(SdkFacade, 'init').mockReturnValue(sdkFacade); - - croct.plug(config); - - expect(initialize).toHaveBeenCalledWith(expect.objectContaining(config)); - - const content: JsonObject = { - title: 'Hello World', - }; - - const fetch = jest.spyOn(sdkFacade.contentFetcher, 'fetch').mockResolvedValue({ - content: content, - }); - - const eapFetch: Plug['fetch'] = jest.fn().mockImplementation(function hook(this: Plug, ...args: any[]) { - if (!this.initialized) { - // Access the initialized property to ensure the proxy is working. - throw new Error('Croct is not plugged in.'); - } - - // eslint-disable-next-line prefer-spread -- Necessary to test the hook. - return this.fetch.apply(this, args); - }); - - window.croctEap = { - fetch: eapFetch, - }; - - const slotId = 'foo'; - const options: FetchOptions = {timeout: 5}; - - await expect(croct.fetch(slotId, options)).resolves.toEqual({ - content: content, - payload: content, - }); - - expect(eapFetch).toHaveBeenLastCalledWith('foo', options); - expect(fetch).toHaveBeenLastCalledWith('foo', options); - - expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining( - 'Accessing the "payload" property of the fetch response is deprecated and ' - + 'will be removed in a future version. Use the "content" property instead.', - )); - }); - it('should fail to fetch a slot content if unplugged', () => { expect(() => croct.fetch('foo')).toThrow('Croct is not plugged in.'); });