rest-client-sdk 6.7.1 → 7.0.0-rc.10

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.
@@ -39,3 +39,15 @@ export function removeUndefinedHeaders(headers: HeadersInit): HeadersInit {
39
39
 
40
40
  return Object.fromEntries(filterEntries(Object.entries(headers)));
41
41
  }
42
+
43
+ export function convertToRecord(headers: HeadersInit): Record<string, string> {
44
+ if (headers instanceof Headers) {
45
+ return Object.fromEntries(headers.entries());
46
+ }
47
+
48
+ if (Array.isArray(headers)) {
49
+ return Object.fromEntries(headers);
50
+ }
51
+
52
+ return headers;
53
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import RestClientSdk from './RestClientSdk';
1
+ import type AsyncStorageInterface from './AsyncStorageInterface';
2
2
  import {
3
3
  BadRequestError,
4
4
  ConflictError,
@@ -11,37 +11,35 @@ import {
11
11
  OauthError,
12
12
  InvalidGrantError,
13
13
  } from './ErrorFactory';
14
- import AbstractClient from './client/AbstractClient';
15
- import TokenStorage from './TokenStorage';
14
+ import Mapping from './Mapping';
15
+ import Attribute from './Mapping/Attribute';
16
+ import ClassMetadata from './Mapping/ClassMetadata';
17
+ import Relation from './Mapping/Relation';
18
+ import RestClientSdk from './RestClientSdk';
19
+ import type { SdkMetadata, MetadataDefinition } from './RestClientSdk';
20
+ // eslint-disable-next-line import/no-duplicates
21
+ import type RestClientSdkInterface from './RestClientSdkInterface';
22
+ // eslint-disable-next-line import/no-duplicates
23
+ import type { Config } from './RestClientSdkInterface';
16
24
  import AbstractTokenGenerator from './TokenGenerator/AbstractTokenGenerator';
17
25
  import AuthorizationCodeFlowTokenGenerator from './TokenGenerator/AuthorizationCodeFlowTokenGenerator';
18
26
  import ClientCredentialsGenerator from './TokenGenerator/ClientCredentialsGenerator';
19
27
  import PasswordGenerator from './TokenGenerator/PasswordGenerator';
20
28
  import ProvidedTokenGenerator from './TokenGenerator/ProvidedTokenGenerator';
21
- import Serializer from './serializer/Serializer';
22
- import type SerializerInterface from './serializer/SerializerInterface';
23
- import Mapping from './Mapping';
24
- import ClassMetadata from './Mapping/ClassMetadata';
25
- import Attribute from './Mapping/Attribute';
26
- import Relation from './Mapping/Relation';
29
+ import type TokenGeneratorInterface from './TokenGenerator/TokenGeneratorInterface';
27
30
  import type {
28
31
  Token,
29
32
  ErrorBody,
30
33
  TokenBody,
31
34
  TokenResponse,
32
35
  } from './TokenGenerator/types';
33
-
34
- import type { SdkMetadata, MetadataDefinition } from './RestClientSdk';
35
- // eslint-disable-next-line import/no-duplicates
36
- import type RestClientSdkInterface from './RestClientSdkInterface';
37
- // eslint-disable-next-line import/no-duplicates
38
- import type { Config } from './RestClientSdkInterface';
39
- // eslint-disable-next-line import/no-duplicates
40
- import type TokenStorageInterface from './TokenStorageInterface';
41
- // eslint-disable-next-line import/no-duplicates
36
+ import TokenStorage from './TokenStorage';
37
+ import TokenStorageInterface from './TokenStorageInterface';
42
38
  import type { HasExpiresAt } from './TokenStorageInterface';
43
- import type TokenGeneratorInterface from './TokenGenerator/TokenGeneratorInterface';
44
- import type AsyncStorageInterface from './AsyncStorageInterface';
39
+ import AbstractClient from './client/AbstractClient';
40
+ import Serializer from './serializer/Serializer';
41
+ import type SerializerInterface from './serializer/SerializerInterface';
42
+ import { Logger, Log, LoggerHistory } from './utils/logging';
45
43
 
46
44
  export default RestClientSdk;
47
45
  export {
@@ -67,6 +65,7 @@ export {
67
65
  ClassMetadata,
68
66
  Attribute,
69
67
  Relation,
68
+ Logger,
70
69
  };
71
70
  export type {
72
71
  SerializerInterface,
@@ -82,4 +81,6 @@ export type {
82
81
  TokenGeneratorInterface,
83
82
  AsyncStorageInterface,
84
83
  MetadataDefinition,
84
+ LoggerHistory,
85
+ Log,
85
86
  };
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable @typescript-eslint/no-unused-vars */
2
- import Serializer from './Serializer';
3
2
  import ClassMetadata from '../Mapping/ClassMetadata';
3
+ import Serializer from './Serializer';
4
4
 
5
5
  class JsSerializer extends Serializer {
6
6
  encodeItem(
@@ -1,11 +1,11 @@
1
1
  /* eslint-disable @typescript-eslint/no-unused-vars */
2
+ import ClassMetadata from '../Mapping/ClassMetadata';
2
3
  import SerializerInterface, {
3
4
  Entity,
4
5
  NormalizedObject,
5
6
  NormalizedList,
6
7
  EntityList,
7
8
  } from './SerializerInterface';
8
- import ClassMetadata from '../Mapping/ClassMetadata';
9
9
 
10
10
  class Serializer implements SerializerInterface {
11
11
  /**
@@ -0,0 +1,96 @@
1
+ import { convertToRecord } from '../client/headerUtils';
2
+
3
+ type SdkRequest = RequestInit & { url: string };
4
+
5
+ // type SdkResponse = Partial<Response>;
6
+ type SdkResponse = {
7
+ status: number;
8
+ headers: Record<string, string>;
9
+ };
10
+
11
+ const generateId = () =>
12
+ `rest-client-sdk|${Date.now()}-${
13
+ Math.floor(Math.random() * (9e12 - 1)) + 1e12
14
+ }`;
15
+
16
+ export type SerializableRequest = {
17
+ url: string;
18
+ method?: string;
19
+ headers?: Record<string, string>;
20
+ body?: string;
21
+ };
22
+
23
+ export type Log = {
24
+ id: string;
25
+ request?: SerializableRequest;
26
+ response?: SdkResponse;
27
+ logTimes: {
28
+ request?: number;
29
+ response?: number;
30
+ };
31
+ };
32
+
33
+ export type LoggerHistory = Array<Log>;
34
+
35
+ function serializeRequest(req: SdkRequest): SerializableRequest {
36
+ const request = { ...req }; // Make a clone, useful for doing destructive things
37
+ return {
38
+ method: request.method, // The Request Method, e.g. GET, POST, DELETE
39
+ url: request.url, // The URL
40
+ headers: request.headers ? convertToRecord(request.headers) : undefined,
41
+ body: typeof request.body === 'string' ? request.body : undefined,
42
+ };
43
+ }
44
+
45
+ // eslint-disable-next-line import/prefer-default-export
46
+ export class Logger {
47
+ #history: LoggerHistory;
48
+
49
+ get history(): LoggerHistory {
50
+ return this.#history;
51
+ }
52
+
53
+ constructor() {
54
+ this.#history = [];
55
+ }
56
+
57
+ logRequest(params: SdkRequest): string {
58
+ const log: Log = {
59
+ id: generateId(),
60
+ request: serializeRequest(params),
61
+ logTimes: { request: Date.now() },
62
+ };
63
+
64
+ this.#history.push(log);
65
+
66
+ return log.id;
67
+ }
68
+
69
+ logResponse(response: Response, requestId?: undefined | string): string {
70
+ const id = requestId || generateId();
71
+
72
+ // console.log('clone');
73
+ // const clonedResponse = response.clone(); // THIS IS THE LINE THAT CAUSES THE PROBLEM
74
+ // console.log('after clone', clonedResponse.status);
75
+
76
+ const logContent: SdkResponse = {
77
+ status: response.status, // clonedResponse.status,
78
+ headers: Object.fromEntries(response.headers),
79
+ };
80
+
81
+ const foundLogIndex = this.#history.findIndex((log) => log.id === id);
82
+
83
+ if (foundLogIndex > -1) {
84
+ this.#history[foundLogIndex].response = logContent;
85
+ this.#history[foundLogIndex].logTimes.response = Date.now();
86
+ } else {
87
+ this.#history.push({
88
+ id,
89
+ response: logContent,
90
+ logTimes: { response: Date.now() },
91
+ });
92
+ }
93
+
94
+ return id;
95
+ }
96
+ }
@@ -1,10 +1,10 @@
1
1
  /* eslint-disable import/prefer-default-export */
2
- import type AbstractClient from '../client/AbstractClient';
3
2
  import type ClassMetadata from '../Mapping/ClassMetadata';
4
3
  // eslint-disable-next-line import/no-duplicates
5
4
  import type RestClientSdk from '../RestClientSdk';
6
5
  // eslint-disable-next-line import/no-duplicates
7
6
  import type { MetadataDefinition, SdkMetadata } from '../RestClientSdk';
7
+ import type AbstractClient from '../client/AbstractClient';
8
8
 
9
9
  export function generateRepository<D extends MetadataDefinition>(
10
10
  sdk: RestClientSdk<SdkMetadata>,