Skip to content

Commit 0f958e3

Browse files
committedSep 23, 2021
first commit
0 parents  commit 0f958e3

File tree

6 files changed

+198
-0
lines changed

6 files changed

+198
-0
lines changed
 

‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
dist
3+
*.log

‎package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "soap-req-json",
3+
"version": "1.0.0",
4+
"description": "Does a SOAP request and returns JSON string.",
5+
"main": "dist/index.js",
6+
"author": "Ervald Culoli",
7+
"license": "MIT",
8+
"devDependencies": {
9+
"@types/node": "^16.9.6",
10+
"typescript": "^4.4.3"
11+
},
12+
"dependencies": {
13+
"xml-js": "^1.6.11"
14+
}
15+
}

‎readme.md

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Soap Request To Json
2+
3+
Does a SOAP request and returns JSON string.
4+
5+
# Usage
6+
7+
## `Installation`
8+
9+
```
10+
npm install soap-req-json
11+
// or
12+
yarn add soap-req-json
13+
```
14+
15+
## `Quick Start`
16+
17+
```
18+
const srj = require('soap-req-json');
19+
20+
// SOAP Envelope
21+
const envelope = `
22+
<SOAP-ENV:Envelope
23+
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
24+
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
25+
<SOAP-ENV:Header>
26+
<t:Transaction :t="some-URI" SOAP-ENV:mustUnderstand="1">5</t:Transaction>
27+
</SOAP-ENV:Header>
28+
</SOAP-ENV:Envelope>`;
29+
30+
// URL
31+
const url = 'https://electrocommerce.org/abc/service';
32+
33+
// The SOAPAction HTTP Header Field
34+
const SOAPAction = 'https://electrocommerce.org/abc#MyMessage';
35+
36+
const jsonString = await srj({ envelope, url, SOAPAction })
37+
```
38+
39+
## `Options`
40+
41+
| Option | Type | Required |
42+
|--------------|--------------------------------------------|----------|
43+
| `envelope` | `string` SOAP Envelope | `true` |
44+
| `url` | `url` | `true` |
45+
| `SOAPAction` | `string` The SOAPAction HTTP Header Field. | `true` |
46+
| `timeout` | `number` Timeout request. Default `0` | `false` |
47+
48+
49+
## `Error messages`
50+
51+
| Message | Description |
52+
|-------------------|------------------------------|
53+
| `INVALID_URL` | URL could not be parsed. |
54+
| `REQUEST_TIMEOUT` | Request timeout. |
55+
| `SERVER_ERROR` | Server responded with error. |
56+
57+
58+
# Dependencies
59+
60+
## `dependencies`
61+
62+
| Package | Description |
63+
|---------|---------------------------------------------------------------------|
64+
| xml-js | Convert XML text to Javascript object / JSON text (and vice versa). |
65+
66+
67+
## `devDependencies`
68+
69+
| Package | Description |
70+
|-------------|----------------------------------------------------------------------|
71+
| @types/node | This package contains type definitions for Node.js |
72+
| typescript | JavaScript compiler/type checker that boosts JavaScript productivity |

‎src/index.ts

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
import { URL } from 'url';
3+
import { request } from 'https';
4+
import { xml2json } from 'xml-js';
5+
6+
export interface SoapRequesJsonParams {
7+
envelope: string;
8+
url: string;
9+
SOAPAction: string;
10+
timeout?: number;
11+
}
12+
13+
export enum ERRORS {
14+
INVALID_URL = 'INVALID_URL',
15+
REQUEST_TIMEOUT = 'REQUEST_TIMEOUT',
16+
SERVER_ERROR = 'SERVER_ERROR'
17+
}
18+
19+
const getOptions = (p: SoapRequesJsonParams) => {
20+
try {
21+
const url = new URL(p.url);
22+
const options = {
23+
hostname: url.host,
24+
port: url.port,
25+
path: url.pathname,
26+
method: 'POST',
27+
headers: {
28+
'Content-Type': 'text/xml; charset=utf-8',
29+
'Content-Length': p.envelope.length,
30+
'SOAPAction': p.SOAPAction
31+
},
32+
timeout: p.timeout ?? 0
33+
};
34+
35+
return options;
36+
37+
} catch (e) {
38+
throw new Error(ERRORS.INVALID_URL);
39+
}
40+
};
41+
42+
const soapRequestJson = async (p: SoapRequesJsonParams): Promise<string | Error> => {
43+
return new Promise((resolve, reject) => {
44+
const req = request(p.url, getOptions(p), (res) => {
45+
let data = '';
46+
res.on('data', (chunk) => data += chunk);
47+
res.on('end', () => {
48+
const json = xml2json(data, { compact: true, spaces: 2 });
49+
resolve(json);
50+
});
51+
});
52+
53+
req.on('error', (err) => {
54+
reject(new Error(ERRORS.SERVER_ERROR));
55+
});
56+
57+
req.on('timeout', () => {
58+
req.destroy();
59+
reject(new Error(ERRORS.REQUEST_TIMEOUT));
60+
});
61+
62+
req.write(p.envelope);
63+
req.end();
64+
});
65+
};
66+
67+
export default soapRequestJson;

‎tsconfig.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2020",
4+
"module": "commonjs",
5+
"removeComments": true,
6+
"moduleResolution": "node",
7+
"strict": true,
8+
"baseUrl": ".",
9+
"outDir": "dist",
10+
"declaration": true
11+
},
12+
"include": [
13+
"src/**/*"
14+
],
15+
"exclude": ["node_modules"],
16+
}

‎yarn.lock

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
"@types/node@^16.9.6":
6+
version "16.9.6"
7+
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.9.6.tgz#040a64d7faf9e5d9e940357125f0963012e66f04"
8+
integrity sha512-YHUZhBOMTM3mjFkXVcK+WwAcYmyhe1wL4lfqNtzI0b3qAy7yuSetnM7QJazgE5PFmgVTNGiLOgRFfJMqW7XpSQ==
9+
10+
sax@^1.2.4:
11+
version "1.2.4"
12+
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
13+
integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==
14+
15+
typescript@^4.4.3:
16+
version "4.4.3"
17+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.4.3.tgz#bdc5407caa2b109efd4f82fe130656f977a29324"
18+
integrity sha512-4xfscpisVgqqDfPaJo5vkd+Qd/ItkoagnHpufr+i2QCHBsNYp+G7UAoyFl8aPtx879u38wPV65rZ8qbGZijalA==
19+
20+
xml-js@^1.6.11:
21+
version "1.6.11"
22+
resolved "https://registry.yarnpkg.com/xml-js/-/xml-js-1.6.11.tgz#927d2f6947f7f1c19a316dd8eea3614e8b18f8e9"
23+
integrity sha512-7rVi2KMfwfWFl+GpPg6m80IVMWXLRjO+PxTq7V2CDhoGak0wzYzFgUY2m4XJ47OGdXd8eLE8EmwfAmdjw7lC1g==
24+
dependencies:
25+
sax "^1.2.4"

0 commit comments

Comments
 (0)
Please sign in to comment.