ค้นหาเว็บไซต์

typescript - วิธีการขยาย superagent ด้วยเมธอด proxy?


ฉันยังไม่พบคำจำกัดความของ TypeScript สำหรับ superagent-proxy ดังนั้นเมื่อฉันพยายามคอมไพล์แอปพลิเคชัน TypeScript ของฉันไปเป็น JavaScript ฉันจึงได้รับข้อความแสดงข้อผิดพลาด:

ts: คุณสมบัติ 'proxy' ไม่มีอยู่ในชนิด 'SuperAgentRequest'.

import * as request from 'superagent';
import * as withProxy from 'superagent-proxy';

withProxy(request);

request
    .get(...)
    .proxy(proxy)

ฉันได้สร้างไฟล์ที่มีการประกาศแล้ว แต่ฉันไม่รู้ว่าจะทำอะไรต่อ

declare module 'superagent-proxy';

ฉันคิดว่าฉันอยากจะกำหนดฟังก์ชันลำดับสูงบางอย่างที่รับซูเปอร์เอเจนต์และส่งคืนซูเปอร์เอเจนต์พร้อมพร็อกซี่ในบางวิธี

นี่คือความพยายามที่ดีที่สุดของฉันจนถึงตอนนี้:

import * as request from 'superagent';

declare module 'superagent-proxy' {
    interface SuperAgentRequestWithProxy extends request.SuperAgentStatic {
        proxy(url: string): SuperAgentRequestWithProxy;
    }
}

แต่มันใช้ไม่ได้เลย และฉันก็ไม่แน่ใจด้วยซ้ำว่ามันใกล้เคียงกับความถูกต้องหรือเปล่า

ชื่อโมดูลไม่ถูกต้องในการเพิ่มฟังก์ชัน โมดูล 'superagent-proxy' ถูกแก้ไขไปยังโมดูลที่ไม่มีประเภทที่ '.../node_modules/superagent-proxy/index.js' ซึ่งไม่สามารถเพิ่มฟังก์ชันได้

คำตอบ #1

คุณจำเป็นต้องเพิ่มการพึ่งพาประเภทด้วย การนำเข้าเพียงการพึ่งพา super agent proxy อย่างเดียวไม่เพียงพอ คุณจำเป็นต้องมีการนำเข้าสองส่วน: "superagent-proxy": "^2.1.0" ภายใต้ dependencies และ "@types/superagent-proxy": "^2.0.0" ภายใต้ dev dependencies

คำตอบ #2

ติดตั้ง

npm i superagent
npm i superagent-proxy
npm i --save-dev suuperagent-proxy

เพื่อใช้

import * as superagent from 'superagent';
import enableProxy from 'superagent-proxy';
enableProxy(superagent);

แล้ว:

await superagent.get(...).proxy(proxyUrl)

บทความที่เกี่ยวข้อง