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 Answer
Reset to default 0The 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()
}
);
: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