Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEVEXP-556: Add snippets for Numbers API #2

Merged
merged 6 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions snippets/numbers/available-regions/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// eslint-disable-next-line no-unused-vars
import { Numbers, NumbersService } from '@sinch/sdk-core';

/** @param {NumbersService} numbersService */
export const execute = async (numbersService) => {

/** @type {Numbers.ListAvailableRegionsRequestData} */
const requestData = {
types: ['LOCAL', 'MOBILE'],
};

const response = await numbersService.availableRegions.list(requestData);

console.log(`Available regions:\n${JSON.stringify(response, null, 2)}`);
};
10 changes: 10 additions & 0 deletions snippets/numbers/callback-configuration/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// eslint-disable-next-line no-unused-vars
import { Numbers, NumbersService } from '@sinch/sdk-core';

/** @param {NumbersService} numbersService */
export const execute = async (numbersService) => {

const response = await numbersService.callbacks.get({});

console.log(`Callback configuration:\n${JSON.stringify(response, null, 2)}`);
};
19 changes: 19 additions & 0 deletions snippets/numbers/callback-configuration/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// eslint-disable-next-line no-unused-vars
import { Numbers, NumbersService } from '@sinch/sdk-core';

/** @param {NumbersService} numbersService */
export const execute = async (numbersService) => {

const hmacSecret = 'New_HMAC_secret';

/** @type {Numbers.UpdateCallbackConfigurationRequestData} */
const requestData = {
updateCallbackConfigurationRequestBody: {
hmacSecret,
},
};

const response = await numbersService.callbacks.update(requestData);

console.log(`Updated callback configuration:\n${JSON.stringify(response, null, 2)}`);
};
25 changes: 25 additions & 0 deletions snippets/numbers/check-availability.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// eslint-disable-next-line no-unused-vars
import { Numbers, NumbersService } from '@sinch/sdk-core';

/** @param {NumbersService} numbersService */
export const execute = async (numbersService) => {

const phoneNumber = 'a_phone_number';

/** @type {Numbers.GetAvailableNumberRequestData} */
const requestData = {
phoneNumber,
};

let response;
try {
response = await numbersService.availableNumber.checkAvailability(requestData);
} catch (error) {
console.error(`ERROR: the phone number ${requestData.phoneNumber} is not available`);
}

if (response) {
JPPortier marked this conversation as resolved.
Show resolved Hide resolved
console.log(`The phone number is available:\n${JSON.stringify(response, null, 2)}`);
}

};
17 changes: 17 additions & 0 deletions snippets/numbers/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// eslint-disable-next-line no-unused-vars
import { Numbers, NumbersService } from '@sinch/sdk-core';

/** @param {NumbersService} numbersService */
export const execute = async (numbersService) => {

const phoneNumber = 'YOUR_rented_phoneNumber';

/** @type {Numbers.GetActiveNumberRequestData} */
const requestData = {
phoneNumber,
};

const response = await numbersService.activeNumber.get(requestData);

console.log(`Rented number details:\n${JSON.stringify(response, null, 2)}`);
};
30 changes: 30 additions & 0 deletions snippets/numbers/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// eslint-disable-next-line no-unused-vars
import { Numbers, NumbersService } from '@sinch/sdk-core';

/** @param {NumbersService} numbersService */
export const execute = async (numbersService) => {

/** @type Numbers.ListActiveNumbersRequestData */
const requestData = {
regionCode: 'US',
type: 'LOCAL',
};

// Method 1: Fetch the data page by page manually

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like providing both methods, but I wonder if it would be better to have two snippets, one that lists page by page manually, and one that uses the iterator, rather than having both in the same file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good suggestion! There are now 2 snippets: list and list-auto

/** @type {Numbers.ActiveNumber[]} */
const activeNumbersList = [];
let response = await numbersService.activeNumber.list(requestData);

do {
activeNumbersList.push(...response.data);
response = response.hasNextPage ? await response.nextPage() : null;
} while (response);
console.log(`Full list of numbers printed at once (length = ${activeNumbersList.length}):\n${JSON.stringify(activeNumbersList, null, 2)}`);

// Method 2: Use the iterator and fetch data from more pages automatically
console.log('List of numbers printed one by one:');
for await (const rentedNumber of numbersService.activeNumber.list(requestData)) {
console.log(JSON.stringify(rentedNumber, null, 2));
}

};
17 changes: 17 additions & 0 deletions snippets/numbers/release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// eslint-disable-next-line no-unused-vars
import { Numbers, NumbersService } from '@sinch/sdk-core';

/** @param {NumbersService} numbersService */
export const execute = async (numbersService) => {

const phoneNumber = 'YOUR_phone_number_to_be_released';

/** @type {Numbers.ReleaseNumberRequestData} */
const requestData = {
phoneNumber,
};

const response = await numbersService.activeNumber.release(requestData);

console.log(`Released number:\n${JSON.stringify(response, null, 2)}`);
};
40 changes: 40 additions & 0 deletions snippets/numbers/rent-any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// eslint-disable-next-line no-unused-vars
import { Numbers, NumbersService } from '@sinch/sdk-core';

/** @param {NumbersService} numbersService */
export const execute = async (numbersService) => {

const servicePlanId = 'YOUR_service_plan_id';
const appId = 'YOUR_app_id';
const callbackUrl = 'YOUR_callback_url';

/** @type {Numbers.RentAnyNumberRequestSmsConfiguration} */
const smsConfiguration = {
servicePlanId,
};

/** @type {Numbers.RentAnyNumberRequestVoiceConfiguration} */
const voiceConfiguration = {
appId,
};

/** @type {Numbers.RentAnyNumberRequestData} */
const requestData = {
rentAnyNumberRequestBody: {
regionCode: 'US',
type: 'LOCAL',
numberPattern: {
pattern: '+1781',
searchPattern: 'START',
},
smsConfiguration,
voiceConfiguration,
callbackUrl,
},
};

const response = await numbersService.availableNumber.rentAny(requestData);

console.log(`Rented number:\n${JSON.stringify(response, null, 2)}`);

};
25 changes: 25 additions & 0 deletions snippets/numbers/rent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// eslint-disable-next-line no-unused-vars
import { Numbers, NumbersService } from '@sinch/sdk-core';

/** @param {NumbersService} numbersService */
export const execute = async (numbersService) => {

// Available numbers list can be retrieved by using list() function from available service, see:
// https://developers.sinch.com/docs/numbers/getting-started/node-sdk/searchavailable/
const phoneNumber = 'available_phone_number_to_be_rented';
const servicePlanId = 'YOUR_service_plan_id';

/** @type {Numbers.RentNumberRequestData} */
const requestData = {
phoneNumber,
rentNumberRequestBody: {
smsConfiguration: {
servicePlanId,
},
},
};

const response = await numbersService.availableNumber.rent(requestData);

console.log(`Rented number:\n${JSON.stringify(response, null, 2)}`);
};
17 changes: 17 additions & 0 deletions snippets/numbers/search-for-available-numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// eslint-disable-next-line no-unused-vars
import { Numbers, NumbersService } from '@sinch/sdk-core';

/** @param {NumbersService} numbersService */
export const execute = async (numbersService) => {

/** @type {Numbers.ListAvailableNumbersRequestData} */
const requestData = {
regionCode: 'US',
type: 'LOCAL',
capabilities: ['SMS', 'VOICE'],
};

const response = await numbersService.availableNumber.list(requestData);

console.log(`Available numbers to rent:\n${JSON.stringify(response, null, 2)}`);
};
29 changes: 29 additions & 0 deletions snippets/numbers/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// eslint-disable-next-line no-unused-vars
import { Numbers, NumbersService } from '@sinch/sdk-core';

/** @param {NumbersService} numbersService */
export const execute = async (numbersService) => {

const phoneNumber = 'YOUR_phone_number_to_be_updated';
const appId = 'YOUR_app_id';
const displayName = 'Updated from Sinch Node.js SDK';

/** @type {Numbers.VoiceConfiguration} */
const voiceConfiguration= {
appId,
type: 'RTC',
};

/** @type {Numbers.UpdateActiveNumberRequestData} */
const requestData = {
phoneNumber,
updateActiveNumberRequestBody: {
displayName,
voiceConfiguration,
},
};

const response = await numbersService.activeNumber.update(requestData);

console.log(`Updated number:\n${JSON.stringify(response, null, 2)}`);
};
Loading