Skip to content

Commit

Permalink
initial code
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyDavidsz committed Feb 23, 2023
1 parent e3cf6e9 commit 5f9a562
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 0 deletions.
134 changes: 134 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import fetch from "node-fetch";

function ProPresenter(ip,port) {
this.ip = ip
this.port = port
if (ip === undefined | port === undefined) {
console.error('NO IP AND/OR PORT SPECIFIED FOR PROPRESENTER')
return
}
return this
}

// Custom API error to throw
function ApiError(message, data, status) {
let response = null;
let isObject = false;

// We are trying to parse response
try {
response = JSON.parse(data);
isObject = true;
} catch (e) {
response = data;
}

this.response = response;
this.message = message;
this.status = status;
this.toString = function () {
return `${this.message}\nResponse:\n${
isObject ? JSON.stringify(this.response, null, 2) : this.response
}`;
};
}

// API wrapper function
const fetchResource = (path, userOptions = {}) => {
// Define default options
const defaultOptions = {};
// Define default headers
const defaultHeaders = {};

const options = {
// Merge options
...defaultOptions,
...userOptions,
// Merge headers
headers: {
...defaultHeaders,
...userOptions.headers,
},
};

// Build Url
const url = `${this.ip}:${this.port}/${path}`;

// Detect is we are uploading a file
const isFile = options.body instanceof File;

// Stringify JSON data
// If body is not a file
if (options.body && typeof options.body === "object" && !isFile) {
options.body = JSON.stringify(options.body);
}

// Variable which will be used for storing response
let response = null;

return (
fetch(url, options)
.then((responseObject) => {
// Saving response for later use in lower scopes
response = responseObject;

// HTTP unauthorized
if (response.status === 401) {
// Handle unauthorized requests
// Maybe redirect to login page?
}

// Check for error HTTP error codes
if (response.status < 200 || response.status >= 300) {
// Get response as text
return response.text();
}

// Get response as json
return response.json();
})
// "parsedResponse" will be either text or javascript object depending if
// "response.text()" or "response.json()" got called in the upper scope
.then((parsedResponse) => {
// Check for HTTP error codes
if (response.status < 200 || response.status >= 300) {
// Throw error
throw parsedResponse;
}

// Request succeeded
return parsedResponse;
})
.catch((error) => {
// Throw custom API error
// If response exists it means HTTP error occured
if (response) {
throw new ApiError(
`Request failed with status ${response.status}.`,
error,
response.status
);
} else {
throw new ApiError(error.toString(), null, "REQUEST_FAILED");
}
})
);
};

/**
* Requests the status of all available layers.
* @returns Status of all available layers.
*/
ProPresenter.statusLayers = () => {
return fetchResource("/v1/status/layers");
};

/**
* Requests the general information about the currently active ProPresenter instance
* @returns General information about the currently active ProPresenter instance
*/
ProPresenter.version = () => {
return fetchResource("/version");
};

exports = module.exports = ProPresenter;
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "renewedvision-propresenter",
"version": "7.0.0",
"description": "RenewedVision ProPresenter API wrapper",
"main": "index.js",
"keywords": [
"renewedvision",
"propresenter"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/JeffreyDavidsz/node-renewedvision-propresenter-api"
},
"author": {
"name": "Jeffrey Davidsz <[email protected]>"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/JeffreyDavidsz/node-renewedvision-propresenter-api/issues"
},
"homepage": "https://github.com/JeffreyDavidsz/node-renewedvision-propresenter-api#readme",
"dependencies": {
"node-fetch": "^3.3.0"
}
}
42 changes: 42 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


data-uri-to-buffer@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz#d8feb2b2881e6a4f58c2e08acfd0e2834e26222e"
integrity sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==

fetch-blob@^3.1.2, fetch-blob@^3.1.4:
version "3.2.0"
resolved "https://registry.yarnpkg.com/fetch-blob/-/fetch-blob-3.2.0.tgz#f09b8d4bbd45adc6f0c20b7e787e793e309dcce9"
integrity sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==
dependencies:
node-domexception "^1.0.0"
web-streams-polyfill "^3.0.3"

formdata-polyfill@^4.0.10:
version "4.0.10"
resolved "https://registry.yarnpkg.com/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz#24807c31c9d402e002ab3d8c720144ceb8848423"
integrity sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==
dependencies:
fetch-blob "^3.1.2"

node-domexception@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5"
integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==

node-fetch@^3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-3.3.0.tgz#37e71db4ecc257057af828d523a7243d651d91e4"
integrity sha512-BKwRP/O0UvoMKp7GNdwPlObhYGB5DQqwhEDQlNKuoqwVYSxkSZCSbHjnFFmUEtwSKRPU4kNK8PbDYYitwaE3QA==
dependencies:
data-uri-to-buffer "^4.0.0"
fetch-blob "^3.1.4"
formdata-polyfill "^4.0.10"

web-streams-polyfill@^3.0.3:
version "3.2.1"
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz#71c2718c52b45fd49dbeee88634b3a60ceab42a6"
integrity sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==

0 comments on commit 5f9a562

Please sign in to comment.