Skip to content

Commit

Permalink
Rename fetch types and allow null payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos authored Feb 11, 2021
1 parent a8c4a24 commit 55d6e4c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/eap.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {JsonObject} from '@croct/sdk/json';
import {ContentId, FetchResponse} from './fetch';
import {SlotId, FetchResponse} from './fetch';
import {NullableJsonObject} from './sdk/json';

export interface EapFeatures {
fetch<P extends JsonObject, I extends ContentId = ContentId>(contentId: I): Promise<FetchResponse<I, P>>;
fetch<P extends NullableJsonObject, I extends SlotId = SlotId>(slotId: I): Promise<FetchResponse<I, P>>;
}

declare global {
Expand Down
12 changes: 6 additions & 6 deletions src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {JsonObject} from './sdk/json';
import {NullableJsonObject} from './sdk/json';

export interface ContentMap {
export interface SlotMap {
}

export type ContentId = keyof ContentMap extends never ? string : keyof ContentMap;
export type SlotId = keyof SlotMap extends never ? string : keyof SlotMap;

export type ContentPayload<I extends ContentId> = I extends keyof ContentMap ? ContentMap[I] : JsonObject;
export type SlotContent<I extends SlotId> = I extends keyof SlotMap ? SlotMap[I] : NullableJsonObject;

export type FetchResponse<I extends ContentId, P extends JsonObject = JsonObject> = {
payload: ContentPayload<I> & P,
export type FetchResponse<I extends SlotId, P extends NullableJsonObject = NullableJsonObject> = {
payload: SlotContent<I> & P,
};
8 changes: 4 additions & 4 deletions src/plug.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Logger} from '@croct/sdk/logging';
import {JsonObject, JsonValue} from '@croct/sdk/json';
import SessionFacade from '@croct/sdk/facade/sessionFacade';
import UserFacade from '@croct/sdk/facade/userFacade';
import TrackerFacade from '@croct/sdk/facade/trackerFacade';
Expand All @@ -19,7 +18,8 @@ import {Plugin, PluginArguments, PluginFactory} from './plugin';
import {CDN_URL} from './constants';
import {factory as playgroundPluginFactory} from './playground';
import {EapFeatures} from './eap';
import {ContentId, FetchResponse} from './fetch';
import {SlotId, FetchResponse} from './fetch';
import {NullableJsonObject, JsonValue} from './sdk/json';

export interface PluginConfigurations {
[key: string]: any;
Expand Down Expand Up @@ -309,8 +309,8 @@ export class GlobalPlug implements Plug {
/**
* This API is unstable and subject to change in future releases.
*/
public fetch<P extends JsonObject, I extends ContentId = ContentId>(contentId: I): Promise<FetchResponse<I, P>> {
return this.eap('fetch')(contentId);
public fetch<P extends NullableJsonObject, I extends SlotId = SlotId>(slotId: I): Promise<FetchResponse<I, P>> {
return this.eap('fetch')(slotId);
}

public async unplug(): Promise<void> {
Expand Down
3 changes: 3 additions & 0 deletions src/sdk/json.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
import {JsonObject} from '@croct/sdk/json';

export * from '@croct/sdk/json';
export type NullableJsonObject = JsonObject | null;
24 changes: 13 additions & 11 deletions test/plug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ describe('The Croct plug', () => {

beforeEach(() => {
croct = new GlobalPlug();

delete window.croctEap;
});

afterEach(async () => {
Expand Down Expand Up @@ -807,20 +809,11 @@ describe('The Croct plug', () => {
expect(close).toHaveBeenCalled();
});

test('should fail to fetch a content if unplugged', () => {
test('should fail to fetch a slot content if unplugged', () => {
expect(() => croct.fetch('foo')).toThrow('Croct is not plugged in.');
});

test('should log a warning message when using EAP features', () => {
croct.plug({appId: APP_ID});

expect(() => croct.fetch('foo')).toThrow(
'The fetch feature is currently available only to accounts participating '
+ 'in our Early-Access Program (EAP).',
);
});

test('should fail to fetch a content if the fetch method is undefined', () => {
test('should fail to fetch a slot content if the fetch method is undefined', () => {
window.croctEap = {
fetch: undefined,
};
Expand Down Expand Up @@ -860,4 +853,13 @@ describe('The Croct plug', () => {
'The fetch API is still unstable and subject to change in future releases.',
));
});

test('should log a warning message when using EAP features', () => {
croct.plug({appId: APP_ID});

expect(() => croct.fetch('foo')).toThrow(
'The fetch feature is currently available only to accounts participating '
+ 'in our Early-Access Program (EAP).',
);
});
});

0 comments on commit 55d6e4c

Please sign in to comment.