-
Notifications
You must be signed in to change notification settings - Fork 180
/
media.ts
175 lines (164 loc) · 5.44 KB
/
media.ts
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { Client, AuthenticationType, APILink } from '@vonage/server-client';
import {
MediaItem,
MediaParameters,
MediaItemPageResponse,
MediaItemResponse,
} from './types';
const apiToSdk = (media: MediaItemResponse): MediaItem =>
Client.transformers.camelCaseObjectKeys(media);
/**
* Client class to interact with the Media API which enables users to manage
* their media items programmatically.
*
* @remarks
* This client is only available as a standalone client. It cannot be
* instantiated from the server-sdk package.
*
* @example
* Create a standalone Secret client
*
* ```ts
* import { Media } from '@vonage/media';
*
* const mediaClient = new Media({
* apiKey: VONAGE_API_KEY,
* apiSecret: VONAGE_API_SECRET
* });
* ```
*/
export class Media extends Client {
protected authType = AuthenticationType.JWT;
/**
* Retrieves a paginated list of media items, yielding each item sequentially.
*
* @param {MediaParameters} params - Optional parameters for customizing the media list request.
* @return {AsyncGenerator<MediaItem, void, undefined>} An asynchronous generator that yields {@link MediaItem} objects.
*
* @example
* List all media items
* ```ts
* for await (const media of mediaClient.listAllMediaItems()) {
* console.log(`Media item ${media.id} is ${media.public ? 'public' : 'private'}`);
* console.log(` - Title: ${media.title}`);
* console.log(` - Description: ${media.description}`);
* };
* ```
*
* @example
* List all public media items
* ```ts
* for await (const media of mediaClient.listAllMediaItems({ public: true })) {
* console.log(`Media item ${media.id} is public`);
* console.log(` - Title: ${media.title}`);
* console.log(` - Description: ${media.description}`);
* };
* ```
*/
async *listAllMediaItems(
params: MediaParameters = {},
): AsyncGenerator<MediaItem, void & MediaItem, undefined> {
let pageIndex = params.pageIndex || 0;
let next: APILink | undefined;
do {
params.pageIndex = pageIndex;
const resp = await this.getMediaPage(params);
yield* resp._embedded?.media?.map(apiToSdk);
pageIndex++;
next = resp?._links?.next;
} while (next);
}
/**
* Retrieves a page of media items based on the specified parameters.
*
* @param {MediaParameters} params - Optional parameters for customizing the media page request.
* @return {Promise<MediaItemPageResponse>} A promise that resolves to a {@link MediaItemPageResponse} object representing the page of media items.
*
* @example
* List the first page of media items
*
* ```ts
* const resp = await mediaClient.getMediaPage();
*
* console.log(`There are ${resp.count} media items in total`);
* console.log(`Showing ${resp._embedded.media.length} media items on this page`);
* ```
*/
async getMediaPage(
params: MediaParameters = {},
): Promise<MediaItemPageResponse> {
const resp = await this.sendGetRequest<MediaItemPageResponse>(
`${this.config.apiHost}/v3/media`,
Client.transformers.snakeCaseObjectKeys(params, true),
);
return resp.data;
}
/**
* Retrieves information about a specific media item by its unique identifier.
*
* @param {string} mediaId - The unique identifier of the media item.
* @return {Promise<MediaItem>} A promise that resolves to a {@link MediaItem} object representing the retrieved media item.
*
* @example
* Retrieve a media item by its ID
*
* ```ts
* const media = await mediaClient.getMediaItem('my-media-id');
* console.log(`Media item ${media.id} is ${media.public ? 'public' : 'private'}`);
* console.log(` - Title: ${media.title}`);
* console.log(` - Description: ${media.description}`);
* ```
*/
async getMediaItem(mediaId: string): Promise<MediaItem> {
const resp = await this.sendGetRequest<MediaItemResponse>(
`${this.config.apiHost}/v3/media/${mediaId}/info`,
);
return apiToSdk(resp.data);
}
/**
* Updates the information of a specific media item based on the provided data.
*
* @param {MediaItem} media - The updated media item data.
* @return {Promise<void>} A promise that resolves once the media item is successfully updated.
*
* @example
* Update a media item
*
* ```ts
* const media = await mediaClient.getMediaItem('my-media-id');
* media.title = 'My new title';
* media.description = 'My new description';
* await mediaClient.updateMediaItem(media);
* ```
*/
async updateMediaItem(media: MediaItem): Promise<void> {
await this.sendPutRequest(
`${this.config.apiHost}/v3/media/${media.id}`,
{
public: media.public,
metadata_primary: media.metadataPrimary,
metadata_secondary: media.metadataSecondary,
title: media.title,
description: media.description,
mime_type: media.mimeType,
max_downloads_allowed: media.maxDownloadsAllowed,
},
);
}
/**
* Deletes a specific media item by its unique identifier.
*
* @param {string} mediaId - The unique identifier of the media item to be deleted.
* @return {Promise<void>} A promise that resolves once the media item is successfully deleted.
*
* @example
* Delete a media item
*
* ```ts
* await mediaClient.deleteMediaItem('my-media-id');
* ```
*/
async deleteMediaItem(mediaId: string): Promise<void> {
await this.sendDeleteRequest(`${this.config.apiHost}/v3/media/${mediaId}`);
}
}