@iota-pico/pal-nodejs
Advanced tools
Comparing version 0.6.0 to 0.6.1
# Changelog | ||
## v0.6.0 | ||
## v0.6.1 | ||
* Explicit initialization of PAL added. | ||
@@ -6,0 +7,0 @@ ## v0.5.2 |
/** | ||
* Combined index of all the modules. | ||
*/ | ||
export * from "./pal"; | ||
export * from "./network/networkClient"; |
@@ -9,2 +9,3 @@ "use strict"; | ||
*/ | ||
__export(require("./pal")); | ||
__export(require("./network/networkClient")); |
@@ -14,3 +14,30 @@ import { INetworkClient } from "@iota-pico/core/dist/interfaces/INetworkClient"; | ||
/** | ||
* Get data asynchronously. | ||
* @param data The data to send. | ||
* @param additionalHeaders Extra headers to send with the request. | ||
* @returns Promise which resolves to the object returned or rejects with error. | ||
*/ | ||
get(additionalHeaders?: { | ||
[header: string]: string; | ||
}): Promise<string>; | ||
/** | ||
* Post data asynchronously. | ||
* @param data The data to send. | ||
* @param additionalHeaders Extra headers to send with the request. | ||
* @returns Promise which resolves to the object returned or rejects with error. | ||
*/ | ||
post(data: string, additionalHeaders?: { | ||
[header: string]: string; | ||
}): Promise<string>; | ||
/** | ||
* Get data asynchronously. | ||
* @typeparam U The generic type for the returned object. | ||
* @param additionalHeaders Extra headers to send with the request. | ||
* @returns Promise which resolves to the object returned or rejects with error. | ||
*/ | ||
getJson<U>(additionalHeaders?: { | ||
[header: string]: string; | ||
}): Promise<U>; | ||
/** | ||
* Post data asynchronously. | ||
* @typeparam T The generic type for the object to send. | ||
@@ -17,0 +44,0 @@ * @typeparam U The generic type for the returned object. |
@@ -28,3 +28,42 @@ "use strict"; | ||
/** | ||
* Get data asynchronously. | ||
* @param data The data to send. | ||
* @param additionalHeaders Extra headers to send with the request. | ||
* @returns Promise which resolves to the object returned or rejects with error. | ||
*/ | ||
async get(additionalHeaders) { | ||
return this.doRequest("GET", undefined, additionalHeaders); | ||
} | ||
/** | ||
* Post data asynchronously. | ||
* @param data The data to send. | ||
* @param additionalHeaders Extra headers to send with the request. | ||
* @returns Promise which resolves to the object returned or rejects with error. | ||
*/ | ||
async post(data, additionalHeaders) { | ||
return this.doRequest("POST", data, additionalHeaders); | ||
} | ||
/** | ||
* Get data asynchronously. | ||
* @typeparam U The generic type for the returned object. | ||
* @param additionalHeaders Extra headers to send with the request. | ||
* @returns Promise which resolves to the object returned or rejects with error. | ||
*/ | ||
async getJson(additionalHeaders) { | ||
return this.doRequest("GET", undefined, additionalHeaders) | ||
.then((responseData) => { | ||
try { | ||
const response = JSON.parse(responseData); | ||
return response; | ||
} | ||
catch (err) { | ||
throw (new coreError_1.CoreError("Failed POST request, unable to parse response", { | ||
endPoint: this._networkEndPoint.getUri(), | ||
response: responseData | ||
})); | ||
} | ||
}); | ||
} | ||
/** | ||
* Post data asynchronously. | ||
* @typeparam T The generic type for the object to send. | ||
@@ -37,11 +76,28 @@ * @typeparam U The generic type for the returned object. | ||
async postJson(data, additionalHeaders) { | ||
const headers = additionalHeaders || {}; | ||
headers["Content-Type"] = "application/json"; | ||
return this.doRequest("POST", JSON.stringify(data), additionalHeaders) | ||
.then((responseData) => { | ||
try { | ||
const response = JSON.parse(responseData); | ||
return response; | ||
} | ||
catch (err) { | ||
throw (new coreError_1.CoreError("Failed POST request, unable to parse response", { | ||
endPoint: this._networkEndPoint.getUri(), | ||
response: responseData | ||
})); | ||
} | ||
}); | ||
} | ||
/* @internal */ | ||
async doRequest(method, data, additionalHeaders) { | ||
return new Promise((resolve, reject) => { | ||
const headers = additionalHeaders || {}; | ||
headers["Content-Type"] = "application/json"; | ||
const options = { | ||
protocol: `${this._networkEndPoint.getProtocol()}:`, | ||
protocol: this._networkEndPoint.getProtocol() ? `${this._networkEndPoint.getProtocol()}:` : undefined, | ||
hostname: this._networkEndPoint.getHost(), | ||
port: this._networkEndPoint.getPort(), | ||
path: this._networkEndPoint.getPath(), | ||
method: "POST", | ||
method: method, | ||
headers | ||
@@ -57,16 +113,6 @@ }; | ||
if (res.statusCode === 200) { | ||
try { | ||
const response = JSON.parse(responseData); | ||
resolve(response); | ||
} | ||
catch (err) { | ||
reject(new coreError_1.CoreError("Unsuccessful POST request, unable to parse response", { | ||
endPoint: this._networkEndPoint.getUri(), | ||
httpStatusCode: res.statusCode, | ||
response: responseData | ||
})); | ||
} | ||
resolve(responseData); | ||
} | ||
else { | ||
reject(new coreError_1.CoreError("Unsuccessful POST request", { | ||
reject(new coreError_1.CoreError(`Failed ${method} request`, { | ||
endPoint: this._networkEndPoint.getUri(), | ||
@@ -80,3 +126,3 @@ httpStatusCode: res.statusCode, | ||
req.on("error", (err) => { | ||
reject(new coreError_1.CoreError("Unsuccessful POST request", { | ||
reject(new coreError_1.CoreError(`Failed ${method} request`, { | ||
endPoint: this._networkEndPoint.getUri(), | ||
@@ -86,3 +132,5 @@ httpError: err | ||
}); | ||
req.write(JSON.stringify(data)); | ||
if (data !== undefined && data !== null) { | ||
req.write(data); | ||
} | ||
req.end(); | ||
@@ -89,0 +137,0 @@ }); |
@@ -25,2 +25,5 @@ [@iota-pico/pal-nodejs](../README.md) > [NetworkClient](../classes/networkclient.md) | ||
* [get](networkclient.md#get) | ||
* [getJson](networkclient.md#getjson) | ||
* [post](networkclient.md#post) | ||
* [postJson](networkclient.md#postjson) | ||
@@ -38,3 +41,3 @@ | ||
*Defined in [network/networkClient.ts:12](https://github.com/iotaeco/iota-pico-pal-nodejs/blob/1f97095/src/network/networkClient.ts#L12)* | ||
*Defined in [network/networkClient.ts:12](https://github.com/iotaeco/iota-pico-pal-nodejs/blob/d3d6b82/src/network/networkClient.ts#L12)* | ||
@@ -62,2 +65,114 @@ | ||
## Methods | ||
<a id="get"></a> | ||
### get | ||
► **get**(additionalHeaders?: *`object`*): `Promise`.<`string`> | ||
*Defined in [network/networkClient.ts:31](https://github.com/iotaeco/iota-pico-pal-nodejs/blob/d3d6b82/src/network/networkClient.ts#L31)* | ||
Get data asynchronously. | ||
**Parameters:** | ||
| Param | Type | Description | | ||
| ------ | ------ | ------ | | ||
| additionalHeaders | `object` | Extra headers to send with the request. | | ||
**Returns:** `Promise`.<`string`> | ||
Promise which resolves to the object returned or rejects with error. | ||
___ | ||
<a id="getjson"></a> | ||
### getJson | ||
► **getJson**U(additionalHeaders?: *`object`*): `Promise`.<`U`> | ||
*Defined in [network/networkClient.ts:51](https://github.com/iotaeco/iota-pico-pal-nodejs/blob/d3d6b82/src/network/networkClient.ts#L51)* | ||
Get data asynchronously. | ||
**Type parameters:** | ||
#### U | ||
The generic type for the returned object. | ||
**Parameters:** | ||
| Param | Type | Description | | ||
| ------ | ------ | ------ | | ||
| additionalHeaders | `object` | Extra headers to send with the request. | | ||
**Returns:** `Promise`.<`U`> | ||
Promise which resolves to the object returned or rejects with error. | ||
___ | ||
<a id="post"></a> | ||
### post | ||
► **post**(data: *`string`*, additionalHeaders?: *`object`*): `Promise`.<`string`> | ||
*Defined in [network/networkClient.ts:41](https://github.com/iotaeco/iota-pico-pal-nodejs/blob/d3d6b82/src/network/networkClient.ts#L41)* | ||
Post data asynchronously. | ||
**Parameters:** | ||
| Param | Type | Description | | ||
| ------ | ------ | ------ | | ||
| data | `string` | The data to send. | | ||
| additionalHeaders | `object` | Extra headers to send with the request. | | ||
**Returns:** `Promise`.<`string`> | ||
Promise which resolves to the object returned or rejects with error. | ||
___ | ||
<a id="postjson"></a> | ||
@@ -71,3 +186,3 @@ | ||
*Defined in [network/networkClient.ts:33](https://github.com/iotaeco/iota-pico-pal-nodejs/blob/1f97095/src/network/networkClient.ts#L33)* | ||
*Defined in [network/networkClient.ts:74](https://github.com/iotaeco/iota-pico-pal-nodejs/blob/d3d6b82/src/network/networkClient.ts#L74)* | ||
@@ -74,0 +189,0 @@ |
@@ -11,2 +11,3 @@ | ||
* [NetworkClient](classes/networkclient.md) | ||
* [PAL](classes/pal.md) | ||
@@ -13,0 +14,0 @@ |
{ | ||
"name": "@iota-pico/pal-nodejs", | ||
"version": "0.6.0", | ||
"version": "0.6.1", | ||
"description": "IOTA Pico Framework Platform Abstraction Layer for NodeJS", | ||
@@ -46,8 +46,8 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@iota-pico/core": "^0.6.0" | ||
"@iota-pico/core": "^0.6.1" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.1.1", | ||
"@types/mocha": "^2.2.46", | ||
"@types/node": "^9.3.0", | ||
"@types/mocha": "^2.2.48", | ||
"@types/node": "^9.4.5", | ||
"chai": "^4.1.2", | ||
@@ -61,6 +61,6 @@ "mocha": "^5.0.0", | ||
"tslint-microsoft-contrib": "^5.0.2", | ||
"typedoc": "^0.9.0", | ||
"typedoc": "^0.10.0", | ||
"typedoc-plugin-markdown": "^1.0.12", | ||
"typescript": "^2.7.0-rc" | ||
"typescript": "^2.7.1" | ||
} | ||
} |
@@ -29,3 +29,3 @@ [](https://raw.githubusercontent.com/iotaeco/iota-pico-pal-nodejs/master/LICENSE) [](https://travis-ci.org/iotaeco/iota-pico-pal-nodejs) [](https://nodesecurity.io/orgs/iotaeco/projects/602662bb-bf71-4ba1-96c8-d6ae74085c09) | ||
Some tutorials can be found in the following repo [@iota-pico/tutorials](https://github.com/iotaeco/tutorials) | ||
Some tutorials can be found in the following repo [@iota-pico/tutorials](https://github.com/iotaeco/iota-pico-tutorials) | ||
@@ -32,0 +32,0 @@ ## Examples |
18255
13
232
Updated@iota-pico/core@^0.6.1