@algolia/client-common 5.0.0-alpha.10 → 5.0.0-alpha.100

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.
Files changed (46) hide show
  1. package/dist/{client-common.cjs.js → client-common.cjs} +168 -205
  2. package/dist/client-common.esm.node.js +168 -204
  3. package/dist/index.d.ts +9 -9
  4. package/dist/src/cache/createBrowserLocalStorageCache.d.ts +2 -2
  5. package/dist/src/cache/createBrowserLocalStorageCache.d.ts.map +1 -1
  6. package/dist/src/cache/createFallbackableCache.d.ts +2 -2
  7. package/dist/src/cache/createMemoryCache.d.ts +2 -2
  8. package/dist/src/cache/createNullCache.d.ts +2 -2
  9. package/dist/src/cache/index.d.ts +4 -4
  10. package/dist/src/constants.d.ts +6 -6
  11. package/dist/src/createAlgoliaAgent.d.ts +2 -2
  12. package/dist/src/createAuth.d.ts +5 -5
  13. package/dist/src/createEchoRequester.d.ts +6 -6
  14. package/dist/src/createEchoRequester.d.ts.map +1 -1
  15. package/dist/src/createIterablePromise.d.ts +12 -12
  16. package/dist/src/getAlgoliaAgent.d.ts +7 -7
  17. package/dist/src/getAlgoliaAgent.d.ts.map +1 -1
  18. package/dist/src/transporter/createStatefulHost.d.ts +2 -2
  19. package/dist/src/transporter/createTransporter.d.ts +2 -2
  20. package/dist/src/transporter/errors.d.ts +37 -20
  21. package/dist/src/transporter/errors.d.ts.map +1 -1
  22. package/dist/src/transporter/helpers.d.ts +8 -8
  23. package/dist/src/transporter/helpers.d.ts.map +1 -1
  24. package/dist/src/transporter/index.d.ts +6 -6
  25. package/dist/src/transporter/responses.d.ts +4 -4
  26. package/dist/src/transporter/stackTrace.d.ts +3 -3
  27. package/dist/src/types/cache.d.ts +60 -46
  28. package/dist/src/types/cache.d.ts.map +1 -1
  29. package/dist/src/types/createClient.d.ts +11 -11
  30. package/dist/src/types/createClient.d.ts.map +1 -1
  31. package/dist/src/types/createIterablePromise.d.ts +35 -35
  32. package/dist/src/types/createIterablePromise.d.ts.map +1 -1
  33. package/dist/src/types/host.d.ts +32 -32
  34. package/dist/src/types/host.d.ts.map +1 -1
  35. package/dist/src/types/index.d.ts +6 -6
  36. package/dist/src/types/requester.d.ts +65 -65
  37. package/dist/src/types/requester.d.ts.map +1 -1
  38. package/dist/src/types/transporter.d.ts +127 -127
  39. package/dist/src/types/transporter.d.ts.map +1 -1
  40. package/package.json +11 -8
  41. package/src/__tests__/cache/browser-local-storage-cache.test.ts +61 -9
  42. package/src/cache/createBrowserLocalStorageCache.ts +55 -6
  43. package/src/createEchoRequester.ts +2 -2
  44. package/src/transporter/errors.ts +39 -3
  45. package/src/transporter/helpers.ts +14 -6
  46. package/src/types/cache.ts +17 -0
@@ -25,7 +25,7 @@ export class ErrorWithStackTrace extends AlgoliaError {
25
25
  export class RetryError extends ErrorWithStackTrace {
26
26
  constructor(stackTrace: StackFrame[]) {
27
27
  super(
28
- 'Unreachable hosts - your application id may be incorrect. If the error persists, contact support@algolia.com.',
28
+ 'Unreachable hosts - your application id may be incorrect. If the error persists, please create a ticket at https://support.algolia.com/ sharing steps we can use to reproduce the issue.',
29
29
  stackTrace,
30
30
  'RetryError'
31
31
  );
@@ -35,8 +35,13 @@ export class RetryError extends ErrorWithStackTrace {
35
35
  export class ApiError extends ErrorWithStackTrace {
36
36
  status: number;
37
37
 
38
- constructor(message: string, status: number, stackTrace: StackFrame[]) {
39
- super(message, stackTrace, 'ApiError');
38
+ constructor(
39
+ message: string,
40
+ status: number,
41
+ stackTrace: StackFrame[],
42
+ name = 'ApiError'
43
+ ) {
44
+ super(message, stackTrace, name);
40
45
  this.status = status;
41
46
  }
42
47
  }
@@ -49,3 +54,34 @@ export class DeserializationError extends AlgoliaError {
49
54
  this.response = response;
50
55
  }
51
56
  }
57
+
58
+ export type DetailedErrorWithMessage = {
59
+ message: string;
60
+ label: string;
61
+ };
62
+
63
+ export type DetailedErrorWithTypeID = {
64
+ id: string;
65
+ type: string;
66
+ name?: string;
67
+ };
68
+
69
+ export type DetailedError = {
70
+ code: string;
71
+ details?: DetailedErrorWithMessage[] | DetailedErrorWithTypeID[];
72
+ };
73
+
74
+ // DetailedApiError is only used by the ingestion client to return more informative error, other clients will use ApiClient.
75
+ export class DetailedApiError extends ApiError {
76
+ error: DetailedError;
77
+
78
+ constructor(
79
+ message: string,
80
+ status: number,
81
+ error: DetailedError,
82
+ stackTrace: StackFrame[]
83
+ ) {
84
+ super(message, status, stackTrace, 'DetailedApiError');
85
+ this.error = error;
86
+ }
87
+ }
@@ -8,7 +8,7 @@ import type {
8
8
  StackFrame,
9
9
  } from '../types';
10
10
 
11
- import { ApiError, DeserializationError } from './errors';
11
+ import { ApiError, DeserializationError, DetailedApiError } from './errors';
12
12
 
13
13
  export function shuffle<TData>(array: TData[]): TData[] {
14
14
  const shuffledArray = array;
@@ -49,11 +49,11 @@ export function serializeQueryParameters(parameters: QueryParameters): string {
49
49
  return Object.keys(parameters)
50
50
  .map(
51
51
  (key) =>
52
- `${key}=${
52
+ `${key}=${encodeURIComponent(
53
53
  isObjectOrArray(parameters[key])
54
54
  ? JSON.stringify(parameters[key])
55
55
  : parameters[key]
56
- }`
56
+ )}`
57
57
  )
58
58
  .join('&');
59
59
  }
@@ -109,11 +109,19 @@ export function deserializeFailure(
109
109
  { content, status }: Response,
110
110
  stackFrame: StackFrame[]
111
111
  ): Error {
112
- let message = content;
113
112
  try {
114
- message = JSON.parse(content).message;
113
+ const parsed = JSON.parse(content);
114
+ if ('error' in parsed) {
115
+ return new DetailedApiError(
116
+ parsed.message,
117
+ status,
118
+ parsed.error,
119
+ stackFrame
120
+ );
121
+ }
122
+ return new ApiError(parsed.message, status, stackFrame);
115
123
  } catch (e) {
116
124
  // ..
117
125
  }
118
- return new ApiError(message, status, stackFrame);
126
+ return new ApiError(content, status, stackFrame);
119
127
  }
@@ -47,12 +47,29 @@ export type BrowserLocalStorageOptions = {
47
47
  */
48
48
  key: string;
49
49
 
50
+ /**
51
+ * The time to live for each cached item in seconds.
52
+ */
53
+ timeToLive?: number;
54
+
50
55
  /**
51
56
  * The native local storage implementation.
52
57
  */
53
58
  localStorage?: Storage;
54
59
  };
55
60
 
61
+ export type BrowserLocalStorageCacheItem = {
62
+ /**
63
+ * The cache item creation timestamp.
64
+ */
65
+ timestamp: number;
66
+
67
+ /**
68
+ * The cache item value.
69
+ */
70
+ value: any;
71
+ };
72
+
56
73
  export type FallbackableCacheOptions = {
57
74
  /**
58
75
  * List of caches order by priority.