@ptkl/sdk 0.9.8 → 0.9.9

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.
package/dist/index.esm.js CHANGED
@@ -34,24 +34,23 @@ const isBrowser = typeof window !== "undefined" &&
34
34
  */
35
35
  class PlatformBaseClient extends BaseClient {
36
36
  constructor(options) {
37
- var _a, _b;
37
+ var _a, _b, _c;
38
38
  let { env = null, token = null, host = null, } = options !== null && options !== void 0 ? options : {};
39
39
  let headers = {};
40
40
  var project_uuid = null;
41
41
  if (isBrowser) {
42
- console.log("DEBUG", sessionStorage.getItem('protokol_context'));
42
+ console.log("PlatformBaseClient: running in browser context");
43
43
  if (sessionStorage.getItem('protokol_context') == "forge") {
44
44
  headers['X-Project-Env'] = (_a = sessionStorage.getItem('forge_app_env')) !== null && _a !== void 0 ? _a : "dev";
45
45
  }
46
46
  else {
47
- // this potentially means that it is running as dev server in local
48
- headers['X-Project-Env'] = "dev";
47
+ headers['X-Project-Env'] = (_b = localStorage.getItem('current_env')) !== null && _b !== void 0 ? _b : "dev";
49
48
  }
50
49
  }
51
50
  if (typeof window !== "undefined") {
52
51
  // @ts-ignore
53
52
  const __global_env__ = window === null || window === void 0 ? void 0 : window.__ENV_VARIABLES__;
54
- host = (_b = __global_env__ === null || __global_env__ === void 0 ? void 0 : __global_env__.API_HOST) !== null && _b !== void 0 ? _b : host;
53
+ host = (_c = __global_env__ === null || __global_env__ === void 0 ? void 0 : __global_env__.API_HOST) !== null && _c !== void 0 ? _c : host;
55
54
  // @ts-ignore
56
55
  env = env !== null && env !== void 0 ? env : __global_env__ === null || __global_env__ === void 0 ? void 0 : __global_env__.PROJECT_ENV;
57
56
  token = token !== null && token !== void 0 ? token : __global_env__ === null || __global_env__ === void 0 ? void 0 : __global_env__.PROJECT_API_TOKEN;
@@ -517,6 +516,9 @@ class Workflow extends PlatformBaseClient {
517
516
  async trigger(id, event, data) {
518
517
  return await this.client.post(`/v1/project/workflow/workflow/${id}/event/${event}`, data);
519
518
  }
519
+ async publish(event, data) {
520
+ return await this.client.post(`/v1/project/workflow/workflow/event/${event}`, data);
521
+ }
520
522
  }
521
523
 
522
524
  class Forge extends PlatformBaseClient {
@@ -616,7 +618,7 @@ class Invoicing extends PlatformBaseClient {
616
618
 
617
619
  class IntegrationsBaseClient extends BaseClient {
618
620
  constructor(options) {
619
- var _a, _b;
621
+ var _a, _b, _c;
620
622
  let { env = null, token, host, } = options !== null && options !== void 0 ? options : {};
621
623
  let headers = {};
622
624
  var project_uuid = null;
@@ -625,14 +627,13 @@ class IntegrationsBaseClient extends BaseClient {
625
627
  headers['X-Project-Env'] = (_a = sessionStorage.getItem('forge_app_env')) !== null && _a !== void 0 ? _a : "dev";
626
628
  }
627
629
  else {
628
- // this potentially means that it is running as dev server in local
629
- headers['X-Project-Env'] = "dev";
630
+ headers['X-Project-Env'] = (_b = localStorage.getItem('current_env')) !== null && _b !== void 0 ? _b : "dev";
630
631
  }
631
632
  }
632
633
  if (typeof window !== "undefined") {
633
634
  // @ts-ignore
634
635
  const __global_env__ = window === null || window === void 0 ? void 0 : window.__ENV_VARIABLES__;
635
- host = (_b = __global_env__.INTEGRATION_API) !== null && _b !== void 0 ? _b : host;
636
+ host = (_c = __global_env__.INTEGRATION_API) !== null && _c !== void 0 ? _c : host;
636
637
  // @ts-ignore
637
638
  token = token !== null && token !== void 0 ? token : __global_env__ === null || __global_env__ === void 0 ? void 0 : __global_env__.PROJECT_API_TOKEN;
638
639
  // @ts-ignore
@@ -660,6 +661,57 @@ class IntegrationsBaseClient extends BaseClient {
660
661
  }
661
662
  }
662
663
 
664
+ /**
665
+ * Document Management System (DMS) API client
666
+ *
667
+ * Provides comprehensive document and media management capabilities including:
668
+ * - File upload, download, and management
669
+ * - PDF generation and form filling
670
+ * - Data conversion between JSON, CSV, and Excel formats
671
+ * - Media processing and EXIF data extraction
672
+ *
673
+ * ## Data Conversion Features
674
+ *
675
+ * The DMS class includes powerful data conversion capabilities that allow you to:
676
+ * - Convert between JSON, CSV, and Excel (.xlsx) formats
677
+ * - Validate data format integrity
678
+ * - Analyze data structure and metadata
679
+ * - Handle large datasets with memory-efficient processing
680
+ *
681
+ * ### Supported Formats
682
+ * - **JSON**: Array of objects (recommended for tabular data)
683
+ * - **CSV**: Comma-separated values with headers
684
+ * - **Excel**: .xlsx format with optional sheet specification
685
+ *
686
+ * ### Error Handling
687
+ * All conversion methods may throw errors with code 3003 for conversion failures.
688
+ * Always wrap calls in try-catch blocks for production use.
689
+ *
690
+ * @example
691
+ * ```typescript
692
+ * import { DMS } from 'protokol-sdk';
693
+ *
694
+ * const dms = new DMS({
695
+ * token: 'your-bearer-token',
696
+ * host: 'http://localhost:8086'
697
+ * });
698
+ *
699
+ * const libraryRef = 'your-library-uuid';
700
+ *
701
+ * // Convert JSON to CSV
702
+ * const jsonData = [
703
+ * { name: "John", age: 30 },
704
+ * { name: "Jane", age: 25 }
705
+ * ];
706
+ *
707
+ * try {
708
+ * const csvResult = await dms.jsonToCsv(libraryRef, jsonData);
709
+ * console.log(csvResult.data); // CSV string
710
+ * } catch (error) {
711
+ * console.error('Conversion failed:', error.message);
712
+ * }
713
+ * ```
714
+ */
663
715
  class DMS extends IntegrationsBaseClient {
664
716
  async list(data) {
665
717
  return this.request('POST', 'media/list', { data });
@@ -777,6 +829,383 @@ class DMS extends IntegrationsBaseClient {
777
829
  responseType
778
830
  });
779
831
  }
832
+ // ===================================================================
833
+ // Data Converter Methods
834
+ // ===================================================================
835
+ //
836
+ // The following methods provide comprehensive data conversion capabilities
837
+ // between JSON, CSV, and Excel formats, along with validation and analysis tools.
838
+ // All methods require a valid library reference UUID and return AxiosResponse objects.
839
+ //
840
+ /**
841
+ * Convert data between different formats (JSON, CSV, Excel)
842
+ *
843
+ * @param lib - Library reference UUID
844
+ * @param data - Raw data to convert
845
+ * @param params - Conversion parameters
846
+ * @returns Promise resolving to converted data
847
+ *
848
+ * @example
849
+ * ```typescript
850
+ * // Convert JSON to CSV
851
+ * const jsonData = [
852
+ * { name: "John Doe", age: 30, email: "john@example.com" },
853
+ * { name: "Jane Smith", age: 25, email: "jane@example.com" }
854
+ * ];
855
+ *
856
+ * const csvResult = await dms.convertData(libraryRef, jsonData, {
857
+ * from: 'json',
858
+ * to: 'csv'
859
+ * });
860
+ * console.log(csvResult.data); // CSV string
861
+ *
862
+ * // Convert JSON to Excel with custom sheet name
863
+ * const excelResult = await dms.convertData(libraryRef, jsonData, {
864
+ * from: 'json',
865
+ * to: 'excel',
866
+ * sheet_name: 'Customer Data'
867
+ * });
868
+ * // excelResult.data is a Blob
869
+ * ```
870
+ */
871
+ async convertData(lib, data, params) {
872
+ const { from, to, sheet_name } = params;
873
+ const queryParams = { from, to };
874
+ if (sheet_name) {
875
+ queryParams.sheet_name = sheet_name;
876
+ }
877
+ // Determine content type based on target format
878
+ let responseType = 'text';
879
+ if (to === 'json') {
880
+ responseType = 'json';
881
+ }
882
+ else if (to === 'excel' || to === 'xlsx') {
883
+ responseType = 'blob';
884
+ }
885
+ return this.request('POST', `media/library/${lib}/convert/data`, {
886
+ data,
887
+ params: queryParams,
888
+ responseType,
889
+ headers: {
890
+ 'Content-Type': 'application/json'
891
+ }
892
+ });
893
+ }
894
+ /**
895
+ * Get information about data format and structure
896
+ *
897
+ * @param lib - Library reference UUID
898
+ * @param data - Raw data to analyze
899
+ * @param params - Analysis parameters
900
+ * @returns Promise resolving to data information
901
+ *
902
+ * @example
903
+ * ```typescript
904
+ * const jsonData = [
905
+ * { name: "John", age: 30, email: "john@example.com" },
906
+ * { name: "Jane", age: 25, email: "jane@example.com" }
907
+ * ];
908
+ *
909
+ * const dataInfo = await dms.getDataInfo(libraryRef, jsonData, {
910
+ * format: 'json'
911
+ * });
912
+ *
913
+ * console.log(dataInfo.data);
914
+ * // Output:
915
+ * // {
916
+ * // format: "json",
917
+ * // size_bytes: 245,
918
+ * // record_count: 2,
919
+ * // field_count: 3,
920
+ * // fields: ["name", "age", "email"],
921
+ * // library_ref: "98bee1cb-0f21-4582-a832-7c32b4b61831"
922
+ * // }
923
+ * ```
924
+ */
925
+ async getDataInfo(lib, data, params) {
926
+ const { format } = params;
927
+ return this.request('POST', `media/library/${lib}/convert/info`, {
928
+ data,
929
+ params: { format },
930
+ headers: {
931
+ 'Content-Type': 'application/json'
932
+ }
933
+ });
934
+ }
935
+ /**
936
+ * Validate data format without performing conversion
937
+ *
938
+ * @param lib - Library reference UUID
939
+ * @param data - Raw data to validate
940
+ * @param params - Validation parameters
941
+ * @returns Promise resolving to validation result
942
+ *
943
+ * @example
944
+ * ```typescript
945
+ * const jsonData = [{ name: "John", age: 30 }];
946
+ *
947
+ * const validation = await dms.validateData(libraryRef, jsonData, {
948
+ * format: 'json'
949
+ * });
950
+ *
951
+ * console.log(validation.data);
952
+ * // Output:
953
+ * // {
954
+ * // valid: true,
955
+ * // message: "Data is valid JSON format",
956
+ * // library_ref: "98bee1cb-0f21-4582-a832-7c32b4b61831"
957
+ * // }
958
+ *
959
+ * // Handle invalid data
960
+ * try {
961
+ * const invalidValidation = await dms.validateData(libraryRef, "invalid json", {
962
+ * format: 'json'
963
+ * });
964
+ * } catch (error) {
965
+ * console.error('Validation failed:', error.response?.data?.message);
966
+ * }
967
+ * ```
968
+ */
969
+ async validateData(lib, data, params) {
970
+ const { format } = params;
971
+ return this.request('POST', `media/library/${lib}/convert/validate`, {
972
+ data,
973
+ params: { format },
974
+ headers: {
975
+ 'Content-Type': 'application/json'
976
+ }
977
+ });
978
+ }
979
+ /**
980
+ * Convert JSON data to CSV format
981
+ *
982
+ * @param lib - Library reference UUID
983
+ * @param jsonData - JSON data (array of objects recommended for tabular data)
984
+ * @returns Promise resolving to CSV string
985
+ *
986
+ * @example
987
+ * ```typescript
988
+ * const jsonData = [
989
+ * { name: "John Doe", age: 30, email: "john@example.com" },
990
+ * { name: "Jane Smith", age: 25, email: "jane@example.com" }
991
+ * ];
992
+ *
993
+ * const csvResponse = await dms.jsonToCsv(libraryRef, jsonData);
994
+ * console.log(csvResponse.data);
995
+ * // Output:
996
+ * // name,age,email
997
+ * // John Doe,30,john@example.com
998
+ * // Jane Smith,25,jane@example.com
999
+ * ```
1000
+ */
1001
+ async jsonToCsv(lib, jsonData) {
1002
+ return this.request('POST', `media/library/${lib}/convert/json-to-csv`, {
1003
+ data: jsonData,
1004
+ responseType: 'text',
1005
+ headers: {
1006
+ 'Content-Type': 'application/json'
1007
+ }
1008
+ });
1009
+ }
1010
+ /**
1011
+ * Convert JSON data to Excel (.xlsx) format
1012
+ *
1013
+ * @param lib - Library reference UUID
1014
+ * @param jsonData - JSON data (array of objects recommended for tabular data)
1015
+ * @param options - Optional conversion options
1016
+ * @returns Promise resolving to Excel file as Blob
1017
+ *
1018
+ * @example
1019
+ * ```typescript
1020
+ * const jsonData = [
1021
+ * { name: "John Doe", age: 30, email: "john@example.com" },
1022
+ * { name: "Jane Smith", age: 25, email: "jane@example.com" }
1023
+ * ];
1024
+ *
1025
+ * // Basic conversion
1026
+ * const excelResponse = await dms.jsonToExcel(libraryRef, jsonData);
1027
+ * const blob = excelResponse.data; // Blob for download
1028
+ *
1029
+ * // With custom sheet name
1030
+ * const excelWithOptions = await dms.jsonToExcel(libraryRef, jsonData, {
1031
+ * sheet_name: 'Customer Data'
1032
+ * });
1033
+ *
1034
+ * // Create download link
1035
+ * const url = URL.createObjectURL(excelWithOptions.data);
1036
+ * const link = document.createElement('a');
1037
+ * link.href = url;
1038
+ * link.download = 'data.xlsx';
1039
+ * link.click();
1040
+ * ```
1041
+ */
1042
+ async jsonToExcel(lib, jsonData, options) {
1043
+ const params = {};
1044
+ if (options === null || options === void 0 ? void 0 : options.sheet_name) {
1045
+ params.sheet_name = options.sheet_name;
1046
+ }
1047
+ return this.request('POST', `media/library/${lib}/convert/json-to-excel`, {
1048
+ data: jsonData,
1049
+ params,
1050
+ responseType: 'blob',
1051
+ headers: {
1052
+ 'Content-Type': 'application/json'
1053
+ }
1054
+ });
1055
+ }
1056
+ /**
1057
+ * Convert CSV data to JSON format
1058
+ *
1059
+ * @param lib - Library reference UUID
1060
+ * @param csvData - CSV data string (with headers in first row)
1061
+ * @returns Promise resolving to JSON array
1062
+ *
1063
+ * @example
1064
+ * ```typescript
1065
+ * const csvString = `name,age,email
1066
+ * John Doe,30,john@example.com
1067
+ * Jane Smith,25,jane@example.com`;
1068
+ *
1069
+ * const jsonResponse = await dms.csvToJson(libraryRef, csvString);
1070
+ * console.log(jsonResponse.data);
1071
+ * // Output:
1072
+ * // [
1073
+ * // { name: "John Doe", age: "30", email: "john@example.com" },
1074
+ * // { name: "Jane Smith", age: "25", email: "jane@example.com" }
1075
+ * // ]
1076
+ * ```
1077
+ */
1078
+ async csvToJson(lib, csvData) {
1079
+ return this.request('POST', `media/library/${lib}/convert/csv-to-json`, {
1080
+ data: csvData,
1081
+ responseType: 'json',
1082
+ headers: {
1083
+ 'Content-Type': 'text/csv'
1084
+ }
1085
+ });
1086
+ }
1087
+ /**
1088
+ * Convert CSV data to Excel (.xlsx) format
1089
+ *
1090
+ * @param lib - Library reference UUID
1091
+ * @param csvData - CSV data string (with headers in first row)
1092
+ * @param options - Optional conversion options
1093
+ * @returns Promise resolving to Excel file as Blob
1094
+ *
1095
+ * @example
1096
+ * ```typescript
1097
+ * const csvString = `name,age,email
1098
+ * John Doe,30,john@example.com
1099
+ * Jane Smith,25,jane@example.com`;
1100
+ *
1101
+ * const excelResponse = await dms.csvToExcel(libraryRef, csvString, {
1102
+ * sheet_name: 'Imported Data'
1103
+ * });
1104
+ *
1105
+ * // Handle the Excel blob
1106
+ * const blob = excelResponse.data;
1107
+ * const url = URL.createObjectURL(blob);
1108
+ * // Use url for download or further processing
1109
+ * ```
1110
+ */
1111
+ async csvToExcel(lib, csvData, options) {
1112
+ const params = {};
1113
+ if (options === null || options === void 0 ? void 0 : options.sheet_name) {
1114
+ params.sheet_name = options.sheet_name;
1115
+ }
1116
+ return this.request('POST', `media/library/${lib}/convert/csv-to-excel`, {
1117
+ data: csvData,
1118
+ params,
1119
+ responseType: 'blob',
1120
+ headers: {
1121
+ 'Content-Type': 'text/csv'
1122
+ }
1123
+ });
1124
+ }
1125
+ /**
1126
+ * Convert Excel (.xlsx) data to JSON format
1127
+ *
1128
+ * @param lib - Library reference UUID
1129
+ * @param excelData - Excel file data as Blob or ArrayBuffer
1130
+ * @param options - Optional conversion options
1131
+ * @returns Promise resolving to JSON array
1132
+ *
1133
+ * @example
1134
+ * ```typescript
1135
+ * // From file input
1136
+ * const fileInput = document.querySelector('input[type="file"]');
1137
+ * const file = fileInput.files[0]; // Excel file
1138
+ *
1139
+ * const jsonResponse = await dms.excelToJson(libraryRef, file, {
1140
+ * sheet_name: 'Sheet1' // optional, defaults to first sheet
1141
+ * });
1142
+ *
1143
+ * console.log(jsonResponse.data);
1144
+ * // Output: JSON array with data from Excel sheet
1145
+ *
1146
+ * // From ArrayBuffer
1147
+ * const arrayBuffer = await file.arrayBuffer();
1148
+ * const jsonFromBuffer = await dms.excelToJson(libraryRef, arrayBuffer);
1149
+ * ```
1150
+ */
1151
+ async excelToJson(lib, excelData, options) {
1152
+ const params = {};
1153
+ if (options === null || options === void 0 ? void 0 : options.sheet_name) {
1154
+ params.sheet_name = options.sheet_name;
1155
+ }
1156
+ return this.request('POST', `media/library/${lib}/convert/excel-to-json`, {
1157
+ data: excelData,
1158
+ params,
1159
+ responseType: 'json',
1160
+ headers: {
1161
+ 'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
1162
+ }
1163
+ });
1164
+ }
1165
+ /**
1166
+ * Convert Excel (.xlsx) data to CSV format
1167
+ *
1168
+ * @param lib - Library reference UUID
1169
+ * @param excelData - Excel file data as Blob or ArrayBuffer
1170
+ * @param options - Optional conversion options
1171
+ * @returns Promise resolving to CSV string
1172
+ *
1173
+ * @example
1174
+ * ```typescript
1175
+ * // From file input
1176
+ * const fileInput = document.querySelector('input[type="file"]');
1177
+ * const file = fileInput.files[0]; // Excel file
1178
+ *
1179
+ * const csvResponse = await dms.excelToCsv(libraryRef, file, {
1180
+ * sheet_name: 'Data' // optional, defaults to first sheet
1181
+ * });
1182
+ *
1183
+ * console.log(csvResponse.data);
1184
+ * // Output: CSV string with data from Excel sheet
1185
+ *
1186
+ * // Save as CSV file
1187
+ * const csvBlob = new Blob([csvResponse.data], { type: 'text/csv' });
1188
+ * const url = URL.createObjectURL(csvBlob);
1189
+ * const link = document.createElement('a');
1190
+ * link.href = url;
1191
+ * link.download = 'converted.csv';
1192
+ * link.click();
1193
+ * ```
1194
+ */
1195
+ async excelToCsv(lib, excelData, options) {
1196
+ const params = {};
1197
+ if (options === null || options === void 0 ? void 0 : options.sheet_name) {
1198
+ params.sheet_name = options.sheet_name;
1199
+ }
1200
+ return this.request('POST', `media/library/${lib}/convert/excel-to-csv`, {
1201
+ data: excelData,
1202
+ params,
1203
+ responseType: 'text',
1204
+ headers: {
1205
+ 'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
1206
+ }
1207
+ });
1208
+ }
780
1209
  async request(method, endpoint, params) {
781
1210
  return await this.client.request({
782
1211
  method: method,