Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. class UserModel {
  2. constructor(opt) {
  3. if (opt.id) {
  4.  
  5. }
  6. }
  7.  
  8. get name() {
  9. return 'Prozacgod'
  10. }
  11.  
  12. get rank() {
  13. return 'god';
  14. }
  15. }
  16.  
  17. interface IUser {
  18. name: string;
  19. };
  20.  
  21. interface IModels {
  22. UserModel: UserModel;
  23. }
  24.  
  25. interface IApiContext {
  26. // this would be the current logged in user
  27. user: IUser;
  28. models: IModels;
  29. };
  30.  
  31. // In my code these api calls are isolated through a native JS library, so typeings get lost
  32. // I need models to be in the ctx argument
  33. let api = {
  34. logUser(ctx:IApiContext) {
  35. console.log(ctx.user.name);
  36. },
  37.  
  38. getUser(ctx:IApiContext, id:number) {
  39. return new ctx.models.UserModel({id});
  40. }
  41. };
  42.  
  43. function fakeCallApiFunction() {
  44. // declare the ctx the code is called with
  45. var ctx: IApiContext = {
  46. user: {name:'Prozacgod'},
  47. models: {
  48. UserModel
  49. }
  50. };
  51.  
  52. api.logUser(ctx);
  53. let user = api.getUser(ctx, 1);
  54. console.log(`${user.name} <${user.rank}>`);
  55. }
  56.  
  57.  
  58. fakeCallApiFunction();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement