this repo has no description
at main 87 lines 3.1 kB view raw
1import { unwrapOptional as unwrap } from "@jet/environment/types/optional"; 2import { attributeAsArrayOrEmpty } from "../foundation/media/attributes"; 3import { dataFromDataContainer } from "../foundation/media/data-structure"; 4import { deviceFamiliesFromData } from "./content/device-family"; 5/** 6 * Retreive the current `PreviewPlatform`, in the format used to create a {@linkcode WithPlatform} `Intent` 7 * 8 * @example 9 * makeIntentWithPlatform({ 10 * ...getPlatform(objectGraph) 11 * ...otherProps 12 * }); 13 */ 14export function getPlatform(objectGraph) { 15 var _a; 16 return { 17 platform: (_a = objectGraph.activeIntent) === null || _a === void 0 ? void 0 : _a.previewPlatform, 18 }; 19} 20/** 21 * Determine the most relevant {@linkcode PreviewPlatform} from a Media API response that 22 * contains "device families", like an `app` or `app-bundle` 23 */ 24export function inferPreviewPlatformFromDeviceFamilies(objectGraph, response) { 25 const data = unwrap(dataFromDataContainer(objectGraph, response)); 26 const deviceFamilies = new Set(deviceFamiliesFromData(objectGraph, data, true)); 27 if (deviceFamilies.has("iphone")) { 28 return "iphone"; 29 } 30 if (deviceFamilies.has("ipad")) { 31 return "ipad"; 32 } 33 if (deviceFamilies.has("mac")) { 34 return "mac"; 35 } 36 if (deviceFamilies.has("realityDevice")) { 37 return "vision"; 38 } 39 if (deviceFamilies.has("tvos")) { 40 return "tv"; 41 } 42 if (deviceFamilies.has("watch")) { 43 return "watch"; 44 } 45 throw new Error("Could not infer platform from device families"); 46} 47/** 48 * Determine the most relevant {@linkcode PreviewPlatform} from a Media API response that 49 * contains "editorial platforms"", like `editorial-items` 50 */ 51export function inferPreviewPlatformFromEditorialPlatforms(objectGraph, container) { 52 const data = unwrap(dataFromDataContainer(objectGraph, container)); 53 const editorialPlatforms = new Set(attributeAsArrayOrEmpty(data, "editorialPlatforms")); 54 if (editorialPlatforms.has("iphone")) { 55 return "iphone"; 56 } 57 if (editorialPlatforms.has("ipad")) { 58 return "ipad"; 59 } 60 if (editorialPlatforms.has("desktop")) { 61 return "mac"; 62 } 63 if (editorialPlatforms.has("realitydevice")) { 64 return "vision"; 65 } 66 if (editorialPlatforms.has("watch")) { 67 return "watch"; 68 } 69 if (editorialPlatforms.has("appletv")) { 70 return "tv"; 71 } 72 throw new Error("Could not infer preview platform from editorial platforms"); 73} 74/** 75 * Set the `previewPlatform` query param on the {@linkcode request}, if necessary 76 * 77 * This should only be used with requests being made for the following resources: 78 * - "Editorial" (Grouping Page, Editorial Page, Today, etc) 79 * - "Search" 80 */ 81export function setPreviewPlatform(objectGraph, request) { 82 var _a; 83 return ((_a = objectGraph.activeIntent) === null || _a === void 0 ? void 0 : _a.previewPlatform) 84 ? request.addingQuery("previewPlatform", objectGraph.activeIntent.platform).addingQuery("platform", "web") 85 : request; 86} 87//# sourceMappingURL=preview-platform.js.map