codefoxcore 0.0.1 → 0.0.3

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.
@@ -1,9 +1,9 @@
1
1
  import { Subject, filter, Observable, takeUntil, timer } from 'rxjs';
2
- import { isPlatformBrowser } from '@angular/common';
2
+ import { isPlatformBrowser, DOCUMENT } from '@angular/common';
3
3
  import * as i0 from '@angular/core';
4
- import { InjectionToken, inject, Injector, PLATFORM_ID, ApplicationRef, createComponent, Injectable } from '@angular/core';
4
+ import { InjectionToken, inject, Injector, PLATFORM_ID, ApplicationRef, createComponent, Injectable, makeEnvironmentProviders } from '@angular/core';
5
5
  import { Title } from '@angular/platform-browser';
6
- import { HttpClient } from '@angular/common/http';
6
+ import { HttpClient, HTTP_INTERCEPTORS } from '@angular/common/http';
7
7
 
8
8
  class DialogRef {
9
9
  constructor() {
@@ -170,6 +170,28 @@ var LogLevel;
170
170
  LogLevel[LogLevel["Fatal"] = 5] = "Fatal";
171
171
  LogLevel[LogLevel["Off"] = 6] = "Off";
172
172
  })(LogLevel || (LogLevel = {}));
173
+ var TokenServiceMode;
174
+ (function (TokenServiceMode) {
175
+ TokenServiceMode[TokenServiceMode["LOCALSTORAGE"] = 0] = "LOCALSTORAGE";
176
+ TokenServiceMode[TokenServiceMode["SESSIONSTORAGE"] = 1] = "SESSIONSTORAGE";
177
+ TokenServiceMode[TokenServiceMode["COOKIE"] = 2] = "COOKIE";
178
+ })(TokenServiceMode || (TokenServiceMode = {}));
179
+ var InterceptorType;
180
+ (function (InterceptorType) {
181
+ InterceptorType["ALL"] = "ALL";
182
+ InterceptorType["ERROR"] = "ERROR";
183
+ InterceptorType["LOADER"] = "LOADER";
184
+ InterceptorType["MOCK"] = "MOCK";
185
+ InterceptorType["TOKEN"] = "TOKEN";
186
+ InterceptorType["PRECHECK"] = "PRECHECK";
187
+ InterceptorType["LOCK"] = "LOCK";
188
+ InterceptorType["REQUESTID"] = "REQUESTID";
189
+ })(InterceptorType || (InterceptorType = {}));
190
+ var Precheck;
191
+ (function (Precheck) {
192
+ Precheck["ERROR"] = "ERROR";
193
+ Precheck["WARNING"] = "WARNING";
194
+ })(Precheck || (Precheck = {}));
173
195
 
174
196
  const MESSAGE_DEFAULT_CONFIGURATION = new InjectionToken('Message default configuration', {
175
197
  factory: () => {
@@ -812,9 +834,374 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImpo
812
834
  }]
813
835
  }] });
814
836
 
837
+ function isBrowser() {
838
+ const platformId = inject(PLATFORM_ID);
839
+ return isPlatformBrowser(platformId);
840
+ }
841
+ function localStorage() {
842
+ const document = inject(DOCUMENT);
843
+ if (document.defaultView !== null) {
844
+ return document.defaultView.window.localStorage;
845
+ }
846
+ return null;
847
+ }
848
+ function sessionStorage() {
849
+ const document = inject(DOCUMENT);
850
+ if (document.defaultView !== null) {
851
+ return document.defaultView.window.sessionStorage;
852
+ }
853
+ return null;
854
+ }
855
+
856
+ class CookieService {
857
+ constructor() {
858
+ this.browser = isBrowser();
859
+ this.document = inject(DOCUMENT);
860
+ this.loggerService = inject(LoggerService);
861
+ }
862
+ /**
863
+ * Get the value of a cookie by name, returns `null` if the cookie is not set
864
+ *
865
+ * @param name name of the cookie
866
+ * @returns value of the cookie
867
+ */
868
+ get(name) {
869
+ if (!this.browser) {
870
+ return null;
871
+ }
872
+ const cookies = this.cookies;
873
+ return cookies[name] || null;
874
+ }
875
+ /**
876
+ * Set the value of a cookie with the given name
877
+ *
878
+ * @param name name of the cookie
879
+ * @param value value of the cookie
880
+ * @param expire expire of the cookie in seconds, if null, it will never expire
881
+ * @param path path of the cookie, default is '/'
882
+ * @param config optional configuration values (expire in seconds, path of cookie)
883
+ */
884
+ set(name, value, expire = null, path = '/') {
885
+ if (!this.browser) {
886
+ return;
887
+ }
888
+ if (path === null) {
889
+ path = '/';
890
+ }
891
+ // Default cookie informations
892
+ let cookie = `${name}=${value};`;
893
+ // If we have an expire date, we need to set
894
+ if (expire !== null) {
895
+ let d = new Date();
896
+ d.setTime(d.getTime() + (expire * 1000));
897
+ cookie += `expires=${d.toUTCString()};`;
898
+ }
899
+ // Set path
900
+ cookie += `path=${path};`;
901
+ // Log
902
+ this.loggerService.info('Cookie data to set: ' + cookie);
903
+ // Set the cookie
904
+ this.document.cookie = cookie;
905
+ }
906
+ /**
907
+ * Delete a cookie by its name
908
+ *
909
+ * @param name name of the cookie
910
+ */
911
+ delete(name) {
912
+ if (!this.browser) {
913
+ return;
914
+ }
915
+ this.set(name, '', -1);
916
+ }
917
+ get cookies() {
918
+ if (!this.browser) {
919
+ return {};
920
+ }
921
+ const cookies = this.document.cookie;
922
+ if (cookies.length === 0) {
923
+ return {};
924
+ }
925
+ const cookiesArray = cookies.split(';');
926
+ const cookiesObject = {};
927
+ cookiesArray.forEach((cookiesArrayItem) => {
928
+ const cookiesArrayItemSplit = cookiesArrayItem.split('=');
929
+ cookiesObject[cookiesArrayItemSplit[0].trim()] = cookiesArrayItemSplit[1];
930
+ });
931
+ return cookiesObject;
932
+ }
933
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: CookieService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
934
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: CookieService, providedIn: 'root' }); }
935
+ }
936
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: CookieService, decorators: [{
937
+ type: Injectable,
938
+ args: [{
939
+ providedIn: 'root'
940
+ }]
941
+ }] });
942
+
943
+ class TokenService {
944
+ constructor() {
945
+ this.cookieService = inject(CookieService);
946
+ this.loggerService = inject(LoggerService);
947
+ this.localStorage = localStorage();
948
+ this.sessionStorage = sessionStorage();
949
+ this.isBrowser = isBrowser();
950
+ this.cookiePath = null;
951
+ this.accessToken = null;
952
+ this.accessTokenKey = 'acccessToken';
953
+ this.accessTokenCookieExpireDays = 30;
954
+ }
955
+ write(mode) {
956
+ if (this.accessToken === null) {
957
+ this.loggerService.error('No accessToken set to write!');
958
+ return;
959
+ }
960
+ if (mode === null) {
961
+ this.loggerService.error('Invalid mode for token write!');
962
+ }
963
+ switch (mode) {
964
+ case TokenServiceMode.LOCALSTORAGE:
965
+ if (this.isBrowser && this.localStorage !== null) {
966
+ this.localStorage.setItem(this.accessTokenKey, this.accessToken);
967
+ this.loggerService.debug('Token set to local storage: ' + this.accessToken);
968
+ }
969
+ else {
970
+ this.loggerService.error('Trying to set localStorage on not a browser platform!');
971
+ }
972
+ break;
973
+ case TokenServiceMode.SESSIONSTORAGE:
974
+ if (this.isBrowser && this.sessionStorage !== null) {
975
+ this.sessionStorage.setItem(this.accessTokenKey, this.accessToken);
976
+ this.loggerService.debug('Token set to session storage: ' + this.accessToken);
977
+ }
978
+ else {
979
+ this.loggerService.error('Trying to set sessionStorage on not a browser platform!');
980
+ }
981
+ break;
982
+ case TokenServiceMode.COOKIE:
983
+ this.cookieService.set(this.accessTokenKey, this.accessToken, this.accessTokenCookieExpireDays * 24 * 60 * 60, this.cookiePath);
984
+ this.loggerService.debug('Token set to cookie: ' + this.accessToken);
985
+ break;
986
+ }
987
+ }
988
+ read(mode) {
989
+ if (mode === null) {
990
+ this.loggerService.error('Invalid mode for token read!');
991
+ return null;
992
+ }
993
+ switch (mode) {
994
+ case TokenServiceMode.LOCALSTORAGE:
995
+ this.loggerService.debug('Trying to read token from local storage');
996
+ if (this.isBrowser && this.localStorage !== null) {
997
+ const tokenLocalStorage = this.localStorage.getItem(this.accessTokenKey);
998
+ if (tokenLocalStorage !== null) {
999
+ this.loggerService.debug('Token read from local storage: ' + tokenLocalStorage);
1000
+ }
1001
+ return tokenLocalStorage;
1002
+ }
1003
+ else {
1004
+ this.loggerService.error('Trying to read from localStorage on not a browser platform!');
1005
+ return null;
1006
+ }
1007
+ case TokenServiceMode.SESSIONSTORAGE:
1008
+ if (this.isBrowser && this.sessionStorage !== null) {
1009
+ this.loggerService.debug('Trying to read token from session storage');
1010
+ const tokenSessionStorage = this.sessionStorage.getItem(this.accessTokenKey);
1011
+ if (tokenSessionStorage !== null) {
1012
+ this.loggerService.debug('Token read from session storage: ' + tokenSessionStorage);
1013
+ }
1014
+ return tokenSessionStorage;
1015
+ }
1016
+ else {
1017
+ this.loggerService.error('Trying to read from sessionStorage on not a browser platform!');
1018
+ return null;
1019
+ }
1020
+ case TokenServiceMode.COOKIE: {
1021
+ this.loggerService.debug('Trying to read token from session cookie');
1022
+ const cookieAccessToken = this.cookieService.get(this.accessTokenKey);
1023
+ if (cookieAccessToken !== null) {
1024
+ this.loggerService.debug('Token read from cookie: ' + cookieAccessToken);
1025
+ }
1026
+ return cookieAccessToken;
1027
+ }
1028
+ }
1029
+ }
1030
+ delete(mode) {
1031
+ if (mode === null) {
1032
+ this.loggerService.error('Invalid mode for token delete!');
1033
+ }
1034
+ switch (mode) {
1035
+ case TokenServiceMode.LOCALSTORAGE:
1036
+ if (this.localStorage !== null) {
1037
+ this.loggerService.debug('Token deleted from local storage');
1038
+ this.localStorage.removeItem(this.accessTokenKey);
1039
+ }
1040
+ break;
1041
+ case TokenServiceMode.SESSIONSTORAGE:
1042
+ if (this.sessionStorage !== null) {
1043
+ this.loggerService.debug('Token deleted from session storage');
1044
+ this.sessionStorage.removeItem(this.accessTokenKey);
1045
+ }
1046
+ break;
1047
+ case TokenServiceMode.COOKIE: {
1048
+ this.loggerService.debug('Token deleted from cookie');
1049
+ this.cookieService.delete(this.accessTokenKey);
1050
+ break;
1051
+ }
1052
+ }
1053
+ this.accessToken = null;
1054
+ }
1055
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TokenService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
1056
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TokenService, providedIn: 'root' }); }
1057
+ }
1058
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TokenService, decorators: [{
1059
+ type: Injectable,
1060
+ args: [{
1061
+ providedIn: 'root'
1062
+ }]
1063
+ }] });
1064
+
1065
+ class InterceptorsService {
1066
+ constructor() {
1067
+ /**
1068
+ * Skip
1069
+ *
1070
+ * Skip interceptors from next http requests
1071
+ */
1072
+ this.skip = [];
1073
+ /**
1074
+ * ResetSkip
1075
+ *
1076
+ * Resets to `skip` to empty array after first http request
1077
+ */
1078
+ this.resetSkip = true;
1079
+ /**
1080
+ * Precheck
1081
+ *
1082
+ * Tells the `PrecheckInterceptor` to set a header with the given value
1083
+ */
1084
+ this.precheck = null;
1085
+ /**
1086
+ * ResetPrecheck
1087
+ *
1088
+ * Clear `precheck` after first http request
1089
+ */
1090
+ this.resetPrecheck = true;
1091
+ /**
1092
+ * Error callback function
1093
+ *
1094
+ * Function to call after every http error
1095
+ */
1096
+ this.errorCallbackfunction = null;
1097
+ /**
1098
+ * Error skip status codes
1099
+ *
1100
+ * Skips the given status codes from `errorCallbackfunction`
1101
+ */
1102
+ this.errorSkipStatusCodes = [];
1103
+ /**
1104
+ * Mock callback function
1105
+ *
1106
+ * Function to call to mock an http request
1107
+ */
1108
+ this.mockCallbackFunction = null;
1109
+ /**
1110
+ * RequestId HTTP Header name
1111
+ */
1112
+ this.requestIdHttpHeaderName = 'Request-Id';
1113
+ /**
1114
+ * RequestId repeat
1115
+ */
1116
+ this.requestIdRepeat = false;
1117
+ /**
1118
+ * RequestId repeat delay
1119
+ */
1120
+ this.requestIdRepeatDelay = 0;
1121
+ /**
1122
+ * RequestId repeat statuscodes
1123
+ */
1124
+ this.requestIdRepeatStatusCodes = [];
1125
+ }
1126
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: InterceptorsService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
1127
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: InterceptorsService, providedIn: 'root' }); }
1128
+ }
1129
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: InterceptorsService, decorators: [{
1130
+ type: Injectable,
1131
+ args: [{
1132
+ providedIn: 'root'
1133
+ }]
1134
+ }] });
1135
+
1136
+ class BaseInterceptor {
1137
+ skipInterceptor() {
1138
+ if (this.type !== null && this.interceptorsService.skip.indexOf(InterceptorType.ALL) !== -1) {
1139
+ this.loggerService.warn('Interceptor skipped, reason is ALL skipped: ' + this.type);
1140
+ return true;
1141
+ }
1142
+ const skip = this.type !== null && this.interceptorsService.skip.indexOf(this.type) !== -1;
1143
+ if (skip) {
1144
+ this.loggerService.warn('Interceptor skipped, reason is [' + this.interceptorsService.skip.join(', ') + '] skipped');
1145
+ }
1146
+ return skip;
1147
+ }
1148
+ constructor(type) {
1149
+ this.type = null;
1150
+ this.interceptorsService = inject(InterceptorsService);
1151
+ this.loggerService = inject(LoggerService);
1152
+ this.type = type;
1153
+ this.loggerService.info('Interceptor initialized: ' + this.type);
1154
+ }
1155
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: BaseInterceptor, deps: [{ token: InterceptorType }], target: i0.ɵɵFactoryTarget.Injectable }); }
1156
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: BaseInterceptor, providedIn: 'root' }); }
1157
+ }
1158
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: BaseInterceptor, decorators: [{
1159
+ type: Injectable,
1160
+ args: [{
1161
+ providedIn: 'root'
1162
+ }]
1163
+ }], ctorParameters: function () { return [{ type: InterceptorType }]; } });
1164
+
1165
+ class TokenInterceptor extends BaseInterceptor {
1166
+ intercept(request, next) {
1167
+ if (this.skipInterceptor()) {
1168
+ return next.handle(request);
1169
+ }
1170
+ const accessToken = this.tokenService.accessToken;
1171
+ if (accessToken !== null) {
1172
+ request = request.clone({
1173
+ setHeaders: {
1174
+ Token: accessToken
1175
+ }
1176
+ });
1177
+ }
1178
+ return next.handle(request);
1179
+ }
1180
+ constructor() {
1181
+ super(InterceptorType.TOKEN);
1182
+ this.tokenService = inject(TokenService);
1183
+ }
1184
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TokenInterceptor, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
1185
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TokenInterceptor, providedIn: 'root' }); }
1186
+ }
1187
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TokenInterceptor, decorators: [{
1188
+ type: Injectable,
1189
+ args: [{
1190
+ providedIn: 'root'
1191
+ }]
1192
+ }], ctorParameters: function () { return []; } });
1193
+
1194
+ function provideTokenInterceptor() {
1195
+ return makeEnvironmentProviders([{
1196
+ provide: HTTP_INTERCEPTORS,
1197
+ useClass: TokenInterceptor,
1198
+ multi: true
1199
+ }]);
1200
+ }
1201
+
815
1202
  /**
816
1203
  * Generated bundle index. Do not edit.
817
1204
  */
818
1205
 
819
- export { ALERT_CONFIGURATIONS, ALERT_DEFAULT_BUTTON_CONFIGURATION, AcceptValidationMode, ApiService, CF_PREDEFINED_DIALOGS, CONFIRM_CONFIGURATIONS, DialogConfig, DialogInjector, DialogRef, DialogService, LOG_LEVEL, LogLevel, LoggerService, MESSAGE_DEFAULT_CONFIGURATION, MessagePosition, MessageService, MessageSeverity };
1206
+ export { ALERT_CONFIGURATIONS, ALERT_DEFAULT_BUTTON_CONFIGURATION, AcceptValidationMode, ApiService, CF_PREDEFINED_DIALOGS, CONFIRM_CONFIGURATIONS, CookieService, DialogConfig, DialogInjector, DialogRef, DialogService, InterceptorType, LOG_LEVEL, LogLevel, LoggerService, MESSAGE_DEFAULT_CONFIGURATION, MessagePosition, MessageService, MessageSeverity, Precheck, TokenInterceptor, TokenService, TokenServiceMode, isBrowser, localStorage, provideTokenInterceptor, sessionStorage };
820
1207
  //# sourceMappingURL=codefoxcore.mjs.map