Skip to content

Commit

Permalink
Merge branch 'v1.2-next' into DEVEXP-446_Refactor-models-in-Verificat…
Browse files Browse the repository at this point in the history
…ion-SDK
  • Loading branch information
asein-sinch authored Jun 4, 2024
2 parents 2efe57a + 6d1a495 commit 0c09c1d
Show file tree
Hide file tree
Showing 32 changed files with 655 additions and 187 deletions.
3 changes: 2 additions & 1 deletion examples/simple-examples/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
.DS_Store

.env
fax-pdf/
fax-pdf/*
!fax-pdf/you-faxed.pdf
40 changes: 21 additions & 19 deletions examples/simple-examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,25 +255,27 @@ yarn run numbers:regions:list

### Fax

| Service | Sample application name and location | Required parameters |
|----------|----------------------------------------------------------------------------------------|-----------------------------------|
| Services | [./src/fax/services/create.ts](./src/fax/services/create.ts) | `PHONE_NUMBER` |
| | [./src/fax/services/get.ts](./src/fax/services/get.ts) | `FAX_SERVICE_ID` |
| | [./src/fax/services/list.ts](./src/fax/services/list.ts) | |
| | [./src/fax/services/listNumbers.ts](./src/fax/services/listNumbers.ts) | `FAX_SERVICE_ID` |
| | [./src/fax/services/listEmailsForNumber.ts](./src/fax/services/listEmailsForNumber.ts) | `PHONE_NUMBER` + `FAX_SERVICE_ID` |
| | [./src/fax/services/update.ts](./src/fax/services/update.ts) | `FAX_SERVICE_ID` |
| | [./src/fax/services/delete.ts](./src/fax/services/delete.ts) | `FAX_SERVICE_ID` |
| Faxes | [./src/fax/faxes/send.ts](./src/fax/faxes/send.ts) | `PHONE_NUMBER` |
| | [./src/fax/faxes/get.ts](./src/fax/faxes/get.ts) | `FAX_ID` |
| | [./src/fax/faxes/list.ts](./src/fax/faxes/list.ts) | |
| | [./src/fax/faxes/downloadContent.ts](./src/fax/faxes/downloadContent.ts) | `FAX_ID` |
| | [./src/fax/faxes/deleteContent.ts](./src/fax/faxes/deleteContent.ts) | `FAX_ID` |
| Emails | [./src/fax/emails/add.ts](./src/fax/emails/add.ts) | `FAX_EMAIL` + `PHONE_NUMBER` |
| | [./src/fax/emails/list.ts](./src/fax/emails/list.ts) | |
| | [./src/fax/emails/listNumbers.ts](./src/fax/emails/listNumbers.ts) | `FAX_EMAIL` |
| | [./src/fax/emails/update.ts](./src/fax/emails/update.ts) | `FAX_EMAIL` + `PHONE_NUMBER` |
| | [./src/fax/emails/delete.ts](./src/fax/emails/delete.ts) | `FAX_EMAIL` |
| Service | Sample application name and location | Required parameters |
|----------|------------------------------------------------------------------------------------------|-------------------------------------|
| Services | [./src/fax/services/create.ts](./src/fax/services/create.ts) | `PHONE_NUMBER` |
| | [./src/fax/services/get.ts](./src/fax/services/get.ts) | `FAX_SERVICE_ID` |
| | [./src/fax/services/list.ts](./src/fax/services/list.ts) | |
| | [./src/fax/services/listNumbers.ts](./src/fax/services/listNumbers.ts) | `FAX_SERVICE_ID` |
| | [./src/fax/services/listEmailsForNumber.ts](./src/fax/services/listEmailsForNumber.ts) | `PHONE_NUMBER` + `FAX_SERVICE_ID` |
| | [./src/fax/services/update.ts](./src/fax/services/update.ts) | `FAX_SERVICE_ID` |
| | [./src/fax/services/delete.ts](./src/fax/services/delete.ts) | `FAX_SERVICE_ID` |
| Faxes | [./src/fax/faxes/send-filePaths.ts](./src/fax/faxes/send-filePaths.ts) | `PHONE_NUMBER` + `FAX_CALLBACK_URL` |
| | [./src/fax/faxes/send-fileBase64.ts](./src/fax/faxes/send-fileBase64.ts) | `PHONE_NUMBER` + `FAX_CALLBACK_URL` |
| | [./src/fax/faxes/send-multipleRecipients.ts](./src/fax/faxes/send-multipleRecipients.ts) | `PHONE_NUMBER` + `FAX_CALLBACK_URL` |
| | [./src/fax/faxes/get.ts](./src/fax/faxes/get.ts) | `FAX_ID` |
| | [./src/fax/faxes/list.ts](./src/fax/faxes/list.ts) | |
| | [./src/fax/faxes/downloadContent.ts](./src/fax/faxes/downloadContent.ts) | `FAX_ID` |
| | [./src/fax/faxes/deleteContent.ts](./src/fax/faxes/deleteContent.ts) | `FAX_ID` |
| Emails | [./src/fax/emails/add.ts](./src/fax/emails/add.ts) | `FAX_EMAIL` + `PHONE_NUMBER` |
| | [./src/fax/emails/list.ts](./src/fax/emails/list.ts) | |
| | [./src/fax/emails/listNumbers.ts](./src/fax/emails/listNumbers.ts) | `FAX_EMAIL` |
| | [./src/fax/emails/update.ts](./src/fax/emails/update.ts) | `FAX_EMAIL` + `PHONE_NUMBER` |
| | [./src/fax/emails/delete.ts](./src/fax/emails/delete.ts) | `FAX_EMAIL` |

### Elastic SIP Trunk

Expand Down
Binary file added examples/simple-examples/fax-pdf/you-faxed.pdf
Binary file not shown.
4 changes: 3 additions & 1 deletion examples/simple-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@
"fax:services:listEmailsForNumber": "ts-node src/fax/services/listEmailsForNumber.ts",
"fax:services:update": "ts-node src/fax/services/update.ts",
"fax:services:delete": "ts-node src/fax/services/delete.ts",
"fax:faxes:send": "ts-node src/fax/faxes/send.ts",
"fax:faxes:send-filePaths": "ts-node src/fax/faxes/send-filePaths.ts",
"fax:faxes:send-fileBase64": "ts-node src/fax/faxes/send-fileBase64.ts",
"fax:faxes:send-multipleRecipients": "ts-node src/fax/faxes/send-multipleRecipients.ts",
"fax:faxes:get": "ts-node src/fax/faxes/get.ts",
"fax:faxes:list": "ts-node src/fax/faxes/list.ts",
"fax:faxes:download": "ts-node src/fax/faxes/downloadContent.ts",
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-examples/src/fax/faxes/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { getFaxIdFromConfig, getPrintFormat, initFaxService, printFullResponse }
if (printFormat === 'pretty') {
console.log(`Fax found: it has been created at '${response.createTime}' and the status is '${response.status}'`);
if (response.status === 'FAILURE') {
console.log(`Error type: ${response.errorType} (${response.errorId}): ${response.errorCode}`);
console.log(`Error type: ${response.errorType} (${response.errorCode}): ${response.errorMessage}`);
}
} else {
printFullResponse(response);
Expand Down
36 changes: 35 additions & 1 deletion examples/simple-examples/src/fax/faxes/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,42 @@ const populateFaxesList = (
console.log('* getFaxes *');
console.log('************');

const today = new Date();
const previousMonth = (today.getUTCMonth() - 1) % 12;
const lastMonth = new Date().setMonth(previousMonth);

// Example of createTime filter
// - fetch last month's faxes (with Date objects)
// => Output = ?createTime>=2024-04-24T11:33:44Z&createTime<=2024-05-24T11:33:44Z
// createTimeFilter: {
// from: new Date(lastMonth),
// to: new Date(),
// }
// - fetch last month's faxes (with DateFormat objets)
// => Output = ?createTime>=2024-04-24&createTime<=2024-05-24
// createTimeFilter: {
// from: {
// date: new Date(lastMonth),
// unit: 'day',
// },
// to: {
// date: new Date(),
// unit: 'day',
// },
// }

const requestData: Fax.ListFaxesRequestData = {
pageSize: 2,
pageSize: 10,
createTimeRange: {
from: {
date: new Date(lastMonth),
unit: 'day',
},
to: {
date: new Date(),
unit: 'day',
},
},
};

const faxService = initFaxService();
Expand Down
60 changes: 60 additions & 0 deletions examples/simple-examples/src/fax/faxes/send-fileBase64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Fax } from '@sinch/sdk-core';
import {
getFaxCallbackUrlFromConfig,
getPhoneNumberFromConfig,
getPrintFormat,
initFaxService,
printFullResponse,
} from '../../config';
import * as fs from 'fs';

const getBase64 = (filePath: string): Fax.FaxBase64File => {
const fileExtension = filePath.split('.').pop()?.toUpperCase();
try {
const fileBuffer = fs.readFileSync(filePath);
return {
file: fileBuffer.toString('base64'),
fileType: Fax.convertToSupportedFileType(fileExtension),
};
} catch (error) {
console.error('Error reading or converting the file:', error);
throw error;
}
};

(async () => {
console.log('*************************');
console.log('* sendFax - base64 file *');
console.log('*************************');

const originPhoneNumber = getPhoneNumberFromConfig();
const destinationPhoneNumber = getPhoneNumberFromConfig();
const faxCallbackUrl = getFaxCallbackUrlFromConfig();

const requestData: Fax.SendSingleFaxRequestData = {
sendFaxRequestBody: {
to: destinationPhoneNumber,
from: originPhoneNumber,
contentUrl: 'https://developers.sinch.com/',
files: [getBase64('./fax-pdf/you-faxed.pdf')],
callbackUrl: faxCallbackUrl,
callbackUrlContentType: 'application/json',
imageConversionMethod: 'MONOCHROME',
headerTimeZone: 'Europe/Paris',
headerText: ' - Sent with the Node.js SDK',
maxRetries: 2,
},
};

const faxService = initFaxService();
const response = await faxService.faxes.send(requestData);

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
const fax = response[0];
console.log(`Fax successfully created at '${fax.createTime?.toISOString()}'. Status = '${fax.status}'`);
} else {
printFullResponse(response);
}
})();
45 changes: 45 additions & 0 deletions examples/simple-examples/src/fax/faxes/send-filePaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Fax } from '@sinch/sdk-core';
import {
getFaxCallbackUrlFromConfig,
getPhoneNumberFromConfig,
getPrintFormat,
initFaxService,
printFullResponse,
} from '../../config';

(async () => {
console.log('**********************');
console.log('* sendFax - filePath *');
console.log('**********************');

const originPhoneNumber = getPhoneNumberFromConfig();
const destinationPhoneNumber = getPhoneNumberFromConfig();
const faxCallbackUrl = getFaxCallbackUrlFromConfig();

const requestData: Fax.SendSingleFaxRequestData = {
sendFaxRequestBody: {
to: destinationPhoneNumber,
from: originPhoneNumber,
contentUrl: 'https://developers.sinch.com/',
filePaths: ['./fax-pdf/you-faxed.pdf', './fax-pdf/you-faxed.pdf'],
callbackUrl: faxCallbackUrl,
callbackUrlContentType: 'application/json',
imageConversionMethod: 'MONOCHROME',
headerTimeZone: 'Europe/Paris',
headerText: ' - Sent with the Node.js SDK',
maxRetries: 2,
},
};

const faxService = initFaxService();
const response = await faxService.faxes.send(requestData);

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
const fax = response[0];
console.log(`Fax successfully created at '${fax.createTime?.toISOString()}'. Status = '${fax.status}'`);
} else {
printFullResponse(response);
}
})();
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@ import {
} from '../../config';

(async () => {
console.log('***********');
console.log('* sendFax *');
console.log('***********');
console.log('*********************************');
console.log('* sendFax - multiple recipients *');
console.log('*********************************');

const originPhoneNumber = getPhoneNumberFromConfig();
const destinationPhoneNumber = getPhoneNumberFromConfig();
const faxCallbackUrl = getFaxCallbackUrlFromConfig();

const requestData: Fax.SendFaxRequestData = {
const requestData: Fax.SendMultipleFaxRequestData = {
sendFaxRequestBody: {
to: destinationPhoneNumber,
to: [destinationPhoneNumber, destinationPhoneNumber],
from: originPhoneNumber,
contentUrl: 'https://developers.sinch.com/fax/fax.pdf',
callbackUrl: faxCallbackUrl,
callbackContentType: 'application/json',
callbackUrlContentType: 'application/json',
imageConversionMethod: 'MONOCHROME',
headerTimeZone: 'Europe/Paris',
headerText: ' - Sent with the Node.js SDK',
maxRetries: 2,
},
};

Expand All @@ -32,7 +36,7 @@ import {
const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
console.log(`Fax successfully created at '${response.createTime?.toISOString()}'. Status = '${response.status}'`);
console.log(`${response.length} fax(es) successfully created.\n${response.map((fax) => ' - ' + fax.id + ' created at ' + fax.createTime?.toISOString()).join('\n')}`);
} else {
printFullResponse(response);
}
Expand Down
10 changes: 4 additions & 6 deletions examples/webhooks/src/services/fax-event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ export class FaxEventService {
}
if (event.event === 'FAX_COMPLETED') {
const faxCompletedEvent = event as Fax.FaxCompletedEventJson;
for (const fileBase64 of faxCompletedEvent.files!) {
this.saveBase64File(fileBase64, event.fax!.id!);
}
this.saveBase64File(faxCompletedEvent, event.fax!.id!);
}
} else if (contentType?.includes('multipart/form-data')) {
console.log(`** multipart/form-data\n${event.event}: ${event.fax!.id} - ${event.eventTime}`);
Expand All @@ -28,10 +26,10 @@ export class FaxEventService {
}
}

private saveBase64File(fileBase64: Fax.FaxBase64File, faxId: string) {
private saveBase64File(event: Fax.IncomingFaxEventJson | Fax.FaxCompletedEventJson, faxId: string) {
console.log('Saving file...');
const filePath = path.join('./fax-upload', faxId + '.' + fileBase64.fileType!.toLowerCase());
const buffer = Buffer.from(fileBase64.file!, 'base64');
const filePath = path.join('./fax-upload', event.event + '-' + faxId + '.' + event.fileType!.toLowerCase());
const buffer = Buffer.from(event.file!, 'base64');
fs.writeFileSync(filePath, buffer);
console.log('File saved! ' + filePath);
}
Expand Down
18 changes: 18 additions & 0 deletions packages/fax/src/models/v3/date-range-filter/date-range-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { DateFormat } from '@sinch/sdk-client';

/**
* Filter calls based on `createTime`. If not value is submitted, the default value is the prior week.
* - `from: '2024-02-15'` will return all faxes from February 2024, 15th
* - `from: '2024-02-01T14:00:00Z'` will return all faxes after 14:00:00 on the first of February 2024.
* - `from: '2024-02-01T14:00:00Z'` + `to: '2024-02-01T15:00:00Z'` will return all faxes between 14:00:00 and 15:00:00 (inclusive) on the first of February 2024.
* - `from: '2024-02-01'` + `to: '2024-02-29'` will return all faxes for all of February 2024.
*
* Note: It is also possible to submit partial dates.
* - `from: '2024-02'` will return all faxes for February 2024
*/
export interface DateRangeFilter {
/** */
from?: string | Date | DateFormat;
/** */
to?: string | Date | DateFormat;
}
1 change: 1 addition & 0 deletions packages/fax/src/models/v3/date-range-filter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { DateRangeFilter } from './date-range-filter';
13 changes: 4 additions & 9 deletions packages/fax/src/models/v3/enums.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
// export type { TypeEnum as BarCodeTypeEnum } from './bar-code/bar-code';
// export type { TypeEnum as CallErrorTypeEnum, ErrorCodeEnum as CallErrorErrorCodeEnum } from './call-error/call-error';
// export type { TypeEnum as DocumentConversionErrorTypeEnum, ErrorCodeEnum as DocumentConversionErrorErrorCodeEnum } from './document-conversion-error/document-conversion-error';
// export type { CallbackContentTypeEnum as FaxCallbackContentTypeEnum, ImageConversionMethodEnum as FaxImageConversionMethodEnum } from './fax/fax';
// export type { FileTypeEnum as FaxBase64FileFileTypeEnum } from './fax-base64-file/fax-base64-file';
// export type { TypeEnum as FaxErrorTypeEnum } from './fax-error/fax-error';
// export type { CallbackContentTypeEnum as SendFaxRequest1CallbackContentTypeEnum, ImageConversionMethodEnum as SendFaxRequest1ImageConversionMethodEnum } from './send-fax-request1/send-fax-request1';

export type ImageConversionMethod = 'HALFTONE' | 'MONOCHROME';

export type WebhookContentType = 'multipart/form-data' | 'application/json';
Expand All @@ -21,7 +13,7 @@ export type FaxDirection = 'OUTBOUND' | 'INBOUND';
export type FaxStatus = 'QUEUED' | 'IN_PROGRESS' | 'COMPLETED' | 'FAILURE';

/**
* Error type
* Type of error for the fax
*/
export type ErrorType =
'DOCUMENT_CONVERSION_ERROR'
Expand All @@ -33,4 +25,7 @@ export type ErrorType =

export type FaxBase64FileType = 'DOC' | 'DOCX' | 'PDF' | 'TIF' | 'JPG' | 'ODT' | 'TXT' | 'HTML' | 'PNG';

export const validBase64FileTypes: FaxBase64FileType[]
= ['DOC', 'DOCX', 'PDF', 'TIF', 'JPG', 'ODT', 'TXT', 'HTML', 'PNG'];

export type FaxWebhookEvent = 'INCOMING_FAX' | 'FAX_COMPLETED';
34 changes: 28 additions & 6 deletions packages/fax/src/models/v3/fax-request/fax-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { ImageConversionMethod, WebhookContentType } from '../enums';
import { FaxContentUrl } from '../fax-content-url';
import { FaxBase64File } from '../fax-base64-file';

export type FaxRequest = FaxRequestJson | FaxRequestFormData;
export type SingleFaxRequest = SingleFaxRequestJson | SingleFaxRequestFormData;

interface FaxRequestBase {
/** A phone number in [E.164](https://community.sinch.com/t5/Glossary/E-164/ta-p/7537) format, including the leading '+'. */
to: string;
export type MultipleFaxRequest = MultipleFaxRequestJson | MultipleFaxRequestFormData;

export interface FaxRequestBase {
/** A phone number in [E.164](https://community.sinch.com/t5/Glossary/E-164/ta-p/7537) format, including the leading '+'. */
from?: string;
/** */
Expand All @@ -24,7 +24,7 @@ interface FaxRequestBase {
/** The URL to which a callback will be sent when the fax is completed. The callback will be sent as a POST request with a JSON body. The callback will be sent to the URL specified in the `callbackUrl` parameter, if provided, otherwise it will be sent to the URL specified in the `callbackUrl` field of the Fax Service object. */
callbackUrl?: string;
/** The content type of the callback. */
callbackContentType?: WebhookContentType;
callbackUrlContentType?: WebhookContentType;
/** Determines how documents are converted to black and white. Defaults to value selected on Fax Service object. */
imageConversionMethod?: ImageConversionMethod;
/** ID of the fax service used. */
Expand All @@ -36,9 +36,31 @@ interface FaxRequestBase {
export type FaxRequestJson = FaxRequestBase & {
/** An array of base64 encoded files */
files: FaxBase64File[];
filePaths?: never;
}

export type SingleFaxRequestJson = FaxRequestJson & {
/** A phone number in [E.164](https://community.sinch.com/t5/Glossary/E-164/ta-p/7537) format, including the leading '+'. */
to: string;
}

export type MultipleFaxRequestJson = FaxRequestJson & {
/** A list of phone number in [E.164](https://community.sinch.com/t5/Glossary/E-164/ta-p/7537) format, including the leading '+'. */
to: string[];
}

export interface FaxRequestFormData extends FaxRequestBase {
/** The file(s) you want to send as a fax as body attachment. */
file?: any;
filePaths?: string | string[];
files?: never;
}

export type SingleFaxRequestFormData = FaxRequestFormData & {
/** A phone number in [E.164](https://community.sinch.com/t5/Glossary/E-164/ta-p/7537) format, including the leading '+'. */
to: string;
}

export type MultipleFaxRequestFormData = FaxRequestFormData & {
/** A list of phone number in [E.164](https://community.sinch.com/t5/Glossary/E-164/ta-p/7537) format, including the leading '+'. */
to: string[];
}
Loading

0 comments on commit 0c09c1d

Please sign in to comment.