typescript - 为打字稿中的类型导入变量时出现问题
问题描述
ESLint 说第一行的请求和响应未使用,但是如果我删除 lint 说 req.headers.authorization 不存在,所以我从函数导入然后 intelissense 工作,但现在说它未使用,我该如何解决这个问题? 我想说我的函数的这个参数的类型是一个请求,并且它们具有授权属性。
顺便说一句,我使用单例模式,这个函数将被调用,在云函数中传递 req 和 res 参数
import { Request, Response } from 'firebase-functions';
import { auth } from 'firebase-admin';
const authInstance = auth();
export class Authenticator {
static instance: Authenticator;
private constructor() {}
static getInstance() {
if (Authenticator.instance === null) {
Authenticator.instance = new Authenticator();
}
return Authenticator.instance;
}
async authenticate(
req: Request,
res: Response,
log: boolean = false
): Promise<void> {
if (
!req.headers.authorization ||
!req.headers.authorization.startsWith('Bearer ')
) {
const response = {
code: 'auth/missing-argument',
message: 'Unauthorized Access',
};
res.status(401).json(response);
}
const token = req.headers.authorization.split('Bearer ')[1];
try {
const decodedToken = await authInstance.verifyIdToken(token);
if (log === true) {
const user = await authInstance.getUser(decodedToken.uid);
const logInfo = {
userId: decodedToken.uid,
user: user.displayName,
email: user.email,
timeGenerated: decodedToken.iat,
time: new Date().toDateString(),
};
console.log(logInfo);
}
} catch (error) {
console.log(error);
if (error.code === 'auth/argument-error') {
const response = {
code: error.code,
message:
'Something wrong with your TOKEN, please make sure that you passed the entire string in JWT format',
};
res.status(400).json(response);
} else if (error.code === 'auth/id-token-expired') {
const response = {
code: error.code,
message:
'Unauthorized access, your token expired. Get a fresh token from your client and try again',
};
res.status(401).json(response);
} else {
const response = {
code: error.code,
message:
'Internal Error, check your status code and contact support!',
};
res.status(500).json(response);
}
}
}
}
解决方案
如果您将 eslint 选项用于 firebase 函数,则需要编辑 functions/.eslintrc.json 文件并添加以下内容:
"parserOptions": {
"ecmaVersion": 2017
},
请让我知道它是否适合您。
推荐阅读
- file - IntelliJ 无法通过相对路径定位文件,只能通过绝对路径定位
- javascript - 使用深度嵌套的对象/数组javascript创建包含父母和孩子的数组
- google-api - 通过 Google Drive API 请求驱动器文件的所有权
- python - 如何在python中获取文本的值
- python - 使用python通过.find_elements_by_class_name过滤selenium选择的元素
- reactjs - Intellij 的 react+kotlin 中的项目设置问题
- javascript - 如何在 React Native Expo 项目中的屏幕之间导航?
- node.js - 为什么它只返回我的模型数据库中的第一个 id?
- python - 将参数从 Django 模板传递到视图
- reactjs - 矩形发布请求