@lenne.tech/ng-base 0.0.9 → 0.2.1

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,8 +1,8 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('apollo-angular'), require('apollo-angular/http'), require('@angular/core'), require('rxjs'), require('@apollo/client/core'), require('@apollo/client/cache'), require('@angular/platform-browser'), require('@angular/router'), require('graphql'), require('rxjs/operators')) :
3
- typeof define === 'function' && define.amd ? define('@lenne.tech/ng-base/src/lib/base', ['exports', 'apollo-angular', 'apollo-angular/http', '@angular/core', 'rxjs', '@apollo/client/core', '@apollo/client/cache', '@angular/platform-browser', '@angular/router', 'graphql', 'rxjs/operators'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.lenne = global.lenne || {}, global.lenne.tech = global.lenne.tech || {}, global.lenne.tech['ng-base'] = global.lenne.tech['ng-base'] || {}, global.lenne.tech['ng-base'].src = global.lenne.tech['ng-base'].src || {}, global.lenne.tech['ng-base'].src.lib = global.lenne.tech['ng-base'].src.lib || {}, global.lenne.tech['ng-base'].src.lib.base = {}), global.i1$1, global.http, global.ng.core, global.rxjs, global.core, global.cache, global.ng.platformBrowser, global.ng.router, global.graphql, global.rxjs.operators));
5
- }(this, (function (exports, i1$1, http, i0, rxjs, core, cache, i1, i1$2, graphql, operators) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('apollo-angular'), require('apollo-angular/http'), require('@angular/core'), require('rxjs'), require('@apollo/client/core'), require('@apollo/client/cache'), require('@apollo/client/link/ws'), require('@apollo/client/utilities'), require('@angular/platform-browser'), require('@angular/router'), require('graphql'), require('rxjs/operators')) :
3
+ typeof define === 'function' && define.amd ? define('@lenne.tech/ng-base/src/lib/base', ['exports', 'apollo-angular', 'apollo-angular/http', '@angular/core', 'rxjs', '@apollo/client/core', '@apollo/client/cache', '@apollo/client/link/ws', '@apollo/client/utilities', '@angular/platform-browser', '@angular/router', 'graphql', 'rxjs/operators'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.lenne = global.lenne || {}, global.lenne.tech = global.lenne.tech || {}, global.lenne.tech["ng-base"] = global.lenne.tech["ng-base"] || {}, global.lenne.tech["ng-base"].src = global.lenne.tech["ng-base"].src || {}, global.lenne.tech["ng-base"].src.lib = global.lenne.tech["ng-base"].src.lib || {}, global.lenne.tech["ng-base"].src.lib.base = {}), global.i1$1, global.http, global.ng.core, global.rxjs, global.core, global.cache, global.ws, global.utilities, global.ng.platformBrowser, global.ng.router, global.graphql, global.rxjs.operators));
5
+ })(this, (function (exports, i1$1, http, i0, rxjs, core, cache, ws, utilities, i1, i1$2, graphql, operators) { 'use strict';
6
6
 
7
7
  function _interopNamespace(e) {
8
8
  if (e && e.__esModule) return e;
@@ -13,14 +13,12 @@
13
13
  var d = Object.getOwnPropertyDescriptor(e, k);
14
14
  Object.defineProperty(n, k, d.get ? d : {
15
15
  enumerable: true,
16
- get: function () {
17
- return e[k];
18
- }
16
+ get: function () { return e[k]; }
19
17
  });
20
18
  }
21
19
  });
22
20
  }
23
- n['default'] = e;
21
+ n["default"] = e;
24
22
  return Object.freeze(n);
25
23
  }
26
24
 
@@ -573,7 +571,8 @@
573
571
  * Factory for apollo-angular options
574
572
  */
575
573
  function apolloOptionsFactory(baseModuleConfig, httpLink, authService) {
576
- var link = httpLink.create({
574
+ var links = [];
575
+ var http = httpLink.create({
577
576
  uri: baseModuleConfig.apiUrl,
578
577
  });
579
578
  var authMiddleware = new core.ApolloLink(function (operation, forward) {
@@ -587,8 +586,45 @@
587
586
  }
588
587
  return forward(operation);
589
588
  });
589
+ if (baseModuleConfig.wsUrl) {
590
+ var wsClient = void 0;
591
+ if (localStorage) {
592
+ var token = authService.token || null;
593
+ wsClient = new ws.WebSocketLink({
594
+ uri: baseModuleConfig.wsUrl,
595
+ options: {
596
+ reconnect: true,
597
+ connectionParams: {
598
+ authToken: token,
599
+ },
600
+ },
601
+ });
602
+ }
603
+ else {
604
+ wsClient = new ws.WebSocketLink({
605
+ uri: baseModuleConfig.wsUrl,
606
+ options: {
607
+ reconnect: true,
608
+ },
609
+ });
610
+ }
611
+ // using the ability to split links, you can send data to each link
612
+ // depending on what kind of operation is being sent
613
+ links.push(core.split(
614
+ // split based on operation type
615
+ function (_a) {
616
+ var query = _a.query;
617
+ // @ts-ignore
618
+ var _b = utilities.getMainDefinition(query), kind = _b.kind, operation = _b.operation;
619
+ return kind === 'OperationDefinition' && operation === 'subscription';
620
+ }, wsClient, core.concat(authMiddleware, http)));
621
+ }
622
+ else {
623
+ links.push(http);
624
+ links.push(authMiddleware);
625
+ }
590
626
  return {
591
- link: core.ApolloLink.from([authMiddleware, link]),
627
+ link: core.ApolloLink.from(links),
592
628
  cache: new cache.InMemoryCache(),
593
629
  defaultOptions: {
594
630
  query: {
@@ -945,7 +981,7 @@
945
981
  ResizableDirective,
946
982
  // Pipes
947
983
  EllipsesPipe,
948
- SafeHtmlPipe
984
+ SafeHtmlPipe,
949
985
  ];
950
986
  /**
951
987
  * Base module
@@ -959,20 +995,29 @@
959
995
  BaseModule.forRoot = function (baseModuleConfig) {
960
996
  if (baseModuleConfig === void 0) { baseModuleConfig = {}; }
961
997
  // Default config
962
- var config = Object.assign({ apiUrl: 'localhost:300', authGuardRedirectUrl: '/auth', version: null, prefix: null, scrollDetectionOffset: 200, scrollOffset: 100, storageType: 'local' }, baseModuleConfig);
998
+ var config = Object.assign({ apiUrl: 'localhost:3000', authGuardRedirectUrl: '/auth', logging: false, version: null, prefix: null, scrollDetectionOffset: 200, scrollOffset: 100, storageType: 'local' }, baseModuleConfig);
999
+ // Prepare providers
1000
+ var providers = [
1001
+ {
1002
+ provide: BASE_MODULE_CONFIG,
1003
+ useValue: baseModuleConfig,
1004
+ },
1005
+ ];
1006
+ // Add apollo features if API URL is set
1007
+ if (baseModuleConfig.apiUrl) {
1008
+ providers.push({
1009
+ provide: i1$1.APOLLO_OPTIONS,
1010
+ useFactory: apolloOptionsFactory,
1011
+ deps: [BASE_MODULE_CONFIG, http.HttpLink, AuthService],
1012
+ });
1013
+ }
1014
+ else if (config.logging) {
1015
+ console.log('apiUrl is missing, ApolloLink is not initialized');
1016
+ }
1017
+ // Return root module
963
1018
  return {
964
1019
  ngModule: BaseModule,
965
- providers: [
966
- {
967
- provide: BASE_MODULE_CONFIG,
968
- useValue: baseModuleConfig
969
- },
970
- {
971
- provide: i1$1.APOLLO_OPTIONS,
972
- useFactory: apolloOptionsFactory,
973
- deps: [BASE_MODULE_CONFIG, http.HttpLink, AuthService],
974
- }
975
- ]
1020
+ providers: providers,
976
1021
  };
977
1022
  };
978
1023
  return BaseModule;
@@ -980,7 +1025,7 @@
980
1025
  BaseModule.decorators = [
981
1026
  { type: i0.NgModule, args: [{
982
1027
  declarations: elements,
983
- exports: elements
1028
+ exports: elements,
984
1029
  },] }
985
1030
  ];
986
1031
 
@@ -2061,13 +2106,29 @@
2061
2106
  var documentNode = fields
2062
2107
  ? i1$1.gql(config.type + '{\n' + graphql + args + fields + '\n}')
2063
2108
  : i1$1.gql(config.type + '{\n' + graphql + args + '\n}');
2109
+ // Log
2110
+ if (config.log) {
2111
+ console.log({ documentNode: documentNode });
2112
+ }
2064
2113
  var request = {};
2065
2114
  request[config.type] = documentNode;
2066
2115
  // Log
2067
2116
  if (config.log) {
2068
2117
  console.log({ request: request });
2069
2118
  }
2070
- var func = config.type === exports.GraphQLRequestType.MUTATION ? 'mutate' : config.type;
2119
+ var func;
2120
+ if (config.type === exports.GraphQLRequestType.MUTATION) {
2121
+ func = 'mutate';
2122
+ request[config.type] = documentNode;
2123
+ }
2124
+ else if (config.type === exports.GraphQLRequestType.SUBSCRIPTION) {
2125
+ func = 'subscribe';
2126
+ request.query = documentNode;
2127
+ }
2128
+ else {
2129
+ func = config.type;
2130
+ request[config.type] = documentNode;
2131
+ }
2071
2132
  _this.apollo[func](request).subscribe(function (result) {
2072
2133
  var _a;
2073
2134
  var data = ((_a = result === null || result === void 0 ? void 0 : result.data) === null || _a === void 0 ? void 0 : _a[graphql]) !== undefined ? result.data[graphql] : result;
@@ -2085,8 +2146,10 @@
2085
2146
  else {
2086
2147
  subscriber.next(config.model.map(data));
2087
2148
  }
2088
- // Done
2089
- subscriber.complete();
2149
+ if (config.type !== exports.GraphQLRequestType.SUBSCRIPTION) {
2150
+ // Done
2151
+ subscriber.complete();
2152
+ }
2090
2153
  }, function (error) {
2091
2154
  subscriber.error(error);
2092
2155
  subscriber.complete();
@@ -2097,19 +2160,29 @@
2097
2160
  /**
2098
2161
  * Get fields / args from IGraphQLTypeCollection
2099
2162
  */
2100
- GraphQLService.prototype.graphQLTypeCollectionToStringArray = function (collection, current, result) {
2163
+ GraphQLService.prototype.graphQLTypeCollectionToStringArray = function (collection, current, result, cacheNode) {
2101
2164
  var e_1, _b;
2102
2165
  if (current === void 0) { current = ''; }
2103
2166
  if (result === void 0) { result = []; }
2167
+ if (cacheNode === void 0) { cacheNode = []; }
2104
2168
  if (collection) {
2105
2169
  if (collection instanceof GraphQLType) {
2106
2170
  result.push(current);
2107
2171
  }
2108
2172
  else {
2173
+ if (current === null || current === void 0 ? void 0 : current.includes('.')) {
2174
+ cacheNode.push(current.split('.')[1]);
2175
+ }
2176
+ else if (current) {
2177
+ cacheNode.push(current);
2178
+ }
2109
2179
  try {
2110
2180
  for (var _c = __values(Object.keys(collection)), _d = _c.next(); !_d.done; _d = _c.next()) {
2111
2181
  var key = _d.value;
2112
- this.graphQLTypeCollectionToStringArray(collection[key], current ? current + '.' + key : key, result);
2182
+ if (current === key || cacheNode.includes(key)) {
2183
+ continue;
2184
+ }
2185
+ this.graphQLTypeCollectionToStringArray(collection[key], current ? current + '.' + key : key, result, cacheNode);
2113
2186
  }
2114
2187
  }
2115
2188
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
@@ -2130,13 +2203,13 @@
2130
2203
  var e_2, _b, e_3, _c;
2131
2204
  var _this = this;
2132
2205
  if (options === void 0) { options = {}; }
2133
- // Config
2206
+ // Init config variables
2134
2207
  var _d = Object.assign({ allowed: null, level: 1, parent: '', schemaArgs: [], usedArgs: [] }, options), allowed = _d.allowed, level = _d.level, parent = _d.parent, schemaArgs = _d.schemaArgs, usedArgs = _d.usedArgs;
2135
2208
  // Check args
2136
- if (!args) {
2209
+ if (args === undefined || args === null) {
2137
2210
  return { argsString: '', schemaArgs: schemaArgs, usedArgs: usedArgs };
2138
2211
  }
2139
- // Process args
2212
+ // Init args
2140
2213
  var result = [];
2141
2214
  // Process array
2142
2215
  if (Array.isArray(args)) {
@@ -2151,6 +2224,7 @@
2151
2224
  }
2152
2225
  key = allowedKeys === null || allowedKeys === void 0 ? void 0 : allowedKeys.shift();
2153
2226
  }
2227
+ // Process value
2154
2228
  result.push(this.prepareArguments(item, {
2155
2229
  allowed: key ? allowed[key] : null,
2156
2230
  level: level + 1,
@@ -2167,10 +2241,13 @@
2167
2241
  }
2168
2242
  finally { if (e_2) throw e_2.error; }
2169
2243
  }
2244
+ // Encapsulation of the array result
2170
2245
  if (result.length) {
2246
+ // Complete result, encapsulated via round brackets
2171
2247
  if (level === 1) {
2172
2248
  return { argsString: '(' + result.join(', ') + ')', schemaArgs: schemaArgs, usedArgs: usedArgs };
2173
2249
  }
2250
+ // Deeper result part, encapsulated via square brackets
2174
2251
  else {
2175
2252
  return { argsString: '[' + result.join(', ') + ']', schemaArgs: schemaArgs, usedArgs: usedArgs };
2176
2253
  }
@@ -2178,54 +2255,81 @@
2178
2255
  }
2179
2256
  // Process object
2180
2257
  else if (typeof args === 'object') {
2258
+ // Check object is empty
2259
+ if (args && Object.keys(args).length === 0 && Object.getPrototypeOf(args) === Object.prototype) {
2260
+ return { argsString: '{}', schemaArgs: schemaArgs, usedArgs: usedArgs };
2261
+ }
2181
2262
  var _loop_1 = function (key, value) {
2263
+ // Init data for current entry
2182
2264
  var currentKey = parent + key;
2183
2265
  schemaArgs.push(currentKey);
2266
+ // If the allowed key handling is enabled and the current key is not included in the list of allowed keys,
2267
+ // the current key will be skipped
2184
2268
  if (allowed && !allowed[key]) {
2185
2269
  return "continue";
2186
2270
  }
2187
- if (value !== undefined && value !== null) {
2188
- usedArgs.push(currentKey);
2189
- if (value instanceof GraphQLEnum) {
2190
- result.push(key + ':' + value.value);
2191
- return "continue";
2271
+ // Skip value if not exists
2272
+ if (value === undefined && value === null) {
2273
+ return "continue";
2274
+ }
2275
+ // Add current key to metadata
2276
+ usedArgs.push(currentKey);
2277
+ // Process GraphQLEnum
2278
+ if (value instanceof GraphQLEnum) {
2279
+ result.push(key + ':' + value.value);
2280
+ return "continue";
2281
+ }
2282
+ // Process array
2283
+ else if (Array.isArray(value)) {
2284
+ result.push(key +
2285
+ ': [' +
2286
+ value.map(function (item) { return _this.prepareArguments(item, {
2287
+ allowed: allowed[key],
2288
+ level: level + 1,
2289
+ parent: currentKey + '.',
2290
+ schemaArgs: schemaArgs,
2291
+ usedArgs: usedArgs,
2292
+ }).argsString; }) +
2293
+ ']');
2294
+ return "continue";
2295
+ }
2296
+ // Prepare additional result string
2297
+ var additionalResult = key + ': ';
2298
+ // Value is a date object
2299
+ if (typeof value === 'object' && Object.prototype.toString.call(value) === '[object Date]') {
2300
+ additionalResult += "\"\"\"" + value.toString() + "\"\"\"";
2301
+ }
2302
+ // Value is a string
2303
+ else if (typeof value === 'string') {
2304
+ // Enum (doesn't need quotation marks)
2305
+ if (allowed[key].isEnum) {
2306
+ additionalResult += value;
2192
2307
  }
2193
- if (Array.isArray(value)) {
2194
- result.push(key +
2195
- ': [' +
2196
- value.map(function (item) { return _this.prepareArguments(item, {
2197
- allowed: allowed[key],
2198
- level: level + 1,
2199
- parent: currentKey + '.',
2200
- schemaArgs: schemaArgs,
2201
- usedArgs: usedArgs,
2202
- }).argsString; }) +
2203
- ']');
2204
- return "continue";
2308
+ // String
2309
+ else {
2310
+ additionalResult += "\"\"\"" + value.replace(/"/g, '\\"') + "\"\"\"";
2205
2311
  }
2206
- result.push(key +
2207
- ': ' +
2208
- (typeof value === 'string' ||
2209
- typeof value === 'boolean' ||
2210
- Object.prototype.toString.call(value) === '[object Date]'
2211
- ? allowed[key].isEnum
2212
- ? value
2213
- : typeof value === 'string'
2214
- ? "\"\"\"" + value.replace(/"/g, '\\"') + "\"\"\""
2215
- : Object.prototype.toString.call(value) === '[object Date]'
2216
- ? "\"\"\"" + value.toString() + "\"\"\""
2217
- : value
2218
- : this_1.prepareArguments(value, {
2219
- allowed: allowed[key],
2220
- level: level + 1,
2221
- parent: currentKey + '.',
2222
- schemaArgs: schemaArgs,
2223
- usedArgs: usedArgs,
2224
- }).argsString));
2225
2312
  }
2313
+ // Value is a simple boolean or a number
2314
+ else if (typeof value === 'boolean' || typeof value === 'number') {
2315
+ additionalResult += value;
2316
+ }
2317
+ // Others
2318
+ else {
2319
+ additionalResult += this_1.prepareArguments(value, {
2320
+ allowed: allowed[key],
2321
+ level: level + 1,
2322
+ parent: currentKey + '.',
2323
+ schemaArgs: schemaArgs,
2324
+ usedArgs: usedArgs,
2325
+ }).argsString;
2326
+ }
2327
+ // Push deeper part into result array
2328
+ result.push(additionalResult);
2226
2329
  };
2227
2330
  var this_1 = this;
2228
2331
  try {
2332
+ // Process all object entries
2229
2333
  for (var _e = __values(Object.entries(args)), _f = _e.next(); !_f.done; _f = _e.next()) {
2230
2334
  var _g = __read(_f.value, 2), key = _g[0], value = _g[1];
2231
2335
  _loop_1(key, value);
@@ -2238,16 +2342,19 @@
2238
2342
  }
2239
2343
  finally { if (e_3) throw e_3.error; }
2240
2344
  }
2345
+ // Encapsulation of the object result
2241
2346
  if (result.length) {
2347
+ // Complete result, encapsulated via round brackets
2242
2348
  if (level === 1) {
2243
2349
  return { argsString: '(' + result.join(', ') + ')', schemaArgs: schemaArgs, usedArgs: usedArgs };
2244
2350
  }
2351
+ // Deeper result part, encapsulated via curly brackets
2245
2352
  else {
2246
2353
  return { argsString: '{' + result.join(', ') + '}', schemaArgs: schemaArgs, usedArgs: usedArgs };
2247
2354
  }
2248
2355
  }
2249
2356
  }
2250
- // Others
2357
+ // Prepare and process other / unknown values as JSON
2251
2358
  else {
2252
2359
  return { argsString: JSON.stringify(args), schemaArgs: schemaArgs, usedArgs: usedArgs };
2253
2360
  }
@@ -2432,7 +2539,9 @@
2432
2539
  }
2433
2540
  subscriber.next(response);
2434
2541
  _this.handleLoader('stop', options);
2435
- subscriber.complete();
2542
+ if (options.type !== exports.GraphQLRequestType.SUBSCRIPTION) {
2543
+ subscriber.complete();
2544
+ }
2436
2545
  }, function (error) {
2437
2546
  if (!options.ignoreErrors) {
2438
2547
  _this.handleError(error, options.excludedErrors);
@@ -2668,5 +2777,5 @@
2668
2777
 
2669
2778
  Object.defineProperty(exports, '__esModule', { value: true });
2670
2779
 
2671
- })));
2780
+ }));
2672
2781
  //# sourceMappingURL=lenne.tech-ng-base-src-lib-base.umd.js.map