Skip to content

Commit fa04b61

Browse files
committed
add Video.Assets.createTrack and Video.Assets.deleteTrack methods
1 parent 0c8e255 commit fa04b61

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

src/video/resources/assets.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,61 @@ class Assets extends Base {
208208
`${buildBasePath(assetId)}/playback-ids/${playbackId}`
209209
);
210210
}
211+
212+
/**
213+
* Create a subtitle text track
214+
* @param {string} assetId - The ID for the asset
215+
* @param {Object} params - subtitle text track JSON parameters
216+
* @returns {Promise} - Returns a resolved Promise with a response from the Mux API
217+
*
218+
* @example
219+
* const { Video } = new Mux(accessToken, secret);
220+
*
221+
* // Create an asset text track
222+
* Video.Assets.createTrack(assetId, {
223+
* url: "https://example.com/myVIdeo_en.srt",
224+
* type: "text",
225+
* text_type: "subtitles",
226+
* language_code: "en-US",
227+
* });
228+
*
229+
* @see https://docs.mux.com/reference#create-a-subtitle-text-track
230+
*/
231+
createTrack(assetId, params) {
232+
if (!assetId) {
233+
return Promise.reject(new Error('An asset ID is required'));
234+
}
235+
236+
if (!params) {
237+
return Promise.reject(new Error('Text track params are required'));
238+
}
239+
return this.http.post(`${buildBasePath(assetId)}/tracks`, params);
240+
}
241+
242+
/**
243+
* Delete an asset text track
244+
* @param {string} assetId - The ID for the asset
245+
* @param {string} trackId - The ID for the asset text track to delete
246+
* @returns {Promise} - Returns a resolved Promise with a response from the Mux API
247+
*
248+
* @example
249+
* const { Video } = new Mux(accessToken, secret);
250+
*
251+
* // Delete an asset text track
252+
* Video.Assets.deleteTrack(assetId, trackId);
253+
*
254+
* @see https://docs.mux.com/reference#delete-a-subtitle-text-track
255+
*/
256+
deleteTrack(assetId, trackId) {
257+
if (!assetId) {
258+
return Promise.reject(new Error('An asset ID is required'));
259+
}
260+
261+
if (!trackId) {
262+
return Promise.reject(new Error('A track ID is required'));
263+
}
264+
return this.http.delete(`${buildBasePath(assetId)}/tracks/${trackId}`);
265+
}
211266
}
212267

213268
module.exports = Assets;

test/unit/video/resources/assets.spec.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,93 @@ describe('Unit::Assets', () => {
328328
expect(err.message).to.equal('A playback ID is required');
329329
}));
330330
});
331+
332+
/** @test {Assets.createTrack} */
333+
describe('Assets.createTrack', () => {
334+
/** @test {Assets.createTrack} */
335+
it('makes a POST request to create a text track for an asset', done => {
336+
moxios.stubRequest(
337+
'https://api.mux.com/video/v1/assets/testAsset/tracks',
338+
{
339+
status: 200,
340+
responseText: '{"data": {"create": true}}',
341+
}
342+
);
343+
344+
const onFulfilled = sinon.spy();
345+
testAssets
346+
.createTrack('testAsset', {
347+
url: 'https://example.com/myVIdeo_en.srt',
348+
type: 'text',
349+
text_type: 'subtitles',
350+
language_code: 'en-US',
351+
})
352+
.then(onFulfilled);
353+
354+
return moxios.wait(() => {
355+
expect(onFulfilled.getCall(0).args[0].create).to.be.true;
356+
done();
357+
});
358+
});
359+
360+
/** @test {Assets.createTrack} */
361+
it('throws an error if an asset id is not given', () =>
362+
testAssets
363+
.createTrack()
364+
.then(res => {
365+
expect(res).to.not.exist;
366+
})
367+
.catch(err => {
368+
expect(err).to.exist;
369+
expect(err.message).to.equal('An asset ID is required');
370+
}));
371+
372+
/** @test {Assets.createTrack} */
373+
it('throws an error if track params are not given', () =>
374+
testAssets
375+
.createTrack('assetid')
376+
.then(res => {
377+
expect(res).to.not.exist;
378+
})
379+
.catch(err => {
380+
expect(err).to.exist;
381+
expect(err.message).to.equal('Text track params are required');
382+
}));
383+
});
384+
385+
/** @test {Assets.deleteTrack} */
386+
describe('Assets.deleteTrack', () => {
387+
/** @test {Assets.deleteTrack} */
388+
it('makes a DELETE request to delete a text track for an asset', done => {
389+
moxios.stubRequest(
390+
'https://api.mux.com/video/v1/assets/testAsset/tracks/testTrackId',
391+
{
392+
status: 200,
393+
responseText: '{"data": {"delete": true}}',
394+
}
395+
);
396+
397+
const onFulfilled = sinon.spy();
398+
testAssets.deleteTrack('testAsset', 'testTrackId').then(onFulfilled);
399+
400+
return moxios.wait(() => {
401+
expect(onFulfilled.getCall(0).args[0].delete).to.be.true;
402+
done();
403+
});
404+
});
405+
406+
/** @test {Assets.deleteTrack} */
407+
it('throws an error if an asset id is not given', () =>
408+
testAssets.deleteTrack().catch(err => {
409+
expect(err).to.exist;
410+
expect(err.message).to.equal('An asset ID is required');
411+
}));
412+
413+
/** @test {Assets.deleteTrack} */
414+
it('throws an error if text track ID is not given', () =>
415+
testAssets.deleteTrack('assetid').catch(err => {
416+
expect(err).to.exist;
417+
expect(err.message).to.equal('A track ID is required');
418+
}));
419+
});
331420
});

0 commit comments

Comments
 (0)