Skip to content

Commit

Permalink
DEVEXP-344: Promote usage of services (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch authored Mar 22, 2024
1 parent c77ac00 commit 8625ab7
Show file tree
Hide file tree
Showing 182 changed files with 832 additions and 664 deletions.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,27 @@ where `sinchClientParameters` is an object containing the properties required to
- `keySecret`
- (For the SMS API, `region` is optional. Default is `US`).

From this client, you have access to all the SDK services supporting the Sinch APIs:
```typescript
import {
SinchClient,
} from '@sinch/sdk-core';

const sinch = new SinchClient(sinchClientParameters);
const conversationService = sinch.conversation;
const faxService = sinch.fax;
const numbersService = sinch.numbers;
const smsService = sinch.sms;
const verificationService = sinch.verification;
const voiceService = sinch.voice;
```

### Promises

All the methods that interact with the Sinch APIs use Promises.

```typescript
const response: SendSMSResponse = await sinchClient.sms.batches.send({
const response: SendSMSResponse = await smsService.batches.send({
sendSMSRequestBody: {
to: [
'+12223334444',
Expand Down Expand Up @@ -128,25 +143,20 @@ Here is the list of the Sinch products and their level of support by the Node.js
| Messaging | SMS API ||
| | Conversation API ||
| | Fax API | 🚧 |
| | RCS API ||
| | MMS API ||
| | Provisioning API ||
| Voice and Video | Voice API ||
| | Elastic SIP Trunking ||
| Numbers & Connectivity | Numbers API ||
| | Brand and Campaign Registration API ||
| | Number Lookup API ||
| Verification | Verification API ||

### Packages

The Sinch Node.js SDK is packaged in the following way:
- [`@sinch/sdk-core`](./packages/sdk-core): package defining the `SinchClient` class and wrapping all the other packages.
- [`@sinch/sms`](./packages/sms): package that contains SMS services: Batches, Delivery reports, Inbounds, Groups and Webhooks callbacks.
- `@sinch/conversation`: IN-PROGRESS
- [`@sinch/conversation`](./packages/conversation): package that contains Conversation services: App, Capability, Contact, Conversation, Events, Messages, Templates V1 and V2, Transcoding, Webhooks management and Webhooks callbacks.
- [`@sinch/voice`](./packages/voice): package that contains the Voice services: Callouts, Calls, Conferences, Applications management and Webhooks callbacks.
- [`@sinch/numbers`](./packages/numbers): package that contains the Numbers services: Available number, Active number, Available regions, Callbacks management and Webhooks callbacks.
- [`@sinch/verification`](./packages/verification): package that contains the Verification services: Verification start and report, Verification status and Webhooks callbacks.
- [`@sinch/fax`](./packages/fax): package that contains the Fax services: Services, Faxes and Faxes-on-emails
- [`@sinch/sdk-client`](./packages/sdk-client): package included by all the other ones that contains the API client classes and helpers.

## Examples
Expand Down
14 changes: 7 additions & 7 deletions examples/simple-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@
"numbers:active:release": "ts-node src/numbers/active/release.ts",
"numbers:callback:get": "ts-node src/numbers/callbacks/get.ts",
"numbers:callback:update": "ts-node src/numbers/callbacks/update.ts",
"sms:groups:list": "ts-node src/sms/groups/list/list.sinch-client.ts",
"sms:groups:create": "ts-node src/sms/groups/create/create.sinch-client.ts",
"sms:groups:get": "ts-node src/sms/groups/get/get.sinch-client.ts",
"sms:groups:getPhoneNumbers": "ts-node src/sms/groups/getPhoneNumbers/getPhoneNumbers.sinch-client.ts",
"sms:groups:replace": "ts-node src/sms/groups/replace/replace.sinch-client.ts",
"sms:groups:update": "ts-node src/sms/groups/update/update.sinch-client.ts",
"sms:groups:delete": "ts-node src/sms/groups/delete/delete.sinch-client.ts",
"sms:groups:list": "ts-node src/sms/groups/list/list.with-projectId.ts",
"sms:groups:create": "ts-node src/sms/groups/create/create.with-projectId.ts",
"sms:groups:get": "ts-node src/sms/groups/get/get.with-projectId.ts",
"sms:groups:getPhoneNumbers": "ts-node src/sms/groups/getPhoneNumbers/getPhoneNumbers.with-projectId.ts",
"sms:groups:replace": "ts-node src/sms/groups/replace/replace.with-projectId.ts",
"sms:groups:update": "ts-node src/sms/groups/update/update.with-projectId.ts",
"sms:groups:delete": "ts-node src/sms/groups/delete/delete.with-projectId.ts",
"sms:batches:send": "ts-node src/sms/batches/send.ts",
"sms:batches:sendTextMessage": "ts-node src/sms/batches/sendTextMessage.ts",
"sms:batches:sendBinaryMessage": "ts-node src/sms/batches/sendBinaryMessage.ts",
Expand Down
46 changes: 42 additions & 4 deletions examples/simple-examples/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,64 @@
import { getRegion, SinchClient, Region } from '@sinch/sdk-core';
import {
getRegion,
SinchClient,
Region,
ConversationService,
FaxService,
NumbersService,
SmsService,
VerificationService,
VoiceService,
} from '@sinch/sdk-core';
require('dotenv').config();

export const initClient = () => {
const initClient = (): Pick<SinchClient, 'conversation' | 'fax' | 'numbers' | 'sms'> => {
const keyId = process.env.SINCH_KEY_ID || '';
const keySecret = process.env.SINCH_KEY_SECRET || '';
const projectId = process.env.SINCH_PROJECT_ID || '';
return new SinchClient({ projectId, keyId, keySecret });
};

export const initSmsClient = () => {
export const initConversationService = (): ConversationService => {
return initClient().conversation;
};

export const initFaxService = (): FaxService => {
return initClient().fax;
};

export const initNumbersService = (): NumbersService => {
return initClient().numbers;
};

export const initSmsServiceWithProjectId = (): SmsService => {
return initClient().sms;
};

const initSmsClient = (): Pick<SinchClient, 'sms'> => {
const servicePlanId = process.env.SINCH_SERVICE_PLAN_ID || '';
const apiToken = process.env.SINCH_API_TOKEN || '';
const region = getRegion(process.env.SMS_REGION) || Region.UNITED_STATES;
return new SinchClient({ servicePlanId, apiToken, region });
};

export const initApplicationClient = () => {
export const initSmsServiceWithServicePlanId = (): SmsService => {
return initSmsClient().sms;
};

const initApplicationClient = (): Pick<SinchClient, 'verification' | 'voice'> => {
const applicationKey = process.env.SINCH_APPLICATION_KEY || '';
const applicationSecret = process.env.SINCH_APPLICATION_SECRET || '';
return new SinchClient({ applicationKey, applicationSecret });
};

export const initVerificationService = (): VerificationService => {
return initApplicationClient().verification;
};

export const initVoiceService = (): VoiceService => {
return initApplicationClient().voice;
};

export const getPrintFormat = (args: string[]): 'pretty' | 'full' => {
const params = args.slice(2);
if (params.length > 0 && params[0] === 'pretty') {
Expand Down
11 changes: 8 additions & 3 deletions examples/simple-examples/src/conversation/app/create.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { CreateAppRequestData } from '@sinch/sdk-core';
import { getMessengerTokenFormConfig, getPrintFormat, initClient, printFullResponse } from '../../config';
import {
getMessengerTokenFormConfig,
getPrintFormat,
initConversationService,
printFullResponse,
} from '../../config';

(async () => {
console.log('*****************');
Expand Down Expand Up @@ -27,8 +32,8 @@ import { getMessengerTokenFormConfig, getPrintFormat, initClient, printFullRespo
},
};

const sinchClient = initClient();
const response = await sinchClient.conversation.app.create(requestData);
const conversationService = initConversationService();
const response = await conversationService.app.create(requestData);

const printFormat = getPrintFormat(process.argv);

Expand Down
6 changes: 3 additions & 3 deletions examples/simple-examples/src/conversation/app/delete.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DeleteAppRequestData } from '@sinch/sdk-core';
import { getAppIdFromConfig, initClient, printFullResponse } from '../../config';
import { getAppIdFromConfig, initConversationService, printFullResponse } from '../../config';

(async () => {
console.log('*****************');
Expand All @@ -12,8 +12,8 @@ import { getAppIdFromConfig, initClient, printFullResponse } from '../../config'
app_id: appId,
};

const sinchClient = initClient();
const response = await sinchClient.conversation.app.delete(requestData);
const conversationService = initConversationService();
const response = await conversationService.app.delete(requestData);

printFullResponse(response);

Expand Down
11 changes: 8 additions & 3 deletions examples/simple-examples/src/conversation/app/get.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { GetAppRequestData } from '@sinch/sdk-core';
import { getAppIdFromConfig, getPrintFormat, initClient, printFullResponse } from '../../config';
import {
getAppIdFromConfig,
getPrintFormat,
initConversationService,
printFullResponse,
} from '../../config';

(async () => {
console.log('**************');
Expand All @@ -12,8 +17,8 @@ import { getAppIdFromConfig, getPrintFormat, initClient, printFullResponse } fro
app_id: appId,
};

const sinchClient = initClient();
const response = await sinchClient.conversation.app.get(requestData);
const conversationService = initConversationService();
const response = await conversationService.app.get(requestData);

const printFormat = getPrintFormat(process.argv);

Expand Down
6 changes: 3 additions & 3 deletions examples/simple-examples/src/conversation/app/list.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ListAppsRequestData } from '@sinch/sdk-core';
import { getPrintFormat, initClient, printFullResponse } from '../../config';
import { getPrintFormat, initConversationService, printFullResponse } from '../../config';

(async () => {
console.log('****************');
Expand All @@ -8,8 +8,8 @@ import { getPrintFormat, initClient, printFullResponse } from '../../config';

const requestData: ListAppsRequestData= {};

const sinchClient = initClient();
const response = await sinchClient.conversation.app.list(requestData);
const conversationService = initConversationService();
const response = await conversationService.app.list(requestData);

const printFormat = getPrintFormat(process.argv);

Expand Down
9 changes: 5 additions & 4 deletions examples/simple-examples/src/conversation/app/update.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { UpdateAppRequestData } from '@sinch/sdk-core';
import {
getAppIdFromConfig, getMessengerTokenFormConfig,
getAppIdFromConfig,
getMessengerTokenFormConfig,
getPrintFormat,
initClient,
initConversationService,
printFullResponse,
} from '../../config';

Expand Down Expand Up @@ -31,8 +32,8 @@ import {
},
};

const sinchClient = initClient();
const response = await sinchClient.conversation.app.update(requestData);
const conversationService = initConversationService();
const response = await conversationService.app.update(requestData);

const printFormat = getPrintFormat(process.argv);

Expand Down
11 changes: 8 additions & 3 deletions examples/simple-examples/src/conversation/capability/lookup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { ContactId, LookupCapabilityRequestData } from '@sinch/sdk-core';
import { getAppIdFromConfig, getContactIdFromConfig, initClient, printFullResponse } from '../../config';
import {
getAppIdFromConfig,
getContactIdFromConfig,
initConversationService,
printFullResponse,
} from '../../config';

(async () => {
console.log('******************************');
Expand All @@ -19,8 +24,8 @@ import { getAppIdFromConfig, getContactIdFromConfig, initClient, printFullRespon
},
};

const sinchClient = initClient();
const response = await sinchClient.conversation.capability.lookup(requestData);
const conversationService = initConversationService();
const response = await conversationService.capability.lookup(requestData);

printFullResponse(response);

Expand Down
6 changes: 3 additions & 3 deletions examples/simple-examples/src/conversation/contact/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getMessengerUserIdFromConfig,
getPhoneNumberFromConfig,
getPrintFormat,
initClient,
initConversationService,
printFullResponse,
} from '../../config';

Expand Down Expand Up @@ -36,8 +36,8 @@ import {
},
};

const sinchClient = initClient();
const response = await sinchClient.conversation.contact.create(requestData);
const conversationService = initConversationService();
const response = await conversationService.contact.create(requestData);

const printFormat = getPrintFormat(process.argv);

Expand Down
6 changes: 3 additions & 3 deletions examples/simple-examples/src/conversation/contact/delete.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DeleteContactRequestData } from '@sinch/sdk-core';
import { getContactIdFromConfig, initClient, printFullResponse } from '../../config';
import { getContactIdFromConfig, initConversationService, printFullResponse } from '../../config';

(async () => {
console.log('*************************');
Expand All @@ -12,8 +12,8 @@ import { getContactIdFromConfig, initClient, printFullResponse } from '../../con
contact_id: contactId,
};

const sinchClient = initClient();
const response = await sinchClient.conversation.contact.delete(requestData);
const conversationService = initConversationService();
const response = await conversationService.contact.delete(requestData);

printFullResponse(response);

Expand Down
6 changes: 3 additions & 3 deletions examples/simple-examples/src/conversation/contact/get.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GetContactRequestData } from '@sinch/sdk-core';
import { getContactIdFromConfig, getPrintFormat, initClient, printFullResponse } from '../../config';
import { getContactIdFromConfig, getPrintFormat, initConversationService, printFullResponse } from '../../config';

(async () => {
console.log('**********************');
Expand All @@ -12,8 +12,8 @@ import { getContactIdFromConfig, getPrintFormat, initClient, printFullResponse }
contact_id: contactId,
};

const sinchClient = initClient();
const response = await sinchClient.conversation.contact.get(requestData);
const conversationService = initConversationService();
const response = await conversationService.contact.get(requestData);

const printFormat = getPrintFormat(process.argv);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { GetChannelProfileRequestData, IdentifiedBy } from '@sinch/sdk-core';
import {
getAppIdFromConfig,
getMessengerUserIdFromConfig,
initClient,
initConversationService,
printFullResponse,
} from '../../config';

Expand Down Expand Up @@ -31,8 +31,8 @@ import {
},
};

const sinchClient = initClient();
const response = await sinchClient.conversation.contact.getChannelProfile(requestData);
const conversationService = initConversationService();
const response = await conversationService.contact.getChannelProfile(requestData);

printFullResponse(response);

Expand Down
8 changes: 4 additions & 4 deletions examples/simple-examples/src/conversation/contact/list.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Contact, ListContactsRequestData, PageResult } from '@sinch/sdk-core';
import { getPrintFormat, initClient, printFullResponse } from '../../config';
import { getPrintFormat, initConversationService, printFullResponse } from '../../config';

const populateContactsList = (
contactPage: PageResult<Contact>,
Expand All @@ -21,12 +21,12 @@ const populateContactsList = (
page_size: 2,
};

const sinchClient = initClient();
const conversationService = initConversationService();

// ----------------------------------------------
// Method 1: Fetch the data page by page manually
// ----------------------------------------------
let response = await sinchClient.conversation.contact.list(requestData);
let response = await conversationService.contact.list(requestData);

const contactList: Contact[] = [];
const contactDetailsList: string[] = [];
Expand Down Expand Up @@ -55,7 +55,7 @@ const populateContactsList = (
// ---------------------------------------------------------------------
// Method 2: Use the iterator and fetch data on more pages automatically
// ---------------------------------------------------------------------
for await (const contact of sinchClient.conversation.contact.list(requestData)) {
for await (const contact of conversationService.contact.list(requestData)) {
if (printFormat === 'pretty') {
console.log(`${contact.id} - ${contact.display_name}`);
} else {
Expand Down
Loading

0 comments on commit 8625ab7

Please sign in to comment.