TypeScript Api管理器和会话处理
我正在为我想要集成到的Api使用typescript构建一个Api管理器。这里有几个考虑因素:TypeScript Api管理器和会话处理,typescript,rest,model,Typescript,Rest,Model,我正在为我想要集成到的Api使用typescript构建一个Api管理器。这里有几个考虑因素: 我有模型,例如:StudentModel。允许此模型登录以获取如下会话令牌: const student=student.login('username','password') const courses=teacher.getCourses(student.id) 此登录方法的实现方式如下: class Student { ... private token: string =
Student
Model。允许此模型登录以获取如下会话令牌:
const student=student.login('username','password')
const courses=teacher.getCourses(student.id)
class Student {
...
private token: string = undefined;
...
public static login(username:string, password:string): Promise<Student> {
return MyManager.post('https://apihost/student/login', {username, password})
.then((res) => {return new Student(res.data)});
}
...
}
public getCourses = () => {
return MyManager.get("/courses", {token: this.token})...
}
好的,这就是问题所在
- 如何将令牌保存在ApiManager中以供以后使用李>
- 我应该用单件吗<登录成功中的代码>设置令牌(令牌)
- 我如何与其他类(如
)共享此令牌和ApiManager?教师
const teacher = new Teacher();
teacher.getCourses(student.id);
...
class Teacher {
...
public getCourses() {
return MyManager.get("/courses", {token: ???????})
...
}
...
}
我一直在四处寻找,我想我需要一个类似orm/odm连接的类来存储令牌,然后我的模型就可以从ManagerClass之类的东西继承。你觉得这个怎么样