-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
130 lines (106 loc) · 3.7 KB
/
index.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
const Promise = require('bluebird');
const Client = require('./lib/client');
const defaultVideoTransform = require('./lib/default-video-transform');
const defaultCollectionTransform = require('./lib/default-collection-transform');
const createChannelCache = require('./lib/create-channel-cache');
const fetchJWPlayerVideo = require('./lib/fetch-jwplayer-video');
const fetchJWPlayerCollection = require('./lib/fetch-jwplayer-playlist');
const utils = require('./lib/utils');
const DEFAULTS = {
baseUrl: 'https://api.jwplatform.com',
// baseUrl: 'https://api.jwplayer.com',
collectionTransform: defaultCollectionTransform,
videoTransform: defaultVideoTransform
};
// options.bus
// options.baseUrl
// options.secretKey
// options.apiKey
// options.collectionTransform
// options.assetTransform
exports.initialize = options => {
options = Object.assign({}, DEFAULTS, options || {});
const bus = options.bus;
const baseUrl = options.baseUrl;
const apiKey = options.apiKey;
const secretKey = options.secretKey;
const role = 'provider';
const cmd = 'get';
if (!bus || typeof bus !== 'object') {
throw new Error('oddworks-jwplayer-provider requires an Oddcast Bus');
}
const collectionTransform = options.collectionTransform;
const videoTransform = options.videoTransform;
const client = new Client({bus, baseUrl, secretKey, apiKey});
const getChannel = createChannelCache(bus);
bus.queryHandler(
{role, cmd, source: 'jwplayer-playlist-provider'},
exports.createPlaylistHandler(bus, getChannel, client, collectionTransform)
);
bus.queryHandler(
{role, cmd, source: 'jwplayer-video-provider'},
exports.createVideoHandler(bus, getChannel, client, videoTransform)
);
return Promise.resolve({
name: 'jwplayer-provider',
client
});
};
exports.createPlaylistHandler = (bus, getChannel, client, transform) => {
const getCollection = fetchJWPlayerCollection(bus, client, transform);
// Called from Oddworks core via bus.query
// Expects:
// args.spec.playlist.id
return args => {
const spec = args.spec;
const playlistId = (spec.playlist || {}).key || (spec.playlist || {}).id;
const channelId = spec.channel;
if (!playlistId || typeof playlistId !== 'string') {
throw new Error(
'jwplayer-playlist-provider spec.playlist.key or spec.playlist.id String is required'
);
}
return getChannel(channelId).then(channel => {
return getCollection({spec, channel});
});
};
};
exports.createVideoHandler = (bus, getChannel, client, transform) => {
const getJWPlayerVideo = fetchJWPlayerVideo(bus, client, transform);
// Called from Oddworks core via bus.query
// Expects:
// args.spec.video
return args => {
const spec = args.spec;
const channelId = spec.channel;
const videoId = (spec.video || {}).mediaid || (spec.video || {}).id;
if (!videoId || typeof videoId !== 'string') {
throw new Error(
'jwplayer-video-provider spec.video.mediaid or spec.video.id String is not available'
);
}
return getChannel(channelId).then(channel => {
return getJWPlayerVideo({spec, channel});
});
};
};
// options.secretKey *required
// options.apiKey *required
// options.bus *optional
// options.baseUrl *optional
exports.createClient = options => {
options = Object.assign({}, DEFAULTS, options || {});
const bus = options.bus;
const baseUrl = options.baseUrl;
const secretKey = options.secretKey;
const apiKey = options.apiKey;
if (!apiKey || typeof apiKey !== 'string') {
throw new Error('oddworks-jwplayer-provider requires an jwplayer apiKey key');
}
if (!secretKey || typeof secretKey !== 'string') {
throw new Error('oddworks-jwplayer-provider requires an jwplayer secretKey key');
}
return new Client({bus, baseUrl, secretKey, apiKey});
};
exports.utils = utils;