@vonage/applications
Version:
Vonage Applications API
264 lines (260 loc) • 8.67 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// lib/index.ts
var index_exports = {};
__export(index_exports, {
Applications: () => Applications,
VoiceRegions: () => VoiceRegions
});
module.exports = __toCommonJS(index_exports);
// lib/enums/VoiceRegions.ts
var VoiceRegions = /* @__PURE__ */ ((VoiceRegions2) => {
VoiceRegions2["NA_EAST"] = "na-east";
VoiceRegions2["NA_WEST"] = "na-west";
VoiceRegions2["EU_WEST"] = "eu-west";
VoiceRegions2["EU_EAST"] = "eu-east";
VoiceRegions2["APAC_SNG"] = "apac-sng";
VoiceRegions2["APAC_AUSTRALIA"] = "apac-australia";
return VoiceRegions2;
})(VoiceRegions || {});
// lib/applications.ts
var import_server_client = require("@vonage/server-client");
var apiToApplication = (response) => {
delete response._links;
return import_server_client.Client.transformers.camelCaseObjectKeys(
response,
true,
true
);
};
var Applications = class extends import_server_client.Client {
authType = import_server_client.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`,
import_server_client.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`,
import_server_client.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}`,
import_server_client.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}`
);
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Applications,
VoiceRegions
});