Skip to content

Commit 69725c9

Browse files
authored
Merge pull request #174 from vimeo/promise-branch
Adds Promise API
2 parents 2fddf7c + 11e407f commit 69725c9

File tree

14 files changed

+1332
-226
lines changed

14 files changed

+1332
-226
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ lib-cov
66
*.out
77
*.pid
88
*.gz
9+
*.lcov
910

1011
pids
1112
logs
@@ -16,3 +17,5 @@ node_modules
1617
package-lock.json
1718

1819
example/config.json
20+
.nyc_output/
21+
.husky/

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [3.0.0] - 2023-02-22
4+
## Added
5+
- Adds Promise API ([#174](https://github.com/vimeo/vimeo.js/pull/174))
6+
37
## [2.3.2] - 2023-01-23
48
## Changed
59
- package.json ([#166](https://github.com/vimeo/vimeo.js/pull/166))
@@ -70,6 +74,7 @@
7074
### Added
7175
- First release.
7276

77+
[3.0.0]: https://github.com/vimeo/vimeo.js/compare/2.3.1...3.0.0
7378
[2.3.1]: https://github.com/vimeo/vimeo.js/compare/2.3.0...2.3.1
7479
[2.3.0]: https://github.com/vimeo/vimeo.js/compare/2.2.0...2.3.0
7580
[2.2.0]: https://github.com/vimeo/vimeo.js/compare/2.1.1...2.2.0

example/auth_example.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
* limitations under the License.
1717
*/
1818

19-
var Vimeo = require('../index').Vimeo
20-
var utilModule = require('util')
19+
const Vimeo = require('../index').Vimeo
20+
const utilModule = require('util')
21+
let config = {}
2122

2223
try {
23-
var config = require('./config.json')
24+
config = require('./config.json')
2425
} catch (error) {
2526
console.error('ERROR: For this example to run properly you must create an API app at ' +
2627
'https://developer.vimeo.com/apps/new and set your callback url to ' +
@@ -30,25 +31,25 @@ try {
3031
process.exit()
3132
}
3233

33-
var httpModule = require('http')
34-
var urlModule = require('url')
34+
const httpModule = require('http')
35+
const urlModule = require('url')
3536

36-
var stateData = {
37+
const stateData = {
3738
state: 'unauthorized'
3839
}
3940

4041
// Here we have to build the Vimeo library using the configured `client_id` and `client_secret`. We
4142
// do not need an access token here because we will generate one. If we already knew our access
4243
// token, we can provide it as the third parameter.
43-
var lib = new Vimeo(config.client_id, config.client_secret, config.access_token)
44+
const lib = new Vimeo(config.client_id, config.client_secret, config.access_token)
4445

45-
var scopes = ['public', 'private', 'edit', 'interact']
46-
var callbackUrl = 'http://localhost:8080/oauth_callback'
46+
const scopes = ['public', 'private', 'edit', 'interact']
47+
const callbackUrl = 'http://localhost:8080/oauth_callback'
4748

4849
// The authorization process requires the user to be redirected back to a webpage, so we can start
4950
// up a simple HTTP server here.
50-
var server = httpModule.createServer(function (request, response) {
51-
var url = urlModule.parse(request.url, true)
51+
const server = httpModule.createServer(function (request, response) {
52+
const url = urlModule.parse(request.url, true) // eslint-disable-line n/no-deprecated-api
5253

5354
// Once the user accepts your app, they will be redirected back to
5455
// `http://localhost:8080/oauth_callback`. If they are not redirected you should check your apps
@@ -121,7 +122,7 @@ server.listen(8080, function () {
121122
console.log('Server started on 8080. Open up http://localhost:8080 in your browser.')
122123
})
123124

124-
var context = require('repl').start({}).context
125+
const context = require('repl').start({}).context
125126

126127
/**
127128
* This will upload the video to the authenticated account.

example/example.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
* limitations under the License.
1717
*/
1818

19-
var Vimeo = require('../index').Vimeo
20-
var util = require('util')
19+
const Vimeo = require('../index').Vimeo
20+
const util = require('util')
21+
let config = {}
2122

2223
try {
23-
var config = require('./config.json')
24+
config = require('./config.json')
2425
} catch (error) {
2526
console.error('ERROR: For this example to run properly you must create an API app at ' +
2627
'https://developer.vimeo.com/apps/new and set your callback url to ' +
@@ -35,7 +36,7 @@ try {
3536
//
3637
// For the request we make below (/channels) the access token can be a client access token instead
3738
// of a user access token.
38-
var lib = new Vimeo(config.client_id, config.client_secret, config.access_token)
39+
const lib = new Vimeo(config.client_id, config.client_secret, config.access_token)
3940

4041
if (config.access_token) {
4142
lib.setAccessToken(config.access_token)

example/search.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
* limitations under the License.
1717
*/
1818

19-
var Vimeo = require('../index').Vimeo
20-
var util = require('util')
19+
const Vimeo = require('../index').Vimeo
20+
const util = require('util')
21+
let config = {}
2122

2223
try {
23-
var config = require('./config.json')
24+
config = require('./config.json')
2425
} catch (error) {
2526
console.error('ERROR: For this example to run properly you must create an API app at ' +
2627
'https://developer.vimeo.com/apps/new and set your callback url to ' +
@@ -59,7 +60,7 @@ function makeRequest (lib) {
5960
})
6061
}
6162

62-
var lib = new Vimeo(config.client_id, config.client_secret, config.access_token)
63+
const lib = new Vimeo(config.client_id, config.client_secret, config.access_token)
6364

6465
if (config.access_token) {
6566
lib.setAccessToken(config.access_token)

example/upload.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
* limitations under the License.
1717
*/
1818

19-
var Vimeo = require('../index').Vimeo
19+
const Vimeo = require('../index').Vimeo
20+
let config = {}
2021

2122
try {
22-
var config = require('./config.json')
23+
config = require('./config.json')
2324
} catch (error) {
2425
console.error('ERROR: For this example to run properly you must create an API app at ' +
2526
'https://developer.vimeo.com/apps/new and set your callback url to ' +
@@ -34,16 +35,16 @@ if (!config.access_token) {
3435
}
3536

3637
// Instantiate the library with your client id, secret and access token (pulled from dev site)
37-
var client = new Vimeo(config.client_id, config.client_secret, config.access_token)
38+
const client = new Vimeo(config.client_id, config.client_secret, config.access_token)
3839

3940
// Create a variable with a hard coded path to your file system
40-
var filePath = '<full path to a video on the filesystem>'
41+
const filePath = '<full path to a video on the filesystem>'
4142

4243
console.log('Uploading: ' + filePath)
4344

44-
var params = {
45-
'name': 'Vimeo API SDK test upload',
46-
'description': "This video was uploaded through the Vimeo API's NodeJS SDK."
45+
const params = {
46+
name: 'Vimeo API SDK test upload',
47+
description: "This video was uploaded through the Vimeo API's NodeJS SDK."
4748
}
4849

4950
client.upload(
@@ -65,8 +66,8 @@ client.upload(
6566
method: 'PATCH',
6667
path: uri,
6768
params: {
68-
'name': 'Vimeo API SDK test edit',
69-
'description': "This video was edited through the Vimeo API's NodeJS SDK."
69+
name: 'Vimeo API SDK test edit',
70+
description: "This video was edited through the Vimeo API's NodeJS SDK."
7071
}
7172
}, function (error, body, statusCode, headers) {
7273
if (error) {
@@ -94,7 +95,7 @@ client.upload(
9495
})
9596
},
9697
function (bytesUploaded, bytesTotal) {
97-
var percentage = (bytesUploaded / bytesTotal * 100).toFixed(2)
98+
const percentage = (bytesUploaded / bytesTotal * 100).toFixed(2)
9899
console.log(bytesUploaded, bytesTotal, percentage + '%')
99100
},
100101
function (error) {

example/upload_promise.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict'
2+
3+
/**
4+
* Copyright 2023 Vimeo
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
const Vimeo = require('../index').Vimeo
20+
21+
let config = {}
22+
23+
try {
24+
config = require('./config.json')
25+
} catch (error) {
26+
console.error('ERROR: For this example to run properly you must create an API app at ' +
27+
'https://developer.vimeo.com/apps/new and set your callback url to ' +
28+
'`http://localhost:8080/oauth_callback`.')
29+
console.error('ERROR: Once you have your app, make a copy of `config.json.example` named ' +
30+
'`config.json` and add your client ID, client secret and access token.')
31+
process.exit()
32+
}
33+
34+
if (!config.access_token) {
35+
throw new Error('You can not upload a video without configuring an access token.')
36+
}
37+
38+
// Instantiate the library with your client id, secret and access token (pulled from dev site)
39+
const client = new Vimeo(config.client_id, config.client_secret, config.access_token)
40+
41+
// Create a variable with a hard coded path to your file system
42+
const filePath = 'path'
43+
44+
const uploadVideo = async (filePath) => {
45+
console.log('Uploading: ' + filePath)
46+
47+
const params = {
48+
name: 'Vimeo API SDK test upload',
49+
description: "This video was uploaded through the Vimeo API's NodeJS SDK."
50+
}
51+
52+
const progressCallback = (bytesUploaded, bytesTotal) => {
53+
const percentage = (bytesUploaded / bytesTotal * 100).toFixed(2)
54+
console.log(bytesUploaded, bytesTotal, percentage + '%')
55+
}
56+
57+
const uri = await client.upload(
58+
filePath,
59+
params,
60+
progressCallback
61+
).catch(e => console.log(e))
62+
63+
// Get the metadata response from the upload and log out the Vimeo.com url
64+
const metadata = await client.request(uri + '?fields=link').catch(e => console.log(e))
65+
66+
console.log('The file has been uploaded to ' + metadata.link)
67+
68+
// Make an API call to edit the title and description of the video.
69+
const editRes = await client.request({
70+
method: 'PATCH',
71+
path: uri,
72+
params: {
73+
name: 'Vimeo API SDK test edit',
74+
description: "This video was edited through the Vimeo API's NodeJS SDK."
75+
}
76+
}).catch(e => console.log(e))
77+
78+
console.log(editRes.statusCode)
79+
console.log('The title and description for ' + uri + ' has been edited')
80+
81+
// Make an API call to see if the video is finished transcoding.
82+
const transcodeStatus = await client.request(uri + '?fields=transcode.status').catch(e => console.log(e))
83+
console.log('The transcode status for ' + uri + ' is: ' + transcodeStatus.body.transcode.status)
84+
}
85+
86+
uploadVideo(filePath)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
'use strict'
2+
3+
/**
4+
* Copyright 2023 Vimeo
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
const fs = require('fs')
20+
const Vimeo = require('../index').Vimeo
21+
let config
22+
23+
try {
24+
config = require('./config.json')
25+
} catch (error) {
26+
console.error('ERROR: For this example to run properly you must create an API app at ' +
27+
'https://developer.vimeo.com/apps/new and set your callback url to ' +
28+
'`http://localhost:8080/oauth_callback`.')
29+
console.error('ERROR: Once you have your app, make a copy of `config.json.example` named ' +
30+
'`config.json` and add your client ID, client secret and access token.')
31+
process.exit()
32+
}
33+
const lib = new Vimeo(config.client_id, config.client_secret, config.access_token)
34+
35+
// Documentation: https://developer.vimeo.com/api/upload/texttracks
36+
37+
const getUploadLink = async (path, token, type = 'captions', language = 'en', name = '') => {
38+
const res = await lib.request(
39+
{
40+
hostname: 'api.vimeo.com',
41+
path,
42+
headers: {
43+
Authorization: 'bearer ' + token,
44+
'Content-Type': 'application/json'
45+
},
46+
method: 'POST',
47+
query: {
48+
type,
49+
language,
50+
name
51+
}
52+
}).catch(e => console.log(e))
53+
console.log(res)
54+
return res
55+
}
56+
57+
const uploadTextTrack = async (textTrackUrl, token, filePath) => {
58+
const vttSubs = fs.readFileSync(
59+
filePath,
60+
'utf-8'
61+
)
62+
63+
const res = await lib.request(
64+
{
65+
hostname: 'captions.cloud.vimeo.com',
66+
path: textTrackUrl.replace('https://captions.cloud.vimeo.com/', ''),
67+
headers: {
68+
Authorization: 'bearer ' + token,
69+
'Content-Type': 'text/plain' // important!
70+
},
71+
method: 'PUT',
72+
body: vttSubs
73+
}
74+
).catch(e => console.log(e))
75+
76+
return res
77+
}
78+
79+
const uploadTextTrackCycle = async () => {
80+
const token = config.access_token
81+
const textTrackUri = '/videos/[video id]/texttracks'
82+
const vttPath = 'path'
83+
84+
const uploadLink = await getUploadLink(textTrackUri, token)
85+
console.log(uploadLink.body.link)
86+
87+
const res = await uploadTextTrack(uploadLink.body.link, token, vttPath)
88+
console.log(res.statusCode)
89+
}
90+
91+
uploadTextTrackCycle()

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var vimeoModule = require('./lib/vimeo')
16+
const vimeoModule = require('./lib/vimeo')
1717
module.exports.vimeo_module = vimeoModule
1818
module.exports.Vimeo = vimeoModule.Vimeo

0 commit comments

Comments
 (0)