-
Notifications
You must be signed in to change notification settings - Fork 2
/
Fax.ts
84 lines (73 loc) · 2.49 KB
/
Fax.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import * as FormData from 'form-data';
import Binary from '../Binary';
import CallerInfo from '../definitions/CallerInfo';
import MessageInfo from '../definitions/MessageInfo';
import PathSegment from '../PathSegment';
import FaxBase from './FaxBase';
export default class Fax extends FaxBase {
constructor(prv: PathSegment, id?: string, service?) {
super(prv, id, service);
}
/**
* Create and Send Fax Message
*/
post(body: PostBody, attachments: Binary[]): Promise<MessageInfo> {
let meta = JSON.stringify(body);
let form = new FormData();
const jsonType = 'application/json';
if (inNode()) {
form.append('json', meta, { contentType: jsonType, filename: 'request.json' });
for (let atch of attachments) {
if (typeof atch === 'string') {
form.append('attachment', atch, { contentType: 'text/plain' });
} else {
form.append('attachment', atch);
}
}
} else if (browserSupportBlob()) {
form.append('json', new Blob([meta], { type: jsonType }));
for (let atch of attachments) {
if (typeof atch === 'string') {
form.append('attachment', new Blob([atch], { type: 'text/plain' }));
} else {
form.append('attachment', atch);
}
}
} else {
return Promise.reject('Your\'re not in node and your environment does not support Blob or File API.');
}
return this.getRest().post(this.getEndpoint(false), form).then<any>((res) => res.json());
}
}
export interface PostBody {
/**
* Recipient information. Phone number property is mandatory. Optional for resend fax request
*/
to?: CallerInfo[];
/**
* Fax resolution
*/
faxResolution?: 'High' | 'Low';
/**
* The datetime to send fax at, in ISO 8601 format including timezone, for example 2016-03-10T18:07:52.534Z. If time is not specified, the fax will be send immediately
*/
sendTime?: string;
/**
* Optional. Cover page index. If not specified, the default cover page which is configured in "Outbound Fax Settings" is attached. See also 'Available Cover Pages' table below
*/
coverIndex?: number;
/**
* Optional. Cover page text, entered by the fax sender and printed on the cover page. Maximum length is limited to 1024 symbols
*/
coverPageText?: string;
/**
* Internal identifier of the original fax message which needs to be resent. Mandatory for resend fax request
*/
originalMessageId?: string;
}
function inNode(): boolean {
return typeof process !== 'undefined' && !process['browser'];
}
function browserSupportBlob(): boolean {
return typeof Blob === 'function';
}