getta 0.4.7 → 1.0.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.
- package/README.md +161 -7
- package/dist/main/index.mjs +621 -0
- package/dist/main/index.mjs.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/{lib → dist}/types/constants.d.ts +2 -2
- package/dist/types/constants.d.ts.map +1 -0
- package/dist/types/helpers/buildEndpoint/index.d.ts +3 -0
- package/dist/types/helpers/buildEndpoint/index.d.ts.map +1 -0
- package/{lib/types/helpers/build-endpoint → dist/types/helpers/buildEndpoint}/types.d.ts +2 -2
- package/dist/types/helpers/buildEndpoint/types.d.ts.map +1 -0
- package/dist/types/helpers/defaultPathTemplateCallback/index.d.ts +2 -0
- package/dist/types/helpers/defaultPathTemplateCallback/index.d.ts.map +1 -0
- package/dist/types/helpers/delay/index.d.ts +2 -0
- package/{lib → dist}/types/helpers/delay/index.d.ts.map +1 -1
- package/dist/types/helpers/getResponseGroup/index.d.ts +2 -0
- package/dist/types/helpers/getResponseGroup/index.d.ts.map +1 -0
- package/dist/types/helpers/isCacheabilityValid/index.d.ts +3 -0
- package/dist/types/helpers/isCacheabilityValid/index.d.ts.map +1 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.d.ts.map +1 -0
- package/{lib → dist}/types/main.d.ts +12 -12
- package/dist/types/main.d.ts.map +1 -0
- package/dist/types/types.d.ts +161 -0
- package/dist/types/types.d.ts.map +1 -0
- package/package.json +113 -145
- package/src/{__tests__ → __testUtils__}/data/136-7317.json +1 -1
- package/src/{__tests__ → __testUtils__}/data/180-1387.json +1 -1
- package/src/{__tests__ → __testUtils__}/data/183-3905.json +1 -1
- package/src/{__tests__ → __testUtils__}/data/202-3315.json +1 -1
- package/src/__testUtils__/data/index.ts +23 -0
- package/src/__testUtils__/helpers/index.ts +49 -0
- package/src/constants.ts +28 -28
- package/src/helpers/buildEndpoint/index.ts +34 -0
- package/src/helpers/{build-endpoint → buildEndpoint}/types.ts +2 -2
- package/src/helpers/defaultPathTemplateCallback/index.test.ts +11 -0
- package/src/helpers/defaultPathTemplateCallback/index.ts +17 -0
- package/src/helpers/delay/index.ts +2 -2
- package/src/helpers/getResponseGroup/index.ts +25 -0
- package/src/helpers/isCacheabilityValid/index.ts +6 -0
- package/src/index.ts +4 -4
- package/src/main.test.ts +283 -411
- package/src/main.ts +238 -241
- package/src/types.ts +114 -16
- package/tsconfig.build.json +14 -0
- package/tsconfig.json +9 -0
- package/CHANGELOG.md +0 -250
- package/jest.setup.js +0 -10
- package/lib/browser/index.js +0 -2
- package/lib/browser/index.js.map +0 -1
- package/lib/browser/production.analysis.txt +0 -91
- package/lib/main/constants.js +0 -97
- package/lib/main/helpers/build-endpoint/index.js +0 -38
- package/lib/main/helpers/build-endpoint/types.js +0 -1
- package/lib/main/helpers/default-path-template-callback/index.js +0 -16
- package/lib/main/helpers/delay/index.js +0 -12
- package/lib/main/helpers/get-response-group/index.js +0 -27
- package/lib/main/helpers/is-cacheability-valid/index.js +0 -13
- package/lib/main/index.js +0 -62
- package/lib/main/main.js +0 -580
- package/lib/main/types.js +0 -1
- package/lib/module/constants.mjs +0 -49
- package/lib/module/helpers/build-endpoint/index.mjs +0 -28
- package/lib/module/helpers/build-endpoint/types.mjs +0 -0
- package/lib/module/helpers/default-path-template-callback/index.mjs +0 -9
- package/lib/module/helpers/delay/index.mjs +0 -4
- package/lib/module/helpers/get-response-group/index.mjs +0 -19
- package/lib/module/helpers/is-cacheability-valid/index.mjs +0 -6
- package/lib/module/index.mjs +0 -4
- package/lib/module/main.mjs +0 -576
- package/lib/module/types.mjs +0 -0
- package/lib/types/__tests__/data/index.d.ts +0 -17
- package/lib/types/__tests__/data/index.d.ts.map +0 -1
- package/lib/types/__tests__/helpers/index.d.ts +0 -29
- package/lib/types/__tests__/helpers/index.d.ts.map +0 -1
- package/lib/types/__tests__/types.d.ts +0 -13
- package/lib/types/__tests__/types.d.ts.map +0 -1
- package/lib/types/constants.d.ts.map +0 -1
- package/lib/types/helpers/build-endpoint/index.d.ts +0 -3
- package/lib/types/helpers/build-endpoint/index.d.ts.map +0 -1
- package/lib/types/helpers/build-endpoint/types.d.ts.map +0 -1
- package/lib/types/helpers/default-path-template-callback/index.d.ts +0 -3
- package/lib/types/helpers/default-path-template-callback/index.d.ts.map +0 -1
- package/lib/types/helpers/default-path-template-callback/index.test.d.ts +0 -2
- package/lib/types/helpers/default-path-template-callback/index.test.d.ts.map +0 -1
- package/lib/types/helpers/delay/index.d.ts +0 -2
- package/lib/types/helpers/get-response-group/index.d.ts +0 -2
- package/lib/types/helpers/get-response-group/index.d.ts.map +0 -1
- package/lib/types/helpers/is-cacheability-valid/index.d.ts +0 -3
- package/lib/types/helpers/is-cacheability-valid/index.d.ts.map +0 -1
- package/lib/types/index.d.ts +0 -5
- package/lib/types/index.d.ts.map +0 -1
- package/lib/types/main.d.ts.map +0 -1
- package/lib/types/main.test.d.ts +0 -2
- package/lib/types/main.test.d.ts.map +0 -1
- package/lib/types/types.d.ts +0 -69
- package/lib/types/types.d.ts.map +0 -1
- package/src/__tests__/data/index.ts +0 -19
- package/src/__tests__/helpers/index.ts +0 -61
- package/src/__tests__/types.ts +0 -14
- package/src/helpers/build-endpoint/index.ts +0 -34
- package/src/helpers/default-path-template-callback/index.test.ts +0 -11
- package/src/helpers/default-path-template-callback/index.ts +0 -16
- package/src/helpers/get-response-group/index.ts +0 -22
- package/src/helpers/is-cacheability-valid/index.ts +0 -6
package/src/constants.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { PlainObject } from
|
|
1
|
+
import type { PlainObject } from './types.ts';
|
|
2
2
|
|
|
3
|
-
export const ARRAY_BUFFER_FORMAT =
|
|
4
|
-
export const BLOB_FORMAT =
|
|
5
|
-
export const FORM_DATA_FORMAT =
|
|
6
|
-
export const JSON_FORMAT =
|
|
7
|
-
export const TEXT_FORMAT =
|
|
3
|
+
export const ARRAY_BUFFER_FORMAT = 'arrayBuffer' as const;
|
|
4
|
+
export const BLOB_FORMAT = 'blob' as const;
|
|
5
|
+
export const FORM_DATA_FORMAT = 'formData' as const;
|
|
6
|
+
export const JSON_FORMAT = 'json' as const;
|
|
7
|
+
export const TEXT_FORMAT = 'text' as const;
|
|
8
8
|
|
|
9
9
|
export const STREAM_READERS = {
|
|
10
10
|
ARRAY_BUFFER_FORMAT,
|
|
@@ -16,48 +16,48 @@ export const STREAM_READERS = {
|
|
|
16
16
|
|
|
17
17
|
export const DEFAULT_BODY_PARSER = (body: PlainObject) => body;
|
|
18
18
|
export const DEFAULT_FETCH_TIMEOUT = 5000 as const;
|
|
19
|
-
export const DEFAULT_HEADERS = {
|
|
19
|
+
export const DEFAULT_HEADERS = { 'content-type': 'application/json' };
|
|
20
20
|
export const DEFAULT_MAX_REDIRECTS = 5 as const;
|
|
21
21
|
export const DEFAULT_MAX_RETRIES = 3 as const;
|
|
22
22
|
export const DEFAULT_PATH_TEMPLATE_REGEX = /({type})|({id})|({id,\+})|({brief\|standard})/g;
|
|
23
|
-
export const OPTIONAL_PATH_TEMPLATE_REGEX = /({
|
|
23
|
+
export const OPTIONAL_PATH_TEMPLATE_REGEX = /({\w+\?})/g;
|
|
24
24
|
export const DEFAULT_RATE_LIMIT = 50;
|
|
25
25
|
export const DEFAULT_REQUEST_RETRY_WAIT = 100;
|
|
26
26
|
|
|
27
27
|
export const MISSING_BASE_PATH_ERROR = `Getta expected to receive 'basePath' in the constructor options,
|
|
28
28
|
but recevied undefined.`;
|
|
29
29
|
|
|
30
|
-
export const MAX_REDIRECTS_EXCEEDED_ERROR =
|
|
30
|
+
export const MAX_REDIRECTS_EXCEEDED_ERROR = 'The request exceeded the maximum number of redirects, which is';
|
|
31
31
|
|
|
32
|
-
export const MAX_RETRIES_EXCEEDED_ERROR =
|
|
32
|
+
export const MAX_RETRIES_EXCEEDED_ERROR = 'The request exceeded the maximum number of retries, which is';
|
|
33
33
|
|
|
34
34
|
export const INVALID_FETCH_METHOD_ERROR = "Getta expected to receive 'get', 'post', 'put' or 'delete', but received";
|
|
35
35
|
|
|
36
|
-
export const RESOURCE_NOT_FOUND_ERROR =
|
|
36
|
+
export const RESOURCE_NOT_FOUND_ERROR = 'The requested resource could not been found.';
|
|
37
37
|
|
|
38
|
-
export const FETCH_TIMEOUT_ERROR =
|
|
38
|
+
export const FETCH_TIMEOUT_ERROR = 'The request timed out. Getta did not get a response within';
|
|
39
39
|
|
|
40
|
-
export const GET_METHOD =
|
|
41
|
-
export const POST_METHOD =
|
|
42
|
-
export const PUT_METHOD =
|
|
43
|
-
export const DELETE_METHOD =
|
|
40
|
+
export const GET_METHOD = 'get' as const;
|
|
41
|
+
export const POST_METHOD = 'post' as const;
|
|
42
|
+
export const PUT_METHOD = 'put' as const;
|
|
43
|
+
export const DELETE_METHOD = 'delete' as const;
|
|
44
44
|
|
|
45
45
|
export const FETCH_METHODS = [GET_METHOD, POST_METHOD, PUT_METHOD, DELETE_METHOD];
|
|
46
46
|
|
|
47
|
-
export const INFORMATION_REPSONSE =
|
|
48
|
-
export const SUCCESSFUL_REPSONSE =
|
|
49
|
-
export const REDIRECTION_REPSONSE =
|
|
50
|
-
export const CLIENT_ERROR_REPSONSE =
|
|
51
|
-
export const SERVER_ERROR_REPSONSE =
|
|
47
|
+
export const INFORMATION_REPSONSE = 'information' as const;
|
|
48
|
+
export const SUCCESSFUL_REPSONSE = 'successful' as const;
|
|
49
|
+
export const REDIRECTION_REPSONSE = 'redirection' as const;
|
|
50
|
+
export const CLIENT_ERROR_REPSONSE = 'clientError' as const;
|
|
51
|
+
export const SERVER_ERROR_REPSONSE = 'serverError' as const;
|
|
52
52
|
|
|
53
53
|
export const NOT_MODIFIED_STATUS_CODE = 304 as const;
|
|
54
54
|
export const NOT_FOUND_STATUS_CODE = 404 as const;
|
|
55
55
|
|
|
56
|
-
export const COOKIE_HEADER =
|
|
57
|
-
export const ETAG_HEADER =
|
|
58
|
-
export const LOCATION_HEADER =
|
|
59
|
-
export const IF_NONE_MATCH_HEADER =
|
|
60
|
-
export const CACHE_CONTROL_HEADER =
|
|
56
|
+
export const COOKIE_HEADER = 'Cookie' as const;
|
|
57
|
+
export const ETAG_HEADER = 'ETag' as const;
|
|
58
|
+
export const LOCATION_HEADER = 'Location' as const;
|
|
59
|
+
export const IF_NONE_MATCH_HEADER = 'If-None-Match' as const;
|
|
60
|
+
export const CACHE_CONTROL_HEADER = 'Cache-Control' as const;
|
|
61
61
|
|
|
62
|
-
export const REQUEST_SENT =
|
|
63
|
-
export const RESPONSE_RECEIVED =
|
|
62
|
+
export const REQUEST_SENT = 'request_sent' as const;
|
|
63
|
+
export const RESPONSE_RECEIVED = 'response_received' as const;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import queryString from 'query-string';
|
|
2
|
+
import { type BuildEndpointOptions } from './types.ts';
|
|
3
|
+
|
|
4
|
+
export const buildEndpoint = (
|
|
5
|
+
basePath: string,
|
|
6
|
+
path: string,
|
|
7
|
+
{
|
|
8
|
+
optionalPathTemplateRegExp,
|
|
9
|
+
pathTemplateCallback,
|
|
10
|
+
pathTemplateData,
|
|
11
|
+
pathTemplateRegExp,
|
|
12
|
+
queryParams,
|
|
13
|
+
}: BuildEndpointOptions
|
|
14
|
+
) => {
|
|
15
|
+
const pathJoiner = basePath.endsWith('/') || path.startsWith('/') ? '' : '/';
|
|
16
|
+
let endpoint = `${basePath}${pathJoiner}${path}`;
|
|
17
|
+
|
|
18
|
+
if (pathTemplateData) {
|
|
19
|
+
endpoint = pathTemplateCallback(endpoint, pathTemplateData, pathTemplateRegExp);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
endpoint = endpoint.replace(optionalPathTemplateRegExp, '');
|
|
23
|
+
|
|
24
|
+
if (endpoint.endsWith('/')) {
|
|
25
|
+
endpoint = endpoint.slice(0, Math.max(0, endpoint.length - 1));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (queryParams && Object.keys(queryParams).length > 0) {
|
|
29
|
+
const queryJoin = queryString.extract(endpoint) ? '&' : '?';
|
|
30
|
+
endpoint = `${endpoint}${queryJoin}${queryString.stringify(queryParams)}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return endpoint;
|
|
34
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { PathTemplateCallback, RequestOptions } from
|
|
1
|
+
import { type PathTemplateCallback, type RequestOptions } from '../../types.ts';
|
|
2
2
|
|
|
3
|
-
export interface BuildEndpointOptions extends Omit<RequestOptions,
|
|
3
|
+
export interface BuildEndpointOptions extends Omit<RequestOptions, 'headers'> {
|
|
4
4
|
optionalPathTemplateRegExp: RegExp;
|
|
5
5
|
pathTemplateCallback: PathTemplateCallback;
|
|
6
6
|
pathTemplateRegExp: RegExp;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { defaultPath, defaultPathTemplateData } from '../../__testUtils__/helpers/index.ts';
|
|
2
|
+
import { DEFAULT_PATH_TEMPLATE_REGEX } from '../../constants.ts';
|
|
3
|
+
import { defaultPathTemplateCallback } from './index.ts';
|
|
4
|
+
|
|
5
|
+
describe('defaultPathTemplateCallback', () => {
|
|
6
|
+
it('SHOULD populate the path template correctly', () => {
|
|
7
|
+
expect(defaultPathTemplateCallback(defaultPath, defaultPathTemplateData, DEFAULT_PATH_TEMPLATE_REGEX)).toBe(
|
|
8
|
+
'/direct/rest/content/catalog/product/136-7317?format=standard'
|
|
9
|
+
);
|
|
10
|
+
});
|
|
11
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export const defaultPathTemplateCallback = (
|
|
2
|
+
pathTemplate: string,
|
|
3
|
+
data: Record<string, string>,
|
|
4
|
+
pathTemplateRegExp: RegExp
|
|
5
|
+
) => {
|
|
6
|
+
const dataKeys = Object.keys(data);
|
|
7
|
+
|
|
8
|
+
return pathTemplate.replace(pathTemplateRegExp, match => {
|
|
9
|
+
for (const key of dataKeys) {
|
|
10
|
+
if (match.includes(key) && data[key]) {
|
|
11
|
+
return data[key]!;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return '';
|
|
16
|
+
});
|
|
17
|
+
};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export
|
|
1
|
+
export const delay = (ms: number) => {
|
|
2
2
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
3
|
-
}
|
|
3
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import * as consts from '../../constants.ts';
|
|
2
|
+
|
|
3
|
+
export const getResponseGroup = (status: number) => {
|
|
4
|
+
switch (true) {
|
|
5
|
+
case status < 200: {
|
|
6
|
+
return consts.INFORMATION_REPSONSE;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
case status < 300: {
|
|
10
|
+
return consts.SUCCESSFUL_REPSONSE;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
case status < 400: {
|
|
14
|
+
return consts.REDIRECTION_REPSONSE;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
case status < 500: {
|
|
18
|
+
return consts.CLIENT_ERROR_REPSONSE;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
default: {
|
|
22
|
+
return consts.SERVER_ERROR_REPSONSE;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export {
|
|
3
|
-
export { Getta, createRestClient } from
|
|
4
|
-
export * from
|
|
1
|
+
export * from './constants.ts';
|
|
2
|
+
export { defaultPathTemplateCallback } from './helpers/defaultPathTemplateCallback/index.ts';
|
|
3
|
+
export { Getta, createRestClient } from './main.ts';
|
|
4
|
+
export * from './types.ts';
|