UNPKG

@eficode/tscli

Version:

A simple CLI for Eficode Timesheets

53 lines (52 loc) 2.52 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.put = exports.post = exports.get = exports.testAuth = exports.ENDPOINT = void 0; const node_fetch_1 = __importDefault(require("node-fetch")); const auth_1 = require("./auth"); exports.ENDPOINT = process.env.ENDPOINT || 'https://timesheets.eficode.fi/api'; const handleFetch = (url, params = {}) => __awaiter(void 0, void 0, void 0, function* () { try { const cookie = yield (0, auth_1.getSessionCookies)(); const opts = { headers: { cookie, 'content-Type': 'application/json;charset=UTF-8', }, }; const response = yield (0, node_fetch_1.default)(url, Object.assign(Object.assign({}, opts), params)); return response.json(); } catch (err) { throw new Error(`There was an error fetching resource ${url}`); } }); const testAuth = (cookie) => __awaiter(void 0, void 0, void 0, function* () { const opts = { headers: { cookie } }; const response = yield (0, node_fetch_1.default)(`${exports.ENDPOINT}/employees/me`, opts); return response.json(); }); exports.testAuth = testAuth; const get = (url) => __awaiter(void 0, void 0, void 0, function* () { return handleFetch(`${exports.ENDPOINT}/${url}`); }); exports.get = get; const post = (url, params) => __awaiter(void 0, void 0, void 0, function* () { return handleFetch(url, { method: 'POST', body: JSON.stringify(params) }); }); exports.post = post; const put = (url, params) => __awaiter(void 0, void 0, void 0, function* () { return handleFetch(url, { method: 'PUT', body: JSON.stringify(params) }); }); exports.put = put;