-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
118 lines (95 loc) · 3.07 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
'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 fetchVimeoVideo = require('./lib/fetch-vimeo-video');
const fetchVimeoAlbum = require('./lib/fetch-vimeo-album');
const DEFAULTS = {
collectionTransform: defaultCollectionTransform,
videoTransform: defaultVideoTransform
};
// options.bus
// options.accessToken
// options.collectionTransform
// options.videoTransform
exports.initialize = options => {
options = Object.assign({}, DEFAULTS, options || {});
const bus = options.bus;
const accessToken = options.accessToken;
const role = 'provider';
const cmd = 'get';
if (!bus || typeof bus !== 'object') {
throw new Error('oddworks-vimeo-provider requires an Oddcast Bus');
}
const collectionTransform = options.collectionTransform;
const videoTransform = options.videoTransform;
const client = new Client({bus, accessToken});
const getChannel = createChannelCache(bus);
bus.queryHandler(
{role, cmd, source: 'vimeo-album'},
exports.createAlbumHandler(bus, getChannel, client, collectionTransform)
);
bus.queryHandler(
{role, cmd, source: 'vimeo-video'},
exports.createVideoHandler(bus, getChannel, client, videoTransform)
);
return Promise.resolve({
name: 'vimeo-provider',
client
});
};
exports.createAlbumHandler = (bus, getChannel, client, transform) => {
const getCollection = fetchVimeoAlbum(bus, client, transform);
// Called from Oddworks core via bus.query
// Expects:
// args.spec.album.uri
return args => {
const spec = args.spec;
const collection = spec.album || {};
const albumUri = collection.uri;
const channelId = spec.channel;
if (!albumUri || typeof albumUri !== 'string') {
throw new Error(
'vimeo-album-provider spec.album.uri String is required'
);
}
return getChannel(channelId).then(channel => {
return getCollection({spec, channel, collection, albumUri});
});
};
};
exports.createVideoHandler = (bus, getChannel, client, transform) => {
const getVideo = fetchVimeoVideo(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 video = spec.video || {};
const videoUri = video.uri;
if (!videoUri || typeof videoUri !== 'string') {
throw new Error(
'vimeo-video-provider spec.video.uri String is required'
);
}
return getChannel(channelId).then(channel => {
return getVideo({spec, channel, videoUri});
});
};
};
// options.accessToken *required
// options.bus *optional
exports.createClient = options => {
options = Object.assign({}, DEFAULTS, options || {});
const bus = options.bus;
const accessToken = options.accessToken;
if (!accessToken || typeof accessToken !== 'string') {
throw new Error(
'oddworks-vimeo-provider requires a Vimeo accessToken'
);
}
return new Client({bus, accessToken});
};