rystem.repository.client 0.2.4 → 0.2.6

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 CHANGED
@@ -51,12 +51,12 @@ RepositoryServices
51
51
  .addRepository<IperUser, string>(x => {
52
52
  x.name = "test";
53
53
  x.path = "SuperUser";
54
- x.addHeadersEnricher((...args) => {
54
+ x.addHeadersEnricher(async (...args) => {
55
55
  return {
56
56
  "Authorization-UI": "Bearer dsjadjalsdjalsdjalsda"
57
57
  }
58
58
  });
59
- x.addHeadersEnricher((endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit, body: any) => {
59
+ x.addHeadersEnricher(async (endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit, body: any) => {
60
60
  return {
61
61
  "Authorization-UI2": "Bearer dsjadjalsdjalsdjalsda"
62
62
  }
@@ -13,6 +13,9 @@ export declare class Repository<T, TKey> implements IRepository<T, TKey> {
13
13
  private transformResponse;
14
14
  makeRequest<TResponse>(endpoint: RepositoryEndpoint, path: string, method: string, body?: any, bodyIsKey?: boolean, skipSerialization?: boolean, skipSerializationOnResponse?: boolean, headers?: HeadersInit): Promise<TResponse>;
15
15
  makeRequestAsStream<TResponse>(endpoint: RepositoryEndpoint, path: string, method: string, reader: (entity: TResponse) => void, body?: any, bodyIsKey?: boolean, skipSerialization?: boolean, skipSerializationOnResponse?: boolean, headers?: HeadersInit, cancellationToken?: CancellationToken | null): Promise<Array<TResponse>>;
16
+ private isPrimitive;
17
+ private objectToString;
18
+ private keyToPlainForQuery;
16
19
  get(key: TKey): Promise<T>;
17
20
  insert(key: TKey, value: T): Promise<State<T, TKey>>;
18
21
  update(key: TKey, value: T): Promise<State<T, TKey>>;
@@ -139,7 +139,7 @@ class Repository {
139
139
  while (retry) {
140
140
  response = await fetch(uri, {
141
141
  method: method,
142
- headers: this.settings.enrichHeaders(endpoint, uri, method, headers, body),
142
+ headers: await this.settings.enrichHeaders(endpoint, uri, method, headers, body),
143
143
  body: body == null ? null : JSON.stringify(skipSerialization ? body : this.serializeRequestBody(body, bodyIsKey))
144
144
  })
145
145
  .then(async (res) => {
@@ -148,8 +148,8 @@ class Repository {
148
148
  throw json;
149
149
  return json;
150
150
  })
151
- .catch((err) => {
152
- retry = this.settings.manageError(endpoint, uri, method, headers, body, err);
151
+ .catch(async (err) => {
152
+ retry = await this.settings.manageError(endpoint, uri, method, headers, body, err);
153
153
  return null;
154
154
  })
155
155
  .then(res => {
@@ -166,7 +166,7 @@ class Repository {
166
166
  let response = [];
167
167
  await fetch(uri, {
168
168
  method: method,
169
- headers: this.settings.enrichHeaders(endpoint, uri, method, headers, body),
169
+ headers: await this.settings.enrichHeaders(endpoint, uri, method, headers, body),
170
170
  body: body == null ? null : JSON.stringify(skipSerialization ? body : this.serializeRequestBody(body, bodyIsKey))
171
171
  })
172
172
  .then(async (res) => {
@@ -196,9 +196,34 @@ class Repository {
196
196
  });
197
197
  return response;
198
198
  }
199
+ // Helper: is primitive
200
+ isPrimitive(value) {
201
+ return (value === null ||
202
+ (typeof value !== "object" && typeof value !== "function"));
203
+ }
204
+ // Helper: deterministic object -> string conversion
205
+ objectToString(obj) {
206
+ if (obj == null)
207
+ return String(obj);
208
+ if (Array.isArray(obj))
209
+ return obj.map(v => String(v)).join(this.settings.keySeparator);
210
+ if (typeof obj === "object")
211
+ return Object.values(obj).map(v => String(v)).join(this.settings.keySeparator);
212
+ return String(obj);
213
+ }
214
+ // Helper: convert key to plain representation for querystring when no keyTransformer is present
215
+ keyToPlainForQuery(key) {
216
+ const keyTransformer = this.settings.keyTransformer;
217
+ if (keyTransformer?.toPlain)
218
+ return String(keyTransformer.toPlain(key));
219
+ if (this.isPrimitive(key))
220
+ return String(key);
221
+ return this.objectToString(key);
222
+ }
199
223
  get(key) {
200
224
  if (!this.settings.complexKey) {
201
- return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Get, `Get?key=${key}`, 'GET');
225
+ const keyString = encodeURIComponent(this.keyToPlainForQuery(key));
226
+ return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Get, `Get?key=${keyString}`, 'GET');
202
227
  }
203
228
  else {
204
229
  return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Get, `Get`, 'POST', key, true, false, false, {
@@ -208,7 +233,8 @@ class Repository {
208
233
  }
209
234
  insert(key, value) {
210
235
  if (!this.settings.complexKey) {
211
- return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Insert, `Insert?key=${key}`, 'POST', value, false, false, false, {
236
+ const keyString = encodeURIComponent(this.keyToPlainForQuery(key));
237
+ return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Insert, `Insert?key=${keyString}`, 'POST', value, false, false, false, {
212
238
  'content-type': 'application/json;charset=UTF-8'
213
239
  });
214
240
  }
@@ -223,7 +249,8 @@ class Repository {
223
249
  }
224
250
  update(key, value) {
225
251
  if (!this.settings.complexKey) {
226
- return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Update, `Update?key=${key}`, 'POST', value, false, false, false, {
252
+ const keyString = encodeURIComponent(this.keyToPlainForQuery(key));
253
+ return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Update, `Update?key=${keyString}`, 'POST', value, false, false, false, {
227
254
  'content-type': 'application/json;charset=UTF-8'
228
255
  });
229
256
  }
@@ -238,7 +265,8 @@ class Repository {
238
265
  }
239
266
  exist(key) {
240
267
  if (!this.settings.complexKey) {
241
- return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Exist, `Exist?key=${key}`, 'GET');
268
+ const keyString = encodeURIComponent(this.keyToPlainForQuery(key));
269
+ return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Exist, `Exist?key=${keyString}`, 'GET');
242
270
  }
243
271
  else {
244
272
  return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Exist, `Exist`, 'POST', key, true, false, false, {
@@ -248,7 +276,8 @@ class Repository {
248
276
  }
249
277
  delete(key) {
250
278
  if (!this.settings.complexKey) {
251
- return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Delete, `Delete?key=${key}`, 'GET');
279
+ const keyString = encodeURIComponent(this.keyToPlainForQuery(key));
280
+ return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Delete, `Delete?key=${keyString}`, 'GET');
252
281
  }
253
282
  else {
254
283
  return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Delete, `Delete`, 'POST', key, true, false, false, {
@@ -10,12 +10,13 @@ export declare class RepositorySettings {
10
10
  case: "PascalCase" | "CamelCase";
11
11
  transformer?: ITransformer<any>;
12
12
  keyTransformer?: ITransformer<any>;
13
+ keySeparator: string;
13
14
  complexKey: boolean;
14
15
  private headersEnrichers;
15
16
  private errorsHandlers;
16
17
  constructor();
17
- addHeadersEnricher(enricher: (endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit, body: any) => HeadersInit): this;
18
- addErrorHandler(handler: (endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit, body: any, err: any) => boolean): this;
19
- enrichHeaders(endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit | undefined, body: any): HeadersInit;
20
- manageError(endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit, body: any, err: any): boolean;
18
+ addHeadersEnricher(enricher: (endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit, body: any) => Promise<HeadersInit>): this;
19
+ addErrorHandler(handler: (endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit, body: any, err: any) => Promise<boolean>): this;
20
+ enrichHeaders(endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit | undefined, body: any): Promise<HeadersInit>;
21
+ manageError(endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit, body: any, err: any): Promise<boolean>;
21
22
  }
@@ -8,6 +8,7 @@ class RepositorySettings {
8
8
  case;
9
9
  transformer; // For T
10
10
  keyTransformer; // For TKey
11
+ keySeparator;
11
12
  complexKey;
12
13
  headersEnrichers;
13
14
  errorsHandlers;
@@ -17,6 +18,7 @@ class RepositorySettings {
17
18
  this.path = null;
18
19
  this.case = "PascalCase";
19
20
  this.complexKey = false;
21
+ this.keySeparator = "|||";
20
22
  this.headersEnrichers = new Array();
21
23
  this.errorsHandlers = new Array();
22
24
  }
@@ -28,7 +30,7 @@ class RepositorySettings {
28
30
  this.errorsHandlers.push(handler);
29
31
  return this;
30
32
  }
31
- enrichHeaders(endpoint, uri, method, headers, body) {
33
+ async enrichHeaders(endpoint, uri, method, headers, body) {
32
34
  const requestHeaders = new Headers();
33
35
  const setHeaders = (currentHeaders) => {
34
36
  let forcedHeaders = currentHeaders;
@@ -42,14 +44,14 @@ class RepositorySettings {
42
44
  setHeaders(headers);
43
45
  }
44
46
  for (let enricher of this.headersEnrichers) {
45
- setHeaders(enricher(endpoint, uri, method, requestHeaders, body));
47
+ setHeaders(await enricher(endpoint, uri, method, requestHeaders, body));
46
48
  }
47
49
  return requestHeaders;
48
50
  }
49
- manageError(endpoint, uri, method, headers, body, err) {
51
+ async manageError(endpoint, uri, method, headers, body, err) {
50
52
  let retry = this.errorsHandlers.length > 0;
51
53
  for (let handler of this.errorsHandlers)
52
- retry &&= handler(endpoint, uri, method, headers, body, err);
54
+ retry &&= await handler(endpoint, uri, method, headers, body, err);
53
55
  return retry;
54
56
  }
55
57
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rystem.repository.client",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
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.",