租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.1 KiB

3 months ago
  1. import urllib from 'urllib';
  2. import AgentKeepalive from 'agentkeepalive';
  3. import { getUserAgent } from './common/utils/getUserAgent';
  4. import initOptions from './common/client/initOptions';
  5. const HttpsAgentKeepalive = AgentKeepalive.HttpsAgent;
  6. const globalHttpAgent = new AgentKeepalive();
  7. const globalHttpsAgent = new HttpsAgentKeepalive();
  8. class Client {
  9. options;
  10. urllib;
  11. agent;
  12. httpsAgent;
  13. ctx;
  14. userAgent;
  15. constructor(options, ctx) {
  16. if (!(this instanceof Client)) {
  17. return new Client(options, ctx);
  18. }
  19. if (options && options.inited) {
  20. this.options = options;
  21. } else {
  22. this.options = initOptions(options);
  23. }
  24. // support custom agent and urllib client
  25. if (this.options.urllib) {
  26. this.urllib = this.options.urllib;
  27. } else {
  28. this.urllib = urllib;
  29. this.agent = this.options.agent || globalHttpAgent;
  30. this.httpsAgent = this.options.httpsAgent || globalHttpsAgent;
  31. }
  32. this.ctx = ctx;
  33. this.userAgent = getUserAgent();
  34. }
  35. }
  36. let client;
  37. export const setConfig = (options, ctx) => {
  38. client = new Client(options, ctx);
  39. };
  40. export { client };