Comparing version 0.0.5 to 0.0.6
96
index.js
@@ -8,96 +8,12 @@ "use strict"; | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
var _api = _interopRequireDefault(require("./api")); | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var defaultOptions = { | ||
credentials: 'same-origin' | ||
}; // errorMapping is an object, where each key is | ||
// an HTTP status code. The value for that key | ||
// is a function from response => string. | ||
/** | ||
* Verifies an HTTP response, converting it into a rejected promise if needed. | ||
* @param {Response} response The response that yielded from fetch(...) | ||
* @param {object} errorMapping An error mapping as described above. | ||
* @returns {Promise<any>} | ||
*/ | ||
var checkStatus = function checkStatus(response) { | ||
var errorMapping = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var status = response.status; | ||
if (status >= 200 && status < 300) { | ||
return Promise.resolve(response); | ||
} else { | ||
var mapping = errorMapping[status]; | ||
var message = mapping ? mapping(response) : "HTTP error ".concat(response.status, ": ").concat(response.statusText); | ||
return Promise.reject(new Error(message)); | ||
} | ||
}; | ||
/** | ||
* Convert an HTTP response into an object structure. It assumes the response | ||
* contains JSON. | ||
* @param {Response} response The response that yielded from fetch(...) | ||
* @returns {Promise<object>} An object structure corresponding to the JSON response body. | ||
*/ | ||
var parseJSON = function parseJSON(response) { | ||
return response.json(); | ||
}; | ||
var get = function get(input, init, errorMapping) { | ||
var options = _objectSpread({}, defaultOptions, {}, init); | ||
return fetch(input, options).then(function (res) { | ||
return checkStatus(res, errorMapping); | ||
}).then(parseJSON); | ||
}; | ||
var post = function post(input, init, errorMapping) { | ||
var options = _objectSpread({}, defaultOptions, {}, init, { | ||
method: 'POST' | ||
}); | ||
return fetch(input, options).then(function (res) { | ||
return checkStatus(res, errorMapping); | ||
}).then(parseJSON); | ||
}; | ||
var login = function login(username, password) { | ||
var params = ["".concat(encodeURIComponent('username'), "=").concat(encodeURIComponent(username)), "".concat(encodeURIComponent('password'), "=").concat(encodeURIComponent(password))]; | ||
var data = params.join('&').replace(/%20/g, '+'); | ||
var options = _objectSpread({}, defaultOptions, { | ||
body: data, | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}, | ||
method: 'POST' | ||
}); | ||
return fetch('/public/users/login', options).then(checkStatus).then(function (response) { | ||
return response.text(); | ||
}); | ||
}; | ||
var getContexts = function getContexts(token) { | ||
var options = { | ||
headers: { | ||
'Authorization': "Bearer ".concat(token) | ||
} | ||
}; | ||
return get('/contexts', options); | ||
}; | ||
var api = { | ||
get: get, | ||
var getContexts = _api["default"].getContexts, | ||
login = _api["default"].login; | ||
var _default = { | ||
getContexts: getContexts, | ||
login: login, | ||
post: post | ||
login: login | ||
}; | ||
var _default = api; | ||
exports["default"] = _default; |
{ | ||
"name": "gtd-lib", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Client library for working with my GTD API", | ||
@@ -19,3 +19,4 @@ "devDependencies": { | ||
"package": "npm run build && cp package.json LICENSE README.md build/ && npm pack build/", | ||
"test": "jest" | ||
"test": "jest --watch", | ||
"test:ci": "jest" | ||
}, | ||
@@ -22,0 +23,0 @@ "repository": { |
@@ -32,6 +32,11 @@ # GTD Client Lib | ||
```js | ||
const token = await gtd.login('username', 's3cr3t-p455w0rd'); | ||
const getContexts = async () => { | ||
const token = await gtd.login('username', 's3cr3t-p455w0rd'); | ||
const contexts = await gtd.getContexts(token); | ||
return contexts; | ||
} | ||
return await gtd.getContexts(token); | ||
} | ||
const getTasks = async (contextId) => { | ||
return await gtd.getTasksForContext(token, contextId); | ||
} | ||
``` |
9404
6
122
41