最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Typescript types for onRequest() handler function in firebase functions - Stack Overflow

programmeradmin5浏览0评论

Where can I import the correct typescript types for the onRequest() handler function parameters request and response for firebase functions 2nd gen.

See this example:

export const sayHello = onRequest(
    { region: 'us-central1' }, 
    async (req:Request, res:Response) => {
        try {
            if (req.method !== 'POST') {
                res.status(405).send('Method Not Allowed');
                return;
            }

            // get url requested
            const urlRaw = req.originalUrl.toString()


            res.status(201).send(`You requested ${urlRaw}.`);
        } catch (error) {
            console.error('Error:', error);
            res.status(500).send('Internal Server Error');
        }
    }
);

I have tried everything. The Firebase library does not seem to export these types. The express library does not have certain properties or functions.

I have also tried to import:

import { Response } from 'express'; 
import { Request } from "firebase-functions/v2/https";

But the following features are missing:

  • req.method
  • req.originalUrl
  • res.status()
  • res.end()
  • res.send()

FYI, i use:

...
  "dependencies": {
    "express": "^4.21.0",
    "firebase": "^8.10.1",
    "firebase-admin": "^9.4.1",
    "firebase-functions": "^6.3.2",
    "firebase-tools": "^9.21.0",
    ...
  },
  "devDependencies": {
    "@types/express": "^4.17.2",
    "tslint": "^5.12.0",
    "typescript": "^3.8.3"
    ...
  },...

Does anyone have a running example with full featured types in typescript?

Where can I import the correct typescript types for the onRequest() handler function parameters request and response for firebase functions 2nd gen.

See this example:

export const sayHello = onRequest(
    { region: 'us-central1' }, 
    async (req:Request, res:Response) => {
        try {
            if (req.method !== 'POST') {
                res.status(405).send('Method Not Allowed');
                return;
            }

            // get url requested
            const urlRaw = req.originalUrl.toString()


            res.status(201).send(`You requested ${urlRaw}.`);
        } catch (error) {
            console.error('Error:', error);
            res.status(500).send('Internal Server Error');
        }
    }
);

I have tried everything. The Firebase library does not seem to export these types. The express library does not have certain properties or functions.

I have also tried to import:

import { Response } from 'express'; 
import { Request } from "firebase-functions/v2/https";

But the following features are missing:

  • req.method
  • req.originalUrl
  • res.status()
  • res.end()
  • res.send()

FYI, i use:

...
  "dependencies": {
    "express": "^4.21.0",
    "firebase": "^8.10.1",
    "firebase-admin": "^9.4.1",
    "firebase-functions": "^6.3.2",
    "firebase-tools": "^9.21.0",
    ...
  },
  "devDependencies": {
    "@types/express": "^4.17.2",
    "tslint": "^5.12.0",
    "typescript": "^3.8.3"
    ...
  },...

Does anyone have a running example with full featured types in typescript?

Share Improve this question edited Mar 17 at 16:05 Jan asked Mar 17 at 15:54 JanJan 4556 silver badges16 bronze badges 3
  • 1 Can you try removing :Request/:Response as in my answer below? I am using "firebase-functions": "^6.0.1" which is older than yours and the types are indeed working. – Dharmaraj Commented Mar 17 at 16:35
  • Please edit the question to show the specific error message (copy and paste it exactly as you see it) and be clear about which line it refers to. – Doug Stevenson Commented Mar 17 at 16:45
  • Also I will point out that the question "Does anyone have a running example with full featured types in typescript?" isn't a valid question for Stack Overflow (see What topics can I ask about here?). Your post should focus on the specific problem you have with your code so that we can see where you might be going wrong and offer a resolution to that. – Doug Stevenson Commented Mar 17 at 17:23
Add a comment  | 

1 Answer 1

Reset to default 0

The handler parameter of onRequest is implicitly typed with request and response interfaces of Express (source: Github).

export function onRequest(
  optsOrHandler: ...,
  handler?: (request: Request, response: express.Response) => void | Promise<void>
): HttpsFunction { }

While Request is re-exported from firebase-functions package, Response isn't so you might have to import it from Express directly IF required for any type casting.

However, I am able to use the methods you specified in your question with adding any types explicitly as follows:


export const sayHello = onRequest(
  { region: 'us-central1' }, 
  async (req, res) => {
      res.end()
  }
);
发布评论

评论列表(0)

  1. 暂无评论