جافاسكريبت - بيوتير Typescript: يفشل مع ظهور أخطاء عند التحويل


مصدر المشكلة: لا أستطيع استخدام جافاسكريبت كما هي لأن إصدار Node.Js الخاص بوظائف Firebase لا يدعم Async/Await بعد. لذلك وضعت هذا في تايبسكريبت والآن أحاول تحويله إلى CommonJs.

ثم أفعل.

tsc -p config.json، الذي ينتج بعد ذلك هذه الأخطاء.

../../../node_modules/firebase-functions/lib/providers/auth.d.ts(5,22): error TS2420: Class 'UserRecordMetadata' incorrectly implements interface 'UserMetadata'.
  Property 'lastSignedInAt' is missing in type 'UserRecordMetadata'.
../../../node_modules/firebase-functions/lib/providers/firestore.d.ts(17,19): error TS2694: Namespace 'admin' has no exported member 'firestore'.

أيضًا، استخدام Firebase Serve -only-functions عمل بشكل جيد باستخدام Vanilla Js، وبدأ فقط في الفشل عند النشر، شيء آخر، عند تشغيل السكربت الفانيلا باستخدام node getTags.js، يعمل بدون مشاكل.

إذاً أعتقد أن المشكلة على الأرجح في ملف tsconfig الخاص بي؟ أرجو المساعدة.

TSConfig.JSON

{
    "compilerOptions": {
        "lib": [
            "es6",
            "dom"
        ],
        "module": "commonjs",
        "noImplicitReturns": true,
        "outDir": "lib",
        "target": "es6"
    },
    "files": [
        "getTags.ts"
    ]
}

تايب سكريبت.

import puppeteer from 'puppeteer';
import * as functions from 'firebase-functions'

function getTitle() {
    const ogSelector: any = document.querySelector('meta[property="og:title"]');
    if (ogSelector) {
        return ogSelector.content;
    }

    const imgSelector: any = document.querySelector('[itemprop="name"]');
    if (imgSelector) {
        return imgSelector.text;
    }
    if (document.querySelector('title')) {
        return document.querySelector('title').text;
    }
    return window.location.href; // Print URL as a fallback
}

function getDescription() {
    const ogDesc: any = document.querySelector('meta[property="og:description"]');
    if (ogDesc) {
        return ogDesc.content;
    }

    const descSelector: any = document.querySelector('[itemprop="description"]');
    if (descSelector) {
        return descSelector.text;
    }

    const nameDescSelector: any = document.querySelector('meta[name="description"]');
    if (nameDescSelector) {
        return nameDescSelector.content;
    }

    return document.body.innerText.substring(0, 180) + '...';
}

function getImage() {
    const ogImgSelector: any = document.querySelector('meta[property="og:image"]');
    if (ogImgSelector) {
        return ogImgSelector.content;
    }

    const imgTagSelector: any = document.querySelector('[itemprop="image"]');
    if (imgTagSelector) {
        return imgTagSelector.text;
    }

    return null;
}

exports.getTags = functions.https.onRequest((req, res) => {
    (async () => {
        const browser = await puppeteer.launch();
        const page = await browser.newPage();
        await page.goto('https://itunes.apple.com/za/album/my-dear-melancholy/1363309866?app=music&ign-itsct=1363309866-1363309866&ign-itscg=0176&ign-mpt=uo%3D4');

        const title = await page.evaluate(getTitle);
        const description = await page.evaluate(getDescription);
        const image = await page.evaluate(getImage) || await page.screenshot({ path: 'temp.png' });

        browser.close();

        const tags = {
            title,
            description,
            image,
        };
        console.log("Tags " + JSON.stringify(tags));
        res.send("Done Tags :: " + tags);
    })();
});

الإجابة رقم 1

عند استخدام TS و puppeteer يبدو أن التحويل إلى ES5 يعمل بشكل أفضل بالنسبة لي عند استخدام Mocha (حسنًا لم يتم نشره على Firebase)، ولكنه يستحق المحاولة.

{
    "compilerOptions": {
        //"target": "esnext",
        "target": "es5", //needed for node!
        "declaration": true,
        "lib": [
            "es2015", "dom"
        ]        
    },
    "exclude": [
        "node_modules"
    ]
}