-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathfetch-config.js
More file actions
86 lines (73 loc) · 2.85 KB
/
fetch-config.js
File metadata and controls
86 lines (73 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import pick from 'lodash/pick';
import { defaultProfiles } from 'cloudinary-video-player-profiles';
import { isRawUrl, getCloudinaryUrlPrefix } from '../plugins/cloudinary/url-helpers';
import { CLOUDINARY_CONFIG_PARAM } from '../video-player.const';
import { utf8ToBase64 } from '../utils/utf8Base64';
import { appendQueryParams } from './querystring';
import { convertKeysToSnakeCase } from './object';
const isDefaultProfile = (profileName) => !!defaultProfiles.find(({ name }) => profileName === name);
const getDefaultProfileConfig = (profileName) => {
const profile = defaultProfiles.find(({ name }) => profileName === name);
if (!profile) {
throw new Error(`Default profile with name ${profileName} does not exist`);
}
return profile.config;
};
const getCloudinaryConfigFromOptions = (options) => {
if (options.cloudinaryConfig) {
return options.cloudinaryConfig;
}
const snakeCaseCloudinaryConfig = pick(convertKeysToSnakeCase(options), CLOUDINARY_CONFIG_PARAM);
return Object.assign({}, snakeCaseCloudinaryConfig);
};
const fetchConfig = async (options) => {
const { profile, publicId, type = 'upload', videoConfig, allowUsageReport = true } = options;
if (profile && isDefaultProfile(profile)) {
return getDefaultProfileConfig(profile);
}
const cloudinaryConfig = getCloudinaryConfigFromOptions(options);
const urlPrefix = getCloudinaryUrlPrefix(cloudinaryConfig) + '/_applet_/video_service';
const queryParams = allowUsageReport ? { _s: `vp-${VERSION}` } : null;
let configUrl;
if (profile) {
configUrl = isRawUrl(profile)
? profile
: appendQueryParams(`${urlPrefix}/video_player_profiles/${profile.replaceAll(' ', '+')}.json`, queryParams);
} else if (publicId && videoConfig !== false) {
configUrl = appendQueryParams(`${urlPrefix}/video_player_config/video/${type}/${utf8ToBase64(publicId)}.json`, queryParams);
} else {
return {};
}
return fetch(configUrl, { method: 'GET' }).then(res => {
if (!res.ok) {
// fail silently
return {};
}
return res.json();
});
};
export const fetchAndMergeConfig = async (options) => {
const profileOptions = await fetchConfig(options);
const fetchedConfig = profileOptions.playerOptions ? Object.keys(profileOptions.playerOptions) : [];
const profileAnalytics = {
_internalAnalyticsMetadata: {
newPlayerMethod: true,
...(options.profile ? {
profile: isDefaultProfile(options.profile) ? options.profile : true
} : {}),
...(!options.profile && options.videoConfig !== false && options.publicId ? {
videoConfig: true
} : {}),
...(fetchedConfig.length > 0 ? {
fetchedConfig: fetchedConfig.join(',')
} : {})
}
};
return Object.assign(
{},
profileOptions.playerOptions || {},
profileOptions.sourceOptions || {},
options,
profileAnalytics
);
};