@vonage/applications
Version:
Vonage Applications API
208 lines • 7.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Applications = void 0;
const server_client_1 = require("@vonage/server-client");
const apiToApplication = (response) => {
delete response._links;
return server_client_1.Client.transformers.camelCaseObjectKeys(response, true, true);
};
class Applications extends server_client_1.Client {
authType = server_client_1.AuthenticationType.BASIC;
/**
* Retrieves a list of applications with optional pagination parameters.
*
* @remarks
* This is used to get a specific page of applications. This will
* return the `snake_case` and the `camelCase` response. Using `snake_case`
* is considered deprecated
*
* @see API Specification {@link https://developer.vonage.com/en/api/application.v2#listApplication}
*
* @param {ListApplicationParams} params - The filter parameters.
* @return {Promise<ApplicationPageResponse>} - A promise resolving to the list of applications.
*
* @example
* List a single page of applications
*
* ```ts
* const applications = await applicationClient.listApplications({});
*
* applications.applications.forEach(application => {
* console.log(application.name);
* });
* ```
*/
async listApplications(params) {
return this.getApplicationPage(params);
}
/**
* Retrieves all applications, iterating over paginated results.
*
* @remarks
* This will keep calling the API until there are no pages left. This will
* return the `snake_case` and the `camelCase` response. Using `snake_case`
* is considered deprecated
*
* @param {ListApplicationParams} [params={}] - Optional filter parameters.
* @yields {MergedApplication} - Yields application items.
* @return {AsyncGenerator<MergedApplication, void, undefined>} - An asynchronous generator.
*
* @example
* List applications with pagination using an iterator
*
* ```ts
* for await (const application of applicationClient.listAllApplications()) {
* console.log(application.name);
* }
* ```
*/
async *listAllApplications(params = {}) {
let next = null;
params.page = params?.page || 1;
do {
const resp = await this.getApplicationPage(params);
yield* resp._embedded?.applications || [];
next = resp._links?.next;
params.page++;
} while (next);
}
/**
* Retrieves a page of applications based on filter parameters.
*
* @param {ListApplicationParams} filter - The filter parameters for pagination.
* @return {Promise<ApplicationPageList>} - A promise resolving to a page of applications.
*
* @remarks
* This will return the `snake_case` and the `camelCase` response. Using
* `snake_case` is considered deprecated
*
* @see API Specification {@link https://developer.vonage.com/en/api/application.v2#listApplication}
*
* @example
* Get a single page of applications
*
* ```ts
* const applications = await applicationClient.getApplicationPage({
* page: 1,
* size: 10
* });
*
* applications.applications.forEach(application => {
* console.log(application.name);
* });
* ```
*/
async getApplicationPage(filter) {
const resp = await this.sendGetRequest(`${this.config.apiHost}/v2/applications`, server_client_1.Client.transformers.snakeCaseObjectKeys(filter));
if (resp.data._embedded?.applications) {
resp.data._embedded.applications
= resp.data._embedded.applications.map(apiToApplication);
}
return {
...resp.data,
totalItems: resp.data.total_items,
totalPages: resp.data.total_pages,
pageSize: resp.data.page_size,
};
}
/**
* Creates a new application with the provided details.
*
* @see API Specification {@link https://developer.vonage.com/en/api/application.v2#createApplication}
*
* @param {Application} application - The application details to be created.
* @return {Promise<MergedApplication>} - A promise resolving to the created application.
*
* @example
* Create a new application
*
* ```ts
* const application = await applicationClient.createApplication({
* name: 'My Application',
* capabilities: {
* voice: {
* webhooks: {
* answerUrl: {
* address: 'https://example.com/answer',
* httpMethod: 'GET'
* },
* eventUrl: {
* address: 'https://example.com/event',
* httpMethod: 'POST'
* }
* }
* }
* }
* });
*
* console.log(application.id);
* ```
*/
async createApplication(application) {
const resp = await this.sendPostRequest(`${this.config.apiHost}/v2/applications`, server_client_1.Client.transformers.snakeCaseObjectKeys(application, true));
return apiToApplication(resp.data);
}
/**
* Retrieves an application by its unique identifier.
*
* @see API Specification {@link https://developer.vonage.com/en/api/application.v2#getApplication}
*
* @param {string} applicationId - The unique identifier of the application to retrieve.
* @return {Promise<MergedApplication>} - A promise resolving to the retrieved application.
*
* @example
* Retrieve an application
*
* ```ts
* const application = await applicationClient.getApplication(APPLICATION_ID);
* console.log(application.name);
* ```
*/
async getApplication(applicationId) {
const resp = await this.sendGetRequest(`${this.config.apiHost}/v2/applications/${applicationId}`);
return apiToApplication(resp.data);
}
/**
* Updates an existing application with the provided details.
*
* @see API Specification {@link https://developer.vonage.com/en/api/application.v2#updateApplication}
*
* @param {Application} application - The application details to be updated.
* @return {Promise<MergedApplication>} - A promise resolving to the updated application.
*
* @example
* Update an application
*
* ```ts
* const application = await applicationClient.updateApplication({
* id: APPLICATION_ID,
* name: 'My Application',
* });
* console.log(application.name);
* ```
*/
async updateApplication(application) {
const resp = await this.sendPutRequest(`${this.config.apiHost}/v2/applications/${application.id}`, server_client_1.Client.transformers.snakeCaseObjectKeys(application, true));
return apiToApplication(resp.data);
}
/**
* Deletes an application by its unique identifier.
*
* @see API Specification {@link https://developer.vonage.com/en/api/application.v2#deleteApplication}
*
* @param {string} applicationId - The unique identifier of the application to delete.
* @return {Promise<void>} - A promise indicating the successful deletion of the application.
*
* @example
* Delete an application
*
* ```ts
* await applicationClient.deleteApplication(APPLICATION_ID);
* ```
*/
async deleteApplication(applicationId) {
await this.sendDeleteRequest(`${this.config.apiHost}/v2/applications/${applicationId}`);
}
}
exports.Applications = Applications;
//# sourceMappingURL=applications.js.map