@sinch/sdk-core 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +273 -33
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -30,17 +30,19 @@ As there are different authentication schemes, the initialization method will de
|
|
|
30
30
|
```typescript
|
|
31
31
|
import { SinchClient } from '@sinch/sdk-core';
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
(async () => {
|
|
34
|
+
// The credentials can be found on the Account dashboard: https://dashboard.sinch.com/account/access-keys
|
|
35
|
+
const sinch: Pick<SinchClient, 'conversation' | 'fax' | 'numbers' | 'sms'> = new SinchClient({
|
|
35
36
|
projectId: 'my-project-id',
|
|
36
37
|
keyId: 'my-key-id',
|
|
37
38
|
keySecret: 'my-key-secret',
|
|
38
|
-
});
|
|
39
|
-
const numbersService = sinch.numbers;
|
|
40
|
-
|
|
41
|
-
const response = await numbersService.availableRegions.list({
|
|
42
|
-
types: 'LOCAL',
|
|
43
|
-
});
|
|
39
|
+
});
|
|
40
|
+
const numbersService = sinch.numbers;
|
|
41
|
+
|
|
42
|
+
const response = await numbersService.availableRegions.list({
|
|
43
|
+
types: ['LOCAL'],
|
|
44
|
+
});
|
|
45
|
+
})();
|
|
44
46
|
```
|
|
45
47
|
The initialization method above will work for the APIs that supports the authentication with OAuth2 (Numbers and SMS on US and EU regions).
|
|
46
48
|
|
|
@@ -51,17 +53,19 @@ If you want to use the SMS API on the other regions (or US and EU too, it will w
|
|
|
51
53
|
```typescript
|
|
52
54
|
import { SinchClient } from '@sinch/sdk-core';
|
|
53
55
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
+
(async () => {
|
|
57
|
+
// The credentials can be found on the Service APIs dashboard: https://dashboard.sinch.com/sms/api/services
|
|
58
|
+
const sinch: Pick<SinchClient, 'sms'> = new SinchClient({
|
|
56
59
|
servicePlanId: 'my-service-plan-id',
|
|
57
60
|
apiToken: 'my-key-id',
|
|
58
|
-
|
|
59
|
-
});
|
|
60
|
-
const smsService = sinch.sms;
|
|
61
|
-
|
|
62
|
-
const response = await smsService.batches.get({
|
|
61
|
+
smsRegion: 'my-region', // Optional, can be 'us', 'eu', 'br', 'au', 'ca'. Default is 'us'
|
|
62
|
+
});
|
|
63
|
+
const smsService = sinch.sms;
|
|
64
|
+
|
|
65
|
+
const response = await smsService.batches.get({
|
|
63
66
|
batch_id: '01HF28S9AABBBCCCCY92BJB569',
|
|
64
|
-
});
|
|
67
|
+
});
|
|
68
|
+
})();
|
|
65
69
|
```
|
|
66
70
|
|
|
67
71
|
### Signed application authentication
|
|
@@ -71,18 +75,254 @@ Both `Verification` and `Voice` APIs are using this authentication method: the r
|
|
|
71
75
|
```typescript
|
|
72
76
|
import { SinchClient } from '@sinch/sdk-core';
|
|
73
77
|
|
|
74
|
-
|
|
75
|
-
//
|
|
76
|
-
// https://dashboard.sinch.com/
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
(async () => {
|
|
79
|
+
// The credentials can be found on the Verification or Voice dashboard:
|
|
80
|
+
// https://dashboard.sinch.com/verification/apps or
|
|
81
|
+
// https://dashboard.sinch.com/voice/apps
|
|
82
|
+
const sinch: Pick<SinchClient, 'verification' | 'voice'> = new SinchClient({
|
|
83
|
+
applicationId: 'my-application-key',
|
|
84
|
+
applicationSecret: 'my-application-secret',
|
|
85
|
+
});
|
|
86
|
+
const verificationService = sinch.verification;
|
|
87
|
+
|
|
88
|
+
const response = await verificationService.verificationStatus.getById({
|
|
89
|
+
id: '018bfc3e-1234-5678-1234-ebdb3fd6d30f',
|
|
90
|
+
});
|
|
91
|
+
})();
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Importing classes and interfaces from other API packages
|
|
95
|
+
|
|
96
|
+
For convenience, importing `@sinch/sdk-core` is sufficient to be able to access to all the APIs' classes and interfaces. In case you need to use a single API, they are also packaged as single NPM packages:
|
|
97
|
+
- SMS: [`@sinch/sms`](https://www.npmjs.com/package/@sinch/sms)
|
|
98
|
+
- Conversation: [`@sinch/conversation`](https://www.npmjs.com/package/@sinch/conversation)
|
|
99
|
+
- Fax: [`@sinch/fax`](https://www.npmjs.com/package/@sinch/fax)
|
|
100
|
+
- Numbers: [`@sinch/numbers`](https://www.npmjs.com/package/@sinch/numbers)
|
|
101
|
+
- Verification: [`@sinch/verification`](https://www.npmjs.com/package/@sinch/verification)
|
|
102
|
+
- Voice: [`@sinch/voice`](https://www.npmjs.com/package/@sinch/voice)
|
|
103
|
+
|
|
104
|
+
All the interfaces are exported with an alias, equal to the API name: `Sms` for the SMS API, `Conversation` for the Conversation API, `Fax` for the Fax API and so on.
|
|
105
|
+
|
|
106
|
+
Here is an example about using the TypeScript types to send a fax:
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
import {
|
|
110
|
+
Fax, // "Fax" is the alias under which are exported all the interfaces
|
|
111
|
+
FaxService, // This is a class so it is exported as is
|
|
112
|
+
SinchClient,
|
|
113
|
+
} from '@sinch/sdk-core';
|
|
114
|
+
|
|
115
|
+
(async () => {
|
|
116
|
+
const sinch = new SinchClient({
|
|
117
|
+
projectId: 'my-project-id',
|
|
118
|
+
keyId: 'my-key-id',
|
|
119
|
+
keySecret: 'my-key-secret',
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const faxService: FaxService = sinch.fax;
|
|
123
|
+
|
|
124
|
+
const requestData: Fax.SendFaxRequestData = {
|
|
125
|
+
sendFaxRequestBody: {
|
|
126
|
+
to: 'a valid destination number',
|
|
127
|
+
from: 'a valid origin number',
|
|
128
|
+
contentUrl: 'https://developers.sinch.com/fax/fax.pdf',
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const response: Fax.Fax = await faxService.faxes.send(requestData);
|
|
133
|
+
})();
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Importing classes for CommonJS
|
|
137
|
+
|
|
138
|
+
Until now, all the examples in this documentation are showcasing ES modules but there may be some cases when the SDK user will want to use CommonJS instead.
|
|
139
|
+
|
|
140
|
+
Here is an example to do the same as above with CommonJS (using `require`):
|
|
141
|
+
```javascript
|
|
142
|
+
const { SinchClient } = require('@sinch/sdk-core');
|
|
143
|
+
|
|
144
|
+
(async () => {
|
|
145
|
+
const sinch = new SinchClient({
|
|
146
|
+
projectId: 'YOUR_project_id',
|
|
147
|
+
keyId: 'YOUR_access_key',
|
|
148
|
+
keySecret: 'YOUR_access_secret',
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const faxService = sinch.fax;
|
|
152
|
+
|
|
153
|
+
const requestData = {
|
|
154
|
+
sendFaxRequestBody: {
|
|
155
|
+
to: 'a valid destination number',
|
|
156
|
+
from: 'a valid origin number',
|
|
157
|
+
contentUrl: 'https://developers.sinch.com/fax/fax.pdf',
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const response = await faxService.faxes.send(requestData);
|
|
162
|
+
})();
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## SinchClient parameters override
|
|
166
|
+
|
|
167
|
+
### Hostname and region override
|
|
168
|
+
|
|
169
|
+
For various reasons (development phase, testing, network restrictions, ...), one may need to update the default API endpoint, pointing to production.
|
|
170
|
+
Each API exposes dedicated parameters to override the default hostname and region:
|
|
171
|
+
|
|
172
|
+
| API Name | Parameter | Region |
|
|
173
|
+
|----------------|------------------------------------|--------------------|
|
|
174
|
+
| Authentication | authHostname | N/A |
|
|
175
|
+
| SMS | smsHostname | smsRegion |
|
|
176
|
+
| Conversation | conversationHostname | conversationRegion |
|
|
177
|
+
| | conversationTemplatesHostname | |
|
|
178
|
+
| Fax | faxHostname | faxRegion |
|
|
179
|
+
| Voice | voiceHostname | voiceRegion |
|
|
180
|
+
| | voiceApplicationManagementHostname | |
|
|
181
|
+
| Numbers | numbersHostname | N/A |
|
|
182
|
+
| Verification | verificationHostname | N/A |
|
|
183
|
+
|
|
184
|
+
And here are the list of supported regions per regionalized API:
|
|
185
|
+
|
|
186
|
+
| Api Name | Supported regions |
|
|
187
|
+
|--------------|---------------------------------------------------------------|
|
|
188
|
+
| SMS | SmsRegion.UNITED_STATES |
|
|
189
|
+
| | SmsRegion.EUROPE |
|
|
190
|
+
| | SmsRegion.BRAZIL (not available for OAuth2 authentication) |
|
|
191
|
+
| | SmsRegion.CANADA (not available for OAuth2 authentication) |
|
|
192
|
+
| | SmsRegion.AUSTRALIA (not available for OAuth2 authentication) |
|
|
193
|
+
| Conversation | ConversationRegion.UNITED_STATES |
|
|
194
|
+
| | ConversationRegion.EUROPE |
|
|
195
|
+
| | ConversationRegion.BRAZIL |
|
|
196
|
+
| Fax | FaxRegion.DEFAULT |
|
|
197
|
+
| | FaxRegion.UNITED_STATES |
|
|
198
|
+
| | FaxRegion.EUROPE |
|
|
199
|
+
| | FaxRegion.SOUTH_AMERICA |
|
|
200
|
+
| | FaxRegion.SOUTHEAST_ASIA_1 |
|
|
201
|
+
| | FaxRegion.SOUTHEAST_ASIA_2 |
|
|
202
|
+
| Voice | VoiceRegion.DEFAULT |
|
|
203
|
+
| | VoiceRegion.UNITED_STATES |
|
|
204
|
+
| | VoiceRegion.EUROPE |
|
|
205
|
+
| | VoiceRegion.SOUTH_AMERICA |
|
|
206
|
+
| | VoiceRegion.SOUTHEAST_ASIA_1 |
|
|
207
|
+
| | VoiceRegion.SOUTHEAST_ASIA_2 |
|
|
208
|
+
|
|
209
|
+
**Use case #1:** overriding the region in production
|
|
210
|
+
|
|
211
|
+
```typescript
|
|
212
|
+
import { SinchClientParameters } from '@sinch/sdk-core';
|
|
213
|
+
|
|
214
|
+
const conversationClientParametersEurope: SinchClientParameters = {
|
|
215
|
+
projectId: 'my-project-id',
|
|
216
|
+
keyId: 'my-key-id',
|
|
217
|
+
keySecret: 'my-key-secret',
|
|
218
|
+
conversationRegion: ConversationRegion.EUROPE,
|
|
219
|
+
};
|
|
220
|
+
```
|
|
221
|
+
**Use case #2:** overriding the hostname
|
|
222
|
+
```typescript
|
|
223
|
+
import { SinchClientParameters } from '@sinch/sdk-core';
|
|
224
|
+
|
|
225
|
+
const conversationClientParametersForTest: SinchClientParameters = {
|
|
226
|
+
projectId: 'my-project-id',
|
|
227
|
+
keyId: 'my-key-id',
|
|
228
|
+
keySecret: 'my-key-secret',
|
|
229
|
+
authHostname: 'http://my-test-server:3000',
|
|
230
|
+
conversationHostname: 'http://my-test-server:3001',
|
|
231
|
+
conversationTemplatesHostname: 'http://my-test-server:3002',
|
|
232
|
+
};
|
|
233
|
+
```
|
|
234
|
+
**Note**: when overriding the hostname, the `region` parameter becomes obsolete and is not taken into account when sending the request.
|
|
235
|
+
|
|
236
|
+
**Note**: The region parameter is permissive: instead of the pre-defined list, you can also set any `string`. This covers the case where a new region is added (in production or in preview) and the SDK is not yet ready to support it or the SDK user doesn't want to update its SDK version to benefit from it.
|
|
237
|
+
```typescript
|
|
238
|
+
import { SinchClientParameters } from '@sinch/sdk-core';
|
|
239
|
+
|
|
240
|
+
const conversationClientParametersNewRegion: SinchClientParameters = {
|
|
241
|
+
projectId: 'my-project-id',
|
|
242
|
+
keyId: 'my-key-id',
|
|
243
|
+
keySecret: 'my-key-secret',
|
|
244
|
+
conversationRegion: 'bzh',
|
|
245
|
+
};
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
On top of that, once you have instantiated an API service, it is possible to override the hostname and the region (in case the API endpoint is regionalized). The services expose the following methods:
|
|
249
|
+
- `setHostname()`
|
|
250
|
+
- `setRegion()`
|
|
251
|
+
|
|
252
|
+
These methods can be used at service level (all the APIs will be updated) or at API domain level (for updating only one at a time):
|
|
253
|
+
|
|
254
|
+
**Use case #1:** overriding the hostname per API domain
|
|
255
|
+
|
|
256
|
+
```typescript
|
|
257
|
+
import { SinchClient } from '@sinch/sdk-core';
|
|
258
|
+
|
|
259
|
+
const sinch: SinchClient = new SinchClient({
|
|
260
|
+
projectId: 'my-project-id',
|
|
261
|
+
keyId: 'my-key-id',
|
|
262
|
+
keySecret: 'my-key-secret',
|
|
263
|
+
});
|
|
264
|
+
const numbersService = sinch.numbers;
|
|
265
|
+
numbersService.setHostname('https://my.new.server');
|
|
266
|
+
numbersService.activeNumber.setHostname('https://my.other.server');
|
|
267
|
+
// Result:
|
|
268
|
+
// numbers.availableRegions -> https://my.new.server
|
|
269
|
+
// numbers.availableNumber -> https://my.new.server
|
|
270
|
+
// numbers.activeNumber -> https://my.other.server
|
|
271
|
+
// numbers.callbacks -> https://my.new.server
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
**Use case #2:** overriding the production region per API domain
|
|
275
|
+
import { SinchClient } from '@sinch/sdk-core';
|
|
276
|
+
|
|
277
|
+
```typescript
|
|
278
|
+
import { SinchClient, VoiceRegion } from '@sinch/sdk-core';
|
|
279
|
+
|
|
280
|
+
const sinch: SinchClient = new SinchClient({
|
|
281
|
+
projectId: 'my-project-id',
|
|
282
|
+
keyId: 'my-key-id',
|
|
283
|
+
keySecret: 'my-key-secret',
|
|
80
284
|
});
|
|
81
|
-
const
|
|
285
|
+
const voiceService = sinch.voice;
|
|
286
|
+
voiceService.setRegion(VoiceRegion.SOUTH_AMERICA);
|
|
287
|
+
voiceService.calls.setRegion(VoiceRegion.SOUTHEAST_ASIA_2);
|
|
288
|
+
// Result:
|
|
289
|
+
// voice.applications -> https://callingapi.sinch.com (This one is not regionalized !)
|
|
290
|
+
// voice.conferences -> https://calling-sae1.api.sinch.com
|
|
291
|
+
// voice.calls -> https://calling-apse2.api.sinch.com
|
|
292
|
+
// voice.callouts -> https://calling-sae1.api.sinch.com
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### Request and response plugins addition
|
|
296
|
+
|
|
297
|
+
When instantiating a service, it creates an ApiClient behind the scene (find more in the [@sinch/sdk-client](../sdk-client/README.md) package) which contains some default plugins:
|
|
298
|
+
- request plugins: `VersionRequest` which will add a `user-agent` with the used version of the SDK and Node.js running the program
|
|
299
|
+
- response plugins:
|
|
300
|
+
- `TimezonePlugin` which will ensure that all the dates returned by the server contain a timezone
|
|
301
|
+
- `ExceptionPlugin` which will catch all invalid response from the server and format them in a common way to handle exceptions
|
|
302
|
+
|
|
303
|
+
On top of that, the Service will add its own plugins:
|
|
304
|
+
- for OAuth2 authentication: `Oauth2TokenRequest`
|
|
305
|
+
- for API Token authentication: `ApiTokenRequest`
|
|
306
|
+
- for Application Signed authentication: `XTimestampRequest` and `SigningRequest`
|
|
307
|
+
|
|
308
|
+
And on top on these plugins, it is possible to add even more of them (existing ones or custom ones) with the following properties of the `SinchClientParameters`:
|
|
309
|
+
- `requestPlugins`
|
|
310
|
+
- `responsePlugins`
|
|
311
|
+
|
|
312
|
+
```typescript
|
|
313
|
+
import { SinchClient, AdditionalHeadersRequest, buildHeader } from '@sinch/sdk-core';
|
|
82
314
|
|
|
83
|
-
const
|
|
84
|
-
|
|
315
|
+
const sinch: SinchClient = new SinchClient({
|
|
316
|
+
projectId: 'my-project-id',
|
|
317
|
+
keyId: 'my-key-id',
|
|
318
|
+
keySecret: 'my-key-secret',
|
|
319
|
+
requestPlugins: [
|
|
320
|
+
new AdditionalHeadersRequest({
|
|
321
|
+
headers: buildHeader('customHeader', 'customHeaderValue'),
|
|
322
|
+
}),
|
|
323
|
+
],
|
|
85
324
|
});
|
|
325
|
+
// => All the requests using services registered on this SinchClient will send an extra-header
|
|
86
326
|
```
|
|
87
327
|
|
|
88
328
|
## Examples
|
|
@@ -95,11 +335,11 @@ Developer Experience team: [devexp@sinch.com](mailto:devexp@sinch.com)
|
|
|
95
335
|
|
|
96
336
|
Here is the list of the Sinch API and there level of support by the Node.js SDK:
|
|
97
337
|
|
|
98
|
-
| API Category | API Name
|
|
99
|
-
|
|
100
|
-
| Messaging | SMS API
|
|
101
|
-
| | Conversation API
|
|
102
|
-
| | Fax API
|
|
103
|
-
| Voice and Video | Voice API
|
|
104
|
-
| Numbers & Connectivity | Numbers API
|
|
105
|
-
| Verification | Verification API
|
|
338
|
+
| API Category | API Name | Status |
|
|
339
|
+
|------------------------|------------------|:------:|
|
|
340
|
+
| Messaging | SMS API | ✅ |
|
|
341
|
+
| | Conversation API | ✅ |
|
|
342
|
+
| | Fax API | ✅ |
|
|
343
|
+
| Voice and Video | Voice API | ✅ |
|
|
344
|
+
| Numbers & Connectivity | Numbers API | ✅ |
|
|
345
|
+
| Verification | Verification API | ✅ |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sinch/sdk-core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Node.js client for the Sinch API platform",
|
|
5
5
|
"homepage": "",
|
|
6
6
|
"repository": {
|
|
@@ -29,12 +29,12 @@
|
|
|
29
29
|
"compile": "tsc --build --verbose"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@sinch/conversation": "^0.0.
|
|
33
|
-
"@sinch/fax": "^0.0.
|
|
34
|
-
"@sinch/numbers": "^0.0.
|
|
35
|
-
"@sinch/sms": "^0.0.
|
|
36
|
-
"@sinch/verification": "^0.0.
|
|
37
|
-
"@sinch/voice": "^0.0.
|
|
32
|
+
"@sinch/conversation": "^0.0.5",
|
|
33
|
+
"@sinch/fax": "^0.0.5",
|
|
34
|
+
"@sinch/numbers": "^0.0.5",
|
|
35
|
+
"@sinch/sms": "^0.0.5",
|
|
36
|
+
"@sinch/verification": "^0.0.5",
|
|
37
|
+
"@sinch/voice": "^0.0.5"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {},
|
|
40
40
|
"publishConfig": {
|