Skip to content

Commit

Permalink
refactor: fix delivery api ad schedule typings
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristiaanScheermeijer committed Aug 8, 2024
1 parent de75eb7 commit e7e3e67
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 19 deletions.
6 changes: 3 additions & 3 deletions packages/common/src/services/ApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { filterMediaOffers } from '../utils/entitlements';
import { useConfigStore as ConfigStore } from '../stores/ConfigStore';
import type { GetPlaylistParams, Playlist, PlaylistItem } from '../../types/playlist';
import type { ContentList, GetContentSearchParams } from '../../types/content-list';
import type { AdSchedule } from '../../types/ad-schedule';
import type { DeliveryAdSchedule } from '../../types/ad-schedule';
import type { EpisodeInSeries, EpisodesRes, EpisodesWithPagination, GetSeriesParams, Series } from '../../types/series';
import env from '../env';
import { logError } from '../logger';
Expand Down Expand Up @@ -255,15 +255,15 @@ export default class ApiService {
return this.transformEpisodes(episodesRes, seasonNumber);
};

getAdSchedule = async (id: string | undefined | null): Promise<AdSchedule | undefined> => {
getAdSchedule = async (id: string | undefined | null): Promise<DeliveryAdSchedule | undefined> => {
if (!id) {
throw new Error('Ad Schedule ID is required');
}

const url = env.APP_API_BASE_URL + `/v2/advertising/schedules/${id}.json`;
const response = await fetch(url, { credentials: 'omit' });

return (await getDataOrThrow(response)) as AdSchedule;
return (await getDataOrThrow(response)) as DeliveryAdSchedule;
};

/**
Expand Down
12 changes: 0 additions & 12 deletions packages/common/src/services/ConfigService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { AppError } from '../utils/error';
import type { Config } from '../../types/config';
import env from '../env';

import ApiService from './ApiService';

/**
* Set config setup changes in both config.service.ts and config.d.ts
* */
Expand All @@ -35,12 +33,6 @@ export default class ConfigService {
features: {},
};

private readonly apiService: ApiService;

constructor(apiService: ApiService) {
this.apiService = apiService;
}

private enrichConfig = (config: Config): Config => {
const { content, siteName } = config;
const updatedContent = content.map((content) => Object.assign({ featured: false }, content));
Expand Down Expand Up @@ -70,10 +62,6 @@ export default class ConfigService {
return source;
};

loadAdSchedule = async (adScheduleId: string | undefined | null) => {
return this.apiService.getAdSchedule(adScheduleId);
};

loadConfig = async (configLocation: string | undefined) => {
const i18n = getI18n();

Expand Down
55 changes: 55 additions & 0 deletions packages/common/types/ad-schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,58 @@ export type AdScheduleUrls = {
};

export type AdDeliveryMethod = 'csai' | 'ssai';

type DeliverySchedule = {
tag: string[];
type: 'linear' | 'nonlinear';
offset: 'pre' | 'post' | string; // seconds, timestamp, percentage
skipoffset?: number;
};

type DeliveryRules = {
startOnSeek: 'pre' | 'none' | 'mid';
timeBetweenAds: number;
startOn?: number;
frequency?: number;
};

type DeliveryBids = {
settings: {
bidTimeout: number;
floorPriceCents: number;
mediationLayerAdServer: 'dfp' | 'jwp' | 'jwpdfp' | 'jwpspotx';
disableConsentManagementOnNoCmp?: boolean;
sendAllBids: boolean;
buckets: { min?: number; max?: number; increment?: number }[]; // anyOf
consentManagement: {
usp: {
cmpApi: 'iab' | 'static';
timeout: number;
};
gdpr: {
cmpApi: 'iab' | 'static';
timeout: number;
defaultGdprScope: boolean;
allowAuctionWithoutConsent: boolean;
rules?: {
purpose: 'basicAds' | 'storage' | 'measurement';
enforcePurpose: boolean;
enforceVendor: boolean;
vendorExceptions?: string[];
}[];
};
};
};
ortbParams: {
plcmt: number;
};
};

export type DeliveryAdSchedule = {
rules: DeliveryRules;
schedule: DeliverySchedule[];
bids: DeliveryBids;
client: 'vast' | 'googima';
vpaidmode?: 'enabled' | 'disabled' | 'insecure';
adscheduleid: string;
};
5 changes: 3 additions & 2 deletions packages/hooks-react/src/useAds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useConfigStore } from '@jwp/ott-common/src/stores/ConfigStore';
import ApiService from '@jwp/ott-common/src/services/ApiService';
import { getModule } from '@jwp/ott-common/src/modules/container';
import { createURL } from '@jwp/ott-common/src/utils/urlFormatting';
import type { AdSchedule } from '@jwp/ott-common/types/ad-schedule';

const CACHE_TIME = 60 * 1000 * 20;

Expand Down Expand Up @@ -33,12 +34,12 @@ export const useAds = ({ mediaId }: { mediaId: string }) => {

const { data: adSchedule, isLoading: isAdScheduleLoading } = useLegacyStandaloneAds({ adScheduleId, enabled: !!adScheduleId });
const adConfig = useAdConfigFlow
? {
? ({
client: 'vast',
schedule: createURL(adScheduleUrls?.xml || '', {
media_id: mediaId,
}),
}
} as AdSchedule)
: undefined;

return {
Expand Down
4 changes: 2 additions & 2 deletions packages/ui-react/src/components/Player/Player.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useCallback, useEffect, useRef, useState } from 'react';
import type { AdSchedule } from '@jwp/ott-common/types/ad-schedule';
import type { AdSchedule, DeliveryAdSchedule } from '@jwp/ott-common/types/ad-schedule';
import type { PlaylistItem } from '@jwp/ott-common/types/playlist';
import { useConfigStore } from '@jwp/ott-common/src/stores/ConfigStore';
import { deepCopy } from '@jwp/ott-common/src/utils/collection';
Expand All @@ -20,7 +20,7 @@ type Props = {
item: PlaylistItem;
startTime?: number;
autostart?: boolean;
adsData?: AdSchedule;
adsData?: AdSchedule | DeliveryAdSchedule;
onReady?: (player?: JWPlayer) => void;
onPlay?: () => void;
onPause?: () => void;
Expand Down

0 comments on commit e7e3e67

Please sign in to comment.