getta 0.4.7 → 1.0.0
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 +165 -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/README.md
CHANGED
|
@@ -8,17 +8,175 @@ An isomorphic rest client based on the Fetch API.
|
|
|
8
8
|
|
|
9
9
|
## Installation
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
```bash
|
|
12
|
+
npm add getta
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
You create an instance of the rest client with the `createRestClient` function. The function takes numerous options, including the base path of the url for all requests, a cache for caching responses against the request, and a performance module to use for recording request durations. The full list of the options are detailed below.
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { Core } from '@cachemap/core';
|
|
21
|
+
import { init as map } from '@cachemap/map';
|
|
22
|
+
import { createRestClient } from 'getta';
|
|
23
|
+
import { performance } from 'node:perf_hooks';
|
|
12
24
|
|
|
13
|
-
|
|
14
|
-
|
|
25
|
+
const restClient = createRestClient({
|
|
26
|
+
basePath: 'https://www.example.com',
|
|
27
|
+
cache: new Core({
|
|
28
|
+
name: 'restClient',
|
|
29
|
+
store: map(),
|
|
30
|
+
type: 'example',
|
|
31
|
+
}),
|
|
32
|
+
performance
|
|
33
|
+
});
|
|
15
34
|
```
|
|
16
35
|
|
|
17
|
-
|
|
18
|
-
|
|
36
|
+
### Rest client options
|
|
37
|
+
|
|
38
|
+
**basePath: `string`**
|
|
39
|
+
|
|
40
|
+
The base path of the url for all requests made from an instance of the rest client, i.e. `'https://www.example.com/api'`.
|
|
41
|
+
|
|
42
|
+
**bodyParser?: `(...args: any[]) => any`**
|
|
43
|
+
|
|
44
|
+
An optional callback to pass each response body through after it has been parsed by the stream reader.
|
|
45
|
+
|
|
46
|
+
**cache?: `Core`**
|
|
47
|
+
|
|
48
|
+
An instance of the @cachemap/core cache for caching responses against requests. Caching is only enabled on `GET` requests. For more info on @cachemap/core see <https://github.com/badbatch/cachemap>.
|
|
49
|
+
|
|
50
|
+
**enableConditionalRequests?: `boolean`**
|
|
51
|
+
|
|
52
|
+
Enables conditional requests with If-None-Match header. Default is `true`.
|
|
53
|
+
|
|
54
|
+
**fetchTimeout?: `number`**
|
|
55
|
+
|
|
56
|
+
How long to wait for a request to respond before timing out and returning an error. Default is `5000`.
|
|
57
|
+
|
|
58
|
+
**headers?: `Record<string, string>`**
|
|
59
|
+
|
|
60
|
+
Any headers to attach to every request.
|
|
61
|
+
|
|
62
|
+
**log?: `(message: string, data: PlainObject, logLevel?: LogLevel) => void`**
|
|
63
|
+
|
|
64
|
+
Log function to pass rest client logs to a logger.
|
|
65
|
+
|
|
66
|
+
**maxRedirects?: `number`**
|
|
67
|
+
|
|
68
|
+
The maximum number of times a request can redirect before the rest client returns an error. Default is `5`.
|
|
69
|
+
|
|
70
|
+
**maxRetries?: `number`**
|
|
71
|
+
|
|
72
|
+
The maximum number of times a request can retry before the rest client returns an error. Default is `3`.
|
|
73
|
+
|
|
74
|
+
**optionalPathTemplateRegExp?: `RegExp`**
|
|
75
|
+
|
|
76
|
+
An optional path template regex that can be used to clean up the template url. Default is `/({\w+\?})/g`.
|
|
77
|
+
|
|
78
|
+
**pathTemplateCallback?: `(pathTemplate: string, data: Record<string, string>, pathTemplateRegExp: RegExp) => string`**
|
|
79
|
+
|
|
80
|
+
A callback that takes the path template, the data to be injected into the template, and the path template regex, and returns the templated url. There is a default template callback that will cater to most needs.
|
|
81
|
+
|
|
82
|
+
**pathTemplateRegExp?: `RegExp`**
|
|
83
|
+
|
|
84
|
+
A regex to tell the rest client where to inject data into the template. There is a default one that will cater to some needs. Default is `/({type})|({id})|({id,\+})|({brief\|standard})/g`.
|
|
85
|
+
|
|
86
|
+
**performance: `Performance`**
|
|
87
|
+
|
|
88
|
+
The performance module to use for recording request durations.
|
|
89
|
+
|
|
90
|
+
**queryParams?: `PlainObject`**
|
|
91
|
+
|
|
92
|
+
Any query params to attach to every request.
|
|
93
|
+
|
|
94
|
+
**rateLimit?: `boolean`**
|
|
95
|
+
|
|
96
|
+
Whether to enable the rate limit feature.
|
|
97
|
+
|
|
98
|
+
**rateLimitPerSecond?: `number`**
|
|
99
|
+
|
|
100
|
+
How many requests per second to throttle the rest client. Default is `50`.
|
|
101
|
+
|
|
102
|
+
**requestRetryWait?: `number`**
|
|
103
|
+
|
|
104
|
+
How many milliseconds to wait before retrying a request. Default is `100`.
|
|
105
|
+
|
|
106
|
+
**streamReader?: `'arrayBuffer' | 'blob' | 'formData' | 'json' | 'text'`**
|
|
107
|
+
|
|
108
|
+
The stream reader to use when parsing the response body. Default is `'json'`.
|
|
109
|
+
|
|
110
|
+
### Making reqeusts
|
|
111
|
+
|
|
112
|
+
The rest client supports `GET`, `POST`, `PUT` and `DELETE` methods and has functions for each method.
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
const pathTemplate = '/direct/rest/content/catalog/{type}/{id,+}?format={brief|standard}';
|
|
116
|
+
const pathTemplateData = { 'brief|standard': 'standard', 'id,+': '136-7317', type: 'product' };
|
|
117
|
+
|
|
118
|
+
// GET
|
|
119
|
+
const getResponse = await restClient.get(pathTemplate, { pathTemplateData });
|
|
120
|
+
|
|
121
|
+
// POST
|
|
122
|
+
const postResponse = await restClient.post('/graphql/api', {
|
|
123
|
+
body: JSON.stringify({ /* payload */ }),
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// PUT
|
|
127
|
+
const putResponse = await restClient.put(pathTemplate, {
|
|
128
|
+
body: JSON.stringify({ /* payload */ }),
|
|
129
|
+
pathTemplateData,
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// DELETE
|
|
133
|
+
const deleteResponse = await restClient.delete(pathTemplate, { pathTemplateData });
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Request options
|
|
137
|
+
|
|
138
|
+
**body?: `ReadableStream | Blob | BufferSource | FormData | URLSearchParams | string`**
|
|
139
|
+
|
|
140
|
+
For `POST` and `PUT` methods, the body to send with the request.
|
|
141
|
+
|
|
142
|
+
**headers?: `Record<string, string>`**
|
|
143
|
+
|
|
144
|
+
Any headers to attach to the request.
|
|
145
|
+
|
|
146
|
+
**method?: `'get' | 'post' | 'put' | 'delete'`**
|
|
147
|
+
|
|
148
|
+
The fetch method.
|
|
149
|
+
|
|
150
|
+
**pathTemplateData?: `Record<string, string>`**
|
|
151
|
+
|
|
152
|
+
Data to be injected into the path template.
|
|
153
|
+
|
|
154
|
+
**queryParams?: `PlainObject`**
|
|
155
|
+
|
|
156
|
+
Any query params to attach to the request.
|
|
157
|
+
|
|
158
|
+
### Shortcuts
|
|
159
|
+
|
|
160
|
+
It is possible to alias a request and bake things like the path template and request options into the alias, reducing the number of options that need to be passed into a request.
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
import { createRestClient } from 'getta';
|
|
164
|
+
import { performance } from 'node:perf_hooks';
|
|
165
|
+
|
|
166
|
+
const restClient = createRestClient<'getProduct'>(
|
|
167
|
+
{ basePath: 'https://www.example.com', performance },
|
|
168
|
+
{
|
|
169
|
+
getProduct: [
|
|
170
|
+
'/direct/rest/content/catalog/{type}/{id,+}?format={brief|standard}'
|
|
171
|
+
{
|
|
172
|
+
method: consts.GET_METHOD,
|
|
173
|
+
pathTemplateData: { 'brief|standard': 'standard', type: 'product' },
|
|
174
|
+
},
|
|
175
|
+
],
|
|
176
|
+
}
|
|
177
|
+
);
|
|
19
178
|
|
|
20
|
-
|
|
21
|
-
yarn add isomorphic-fetch core-js @babel/runtime lodash
|
|
179
|
+
const getResponse = await restClient.getProduct({ pathTemplateData: { 'id,+': '136-7317' } });
|
|
22
180
|
```
|
|
23
181
|
|
|
24
182
|
## Changelog
|