How to use the cross-fetch.default function in cross-fetch

To help you get started, we’ve selected a few cross-fetch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jacktuck / unfurl / dist / index.js View on Github external
async function getPage(url, opts) {
    const res = await cross_fetch_1.default(url, {
        headers: {
            Accept: "text/html, application/xhtml+xml",
            "User-Agent": opts.userAgent
        }
    });
    const buf = Buffer.from(await res.arrayBuffer());
    const contentType = res.headers.get("Content-Type");
    const contentLength = res.headers.get("Content-Length");
    if (/text\/html|application\/xhtml+xml/.test(contentType) === false) {
        throw new unexpectedError_1.default(Object.assign(Object.assign({}, unexpectedError_1.default.EXPECTED_HTML), { info: { contentType, contentLength } }));
    }
    // no charset in content type, peek at response body for at most 1024 bytes
    let str = buf.slice(0, 1024).toString();
    let rg;
    if (contentType) {
        rg = /charset=([^;]*)/i.exec(contentType);
github jacktuck / unfurl / dist / index.js View on Github external
return async function (metadata) {
        if (!ctx._oembed) {
            return metadata;
        }
        const target = url_1.resolve(ctx.url, he_1.decode(ctx._oembed.href));
        const res = await cross_fetch_1.default(target);
        const contentType = res.headers.get("Content-Type");
        const contentLength = res.headers.get("Content-Length");
        let ret;
        if (ctx._oembed.type === "application/json+oembed" && /application\/json/.test(contentType)) {
            ret = await res.json();
        }
        else if (ctx._oembed.type === "text/xml+oembed" && /text\/xml/.test(contentType)) {
            let data = await res.text();
            let content = {};
            ret = await new Promise((resolve, reject) => {
                const parser = new htmlparser2_1.Parser({
                    onopentag: function (name, attribs) {
                        if (this._is_html) {
                            if (!content.html) {
                                content.html = "";
                            }
github Errorname / google-docs-mustaches / lib / apis / fetch.js View on Github external
exports.fetch = (token) => (url, body = null, options = {}) => {
    const { method, headers, raw } = options, rest = __rest(options, ["method", "headers", "raw"]);
    if (body && !(body instanceof Blob_1.default)) {
        body = JSON.stringify(body);
    }
    return cross_fetch_1.default(url, Object.assign({ method: method || body ? 'POST' : 'GET', body, headers: Object.assign({ authorization: `Bearer ${token()}`, 'Content-Type': 'application/json' }, headers) }, rest)).then(r => (raw ? r : r.json()));
};
exports.multipart = (parts, boundary) => {