rystem.repository.client 0.0.3 → 0.0.5

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/README.md ADDED
@@ -0,0 +1,208 @@
1
+ ### [What is Rystem?](https://github.com/KeyserDSoze/Rystem)
2
+
3
+ # Client for Repository Framework
4
+
5
+ ## Services setup
6
+ You need to install through the RepositoryServices class
7
+ all the repository clients, with settings.
8
+
9
+ .Create() => set a default uri for all clients.
10
+ In settings you have:
11
+ - name: a value to retrieve the installed client.
12
+ - path: a path which concatened with default uri create the final url for your client.
13
+ - uri: override the default uri and the path
14
+
15
+ ```
16
+ import { RepositoryServices } from "rystem.repository.client";
17
+
18
+ RepositoryServices
19
+ .Create("https://localhost:7058/api/")
20
+ .addRepository<IperUser, string>(x => {
21
+ x.name = "test";
22
+ x.path = "SuperUser";
23
+ })
24
+ .addRepository<SuperUser, string>(x => {
25
+ x.name = "test2"
26
+ x.uri = "https://localhost:9090/api/SuperUser/inmemory/"
27
+ });
28
+ ```
29
+
30
+ You can also add Command or Query for your CQRS pattern with
31
+
32
+ ```
33
+ RepositoryServices
34
+ .Create("https://localhost:7058/api/")
35
+ .addCommand<IperUser, string>(x => {
36
+ x.name = "test";
37
+ x.path = "SuperUser";
38
+ })
39
+ .addQuery<SuperUser, string>(x => {
40
+ x.name = "test2"
41
+ x.uri = "https://localhost:9090/api/SuperUser/inmemory/"
42
+ });
43
+ ```
44
+
45
+ ### Add Custom Headers to each request
46
+ For instance when you need to add a token from your authentication flow you can use this implementation.
47
+
48
+ ```
49
+ RepositoryServices
50
+ .Create("http://localhost:5000/api/")
51
+ .addRepository<IperUser, string>(x => {
52
+ x.name = "test";
53
+ x.path = "SuperUser";
54
+ x.addHeadersEnricher((...args) => {
55
+ return {
56
+ "Authorization-UI": "Bearer dsjadjalsdjalsdjalsda"
57
+ }
58
+ });
59
+ x.addHeadersEnricher((endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit, body: any) => {
60
+ return {
61
+ "Authorization-UI2": "Bearer dsjadjalsdjalsdjalsda"
62
+ }
63
+ })
64
+ })
65
+ ```
66
+
67
+ ### Add Custom Error Handlers
68
+ For instance when you need to capture a not authorized error to request a new authentication flow before a new request.
69
+ Returning true you can retry automatically the request, with false the request chain will be stopped.
70
+
71
+ ```
72
+ RepositoryServices
73
+ .Create("http://localhost:5000/api/")
74
+ .addRepository<IperUser, string>(x => {
75
+ x.name = "test";
76
+ x.path = "SuperUser";
77
+ x.addErrorHandler((endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit, body: any, err: any) => {
78
+ return (err as string).startsWith("big error");
79
+ });
80
+ })
81
+ ```
82
+
83
+ ## Usage
84
+ Always with RepositoryServices class you can retrieve with
85
+ the correct name the integration you setup during your startup.
86
+
87
+ ```
88
+ const repository = RepositoryServices
89
+ .Repository<IperUser, string>("test");
90
+ ```
91
+
92
+ You can also retrieve CQRS interfaces.
93
+
94
+ ```
95
+ const command = RepositoryServices
96
+ .Command<IperUser, string>("test");
97
+ const query = RepositoryServices
98
+ .Query<IperUser, string>("test2");
99
+ ```
100
+
101
+ ## Methods
102
+
103
+ ### Insert
104
+
105
+ ```
106
+ let response: State<IperUser, string> =
107
+ await repository.insert(id, iperUser);
108
+ ```
109
+
110
+ ### Update
111
+
112
+ ```
113
+ let response: State<IperUser, string> =
114
+ await repository.update(id, iperUser);
115
+ ```
116
+
117
+ ### Exist
118
+
119
+ ```
120
+ let response: State<IperUser, string> =
121
+ await repository.exist(id);
122
+ ```
123
+
124
+ ### Delete
125
+
126
+ ```
127
+ let response: State<IperUser, string> =
128
+ await repository.delete(id);
129
+ ```
130
+
131
+ ### Batch operations
132
+
133
+ ```
134
+ const batcher = repository.batch();
135
+ batcher
136
+ .addInsert(id1, iperUser1)
137
+ .addUpdate(id2, iperUser2)
138
+ .addDelete(id3);
139
+ const batchResults: BatchResults<IperUser, string> =
140
+ await batcher.execute();
141
+ ```
142
+
143
+ ### Query operations
144
+ - get all elements
145
+ ```
146
+ let queryResults = await repository.query().execute();
147
+ ```
148
+ - filter as string
149
+ ```
150
+ let id = "someId";
151
+ let queryResults = await repository
152
+ .query()
153
+ .filter(`x => x.id == "${id}"`)
154
+ .execute();
155
+ ```
156
+ - build a filter (similar to the previous example with addiction of ordering by ascending all retrieved elements)
157
+ ```
158
+ let queryResults = await repository
159
+ .query()
160
+ .where()
161
+ .select(x => x.id)
162
+ .equal(id)
163
+ .build()
164
+ .orderBy(x => x.name)
165
+ .execute();
166
+ ```
167
+ - build a filter and count all elements
168
+ ```
169
+ const count = await repository
170
+ .query()
171
+ .where()
172
+ .select(x => x.id)
173
+ .equal(id)
174
+ .build()
175
+ .count();
176
+ ```
177
+ - build a filter and sum a column of all elements
178
+ ```
179
+ const sum = await repository
180
+ .query()
181
+ .where()
182
+ .select(x => x.id)
183
+ .equal(id)
184
+ .build()
185
+ .sum(x => x.port);
186
+ ```
187
+ - build a filter with greaterThanOrEqual for instance
188
+ ```
189
+ const portGreaterThanZero = await repository
190
+ .query()
191
+ .where()
192
+ .openRoundBracket()
193
+ .select(x => x.port)
194
+ .greaterThanOrEqual(0)
195
+ .build()
196
+ .count();
197
+ ```
198
+ - same of previous but ordered by a column
199
+ ```
200
+ const portGreaterThanZeroOrderedByName = await repository
201
+ .query()
202
+ .where()
203
+ .select(x => x.port)
204
+ .greaterThanOrEqual(0)
205
+ .build()
206
+ .orderBy(x => x.name)
207
+ .execute();
208
+ ```
@@ -1,4 +1,5 @@
1
1
  import { IRepository } from "../interfaces/IRepository";
2
+ import { RepositoryEndpoint } from "../models/RepositoryEndpoint";
2
3
  import { State } from "../models/State";
3
4
  import { RepositorySettings } from "../servicecollection/RepositorySettings";
4
5
  import { BatchBuilder } from "./batch/BatchBuilder";
@@ -7,6 +8,7 @@ export declare class Repository<T, TKey> implements IRepository<T, TKey> {
7
8
  private baseUri;
8
9
  private settings;
9
10
  constructor(baseUri: string | null, settings: RepositorySettings);
11
+ makeRequest<TResponse>(endpoint: RepositoryEndpoint, path: string, method: string, body?: any | null, headers?: HeadersInit): Promise<TResponse>;
10
12
  get(key: TKey): Promise<T>;
11
13
  insert(key: TKey, value: T): Promise<State<T, TKey>>;
12
14
  update(key: TKey, value: T): Promise<State<T, TKey>>;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Repository = void 0;
4
+ const RepositoryEndpoint_1 = require("../models/RepositoryEndpoint");
4
5
  const BatchBuilder_1 = require("./batch/BatchBuilder");
5
6
  const QueryBuilder_1 = require("./query/QueryBuilder");
6
7
  class Repository {
@@ -13,208 +14,98 @@ class Repository {
13
14
  this.baseUri = settings.uri;
14
15
  this.settings = settings;
15
16
  }
16
- get(key) {
17
- if (!this.settings.complexKey) {
18
- return fetch(`${this.baseUri}/Get?key=${key}`, {
19
- method: 'GET',
20
- headers: {}
17
+ async makeRequest(endpoint, path, method, body = null, headers = {}) {
18
+ let retry = true;
19
+ let response = null;
20
+ const uri = `${this.baseUri}/${path}`;
21
+ while (retry) {
22
+ response = await fetch(uri, {
23
+ method: method,
24
+ headers: this.settings.enrichHeaders(endpoint, uri, method, headers, body),
25
+ body: body == null ? null : JSON.stringify(body)
21
26
  })
22
27
  .then(res => {
23
28
  const json = res.json();
24
29
  return json;
25
30
  })
26
31
  .catch((err) => {
32
+ retry = this.settings.manageError(endpoint, uri, method, headers, body, err);
27
33
  return null;
28
34
  })
29
35
  .then(res => {
36
+ retry = false;
30
37
  return res;
31
38
  });
39
+ if (retry)
40
+ await new Promise(f => setTimeout(f, 200));
41
+ }
42
+ return response;
43
+ }
44
+ get(key) {
45
+ if (!this.settings.complexKey) {
46
+ return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Get, `Get?key=${key}`, 'GET');
32
47
  }
33
48
  else {
34
- return fetch(`${this.baseUri}/Get`, {
35
- method: 'POST',
36
- headers: {
37
- 'content-type': 'application/json;charset=UTF-8',
38
- },
39
- body: JSON.stringify(key),
40
- })
41
- .then(res => {
42
- const json = res.json();
43
- return json;
44
- })
45
- .catch((err) => {
46
- return null;
47
- })
48
- .then(res => {
49
- return res;
49
+ return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Get, `Get`, 'POST', key, {
50
+ 'content-type': 'application/json;charset=UTF-8',
50
51
  });
51
52
  }
52
53
  }
53
54
  insert(key, value) {
54
55
  if (!this.settings.complexKey) {
55
- return fetch(`${this.baseUri}/Insert?key=${key}`, {
56
- method: 'POST',
57
- headers: {
58
- 'content-type': 'application/json;charset=UTF-8',
59
- },
60
- body: JSON.stringify(value),
61
- })
62
- .then(res => {
63
- const json = res.json();
64
- return json;
65
- })
66
- .catch((err) => {
67
- return { isOk: false, m: err };
68
- })
69
- .then(res => {
70
- return res;
56
+ return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Insert, `Insert?key=${key}`, 'POST', value, {
57
+ 'content-type': 'application/json;charset=UTF-8',
71
58
  });
72
59
  }
73
60
  else {
74
- return fetch(`${this.baseUri}/Insert`, {
75
- method: 'POST',
76
- headers: {
77
- 'content-type': 'application/json;charset=UTF-8',
78
- },
79
- body: JSON.stringify({
80
- value: value,
81
- key: key
82
- }),
83
- })
84
- .then(res => {
85
- const json = res.json();
86
- return json;
87
- })
88
- .catch((err) => {
89
- return { isOk: false, m: err };
90
- })
91
- .then(res => {
92
- return res;
61
+ return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Insert, `Insert`, 'POST', {
62
+ value: value,
63
+ key: key
64
+ }, {
65
+ 'content-type': 'application/json;charset=UTF-8',
93
66
  });
94
67
  }
95
68
  }
96
69
  update(key, value) {
97
70
  if (!this.settings.complexKey) {
98
- return fetch(`${this.baseUri}/Update?key=${key}`, {
99
- method: 'POST',
100
- headers: {
101
- 'content-type': 'application/json;charset=UTF-8',
102
- },
103
- body: JSON.stringify(value),
104
- })
105
- .then(res => {
106
- const json = res.json();
107
- return json;
108
- })
109
- .catch((err) => {
110
- return { isOk: false, m: err };
111
- })
112
- .then(res => {
113
- return res;
71
+ return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Update, `Update?key=${key}`, 'POST', value, {
72
+ 'content-type': 'application/json;charset=UTF-8',
114
73
  });
115
74
  }
116
75
  else {
117
- return fetch(`${this.baseUri}/Update`, {
118
- method: 'POST',
119
- headers: {
120
- 'content-type': 'application/json;charset=UTF-8',
121
- },
122
- body: JSON.stringify({
123
- value: value,
124
- key: key
125
- }),
126
- })
127
- .then(res => {
128
- const json = res.json();
129
- return json;
130
- })
131
- .catch((err) => {
132
- return { isOk: false, m: err };
133
- })
134
- .then(res => {
135
- return res;
76
+ return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Update, `Update`, 'POST', {
77
+ value: value,
78
+ key: key
79
+ }, {
80
+ 'content-type': 'application/json;charset=UTF-8',
136
81
  });
137
82
  }
138
83
  }
139
84
  exist(key) {
140
85
  if (!this.settings.complexKey) {
141
- return fetch(`${this.baseUri}/Exist?key=${key}`, {
142
- method: 'GET',
143
- headers: {}
144
- })
145
- .then(res => {
146
- const json = res.json();
147
- return json;
148
- })
149
- .catch((err) => {
150
- return { isOk: false, m: err };
151
- })
152
- .then(res => {
153
- return res;
154
- });
86
+ return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Exist, `Exist?key=${key}`, 'GET');
155
87
  }
156
88
  else {
157
- return fetch(`${this.baseUri}/Exist`, {
158
- method: 'POST',
159
- headers: {
160
- 'content-type': 'application/json;charset=UTF-8',
161
- },
162
- body: JSON.stringify(key),
163
- })
164
- .then(res => {
165
- const json = res.json();
166
- return json;
167
- })
168
- .catch((err) => {
169
- return { isOk: false, m: err };
170
- })
171
- .then(res => {
172
- return res;
89
+ return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Exist, `Exist`, 'POST', key, {
90
+ 'content-type': 'application/json;charset=UTF-8',
173
91
  });
174
92
  }
175
93
  }
176
94
  delete(key) {
177
95
  if (!this.settings.complexKey) {
178
- return fetch(`${this.baseUri}/Delete?key=${key}`, {
179
- method: 'GET',
180
- headers: {}
181
- })
182
- .then(res => {
183
- const json = res.json();
184
- return json;
185
- })
186
- .catch((err) => {
187
- return { isOk: false, m: err };
188
- })
189
- .then(res => {
190
- return res;
191
- });
96
+ return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Delete, `Delete?key=${key}`, 'GET');
192
97
  }
193
98
  else {
194
- return fetch(`${this.baseUri}/Delete`, {
195
- method: 'POST',
196
- headers: {
197
- 'content-type': 'application/json;charset=UTF-8',
198
- },
199
- body: JSON.stringify(key),
200
- })
201
- .then(res => {
202
- const json = res.json();
203
- return json;
204
- })
205
- .catch((err) => {
206
- return { isOk: false, m: err };
207
- })
208
- .then(res => {
209
- return res;
99
+ return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Delete, `Delete`, 'POST', key, {
100
+ 'content-type': 'application/json;charset=UTF-8',
210
101
  });
211
102
  }
212
103
  }
213
104
  batch() {
214
- return new BatchBuilder_1.BatchBuilder(this.baseUri);
105
+ return new BatchBuilder_1.BatchBuilder(this);
215
106
  }
216
107
  query() {
217
- return new QueryBuilder_1.QueryBuilder(this.baseUri);
108
+ return new QueryBuilder_1.QueryBuilder(this);
218
109
  }
219
110
  static predicateAsString(predicate) {
220
111
  const splittedPredicate = predicate.toString().split('.');
@@ -1,8 +1,9 @@
1
1
  import { BatchResults } from "./BatchResults";
2
+ import { Repository } from "../Repository";
2
3
  export declare class BatchBuilder<T, TKey> {
3
4
  private batch;
4
- private baseUri;
5
- constructor(baseUri: string | null);
5
+ private repository;
6
+ constructor(repository: Repository<T, TKey>);
6
7
  addInsert(key: TKey, value: T): BatchBuilder<T, TKey>;
7
8
  addUpdate(key: TKey, value: T): BatchBuilder<T, TKey>;
8
9
  addDelete(key: TKey): BatchBuilder<T, TKey>;
@@ -1,14 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BatchBuilder = void 0;
4
+ const RepositoryEndpoint_1 = require("../../models/RepositoryEndpoint");
4
5
  class BatchBuilder {
5
6
  batch;
6
- baseUri;
7
- constructor(baseUri) {
7
+ repository;
8
+ constructor(repository) {
8
9
  this.batch = {
9
10
  v: []
10
11
  };
11
- this.baseUri = baseUri;
12
+ this.repository = repository;
12
13
  }
13
14
  addInsert(key, value) {
14
15
  this.batch.v.push({
@@ -34,22 +35,8 @@ class BatchBuilder {
34
35
  return this;
35
36
  }
36
37
  execute() {
37
- return fetch(`${this.baseUri}/Batch`, {
38
- method: 'POST',
39
- headers: {
40
- 'content-type': 'application/json;charset=UTF-8',
41
- },
42
- body: JSON.stringify(this.batch),
43
- })
44
- .then(res => {
45
- const json = res.json();
46
- return json;
47
- })
48
- .catch((err) => {
49
- return {};
50
- })
51
- .then(res => {
52
- return res;
38
+ return this.repository.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Batch, `Batch`, 'POST', this.batch, {
39
+ 'content-type': 'application/json;charset=UTF-8',
53
40
  });
54
41
  }
55
42
  }
@@ -1,9 +1,10 @@
1
1
  import { Entity } from "../../models/Entity";
2
+ import { Repository } from "../Repository";
2
3
  import { WhereBuilder } from "./filter/WhereBuilder";
3
4
  export declare class QueryBuilder<T, TKey> {
4
5
  private filters;
5
- private baseUri;
6
- constructor(baseUri: string | null);
6
+ private repository;
7
+ constructor(repository: Repository<T, TKey>);
7
8
  where(): WhereBuilder<T, TKey>;
8
9
  filter(predicate: string): QueryBuilder<T, TKey>;
9
10
  top(value: number): QueryBuilder<T, TKey>;
@@ -1,17 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.QueryBuilder = void 0;
4
+ const RepositoryEndpoint_1 = require("../../models/RepositoryEndpoint");
4
5
  const Repository_1 = require("../Repository");
5
6
  const WhereBuilder_1 = require("./filter/WhereBuilder");
6
7
  const FilterOperations_1 = require("./FilterOperations");
7
8
  class QueryBuilder {
8
9
  filters;
9
- baseUri;
10
- constructor(baseUri) {
10
+ repository;
11
+ constructor(repository) {
11
12
  this.filters = {
12
13
  o: []
13
14
  };
14
- this.baseUri = baseUri;
15
+ this.repository = repository;
15
16
  }
16
17
  where() {
17
18
  return new WhereBuilder_1.WhereBuilder(this);
@@ -66,22 +67,8 @@ class QueryBuilder {
66
67
  return this;
67
68
  }
68
69
  execute() {
69
- return fetch(`${this.baseUri}/Query`, {
70
- method: 'POST',
71
- headers: {
72
- 'content-type': 'application/json;charset=UTF-8',
73
- },
74
- body: JSON.stringify(this.filters),
75
- })
76
- .then(res => {
77
- const json = res.json();
78
- return json;
79
- })
80
- .catch((err) => {
81
- return {};
82
- })
83
- .then(res => {
84
- return res;
70
+ return this.repository.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Query, `Query`, 'POST', this.filters, {
71
+ 'content-type': 'application/json;charset=UTF-8',
85
72
  });
86
73
  }
87
74
  count() {
@@ -116,22 +103,8 @@ class QueryBuilder {
116
103
  return this.executeOperation("Sum", "decimal");
117
104
  }
118
105
  executeOperation(operation, returnType) {
119
- return fetch(`${this.baseUri}/Operation?op=${operation}&returnType=${returnType}`, {
120
- method: 'POST',
121
- headers: {
122
- 'content-type': 'application/json;charset=UTF-8',
123
- },
124
- body: JSON.stringify(this.filters),
125
- })
126
- .then(res => {
127
- const json = res.json();
128
- return json;
129
- })
130
- .catch((err) => {
131
- throw new Error(err);
132
- })
133
- .then(res => {
134
- return res;
106
+ return this.repository.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Operation, `Operation?op=${operation}&returnType=${returnType}`, 'POST', this.filters, {
107
+ 'content-type': 'application/json;charset=UTF-8',
135
108
  });
136
109
  }
137
110
  }
package/lib/index.d.ts CHANGED
@@ -1,9 +1,8 @@
1
- import { BatchResults } from "./engine/batch/BatchResults";
2
- import { ICommand } from "./interfaces/ICommand";
3
- import { IQuery } from "./interfaces/IQuery";
4
- import { IRepository } from "./interfaces/IRepository";
5
- import { Entity } from "./models/Entity";
6
- import { State } from "./models/State";
7
- import { RepositoryServices } from "./servicecollection/RepositoryServices";
8
- export { RepositoryServices };
9
- export type { IRepository, ICommand, IQuery, State, BatchResults, Entity };
1
+ export type { BatchResults } from "./engine/batch/BatchResults";
2
+ export type { ICommand } from "./interfaces/ICommand";
3
+ export type { IQuery } from "./interfaces/IQuery";
4
+ export type { IRepository } from "./interfaces/IRepository";
5
+ export type { Entity } from "./models/Entity";
6
+ export type { State } from "./models/State";
7
+ export { RepositoryEndpoint } from "./models/RepositoryEndpoint";
8
+ export { RepositoryServices } from "./servicecollection/RepositoryServices";
package/lib/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RepositoryServices = void 0;
4
- const RepositoryServices_1 = require("./servicecollection/RepositoryServices");
3
+ exports.RepositoryServices = exports.RepositoryEndpoint = void 0;
4
+ var RepositoryEndpoint_1 = require("./models/RepositoryEndpoint");
5
+ Object.defineProperty(exports, "RepositoryEndpoint", { enumerable: true, get: function () { return RepositoryEndpoint_1.RepositoryEndpoint; } });
6
+ var RepositoryServices_1 = require("./servicecollection/RepositoryServices");
5
7
  Object.defineProperty(exports, "RepositoryServices", { enumerable: true, get: function () { return RepositoryServices_1.RepositoryServices; } });
@@ -0,0 +1,10 @@
1
+ export declare enum RepositoryEndpoint {
2
+ Get = 1,
3
+ Exist = 2,
4
+ Query = 3,
5
+ Operation = 4,
6
+ Insert = 5,
7
+ Update = 6,
8
+ Delete = 7,
9
+ Batch = 8
10
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RepositoryEndpoint = void 0;
4
+ var RepositoryEndpoint;
5
+ (function (RepositoryEndpoint) {
6
+ RepositoryEndpoint[RepositoryEndpoint["Get"] = 1] = "Get";
7
+ RepositoryEndpoint[RepositoryEndpoint["Exist"] = 2] = "Exist";
8
+ RepositoryEndpoint[RepositoryEndpoint["Query"] = 3] = "Query";
9
+ RepositoryEndpoint[RepositoryEndpoint["Operation"] = 4] = "Operation";
10
+ RepositoryEndpoint[RepositoryEndpoint["Insert"] = 5] = "Insert";
11
+ RepositoryEndpoint[RepositoryEndpoint["Update"] = 6] = "Update";
12
+ RepositoryEndpoint[RepositoryEndpoint["Delete"] = 7] = "Delete";
13
+ RepositoryEndpoint[RepositoryEndpoint["Batch"] = 8] = "Batch";
14
+ })(RepositoryEndpoint || (exports.RepositoryEndpoint = RepositoryEndpoint = {}));
@@ -1,7 +1,14 @@
1
+ import { RepositoryEndpoint } from "../models/RepositoryEndpoint";
1
2
  export declare class RepositorySettings {
2
3
  name: string;
3
4
  uri: string | null;
4
5
  path: string | null;
5
6
  complexKey: boolean;
7
+ private headersEnrichers;
8
+ private errorsHandlers;
6
9
  constructor();
10
+ addHeadersEnricher(enricher: (endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit, body: any) => HeadersInit): this;
11
+ addErrorHandler(handler: (endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit, body: any, err: any) => boolean): this;
12
+ enrichHeaders(endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit | undefined, body: any): HeadersInit;
13
+ manageError(endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit, body: any, err: any): boolean;
7
14
  }
@@ -6,11 +6,47 @@ class RepositorySettings {
6
6
  uri;
7
7
  path;
8
8
  complexKey;
9
+ headersEnrichers;
10
+ errorsHandlers;
9
11
  constructor() {
10
12
  this.name = "";
11
13
  this.uri = null;
12
14
  this.path = null;
13
15
  this.complexKey = false;
16
+ this.headersEnrichers = new Array();
17
+ this.errorsHandlers = new Array();
18
+ }
19
+ addHeadersEnricher(enricher) {
20
+ this.headersEnrichers.push(enricher);
21
+ return this;
22
+ }
23
+ addErrorHandler(handler) {
24
+ this.errorsHandlers.push(handler);
25
+ return this;
26
+ }
27
+ enrichHeaders(endpoint, uri, method, headers, body) {
28
+ const requestHeaders = new Headers();
29
+ const setHeaders = (currentHeaders) => {
30
+ let forcedHeaders = currentHeaders;
31
+ for (let header in currentHeaders) {
32
+ if (!requestHeaders.has(header)) {
33
+ requestHeaders.set(header, forcedHeaders[header]);
34
+ }
35
+ }
36
+ };
37
+ if (headers != undefined) {
38
+ setHeaders(headers);
39
+ }
40
+ for (let enricher of this.headersEnrichers) {
41
+ setHeaders(enricher(endpoint, uri, method, requestHeaders, body));
42
+ }
43
+ return requestHeaders;
44
+ }
45
+ manageError(endpoint, uri, method, headers, body, err) {
46
+ let retry = this.errorsHandlers.length > 0;
47
+ for (let handler of this.errorsHandlers)
48
+ retry &&= handler(endpoint, uri, method, headers, body, err);
49
+ return retry;
14
50
  }
15
51
  }
16
52
  exports.RepositorySettings = RepositorySettings;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rystem.repository.client",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "author": "Alessandro Rapiti <alessandro.rapiti44@gmail.com>",
5
5
  "license": "MIT",
6
6
  "description": "It can simplify the integration with rystem repository server.",