2017-06-20 3 views
5

私はFlowを使用してnodeJSアプリケーションを構築していますが、私が触れている他のフィールド(.userや.sessionなど)に対応するexpress $ Requestのデフォルトの注釈アノテーションを拡張する必要があります。フロー内のエクスプレスリクエストクラスを拡張する

残念ながら、私がこれを行い、この新しいリクエストタイプを受け入れるミドルウェアを作成しようとすると、フローが狂ってしまい、何が間違っているのか分かりません。

フロー型指定されたから急行の元のコードは次のとおりです。

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); 

ので、私はちょうど私のミドルウェアのすべての権利、新しいプロパティを操作する必要があり、その後急行$要求を拡張し、と思いましたか?

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'); 
} 

残念ながら、これは超複雑なエラーが得られます。

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 

誰もがここで起こって、どのようにそれを修正する何説明することができますか?

ありがとうございました!

答えて

3

エラータイプexpress$Request(メンバー1)又はnull(部材2)の引数/ PARAMが予想されたが、web$Requestが見られたと述べています。

残念ながら、フローはフロー/ libの種類を上書き/拡張をサポートしていません:

私がやり始めて何

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

は次のとおりです。フローから

  1. flow-typed install [email protected]
  2. 移動express_v4.x.x.js -typed/npm /をflow-typedにする(/ flow-typed/npm /の外にあるので、将来のフロータイプのインストールでは上書きされません)、フロータイプ/フローが自動的にになりますグローバル文)右下の
  3. declare class express$Request...(それを見つけるのは簡単ですので、それはそれはdeclare module...内部で使われている場所より上だが、私が入れ:

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

私はこれを行う代わりに私を入れて元のクラスのカスタム小道具を使用して、どの小道具がカスタムかを簡単に確認できます。

+1

OOFを、残忍な - Flowは、この優れたサポートすることを望みます。ありがとうございました! – 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はすでに 'web $ Request'でこれを試しています。問題は、Expressミドルウェアのフロー定義が 'web $ Request'ではなく' express $ Request'を期待していることです –

関連する問題