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.
Files changed (104) hide show
  1. package/README.md +161 -7
  2. package/dist/main/index.mjs +621 -0
  3. package/dist/main/index.mjs.map +1 -0
  4. package/dist/tsconfig.build.tsbuildinfo +1 -0
  5. package/{lib → dist}/types/constants.d.ts +2 -2
  6. package/dist/types/constants.d.ts.map +1 -0
  7. package/dist/types/helpers/buildEndpoint/index.d.ts +3 -0
  8. package/dist/types/helpers/buildEndpoint/index.d.ts.map +1 -0
  9. package/{lib/types/helpers/build-endpoint → dist/types/helpers/buildEndpoint}/types.d.ts +2 -2
  10. package/dist/types/helpers/buildEndpoint/types.d.ts.map +1 -0
  11. package/dist/types/helpers/defaultPathTemplateCallback/index.d.ts +2 -0
  12. package/dist/types/helpers/defaultPathTemplateCallback/index.d.ts.map +1 -0
  13. package/dist/types/helpers/delay/index.d.ts +2 -0
  14. package/{lib → dist}/types/helpers/delay/index.d.ts.map +1 -1
  15. package/dist/types/helpers/getResponseGroup/index.d.ts +2 -0
  16. package/dist/types/helpers/getResponseGroup/index.d.ts.map +1 -0
  17. package/dist/types/helpers/isCacheabilityValid/index.d.ts +3 -0
  18. package/dist/types/helpers/isCacheabilityValid/index.d.ts.map +1 -0
  19. package/dist/types/index.d.ts +5 -0
  20. package/dist/types/index.d.ts.map +1 -0
  21. package/{lib → dist}/types/main.d.ts +12 -12
  22. package/dist/types/main.d.ts.map +1 -0
  23. package/dist/types/types.d.ts +161 -0
  24. package/dist/types/types.d.ts.map +1 -0
  25. package/package.json +113 -145
  26. package/src/{__tests__ → __testUtils__}/data/136-7317.json +1 -1
  27. package/src/{__tests__ → __testUtils__}/data/180-1387.json +1 -1
  28. package/src/{__tests__ → __testUtils__}/data/183-3905.json +1 -1
  29. package/src/{__tests__ → __testUtils__}/data/202-3315.json +1 -1
  30. package/src/__testUtils__/data/index.ts +23 -0
  31. package/src/__testUtils__/helpers/index.ts +49 -0
  32. package/src/constants.ts +28 -28
  33. package/src/helpers/buildEndpoint/index.ts +34 -0
  34. package/src/helpers/{build-endpoint → buildEndpoint}/types.ts +2 -2
  35. package/src/helpers/defaultPathTemplateCallback/index.test.ts +11 -0
  36. package/src/helpers/defaultPathTemplateCallback/index.ts +17 -0
  37. package/src/helpers/delay/index.ts +2 -2
  38. package/src/helpers/getResponseGroup/index.ts +25 -0
  39. package/src/helpers/isCacheabilityValid/index.ts +6 -0
  40. package/src/index.ts +4 -4
  41. package/src/main.test.ts +283 -411
  42. package/src/main.ts +238 -241
  43. package/src/types.ts +114 -16
  44. package/tsconfig.build.json +14 -0
  45. package/tsconfig.json +9 -0
  46. package/CHANGELOG.md +0 -250
  47. package/jest.setup.js +0 -10
  48. package/lib/browser/index.js +0 -2
  49. package/lib/browser/index.js.map +0 -1
  50. package/lib/browser/production.analysis.txt +0 -91
  51. package/lib/main/constants.js +0 -97
  52. package/lib/main/helpers/build-endpoint/index.js +0 -38
  53. package/lib/main/helpers/build-endpoint/types.js +0 -1
  54. package/lib/main/helpers/default-path-template-callback/index.js +0 -16
  55. package/lib/main/helpers/delay/index.js +0 -12
  56. package/lib/main/helpers/get-response-group/index.js +0 -27
  57. package/lib/main/helpers/is-cacheability-valid/index.js +0 -13
  58. package/lib/main/index.js +0 -62
  59. package/lib/main/main.js +0 -580
  60. package/lib/main/types.js +0 -1
  61. package/lib/module/constants.mjs +0 -49
  62. package/lib/module/helpers/build-endpoint/index.mjs +0 -28
  63. package/lib/module/helpers/build-endpoint/types.mjs +0 -0
  64. package/lib/module/helpers/default-path-template-callback/index.mjs +0 -9
  65. package/lib/module/helpers/delay/index.mjs +0 -4
  66. package/lib/module/helpers/get-response-group/index.mjs +0 -19
  67. package/lib/module/helpers/is-cacheability-valid/index.mjs +0 -6
  68. package/lib/module/index.mjs +0 -4
  69. package/lib/module/main.mjs +0 -576
  70. package/lib/module/types.mjs +0 -0
  71. package/lib/types/__tests__/data/index.d.ts +0 -17
  72. package/lib/types/__tests__/data/index.d.ts.map +0 -1
  73. package/lib/types/__tests__/helpers/index.d.ts +0 -29
  74. package/lib/types/__tests__/helpers/index.d.ts.map +0 -1
  75. package/lib/types/__tests__/types.d.ts +0 -13
  76. package/lib/types/__tests__/types.d.ts.map +0 -1
  77. package/lib/types/constants.d.ts.map +0 -1
  78. package/lib/types/helpers/build-endpoint/index.d.ts +0 -3
  79. package/lib/types/helpers/build-endpoint/index.d.ts.map +0 -1
  80. package/lib/types/helpers/build-endpoint/types.d.ts.map +0 -1
  81. package/lib/types/helpers/default-path-template-callback/index.d.ts +0 -3
  82. package/lib/types/helpers/default-path-template-callback/index.d.ts.map +0 -1
  83. package/lib/types/helpers/default-path-template-callback/index.test.d.ts +0 -2
  84. package/lib/types/helpers/default-path-template-callback/index.test.d.ts.map +0 -1
  85. package/lib/types/helpers/delay/index.d.ts +0 -2
  86. package/lib/types/helpers/get-response-group/index.d.ts +0 -2
  87. package/lib/types/helpers/get-response-group/index.d.ts.map +0 -1
  88. package/lib/types/helpers/is-cacheability-valid/index.d.ts +0 -3
  89. package/lib/types/helpers/is-cacheability-valid/index.d.ts.map +0 -1
  90. package/lib/types/index.d.ts +0 -5
  91. package/lib/types/index.d.ts.map +0 -1
  92. package/lib/types/main.d.ts.map +0 -1
  93. package/lib/types/main.test.d.ts +0 -2
  94. package/lib/types/main.test.d.ts.map +0 -1
  95. package/lib/types/types.d.ts +0 -69
  96. package/lib/types/types.d.ts.map +0 -1
  97. package/src/__tests__/data/index.ts +0 -19
  98. package/src/__tests__/helpers/index.ts +0 -61
  99. package/src/__tests__/types.ts +0 -14
  100. package/src/helpers/build-endpoint/index.ts +0 -34
  101. package/src/helpers/default-path-template-callback/index.test.ts +0 -11
  102. package/src/helpers/default-path-template-callback/index.ts +0 -16
  103. package/src/helpers/get-response-group/index.ts +0 -22
  104. package/src/helpers/is-cacheability-valid/index.ts +0 -6
package/README.md CHANGED
@@ -8,17 +8,171 @@ An isomorphic rest client based on the Fetch API.
8
8
 
9
9
  ## Installation
10
10
 
11
- You can install Getta with the command below.
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';
24
+
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
+ });
34
+ ```
35
+
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`.
12
85
 
13
- ```shell
14
- yarn add getta
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. The first argument is the path to make the request to. This can also be a path template with variable placeholders that will be replaced with data passed in on the `pathTemplateData` request option.
113
+
114
+ ```typescript
115
+ // GET
116
+ const getResponse = await restClient.get('/path/to/resource');
117
+
118
+ // POST
119
+ const postResponse = await restClient.post('/graphql/api', {
120
+ body: JSON.stringify({ /* payload */ }),
121
+ });
122
+
123
+ // PUT
124
+ const putResponse = await restClient.put('/path/to/resource', {
125
+ body: JSON.stringify({ /* payload */ }),
126
+ });
127
+
128
+ // DELETE
129
+ const deleteResponse = await restClient.delete('/path/to/resource');
15
130
  ```
16
131
 
17
- The library depends on isomorphic-fetch, so this will also need to be installed in your project,
18
- along with core-js, @babel/runtime and lodash.
132
+ ### Request options
133
+
134
+ **body?: `ReadableStream | Blob | BufferSource | FormData | URLSearchParams | string`**
135
+
136
+ For `POST` and `PUT` methods, the body to send with the request.
137
+
138
+ **headers?: `Record<string, string>`**
139
+
140
+ Any headers to attach to the request.
141
+
142
+ **method?: `'get' | 'post' | 'put' | 'delete'`**
143
+
144
+ The fetch method.
145
+
146
+ **pathTemplateData?: `Record<string, string>`**
147
+
148
+ Data to be injected into the path template.
149
+
150
+ **queryParams?: `PlainObject`**
151
+
152
+ Any query params to attach to the request.
153
+
154
+ ### Shortcuts
155
+
156
+ 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.
157
+
158
+ ```typescript
159
+ import { createRestClient } from 'getta';
160
+ import { performance } from 'node:perf_hooks';
161
+
162
+ const restClient = createRestClient<'getProduct'>(
163
+ { basePath: 'https://www.example.com', performance },
164
+ {
165
+ getProduct: [
166
+ '/direct/rest/content/catalog/{type}/{id,+}?format={brief|standard}'
167
+ {
168
+ method: consts.GET_METHOD,
169
+ pathTemplateData: { 'brief|standard': 'standard', type: 'product' },
170
+ },
171
+ ],
172
+ }
173
+ );
19
174
 
20
- ```shell
21
- yarn add isomorphic-fetch core-js @babel/runtime lodash
175
+ const getResponse = await restClient.getProduct({ pathTemplateData: { 'id,+': '136-7317' } });
22
176
  ```
23
177
 
24
178
  ## Changelog