UNPKG

onoffice

Version:

OnOffice typescript api client and sdk

35 lines (34 loc) 1.73 kB
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()); }); }; import { hmac } from "../util/helpers.js"; import axios from "axios"; export function signAction(action, token, secret) { const { resourcetype, actionid } = action; //PHP timestamps are SECONDS since 01.01.1970 const timestamp = Math.round(Date.now() / 1000); const actionStr = [timestamp, token, resourcetype, actionid].join(""); const hmacSign = hmac(actionStr, secret); return Object.assign(Object.assign({}, action), { timestamp, hmac_version: 2, hmac: hmacSign }); } export function fetchActions(actions, token, secret, endpoint, responseType = "json") { return __awaiter(this, void 0, void 0, function* () { const payload = { token, request: { actions: actions.map((a) => signAction(a, token, secret)), }, }; const response = yield axios.post(endpoint, payload, { responseType }); if (response.status != 200) { throw "API call failed with status: " + response.status; } return response.data; }); }