@serenity-js/rest 3.41.1 → 3.42.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.
Files changed (213) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/esm/index.d.ts +3 -0
  3. package/esm/index.d.ts.map +1 -0
  4. package/esm/index.js +3 -0
  5. package/esm/index.js.map +1 -0
  6. package/esm/io/AxiosRequestConfigDefaults.d.ts +15 -0
  7. package/esm/io/AxiosRequestConfigDefaults.d.ts.map +1 -0
  8. package/esm/io/AxiosRequestConfigDefaults.js +2 -0
  9. package/esm/io/AxiosRequestConfigDefaults.js.map +1 -0
  10. package/esm/io/EnvironmentVariables.d.ts +9 -0
  11. package/esm/io/EnvironmentVariables.d.ts.map +1 -0
  12. package/esm/io/EnvironmentVariables.js +35 -0
  13. package/esm/io/EnvironmentVariables.js.map +1 -0
  14. package/esm/io/ProxyAgent.d.ts +52 -0
  15. package/esm/io/ProxyAgent.d.ts.map +1 -0
  16. package/esm/io/ProxyAgent.js +83 -0
  17. package/esm/io/ProxyAgent.js.map +1 -0
  18. package/esm/io/ProxyBypass.d.ts +6 -0
  19. package/esm/io/ProxyBypass.d.ts.map +1 -0
  20. package/esm/io/ProxyBypass.js +66 -0
  21. package/esm/io/ProxyBypass.js.map +1 -0
  22. package/esm/io/createAxios.d.ts +9 -0
  23. package/esm/io/createAxios.d.ts.map +1 -0
  24. package/esm/io/createAxios.js +40 -0
  25. package/esm/io/createAxios.js.map +1 -0
  26. package/esm/io/createUrl.d.ts +9 -0
  27. package/esm/io/createUrl.d.ts.map +1 -0
  28. package/esm/io/createUrl.js +26 -0
  29. package/esm/io/createUrl.js.map +1 -0
  30. package/esm/io/index.d.ts +3 -0
  31. package/esm/io/index.d.ts.map +1 -0
  32. package/esm/io/index.js +3 -0
  33. package/esm/io/index.js.map +1 -0
  34. package/esm/io/proxy.d.ts +14 -0
  35. package/esm/io/proxy.d.ts.map +1 -0
  36. package/esm/io/proxy.js +66 -0
  37. package/esm/io/proxy.js.map +1 -0
  38. package/esm/screenplay/abilities/AxiosRequestConfigDefaults.d.ts +14 -0
  39. package/esm/screenplay/abilities/AxiosRequestConfigDefaults.d.ts.map +1 -0
  40. package/esm/screenplay/abilities/AxiosRequestConfigDefaults.js +2 -0
  41. package/esm/screenplay/abilities/AxiosRequestConfigDefaults.js.map +1 -0
  42. package/esm/screenplay/abilities/CallAnApi.d.ts +443 -0
  43. package/esm/screenplay/abilities/CallAnApi.d.ts.map +1 -0
  44. package/esm/screenplay/abilities/CallAnApi.js +555 -0
  45. package/esm/screenplay/abilities/CallAnApi.js.map +1 -0
  46. package/esm/screenplay/abilities/index.d.ts +2 -0
  47. package/esm/screenplay/abilities/index.d.ts.map +1 -0
  48. package/esm/screenplay/abilities/index.js +2 -0
  49. package/esm/screenplay/abilities/index.js.map +1 -0
  50. package/esm/screenplay/index.d.ts +5 -0
  51. package/esm/screenplay/index.d.ts.map +1 -0
  52. package/esm/screenplay/index.js +5 -0
  53. package/esm/screenplay/index.js.map +1 -0
  54. package/esm/screenplay/interactions/ChangeApiConfig.d.ts +135 -0
  55. package/esm/screenplay/interactions/ChangeApiConfig.d.ts.map +1 -0
  56. package/esm/screenplay/interactions/ChangeApiConfig.js +212 -0
  57. package/esm/screenplay/interactions/ChangeApiConfig.js.map +1 -0
  58. package/esm/screenplay/interactions/Send.d.ts +48 -0
  59. package/esm/screenplay/interactions/Send.d.ts.map +1 -0
  60. package/esm/screenplay/interactions/Send.js +83 -0
  61. package/esm/screenplay/interactions/Send.js.map +1 -0
  62. package/esm/screenplay/interactions/index.d.ts +3 -0
  63. package/esm/screenplay/interactions/index.d.ts.map +1 -0
  64. package/esm/screenplay/interactions/index.js +3 -0
  65. package/esm/screenplay/interactions/index.js.map +1 -0
  66. package/esm/screenplay/models/DeleteRequest.d.ts +66 -0
  67. package/esm/screenplay/models/DeleteRequest.d.ts.map +1 -0
  68. package/esm/screenplay/models/DeleteRequest.js +69 -0
  69. package/esm/screenplay/models/DeleteRequest.js.map +1 -0
  70. package/esm/screenplay/models/GetRequest.d.ts +73 -0
  71. package/esm/screenplay/models/GetRequest.d.ts.map +1 -0
  72. package/esm/screenplay/models/GetRequest.js +76 -0
  73. package/esm/screenplay/models/GetRequest.js.map +1 -0
  74. package/esm/screenplay/models/HTTPRequest.d.ts +40 -0
  75. package/esm/screenplay/models/HTTPRequest.d.ts.map +1 -0
  76. package/esm/screenplay/models/HTTPRequest.js +62 -0
  77. package/esm/screenplay/models/HTTPRequest.js.map +1 -0
  78. package/esm/screenplay/models/HeadRequest.d.ts +58 -0
  79. package/esm/screenplay/models/HeadRequest.d.ts.map +1 -0
  80. package/esm/screenplay/models/HeadRequest.js +61 -0
  81. package/esm/screenplay/models/HeadRequest.js.map +1 -0
  82. package/esm/screenplay/models/OptionsRequest.d.ts +61 -0
  83. package/esm/screenplay/models/OptionsRequest.d.ts.map +1 -0
  84. package/esm/screenplay/models/OptionsRequest.js +64 -0
  85. package/esm/screenplay/models/OptionsRequest.js.map +1 -0
  86. package/esm/screenplay/models/PatchRequest.d.ts +65 -0
  87. package/esm/screenplay/models/PatchRequest.d.ts.map +1 -0
  88. package/esm/screenplay/models/PatchRequest.js +70 -0
  89. package/esm/screenplay/models/PatchRequest.js.map +1 -0
  90. package/esm/screenplay/models/PostRequest.d.ts +105 -0
  91. package/esm/screenplay/models/PostRequest.d.ts.map +1 -0
  92. package/esm/screenplay/models/PostRequest.js +110 -0
  93. package/esm/screenplay/models/PostRequest.js.map +1 -0
  94. package/esm/screenplay/models/PutRequest.d.ts +75 -0
  95. package/esm/screenplay/models/PutRequest.d.ts.map +1 -0
  96. package/esm/screenplay/models/PutRequest.js +80 -0
  97. package/esm/screenplay/models/PutRequest.js.map +1 -0
  98. package/esm/screenplay/models/index.d.ts +9 -0
  99. package/esm/screenplay/models/index.d.ts.map +1 -0
  100. package/esm/screenplay/models/index.js +9 -0
  101. package/esm/screenplay/models/index.js.map +1 -0
  102. package/esm/screenplay/questions/LastResponse.d.ts +189 -0
  103. package/esm/screenplay/questions/LastResponse.d.ts.map +1 -0
  104. package/esm/screenplay/questions/LastResponse.js +205 -0
  105. package/esm/screenplay/questions/LastResponse.js.map +1 -0
  106. package/esm/screenplay/questions/index.d.ts +2 -0
  107. package/esm/screenplay/questions/index.d.ts.map +1 -0
  108. package/esm/screenplay/questions/index.js +2 -0
  109. package/esm/screenplay/questions/index.js.map +1 -0
  110. package/lib/index.d.ts +2 -2
  111. package/lib/index.d.ts.map +1 -1
  112. package/lib/index.js +2 -2
  113. package/lib/index.js.map +1 -1
  114. package/lib/io/ProxyBypass.js +2 -2
  115. package/lib/io/ProxyBypass.js.map +1 -1
  116. package/lib/io/createAxios.d.ts +1 -1
  117. package/lib/io/createAxios.d.ts.map +1 -1
  118. package/lib/io/createAxios.js +2 -2
  119. package/lib/io/createAxios.js.map +1 -1
  120. package/lib/io/index.d.ts +2 -2
  121. package/lib/io/index.d.ts.map +1 -1
  122. package/lib/io/index.js +2 -2
  123. package/lib/io/index.js.map +1 -1
  124. package/lib/io/proxy.d.ts +2 -2
  125. package/lib/io/proxy.d.ts.map +1 -1
  126. package/lib/io/proxy.js +9 -9
  127. package/lib/io/proxy.js.map +1 -1
  128. package/lib/package.json +1 -0
  129. package/lib/screenplay/abilities/CallAnApi.d.ts +1 -1
  130. package/lib/screenplay/abilities/CallAnApi.d.ts.map +1 -1
  131. package/lib/screenplay/abilities/CallAnApi.js +4 -4
  132. package/lib/screenplay/abilities/CallAnApi.js.map +1 -1
  133. package/lib/screenplay/abilities/index.d.ts +1 -1
  134. package/lib/screenplay/abilities/index.d.ts.map +1 -1
  135. package/lib/screenplay/abilities/index.js +1 -1
  136. package/lib/screenplay/abilities/index.js.map +1 -1
  137. package/lib/screenplay/index.d.ts +4 -4
  138. package/lib/screenplay/index.d.ts.map +1 -1
  139. package/lib/screenplay/index.js +4 -4
  140. package/lib/screenplay/index.js.map +1 -1
  141. package/lib/screenplay/interactions/ChangeApiConfig.js +4 -4
  142. package/lib/screenplay/interactions/ChangeApiConfig.js.map +1 -1
  143. package/lib/screenplay/interactions/Send.js +3 -3
  144. package/lib/screenplay/interactions/Send.js.map +1 -1
  145. package/lib/screenplay/interactions/index.d.ts +2 -2
  146. package/lib/screenplay/interactions/index.d.ts.map +1 -1
  147. package/lib/screenplay/interactions/index.js +2 -2
  148. package/lib/screenplay/interactions/index.js.map +1 -1
  149. package/lib/screenplay/models/DeleteRequest.d.ts +1 -1
  150. package/lib/screenplay/models/DeleteRequest.d.ts.map +1 -1
  151. package/lib/screenplay/models/DeleteRequest.js +2 -2
  152. package/lib/screenplay/models/DeleteRequest.js.map +1 -1
  153. package/lib/screenplay/models/GetRequest.d.ts +1 -1
  154. package/lib/screenplay/models/GetRequest.d.ts.map +1 -1
  155. package/lib/screenplay/models/GetRequest.js +2 -2
  156. package/lib/screenplay/models/GetRequest.js.map +1 -1
  157. package/lib/screenplay/models/HTTPRequest.js +1 -1
  158. package/lib/screenplay/models/HTTPRequest.js.map +1 -1
  159. package/lib/screenplay/models/HeadRequest.d.ts +1 -1
  160. package/lib/screenplay/models/HeadRequest.d.ts.map +1 -1
  161. package/lib/screenplay/models/HeadRequest.js +2 -2
  162. package/lib/screenplay/models/HeadRequest.js.map +1 -1
  163. package/lib/screenplay/models/OptionsRequest.d.ts +1 -1
  164. package/lib/screenplay/models/OptionsRequest.d.ts.map +1 -1
  165. package/lib/screenplay/models/OptionsRequest.js +2 -2
  166. package/lib/screenplay/models/OptionsRequest.js.map +1 -1
  167. package/lib/screenplay/models/PatchRequest.d.ts +1 -1
  168. package/lib/screenplay/models/PatchRequest.d.ts.map +1 -1
  169. package/lib/screenplay/models/PatchRequest.js +2 -2
  170. package/lib/screenplay/models/PatchRequest.js.map +1 -1
  171. package/lib/screenplay/models/PostRequest.d.ts +1 -1
  172. package/lib/screenplay/models/PostRequest.d.ts.map +1 -1
  173. package/lib/screenplay/models/PostRequest.js +2 -2
  174. package/lib/screenplay/models/PostRequest.js.map +1 -1
  175. package/lib/screenplay/models/PutRequest.d.ts +1 -1
  176. package/lib/screenplay/models/PutRequest.d.ts.map +1 -1
  177. package/lib/screenplay/models/PutRequest.js +2 -2
  178. package/lib/screenplay/models/PutRequest.js.map +1 -1
  179. package/lib/screenplay/models/index.d.ts +8 -8
  180. package/lib/screenplay/models/index.d.ts.map +1 -1
  181. package/lib/screenplay/models/index.js +8 -8
  182. package/lib/screenplay/models/index.js.map +1 -1
  183. package/lib/screenplay/questions/LastResponse.js +5 -5
  184. package/lib/screenplay/questions/LastResponse.js.map +1 -1
  185. package/lib/screenplay/questions/index.d.ts +1 -1
  186. package/lib/screenplay/questions/index.d.ts.map +1 -1
  187. package/lib/screenplay/questions/index.js +1 -1
  188. package/lib/screenplay/questions/index.js.map +1 -1
  189. package/package.json +29 -11
  190. package/src/index.ts +2 -2
  191. package/src/io/ProxyBypass.ts +1 -1
  192. package/src/io/createAxios.ts +2 -2
  193. package/src/io/index.ts +2 -2
  194. package/src/io/proxy.ts +5 -5
  195. package/src/screenplay/abilities/CallAnApi.ts +3 -3
  196. package/src/screenplay/abilities/index.ts +1 -1
  197. package/src/screenplay/index.ts +4 -4
  198. package/src/screenplay/interactions/ChangeApiConfig.ts +1 -1
  199. package/src/screenplay/interactions/Send.ts +3 -3
  200. package/src/screenplay/interactions/index.ts +2 -2
  201. package/src/screenplay/models/DeleteRequest.ts +1 -1
  202. package/src/screenplay/models/GetRequest.ts +1 -1
  203. package/src/screenplay/models/HTTPRequest.ts +1 -1
  204. package/src/screenplay/models/HeadRequest.ts +1 -1
  205. package/src/screenplay/models/OptionsRequest.ts +1 -1
  206. package/src/screenplay/models/PatchRequest.ts +1 -1
  207. package/src/screenplay/models/PostRequest.ts +1 -1
  208. package/src/screenplay/models/PutRequest.ts +1 -1
  209. package/src/screenplay/models/index.ts +8 -8
  210. package/src/screenplay/questions/LastResponse.ts +1 -1
  211. package/src/screenplay/questions/index.ts +1 -1
  212. package/tsconfig-cjs.build.json +17 -0
  213. package/tsconfig-esm.build.json +18 -0
@@ -0,0 +1,443 @@
1
+ import { URL } from 'node:url';
2
+ import { Ability, type SerialisedAbility } from '@serenity-js/core';
3
+ import { type AxiosDefaults, type AxiosInstance, type AxiosRequestConfig, type AxiosResponse } from 'axios';
4
+ import type { AxiosRequestConfigDefaults } from '../../io/index.js';
5
+ /**
6
+ * An [ability](https://serenity-js.org/api/core/class/Ability/) that wraps [axios client](https://axios-http.com/docs/api_intro) and enables
7
+ * the [actor](https://serenity-js.org/api/core/class/Actor/) to [send](https://serenity-js.org/api/rest/class/Send/)
8
+ * [HTTP requests](https://serenity-js.org/api/rest/class/HTTPRequest/) to HTTP APIs.
9
+ *
10
+ * ## Configuring the ability to call an API
11
+ *
12
+ * The easiest way to configure the ability to `CallAnApi` is to provide the `baseURL` of your HTTP API,
13
+ * and rely on Serenity/JS to offer other sensible defaults:
14
+ *
15
+ * ```ts
16
+ * import { actorCalled } from '@serenity-js/core'
17
+ * import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'
18
+ * import { Ensure, equals } from '@serenity-js/assertions'
19
+ *
20
+ * await actorCalled('Apisitt')
21
+ * .whoCan(
22
+ * CallAnApi.at('https://api.example.org/')
23
+ * )
24
+ * .attemptsTo(
25
+ * Send.a(GetRequest.to('/v1/users/2')), // GET https://api.example.org/v1/users/2
26
+ * Ensure.that(LastResponse.status(), equals(200)),
27
+ * )
28
+ * ```
29
+ *
30
+ * Please note that the following Serenity/JS test runner adapters already provide the ability to `CallAnApi` as part of their default configuration,
31
+ * so you don't need to configure it yourself:
32
+ * - [Playwright Test](https://serenity-js.org/handbook/test-runners/playwright-test/)
33
+ * - [WebdriverIO](https://serenity-js.org/handbook/test-runners/webdriverio/)
34
+ * - [Protractor](https://serenity-js.org/handbook/test-runners/protractor/)
35
+ *
36
+ * ### Resolving relative URLs
37
+ *
38
+ * Serenity/JS resolves request URLs using Node.js [WHATWG URL API](https://nodejs.org/api/url.html#new-urlinput-base).
39
+ * This means that the request URL is determined using the resource path resolved in the context of base URL, i.e. `new URL(resourcePath, [baseURL])`.
40
+ *
41
+ * Consider the following example:
42
+ *
43
+ * ```ts
44
+ * import { actorCalled } from '@serenity-js/core'
45
+ * import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'
46
+ * import { Ensure, equals } from '@serenity-js/assertions'
47
+ *
48
+ * await actorCalled('Apisitt')
49
+ * .whoCan(
50
+ * CallAnApi.at(baseURL)
51
+ * )
52
+ * .attemptsTo(
53
+ * Send.a(GetRequest.to(resourcePath)),
54
+ * Ensure.that(LastResponse.status(), equals(200)),
55
+ * )
56
+ * ```
57
+ *
58
+ * In the above example:
59
+ * - when `resourcePath` is defined as a full URL, it overrides the base URL
60
+ * - when `resourcePath` starts with a forward slash `/`, it replaces any path defined in the base URL
61
+ * - when `resourcePath` is not a full URL and doesn't start with a forward slash, it gets appended to the base URL
62
+ *
63
+ * | baseURL | resourcePath | result |
64
+ * | ----------------------------- | -------------------------- | ------------------------------------ |
65
+ * | `https://api.example.org/` | `/v1/users/2` | `https://api.example.org/v1/users/2` |
66
+ * | `https://example.org/api/v1/` | `users/2` | `https://example.org/api/v1/users/2` |
67
+ * | `https://example.org/api/v1/` | `/secure/oauth` | `https://example.org/secure/oauth` |
68
+ * | `https://v1.example.org/api/` | `https://v2.example.org/` | `https://v2.example.org/` |
69
+ *
70
+ * ### Using Axios configuration object
71
+ *
72
+ * When you need more control over how your Axios instance is configured, provide
73
+ * an [Axios configuration object](https://axios-http.com/docs/req_config). For example:
74
+ *
75
+ * ```ts
76
+ * import { actorCalled } from '@serenity-js/core'
77
+ * import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'
78
+ * import { Ensure, equals } from '@serenity-js/assertions'
79
+ *
80
+ * await actorCalled('Apisitt')
81
+ * .whoCan(
82
+ * CallAnApi.using({
83
+ * baseURL: 'https://api.example.org/',
84
+ * timeout: 30_000,
85
+ * // ... other configuration options
86
+ * })
87
+ * )
88
+ * .attemptsTo(
89
+ * Send.a(GetRequest.to('/users/2')),
90
+ * Ensure.that(LastResponse.status(), equals(200)),
91
+ * )
92
+ * ```
93
+ *
94
+ * ## Working with proxy servers
95
+ *
96
+ * `CallAnApi` uses an approach described in ["Node.js Axios behind corporate proxies"](https://janmolak.com/node-js-axios-behind-corporate-proxies-8b17a6f31f9d)
97
+ * to automatically detect proxy server configuration based on your environment variables.
98
+ *
99
+ * You can override this default proxy detection mechanism by providing your own proxy configuration object.
100
+ *
101
+ * ### Automatic proxy support
102
+ *
103
+ * When the URL you're sending the request to uses the HTTP protocol, Serenity/JS will automatically detect your proxy configuration based on the following environment variables:
104
+ * - `npm_config_http_proxy`
105
+ * - `http_proxy` and `HTTP_PROXY`
106
+ * - `npm_config_proxy`
107
+ * - `all_proxy`
108
+ *
109
+ * Similarly, when the request target URL uses the HTTPS protocol, the following environment variables are used instead:
110
+ * - `npm_config_https_proxy`
111
+ * - `https_proxy` and `HTTPS_PROXY`
112
+ * - `npm_config_proxy`
113
+ * - `all_proxy`
114
+ *
115
+ * Proxy configuration is ignored for both HTTP and HTTPS target URLs matching the proxy bypass rules defined in the following environment variables:
116
+ * - `npm_config_no_proxy`
117
+ * - `no_proxy` and `NO_PROXY`
118
+ *
119
+ * ### Custom proxy configuration
120
+ *
121
+ * To override the automatic proxy detection based on the environment variables, provide a proxy configuration object:
122
+ *
123
+ * ```ts
124
+ * import { actorCalled } from '@serenity-js/core'
125
+ * import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'
126
+ * import { Ensure, equals } from '@serenity-js/assertions'
127
+ *
128
+ * await actorCalled('Apisitt')
129
+ * .whoCan(
130
+ * CallAnApi.using({
131
+ * baseURL: 'https://api.example.org/',
132
+ * proxy: {
133
+ * protocol: 'http',
134
+ * host: 'proxy.example.org',
135
+ * port: 9000,
136
+ * auth: { // optional
137
+ * username: 'proxy-username',
138
+ * password: 'proxy-password',
139
+ * },
140
+ * bypass: 'status.example.org, internal.example.org' // optional
141
+ * }
142
+ * // ... other configuration options
143
+ * })
144
+ * )
145
+ * .attemptsTo(
146
+ * Send.a(GetRequest.to('/users/2')),
147
+ * Ensure.that(LastResponse.status(), equals(200)),
148
+ * )
149
+ * ```
150
+ *
151
+ * Note that Serenity/JS uses [proxy-agents](https://github.com/TooTallNate/proxy-agents)
152
+ * and the approach described in ["Node.js Axios behind corporate proxies"](https://janmolak.com/node-js-axios-behind-corporate-proxies-8b17a6f31f9d)
153
+ * to work around [limited proxy support capabilities](https://github.com/axios/axios/issues?q=is%3Aissue+is%3Aopen+proxy) in Axios itself.
154
+ *
155
+ * ### Bypassing proxy configuration
156
+ *
157
+ * To bypass the proxy configuration for specific hostnames and IP addresses, you can either:
158
+ * - provide the `bypass` property in the proxy configuration object, or
159
+ * - use the `no_proxy` environment variable.
160
+ *
161
+ * The value of the `bypass` property or the `no_proxy` environment variable should be a comma-separated list of hostnames and IP addresses
162
+ * that should not be routed through the proxy server, for example: `.com, .serenity-js.org, .domain.com`.
163
+ *
164
+ * Note that setting the `bypass` property to `example.org` makes the requests to following URLs bypass the proxy server:
165
+ * - `api.example.org`
166
+ * - `sub.sub.example.org`
167
+ * - `example.org`
168
+ * - `my-example.org`
169
+ *
170
+ * :::info
171
+ * Serenity/JS doesn't currently support `bypass` rules expressed using CIDR notation, like `192.168.17.0/24`.
172
+ * Instead, it uses a simple comma-separated list of hostnames and IP addresses.
173
+ * If you need support for CIDR notation, please [raise an issue](https://github.com/serenity-js/serenity-js/issues).
174
+ * :::
175
+ *
176
+ * ### Using Axios instance with proxy support
177
+ *
178
+ * To have full control over the Axios instance used by the ability to `CallAnApi`, you can create it yourself
179
+ * and inject it into the ability.
180
+ * This approach allows you to still benefit from automated proxy detection in configuration, while taking advantage
181
+ * of the many [Axios plugins](https://www.npmjs.com/search?q=axios).
182
+ *
183
+ * ```ts
184
+ * import { actorCalled } from '@serenity-js/core'
185
+ * import { createAxios, CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'
186
+ * import { Ensure, equals } from '@serenity-js/assertions'
187
+ *
188
+ * import axiosRetry from 'axios-retry'
189
+ *
190
+ * const instance = createAxios({ baseURL: 'https://api.example.org/' })
191
+ * axiosRetry(instance, { retries: 3 })
192
+ *
193
+ * await actorCalled('Apisitt')
194
+ * .whoCan(
195
+ * CallAnApi.using(instance)
196
+ * )
197
+ * .attemptsTo(
198
+ * Send.a(GetRequest.to('/users/2')),
199
+ * Ensure.that(LastResponse.status(), equals(200)),
200
+ * )
201
+ * ```
202
+ *
203
+ * ### Using raw Axios instance
204
+ *
205
+ * If you don't want Serenity/JS to enhance your Axios instance with proxy support, instantiate the ability to
206
+ * `CallAnApi` using its constructor directly.
207
+ * Note, however, that by using this approach you're taking the responsibility for all the aspects of configuring Axios.
208
+ *
209
+ * ```ts
210
+ * import { actorCalled } from '@serenity-js/core'
211
+ * import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'
212
+ * import { Ensure, equals } from '@serenity-js/assertions'
213
+ *
214
+ * import { axiosCreate } from '@serenity-js/rest'
215
+ * import axiosRetry from 'axios-retry'
216
+ *
217
+ * const instance = axiosCreate({ baseURL: 'https://api.example.org/' })
218
+ * axiosRetry(instance, { retries: 3 })
219
+ *
220
+ * await actorCalled('Apisitt')
221
+ * .whoCan(
222
+ * new CallAnApi(instance) // using the constructor ensures your axios instance is not modified in any way.
223
+ * )
224
+ * .attemptsTo(
225
+ * // ...
226
+ * )
227
+ * ```
228
+ *
229
+ * ### Serenity/JS defaults
230
+ *
231
+ * When using [`CallAnApi.at`](https://serenity-js.org/api/rest/class/CallAnApi/#at) or [`CallAnApi.using`](https://serenity-js.org/api/rest/class/CallAnApi/#using) with a configuration object, Serenity/JS
232
+ * merges your [Axios request configuration](https://axios-http.com/docs/req_config) with the following defaults:
233
+ * - `timeout`: 10 seconds
234
+ *
235
+ *
236
+ * You can override them by specifying the given property in your configuration object, for example:
237
+ * ```ts
238
+ * import { actorCalled } from '@serenity-js/core'
239
+ * import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'
240
+ * import { Ensure, equals } from '@serenity-js/assertions'
241
+ *
242
+ * await actorCalled('Apisitt')
243
+ * .whoCan(
244
+ * CallAnApi.using({
245
+ * baseURL: 'https://api.example.org/',
246
+ * timeout: 30_000
247
+ * })
248
+ * )
249
+ * .attemptsTo(
250
+ * Send.a(GetRequest.to('/users/2')),
251
+ * Ensure.that(LastResponse.status(), equals(200)),
252
+ * )
253
+ * ```
254
+ *
255
+ * ## Interacting with multiple APIs
256
+ *
257
+ * Some test scenarios might require you to interact with multiple HTTP APIs. With Serenity/JS you can do this
258
+ * using either API-specific actors, or by specifying full URLs when performing the requests.
259
+ *
260
+ * The following examples will assume that the test scenarios needs to interact with the following APIs:
261
+ * - `https://testdata.example.org/api/v1/`
262
+ * - `https://shop.example.org/api/v1/`
263
+ *
264
+ * Let's also assume that the `testdata` API allows the automation to manage the test data used by the `shop` API.
265
+ *
266
+ * ### Using API-specific actors
267
+ *
268
+ * To create API-specific actors, configure your [test runner](https://serenity-js.org/handbook/test-runners/) with a [cast](https://serenity-js.org/api/core/class/Cast/)
269
+ * that gives your actors appropriate abilities based, for example, on their name:
270
+ *
271
+ * ```ts
272
+ * import { beforeEach } from 'mocha'
273
+ * import { Actor, Cast, engage } from '@serenity-js/core'
274
+ * import { CallAnApi } from '@serenity-js/rest'
275
+ *
276
+ * export class MyActors implements Cast {
277
+ * prepare(actor: Actor): Actor {
278
+ * switch(actor.name) {
279
+ * case 'Ted':
280
+ * return actor.whoCan(CallAnApi.at('https://testdata.example.org/api/v1/'))
281
+ * case 'Shelly':
282
+ * return actor.whoCan(CallAnApi.at('https://shop.example.org/api/v1/'))
283
+ * default:
284
+ * return actor;
285
+ * }
286
+ * }
287
+ * }
288
+ *
289
+ * beforeEach(() => engage(new MyActors()))
290
+ * ```
291
+ *
292
+ * Next, retrieve the appropriate actor in your test scenario using [`actorCalled`](https://serenity-js.org/api/core/function/actorCalled/), for example:
293
+ *
294
+ * ```ts
295
+ * import { describe, it, beforeEach } from 'mocha'
296
+ * import { actorCalled, engage } from '@serenity-js/core
297
+ * import { Send, GetRequest, PostRequest, LastResponse } from '@serenity-js/rest'
298
+ * import { Ensure, equals } from '@serenity-js/assertions'
299
+ *
300
+ * describe('Multi-actor API testing', () => {
301
+ * beforeEach(() => engage(new MyActors()))
302
+ *
303
+ * it('allows each actor to interact with their API', async () => {
304
+ *
305
+ * await actorCalled('Ted').attemptsTo(
306
+ * Send.a(PostRequest.to('products').with({ name: 'Apples', price: '£2.50' })),
307
+ * Ensure.that(LastResponse.status(), equals(201)),
308
+ * )
309
+ *
310
+ * await actorCalled('Shelly').attemptsTo(
311
+ * Send.a(GetRequest.to('?product=Apples')),
312
+ * Ensure.that(LastResponse.status(), equals(200)),
313
+ * Ensure.that(LastResponse.body(), equals([
314
+ * { name: 'Apples', price: '£2.50' }
315
+ * ])),
316
+ * )
317
+ * })
318
+ * })
319
+ * ```
320
+ *
321
+ * ### Using full URLs
322
+ *
323
+ * If you prefer to have a single actor interacting with multiple APIs, you can specify the full URL for every request:
324
+ *
325
+ * ```ts
326
+ * import { describe, it, beforeEach } from 'mocha'
327
+ * import { actorCalled, Cast, engage } from '@serenity-js/core
328
+ * import { CallAnApi, Send, GetRequest, PostRequest, LastResponse } from '@serenity-js/rest'
329
+ * import { Ensure, equals } from '@serenity-js/assertions'
330
+ *
331
+ * describe('Multi-actor API testing', () => {
332
+ * beforeEach(() => engage(
333
+ * Cast.where(actor => actor.whoCan(CallAnApi.using({})))
334
+ * ))
335
+ *
336
+ * it('allows each actor to interact with their API', async () => {
337
+ *
338
+ * await actorCalled('Alice').attemptsTo(
339
+ * Send.a(PostRequest.to('https://testdata.example.org/api/v1/products')
340
+ * .with({ name: 'Apples', price: '£2.50' })),
341
+ * Ensure.that(LastResponse.status(), equals(201)),
342
+ *
343
+ * Send.a(GetRequest.to('https://shop.example.org/api/v1/?product=Apples')),
344
+ * Ensure.that(LastResponse.status(), equals(200)),
345
+ * Ensure.that(LastResponse.body(), equals([
346
+ * { name: 'Apples', price: '£2.50' }
347
+ * ])),
348
+ * )
349
+ * })
350
+ * })
351
+ * ```
352
+ *
353
+ * ## Learn more
354
+ * - [Axios: Configuring requests](https://axios-http.com/docs/req_config)
355
+ * - [MDN: HTTP methods documentation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)
356
+ *
357
+ * @group Abilities
358
+ */
359
+ export declare class CallAnApi extends Ability {
360
+ private readonly axiosInstance;
361
+ private lastResponse;
362
+ /**
363
+ * Produces an [ability](https://serenity-js.org/api/core/class/Ability/) to call a REST API at a specified `baseURL`;
364
+ *
365
+ * This is the same as invoking `CallAnApi.using({ baseURL: 'https://example.org' })`
366
+ *
367
+ * @param baseURL
368
+ */
369
+ static at(baseURL: URL | string): CallAnApi;
370
+ /**
371
+ * Produces an [ability](https://serenity-js.org/api/core/class/Ability/) to call an HTTP API using the given Axios instance,
372
+ * or an Axios request configuration object.
373
+ *
374
+ * When you provide an [Axios configuration object](https://axios-http.com/docs/req_config),
375
+ * it gets shallow-merged with the following defaults:
376
+ * - request timeout of 10 seconds
377
+ * - automatic proxy support based on
378
+ * your [environment variables](https://www.npmjs.com/package/proxy-from-env#environment-variables)
379
+ *
380
+ * When you provide an Axios instance, it's enhanced with proxy support and no other modifications are made.
381
+ *
382
+ * If you don't want Serenity/JS to augment or modify your Axios instance in any way,
383
+ * please use the [`CallAnApi.constructor`](https://serenity-js.org/api/rest/class/CallAnApi/#constructor) directly.
384
+ *
385
+ * @param axiosInstanceOrConfig
386
+ */
387
+ static using(axiosInstanceOrConfig: AxiosInstance | AxiosRequestConfigDefaults): CallAnApi;
388
+ /**
389
+ * #### Learn more
390
+ * - [AxiosInstance](https://axios-http.com/docs/instance)
391
+ *
392
+ * @param axiosInstance
393
+ * A pre-configured instance of the Axios HTTP client
394
+ */
395
+ constructor(axiosInstance: AxiosInstance);
396
+ /**
397
+ * Allows for the original Axios config to be changed after
398
+ * the [ability](https://serenity-js.org/api/core/class/Ability/) to [`CallAnApi`](https://serenity-js.org/api/rest/class/CallAnApi/)
399
+ * has been instantiated and given to the [`Actor`](https://serenity-js.org/api/core/class/Actor/).
400
+ *
401
+ * #### Learn more
402
+ * - [AxiosRequestConfig](https://axios-http.com/docs/req_config)
403
+ *
404
+ * @param fn
405
+ */
406
+ modifyConfig(fn: (original: AxiosDefaults<any>) => any): void;
407
+ /**
408
+ * Sends an HTTP request to a specified url.
409
+ * Response will be cached and available via [`CallAnApi.mapLastResponse`](https://serenity-js.org/api/rest/class/CallAnApi/#mapLastResponse).
410
+ *
411
+ * #### Learn more
412
+ * - [AxiosRequestConfig](https://axios-http.com/docs/req_config)
413
+ * - [AxiosResponse](https://axios-http.com/docs/res_schema)
414
+ *
415
+ * @param config
416
+ * Axios request configuration, which can be used to override the defaults
417
+ * provided when the [ability](https://serenity-js.org/api/core/class/Ability/)
418
+ * to [`CallAnApi`](https://serenity-js.org/api/rest/class/CallAnApi/) was instantiated.
419
+ */
420
+ request(config: AxiosRequestConfig): Promise<AxiosResponse>;
421
+ /**
422
+ * Resolves the final URL, based on the [`AxiosRequestConfig`](https://axios-http.com/docs/req_config) provided
423
+ * and any defaults that the [`AxiosInstance`](https://axios-http.com/docs/instance) has been configured with.
424
+ *
425
+ * Note that unlike Axios, this method uses the Node.js [WHATWG URL API](https://nodejs.org/api/url.html#new-urlinput-base)
426
+ * to ensure URLs are correctly resolved.
427
+ *
428
+ * @param config
429
+ */
430
+ resolveUrl(config: AxiosRequestConfig): string;
431
+ /**
432
+ * Maps the last cached response to another type.
433
+ * Useful when you need to extract a portion of the [`AxiosResponse`](https://axios-http.com/docs/res_schema) object.
434
+ *
435
+ * #### Learn more
436
+ * - [AxiosResponse](https://axios-http.com/docs/res_schema)
437
+ *
438
+ * @param mappingFunction
439
+ */
440
+ mapLastResponse<T>(mappingFunction: (response: AxiosResponse) => T): T;
441
+ toJSON(): SerialisedAbility;
442
+ }
443
+ //# sourceMappingURL=CallAnApi.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CallAnApi.d.ts","sourceRoot":"","sources":["../../../src/screenplay/abilities/CallAnApi.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAE/B,OAAO,EACH,OAAO,EAGP,KAAK,iBAAiB,EAEzB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EACH,KAAK,aAAa,EAElB,KAAK,aAAa,EAClB,KAAK,kBAAkB,EACvB,KAAK,aAAa,EACrB,MAAM,OAAO,CAAC;AAIf,OAAO,KAAK,EAAE,0BAA0B,EAAmC,MAAM,mBAAmB,CAAC;AAGrG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiWG;AACH,qBAAa,SAAU,SAAQ,OAAO;IA+CtB,OAAO,CAAC,QAAQ,CAAC,aAAa;IA7C1C,OAAO,CAAC,YAAY,CAAgB;IAEpC;;;;;;OAMG;IACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,GAAG,MAAM,GAAG,SAAS;IAQ3C;;;;;;;;;;;;;;;;OAgBG;IACH,MAAM,CAAC,KAAK,CAAC,qBAAqB,EAAE,aAAa,GAAG,0BAA0B,GAAG,SAAS;IAI1F;;;;;;OAMG;gBAC0B,aAAa,EAAE,aAAa;IAIzD;;;;;;;;;OASG;IACH,YAAY,CAAC,EAAE,EAAE,CAAC,QAAQ,EAAE,aAAa,CAAC,GAAG,CAAC,KAAK,GAAG,GAAG,IAAI;IAI7D;;;;;;;;;;;;OAYG;IACG,OAAO,CAAC,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC,aAAa,CAAC;IA+BjE;;;;;;;;OAQG;IACH,UAAU,CAAC,MAAM,EAAE,kBAAkB,GAAG,MAAM;IAQ9C;;;;;;;;OAQG;IACH,eAAe,CAAC,CAAC,EAAE,eAAe,EAAE,CAAC,QAAQ,EAAE,aAAa,KAAK,CAAC,GAAG,CAAC;IAQtE,MAAM,IAAI,iBAAiB;CAkB9B"}