|
| 1 | +import fs from 'fs'; |
| 2 | + |
1 | 3 | export interface Transport { |
2 | 4 | encode(hex: string): string; |
3 | 5 | decode(data: string): string; |
4 | 6 | getEndpoint(network?: number | string, countriesList?: string | Array<string>): { [key: string]: Array<string> }; |
5 | 7 | sms(number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string; |
6 | 8 | mms(number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string; |
7 | 9 | generateMessageUri(type: 'sms' | 'mms', number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string; |
| 10 | + downloadMessage(hex: string, optionalFilename?: string): void; |
8 | 11 | } |
9 | 12 |
|
10 | 13 | export interface Error extends globalThis.Error { |
@@ -39,6 +42,14 @@ export function addCountry(networkId: number | string, countryCode: string, phon |
39 | 42 | countries[networkKey][countryCode] = phoneNumbers; |
40 | 43 | } |
41 | 44 |
|
| 45 | +function slugify(str: string): string { |
| 46 | + return str |
| 47 | + .toLowerCase() |
| 48 | + .replace(/[^\w\s-]/g, '') |
| 49 | + .replace(/[\s_-]+/g, '-') |
| 50 | + .replace(/^-+|-+$/g, ''); |
| 51 | +} |
| 52 | + |
42 | 53 | const txms: Transport = { |
43 | 54 | encode(hex: string): string { |
44 | 55 | let data = ''; |
@@ -156,6 +167,21 @@ const txms: Transport = { |
156 | 167 | } |
157 | 168 |
|
158 | 169 | return endpoint ? `${type}:${endpoint}${encodedMessage ? `${platform === 'ios' ? '&' : '?'}body=${encodedMessage}` : ''}` : `${type}:${platform === 'ios' ? '&' : '?'}body=${encodedMessage}`; |
| 170 | + }, |
| 171 | + |
| 172 | + downloadMessage(hex: string, optionalFilename?: string): void { |
| 173 | + const encodedMessage = this.encode(hex); |
| 174 | + |
| 175 | + let filename: string; |
| 176 | + const cleanedHex = hex.startsWith('0x') ? hex.slice(2) : hex; |
| 177 | + const first6 = cleanedHex.slice(0, 6); |
| 178 | + const last6 = cleanedHex.slice(-6); |
| 179 | + filename = `${first6}${last6}.txms.txt`; |
| 180 | + if (optionalFilename) { |
| 181 | + filename = `${slugify(filename)}.txms.txt`; |
| 182 | + } |
| 183 | + const buffer = Buffer.from(encodedMessage, 'utf16le').swap16(); |
| 184 | + fs.writeFileSync(filename, buffer); |
159 | 185 | } |
160 | 186 | }; |
161 | 187 |
|
|
0 commit comments