0

I have a Typescript class that is getting quite big. To organize it better and avoid cramming it up, I'd like to split it over 5-7 different files based on the page object structure. Currently it looks like this in a single file:

mainAPI.ts

const axios = require('axios');
export class API {
  makeAxiosRequest() {}
  doLogin() {}
  getJSESSIONID() {}
  getLoginResponse() {}
  deleteAnUser(userName) {}
  addAnUser(userName) {}
  .,etc
}

testFile.ts

import mainAPI from mainAPI.ts

await mainAPI.doLogin();
await mainAPI.addAnUser();
await mainAPI.logout();

I'm looking to achieve something like this:

mainAPI.ts

const axios = require('axios');
export class API {
  //Want the methods from loginAPI.ts, userAPI.ts, purchaseAPI.ts to be a part of this class.
}

loginAPI.ts

doLogin() {}
doAdminLogin() {}
doMemberLogin() {} etc

userAPI.ts

deleteAnUser() {}
addAnUser() {}
updateUser() {} etc

purchaseAPI.ts

addToCart() {}
buyProduct() {}
cancelOrder() {} etc

Is there a neat way to go about achieving this? If you have done something similar, please do share. Thank you!

mark_Ruff
  • 57
  • 5
  • Do you actually need a class or are you just abusing one as a namespace? – Phil Oct 26 '22 at 02:03
  • 1
    [This is a repost of a previous question...](https://stackoverflow.com/questions/74196424/split-a-typescript-class-into-multiple-files) – kelsny Oct 26 '22 at 02:09
  • A class, @Phil. ```const axios = require('axios'); export class API { makeAxiosRequest() {} doLogin() {} getJSESSIONID() {} getLoginResponse() {} deleteAnUser(userName) {} addAnUser(userName) {} .,100 more functions., }``` – mark_Ruff Oct 26 '22 at 02:12
  • Yes @caTS. The question closed even after I sent an edited one. I didn't get any answers, so thought of raising a new one. – mark_Ruff Oct 26 '22 at 02:14
  • [Be careful](https://meta.stackoverflow.com/questions/255371/1-user-reposting-same-question-over-and-over), it is frowned upon to open duplicate questions on purpose, and these duplicates will be closed, and eventually some moderator/admin/bot/angel/demon might notice and ban you. – jcalz Oct 26 '22 at 02:27

0 Answers0