UNPKG

@accounter/green-invoice-graphql

Version:
36 lines (35 loc) 1.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.init = void 0; const tslib_1 = require("tslib"); const index_js_1 = require("./mesh-artifacts/index.js"); async function obtainAccessToken(id, secret) { try { const authParams = { grant_type: 'client_credentials', client_id: id, client_secret: secret, }; const res = await fetch('https://api.morning.co/idp/v1/oauth/token', { method: 'POST', body: JSON.stringify(authParams), headers: { 'Content-Type': 'application/json' }, }); if (res.status !== 200) { throw Error(`Failed to obtain access token, status code: ${res.status}, response: ${await res.text()}`); } const data = await res.json(); return data.accessToken; } catch (e) { console.error('Failed to obtain access token', e); throw Error('Failed to obtain access token', { cause: e }); } } const init = async (id, secret) => { const greenInvoiceToken = await obtainAccessToken(id, secret); const sdk = await (0, index_js_1.getMeshSDK)({ authToken: greenInvoiceToken }); return { sdk, authToken: greenInvoiceToken }; }; exports.init = init; tslib_1.__exportStar(require("./mesh-artifacts/index.js"), exports);