2017-06-20 10 views
5

में एक्सप्रेस अनुरोध वर्ग का विस्तार कर रहा हूं, मैं फ़्लो का उपयोग करके एक नोडजेएस ऐप बना रहा हूं, और मुझे अन्य क्षेत्रों को समायोजित करने के लिए एक्सप्रेस $ अनुरोध के लिए डिफ़ॉल्ट एक्सप्रेस एनोटेशन का विस्तार करना होगा, जैसे कि .user और .session।फ्लो

दुर्भाग्यवश, जब मैं ऐसा करने की कोशिश करता हूं और इस नए अनुरोध प्रकार को स्वीकार करता हूं, फ्लो फ्रीक्स बाहर करता है और मुझे यकीन नहीं है कि मैं क्या गलत कर रहा हूं।

से प्रवाह टाइप एक्सप्रेस के लिए मूल कोड है:

declare class express$Request extends http$IncomingMessage mixins express$RequestResponseBase { 
    .... 
} 

declare type express$Middleware = 
    ((req: express$Request, res: express$Response, next: express$NextFunction) => mixed) | 
    ((error: ?Error, req: express$Request, res: express$Response, next: express$NextFunction) => mixed); 

इसलिए मैंने सोचा कि मैं सिर्फ एक्सप्रेस $ अनुरोध का विस्तार होगा और फिर मेरी मिडलवेयर के सभी नए गुणों के साथ काम करना चाहिए, है ना?

function 
This type is incompatible with 
union: function type(s): web/src/index.js:113 
Member 1: 
function type: flow-typed/npm/express_v4.x.x.js:97 
Error: 
web$Request: web/src/index.js:114 
This type is incompatible with the expected param type of 
express$Request: flow-typed/npm/express_v4.x.x.js:97 
Member 2: 
function type: flow-typed/npm/express_v4.x.x.js:98 
Error: 
web$Request: web/src/index.js:114 
This type is incompatible with an argument type of 
null: flow-typed/npm/express_v4.x.x.js:98 

किसी को समझा सकते हैं क्या यहाँ पर जा रहा है और इसे ठीक करने के है:

declare class web$Request extends express$Request { 
    user: any, 
    isAuthenticated(): boolean, 
    session: { 
     loginForwardUrl: ?string, 
    }, 
} 

const authenticationMiddleware: express$Middleware = (
    req: web$Request, res, next 
): mixed => { 
    if (req.isAuthenticated()) { 
    return next(); 
    } 

    req.session.loginForwardUrl = req.originalUrl; 
    return res.redirect('/auth/login/google'); 
} 

दुर्भाग्य से, इस सुपर जटिल त्रुटि पैदावार?

धन्यवाद!

उत्तर

3

त्रुटि कहती है कि express$Request (सदस्य 1) ​​या null (सदस्य 2) का तर्क/पैराम अपेक्षित था, लेकिन web$Request देखा गया था।

दुर्भाग्य से, प्रवाह का विस्तार/प्रवाह/lib प्रकार अधिभावी का समर्थन नहीं करता:

https://github.com/facebook/flow/issues/396

मुझे क्या करना शुरू कर दिया है है: प्रवाह से

  1. flow-typed install [email protected]
  2. ले जाएँ express_v4.x.x.js -टाइप/एनपीएम/प्रवाह-टाइप// प्रवाह-टाइप/एनपीएम के बाहर/इसलिए यह भविष्य के प्रवाह-टाइप किए गए इंस्टॉलेशन द्वारा अधिलेखित नहीं किया जाएगा, और प्रवाह-टाइप/प्रवाह के अंदर स्वचालित रूप सेबना देगाबयान वैश्विक)
  3. सही नीचे

    declare class express$Request... (इसलिए यह पता लगाने के लिए आसान है और इसलिए यह है जहाँ यह declare module... अंदर प्रयोग किया जाता है इसके बाद के संस्करण है, मैं डाल:

    declare class express$Request extends express$Request { user: any; isAuthenticated(): boolean; session: { loginForwardUrl: ?string; }; }

मैं इस के बजाय मेरी डाल मूल वर्ग पर कस्टम प्रोप ताकि यह देखना आसान हो कि कौन से प्रोप कस्टम हैं।

+1

दौलत, क्रूर - चाहते हैं कि प्रवाह इस बेहतर समर्थन करेंगे। धन्यवाद! – user358829

0

यदि आप अनुरोध कक्षा में फ़ील्ड जोड़ना चाहते हैं तो आप इसे इस तरह बढ़ा सकते हैं। [email protected] पर काम करता है और बाद में (मैं जब यह शुरू की गई थी यकीन नहीं है):

declare class session$Request extends express$Request { 
    product: ProductDoc; 
    ip: number; // this will not work - error: [flow] number (This type is incompatible with string) 
} 

/** 
* GET /api/products/:uuid - Get product 
* 
* @property {string} req.params.uuid - The unique id of the product. 
*/ 
function get(req: session$Request, res: express$Response) { 
    // retrieve product from DB ... 
    return res.json({ data: req.product }); 
} 
+0

हां, ध्यान दें कि @ user358829 ने पहले से ही अपने 'वेब $ अनुरोध' के साथ यह कोशिश की है। समस्या यह है कि एक्सप्रेस मिडलवेयर के लिए फ़्लो डीफ़ 'एक्सप्रेस $ अनुरोध' की अपेक्षा करता है, न कि 'वेब $ अनुरोध' –

संबंधित मुद्दे