Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Fix response parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
rosahaj committed May 4, 2024
1 parent 701c129 commit f6cc1bf
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"prepare": "simple-git-hooks"
},
"dependencies": {
"cheerio": "1.0.0-rc.12",
"ky": "^1.2.4",
"undici": "^6.15.0",
"xml-js": "^1.6.11"
Expand Down
108 changes: 108 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export enum ErrorCode {
InvalidCredentials = 'INVALID_CREDENTIALS',
InvalidFaxNumber = 'INVALID_FAX_NUMBER',
InsufficientBalance = 'INSUFFICIENT_BALANCE',
InvalidDocument = 'INVALID_DOCUMENT',
FaultyDocument = 'FAULTY_DOCUMENT',
InternalError = 'INTERNAL_ERROR',
Other = 'OTHER',
}
Expand All @@ -28,7 +28,7 @@ export class SimpleFaxError extends Error {
break

case 'Dokument fehlerhaft':
this.code = ErrorCode.InsufficientBalance
this.code = ErrorCode.FaultyDocument
break

default:
Expand Down
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { js2xml } from 'xml-js'
import type { Options } from 'ky'
import ky from 'ky'
import { request } from 'undici'
import * as cheerio from 'cheerio'
import { SimpleFaxError } from './errors.js'
import type { EnvelopeOptions } from './types.js'
import template from './template.json'
Expand Down Expand Up @@ -38,16 +37,20 @@ export async function sendFax(
): Promise<string> {
const envelope = createEnvelope(options)

const response = await request('http://longisland.simple-fax.de/soap/index.php', {
const xml = await request('https://longisland.simple-fax.de/soap/index.php', {
method: 'POST',
body: envelope,
headers: {
'Content-Type': 'text/xml;charset=UTF-8',
'SOAPAction': `"urn:soapservice#Service#sendfax"`,
},
}).then(({ body }) => body.text())

const $ = cheerio.load(xml)
const response = $('ns4\\:sendfaxResponse').text()

// Response format: "status;message."
const [status, message] = response.trim().replace(/\.$/, '').split(';')
const [status, message] = response.trim().replace(/\..*$/, '').split(';')

if (status === 'success')
return message
Expand Down
7 changes: 6 additions & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ export interface EnvelopeOptions {
* Callback URL
*
* After transmission, a `GET` request with the following parameters is sent to the callback URL:
* - `faxid`
* - `faxid` (internal suid rather than the fax ID displayed in the "Versandbox")
* - `status` (either "done" or "failed")
* - `reason` (error message)
* - `timestamp` (unix timestamp in seconds)
* - `totpages` (number of pages)
* - `tsi` (unknown parameter)
* - `number` (recipient fax number)
* - `price` (price in `-x.xx` format)
*/
statusurl: string
}

0 comments on commit f6cc1bf

Please sign in to comment.