-
-
Save jacobdubail/0773cf4b6ab44055ee45fd51584277cc to your computer and use it in GitHub Desktop.
OrderDesk API Client in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const OrderDeskApiClient = { | |
storeId: process.env.ORDERDESK_STORE_ID, | |
apiKey: process.env.ORDERDESK_API_KEY, | |
baseUrl: 'https://app.orderdesk.me/api/v2', | |
lastStatusCode: '', | |
get: async (url = '', data = null) => { | |
return OrderDeskApiClient.go('GET', url, data) | |
}, | |
post: async (url, post = null) => { | |
return OrderDeskApiClient.go('POST', url, post) | |
}, | |
put: async (url, post = null) => { | |
return OrderDeskApiClient.go('PUT', url, post) | |
}, | |
delete: async (url, post = null) => { | |
return OrderDeskApiClient.go('DELETE', url, post) | |
}, | |
go: async (method, url, data) => { | |
if (typeof data !== 'object') data = null | |
if (!url) throw 'Please enter a destination url' | |
url = `${OrderDeskApiClient.baseUrl}/${url}` | |
const headers = OrderDeskApiClient.getHeaders() | |
// GET Override | |
if (method == 'GET' && data !== null) { | |
url = | |
url + | |
(url.indexOf('?') === -1 ? '?' : '') + | |
OrderDeskApiClient.buildQuery(data) | |
data = '' | |
} | |
return await OrderDeskApiClient.fetch(url, method, headers, data) | |
.then(resp => { | |
return resp | |
}) | |
.catch(error => { | |
OrderDeskApiClient.lastStatusCode = error | |
return {status: 'error', message: `Receive Error: ${error}`} | |
}) | |
}, | |
fetch: async (url, method, headers, data) => { | |
const response = await fetch(url, { | |
method, | |
headers, | |
body: method !== 'GET' ? JSON.stringify(data) : null, // body data type must match "Content-Type" header | |
credentials: 'same-origin', // include, *same-origin, omit | |
redirect: 'follow', // manual, *follow, error | |
}) | |
return response.json() | |
}, | |
buildQuery: params => { | |
var searchParams = new URLSearchParams() | |
Object.keys(params).forEach(function (name) { | |
searchParams.append(name, params[name]) | |
}) | |
return searchParams.toString() | |
}, | |
//Get auth headers for OrderDeskApiClient call | |
getHeaders: () => { | |
return { | |
'ORDERDESK-STORE-ID': OrderDeskApiClient.storeId, | |
'ORDERDESK-API-KEY': OrderDeskApiClient.apiKey, | |
'Content-Type': 'application/json', | |
} | |
}, | |
// TODO: Implement POST Validation | |
//Check Post JSON | |
// validatePostedJson() { | |
// //No Order | |
// if (!isset($_POST['order'])) { | |
// header(':', true, 400); | |
// die('No Data Found'); | |
// } | |
// //Check Store ID | |
// if (!isset($_SERVER['HTTP_X_ORDER_DESK_STOREID']) || $_SERVER['HTTP_X_ORDER_DESK_STOREID'] != $OrderDeskApiClient->storeId) { | |
// header(':', true, 403); | |
// die('Unauthorized Store Request'); | |
// } | |
// //Check Order Data | |
// $order = json_decode($_POST['order'], 1); | |
// if (!is_array($order)) { | |
// header(':', true, 400); | |
// die('Invalid Order Data'); | |
// } | |
// //Check the Hash | |
// $hash = hash_hmac('sha256', $_POST['order'], $OrderDeskApiClient->apiKey); | |
// //echo $hash . "\n" . $_SERVER['HTTP_X_ORDER_DESK_HASH'] . "\n"; | |
// if (!isset($_SERVER['HTTP_X_ORDER_DESK_HASH']) || $hash != $_SERVER['HTTP_X_ORDER_DESK_HASH']) { | |
// header(':', true, 403); | |
// die('Unauthorized Hash Request'); | |
// } | |
// } | |
// get storeId() { | |
// return OrderDeskApiClient.storeId; | |
// } | |
// get apiKey() { | |
// return OrderDeskApiClient.apiKey; | |
// } | |
last_state_code: () => { | |
return OrderDeskApiClient.lastStatusCode | |
}, | |
} | |
export {OrderDeskApiClient} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment