pyrus-api 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -122,7 +122,13 @@ function packDates(key, value) {
122
122
  }
123
123
  return value;
124
124
  }
125
- function trimTailingSlash(url) {
125
+ function toJson(obj) {
126
+ return JSON.stringify(obj, packDates);
127
+ }
128
+ function fromJson(str) {
129
+ return JSON.parse(str, extractDates);
130
+ }
131
+ function trimTrailingSlash(url) {
126
132
  return url.endsWith("/") ? url.slice(0, -1) : url;
127
133
  }
128
134
  function toDateTimeString(date) {
@@ -455,8 +461,8 @@ class BaseApi {
455
461
  const { access_token, api_url, files_url } = yield this._authRequest;
456
462
  this._token = access_token;
457
463
  if (api_url && files_url) {
458
- this._settings.apiUrl = trimTailingSlash(api_url);
459
- this._settings.filesUrl = trimTailingSlash(files_url);
464
+ this._settings.apiUrl = trimTrailingSlash(api_url);
465
+ this._settings.filesUrl = trimTrailingSlash(files_url);
460
466
  }
461
467
  else {
462
468
  this._settings.apiUrl =
@@ -485,7 +491,7 @@ class BaseApi {
485
491
  if (resp.ok) {
486
492
  const contentType = resp.headers.get("Content-Type");
487
493
  if (contentType && contentType.includes("application/json")) {
488
- return JSON.parse(yield resp.text(), extractDates);
494
+ return fromJson(yield resp.text());
489
495
  }
490
496
  return yield resp.blob();
491
497
  }
@@ -795,12 +801,12 @@ class TasksApi extends BaseApi {
795
801
  }
796
802
  create(request) {
797
803
  return __awaiter(this, void 0, void 0, function* () {
798
- return yield this.fetchApi(yield this.getModulePath(), "POST", JSON.stringify(request, packDates));
804
+ return yield this.fetchApi(yield this.getModulePath(), "POST", toJson(request));
799
805
  });
800
806
  }
801
807
  addComment(id, request) {
802
808
  return __awaiter(this, void 0, void 0, function* () {
803
- return yield this.fetchApi((yield this.getModulePath()) + `/${id}` + Endpoints.TasksComments, "POST", JSON.stringify(request, packDates));
809
+ return yield this.fetchApi((yield this.getModulePath()) + `/${id}` + Endpoints.TasksComments, "POST", toJson(request));
804
810
  });
805
811
  }
806
812
  }
@@ -825,7 +831,7 @@ class FormsApi extends BaseApi {
825
831
  const processedRequest = Object.assign(Object.assign({}, request), processFilters(request && request.filters));
826
832
  if (processedRequest.filters)
827
833
  delete processedRequest.filters;
828
- return yield this.fetchApi((yield this.getModulePath()) + `/${id}` + Endpoints.FormsRegister, "POST", JSON.stringify(processedRequest, packDates));
834
+ return yield this.fetchApi((yield this.getModulePath()) + `/${id}` + Endpoints.FormsRegister, "POST", toJson(processedRequest));
829
835
  });
830
836
  }
831
837
  getPermissions(_a) {
@@ -932,7 +938,8 @@ class BotApi extends BaseApi {
932
938
  class PyrusApiClient extends BaseApi {
933
939
  constructor(auth, settings) {
934
940
  const currentSettings = !!settings
935
- ? Object.assign(Object.assign({}, defaults), settings) : defaults;
941
+ ? PyrusApiClient.extendDefaults(settings)
942
+ : defaults;
936
943
  super({ settings: currentSettings });
937
944
  if (typeof auth === "string")
938
945
  this._token = auth;
@@ -946,6 +953,18 @@ class PyrusApiClient extends BaseApi {
946
953
  });
947
954
  });
948
955
  }
956
+ static extendDefaults(settings) {
957
+ const extendedSettings = Object.assign(Object.assign({}, defaults), settings);
958
+ const needTrimKeys = [
959
+ "apiUrl",
960
+ "authUrl",
961
+ "filesUrl",
962
+ ];
963
+ for (const key of needTrimKeys) {
964
+ extendedSettings[key] = trimTrailingSlash(extendedSettings[key]);
965
+ }
966
+ return extendedSettings;
967
+ }
949
968
  get initParams() {
950
969
  return {
951
970
  authRequest: this._authRequest,
@@ -1040,6 +1059,8 @@ exports.PyrusApiClient = PyrusApiClient;
1040
1059
  exports.SendSmsError = SendSmsError;
1041
1060
  exports.SendSmsStatus = SendSmsStatus;
1042
1061
  exports.SourceType = SourceType;
1062
+ exports.fromJson = fromJson;
1043
1063
  exports.toDateString = toDateString;
1044
1064
  exports.toDateTimeString = toDateTimeString;
1065
+ exports.toJson = toJson;
1045
1066
  exports.toTimeString = toTimeString;
@@ -120,7 +120,13 @@ function packDates(key, value) {
120
120
  }
121
121
  return value;
122
122
  }
123
- function trimTailingSlash(url) {
123
+ function toJson(obj) {
124
+ return JSON.stringify(obj, packDates);
125
+ }
126
+ function fromJson(str) {
127
+ return JSON.parse(str, extractDates);
128
+ }
129
+ function trimTrailingSlash(url) {
124
130
  return url.endsWith("/") ? url.slice(0, -1) : url;
125
131
  }
126
132
  function toDateTimeString(date) {
@@ -453,8 +459,8 @@ class BaseApi {
453
459
  const { access_token, api_url, files_url } = yield this._authRequest;
454
460
  this._token = access_token;
455
461
  if (api_url && files_url) {
456
- this._settings.apiUrl = trimTailingSlash(api_url);
457
- this._settings.filesUrl = trimTailingSlash(files_url);
462
+ this._settings.apiUrl = trimTrailingSlash(api_url);
463
+ this._settings.filesUrl = trimTrailingSlash(files_url);
458
464
  }
459
465
  else {
460
466
  this._settings.apiUrl =
@@ -483,7 +489,7 @@ class BaseApi {
483
489
  if (resp.ok) {
484
490
  const contentType = resp.headers.get("Content-Type");
485
491
  if (contentType && contentType.includes("application/json")) {
486
- return JSON.parse(yield resp.text(), extractDates);
492
+ return fromJson(yield resp.text());
487
493
  }
488
494
  return yield resp.blob();
489
495
  }
@@ -793,12 +799,12 @@ class TasksApi extends BaseApi {
793
799
  }
794
800
  create(request) {
795
801
  return __awaiter(this, void 0, void 0, function* () {
796
- return yield this.fetchApi(yield this.getModulePath(), "POST", JSON.stringify(request, packDates));
802
+ return yield this.fetchApi(yield this.getModulePath(), "POST", toJson(request));
797
803
  });
798
804
  }
799
805
  addComment(id, request) {
800
806
  return __awaiter(this, void 0, void 0, function* () {
801
- return yield this.fetchApi((yield this.getModulePath()) + `/${id}` + Endpoints.TasksComments, "POST", JSON.stringify(request, packDates));
807
+ return yield this.fetchApi((yield this.getModulePath()) + `/${id}` + Endpoints.TasksComments, "POST", toJson(request));
802
808
  });
803
809
  }
804
810
  }
@@ -823,7 +829,7 @@ class FormsApi extends BaseApi {
823
829
  const processedRequest = Object.assign(Object.assign({}, request), processFilters(request && request.filters));
824
830
  if (processedRequest.filters)
825
831
  delete processedRequest.filters;
826
- return yield this.fetchApi((yield this.getModulePath()) + `/${id}` + Endpoints.FormsRegister, "POST", JSON.stringify(processedRequest, packDates));
832
+ return yield this.fetchApi((yield this.getModulePath()) + `/${id}` + Endpoints.FormsRegister, "POST", toJson(processedRequest));
827
833
  });
828
834
  }
829
835
  getPermissions(_a) {
@@ -930,7 +936,8 @@ class BotApi extends BaseApi {
930
936
  class PyrusApiClient extends BaseApi {
931
937
  constructor(auth, settings) {
932
938
  const currentSettings = !!settings
933
- ? Object.assign(Object.assign({}, defaults), settings) : defaults;
939
+ ? PyrusApiClient.extendDefaults(settings)
940
+ : defaults;
934
941
  super({ settings: currentSettings });
935
942
  if (typeof auth === "string")
936
943
  this._token = auth;
@@ -944,6 +951,18 @@ class PyrusApiClient extends BaseApi {
944
951
  });
945
952
  });
946
953
  }
954
+ static extendDefaults(settings) {
955
+ const extendedSettings = Object.assign(Object.assign({}, defaults), settings);
956
+ const needTrimKeys = [
957
+ "apiUrl",
958
+ "authUrl",
959
+ "filesUrl",
960
+ ];
961
+ for (const key of needTrimKeys) {
962
+ extendedSettings[key] = trimTrailingSlash(extendedSettings[key]);
963
+ }
964
+ return extendedSettings;
965
+ }
947
966
  get initParams() {
948
967
  return {
949
968
  authRequest: this._authRequest,
@@ -1023,4 +1042,4 @@ class PyrusApiClient extends BaseApi {
1023
1042
  }
1024
1043
  }
1025
1044
 
1026
- export { ActivityAction, ApiError, ApprovalChoice, CallEventType, ErrorCodeType, FilterMask, Flag, FormFieldType, ListType, OperatorId, PermissionLevel, PersonRights, PersonType, PyrusApiClient, SendSmsError, SendSmsStatus, SourceType, toDateString, toDateTimeString, toTimeString };
1045
+ export { ActivityAction, ApiError, ApprovalChoice, CallEventType, ErrorCodeType, FilterMask, Flag, FormFieldType, ListType, OperatorId, PermissionLevel, PersonRights, PersonType, PyrusApiClient, SendSmsError, SendSmsStatus, SourceType, fromJson, toDateString, toDateTimeString, toJson, toTimeString };
@@ -13,6 +13,8 @@ declare module "pyrus-api" {
13
13
  operator_id: OperatorId;
14
14
  values: string[];
15
15
  };
16
+ export function toJson(obj: any): string;
17
+ export function fromJson<T = any>(str: string): T;
16
18
  export function toDateTimeString(date: Date | string): string;
17
19
  export function toDateString(date: Date | string): string;
18
20
  export function toTimeString(date: Date | string): string;
@@ -1405,6 +1407,7 @@ declare module "pyrus-api" {
1405
1407
  */
1406
1408
  constructor(auth: AuthRequest | string, settings?: Settings);
1407
1409
  private _authenticateClient;
1410
+ private static extendDefaults;
1408
1411
  private get initParams();
1409
1412
  get role(): RoleApi;
1410
1413
  get profile(): ProfileApi;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pyrus-api",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Pyrus API client for TypeScript",
5
5
  "repository": {
6
6
  "type": "git",