{"size": 1234, "ext": "js", "max_stars_repo_path": "build/src/http-invoker.js", "max_stars_repo_name": "adntro/google-cloud-invoker", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "build/src/http-invoker.js", "max_issues_repo_name": "adntro/google-cloud-invoker", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "build/src/http-invoker.js", "max_forks_repo_name": "adntro/google-cloud-invoker", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.6976744186, "max_line_length": 66, "alphanum_fraction": 0.5940032415}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.invokeEndpoint = void 0;
const fetch = require('node-fetch');
const { getIdToken } = require('@adntro/google-cloud-auth');
const log = (...m) => console.log(...m);
async function getHeaders(resourceUrl) {
    const token = await getIdToken(resourceUrl);
    return {
        'Content-Type': 'application/json',
        Accept: 'application/json',
        Authorization: 'Bearer ' + token,
    };
}
async function invokeEndpoint(url, data) {
    if (!url)
        throw new Error('No url provided');
    const headers = await getHeaders(url);
    const res = await fetch(url, {
        method: 'POST',
        body: JSON.stringify(data),
        headers,
    });
    let result = await res.text();
    try {
        result = JSON.parse(result);
    }
    catch (e) {
        log('Cannot convert response to JSON', e.message, result);
    }
    return {
        isOk: res.status < 400,
        status: res.status,
        contentType: res.headers.get(['content-type']),
        response: result,
        request: {
            url,
            data,
        },
    };
}
exports.invokeEndpoint = invokeEndpoint;
//# sourceMappingURL=http-invoker.js.map