|
| 1 | +--- |
| 2 | +sidebar_position: 20 |
| 3 | +--- |
| 4 | + |
| 5 | +# Reference |
| 6 | + |
| 7 | +## Class OpenAPIClientAxios |
| 8 | + |
| 9 | +OpenAPIClientAxios is the main class of this module. However, it's entire purpose is to create an axios client instance |
| 10 | +configured to consume an API described by the OpenAPI definition. |
| 11 | + |
| 12 | +### new OpenAPIClientAxios(opts) |
| 13 | + |
| 14 | +Creates an instance of OpenAPIClientAxios and returns it. |
| 15 | + |
| 16 | +Example: |
| 17 | +```javascript |
| 18 | +const api = new OpenAPIClientAxios({ |
| 19 | + definition: '/openapi.json', |
| 20 | + withServer: 0, |
| 21 | + axiosConfigDefaults: { |
| 22 | + withCredentials: true, |
| 23 | + headers: { |
| 24 | + 'Cache-Control': 'no-cache', |
| 25 | + }, |
| 26 | + }, |
| 27 | +}); |
| 28 | +``` |
| 29 | + |
| 30 | +#### Parameter: opts |
| 31 | + |
| 32 | +Constructor options |
| 33 | + |
| 34 | +#### Parameter: opts.definition |
| 35 | + |
| 36 | +The OpenAPI definition as a URL or [Document object](#document-object). |
| 37 | + |
| 38 | +To support YAML openapi files, `js-yaml` must be installed. |
| 39 | + |
| 40 | +Type: `Document | string` |
| 41 | + |
| 42 | +#### Parameter: opts.withServer |
| 43 | + |
| 44 | +The default server to use. Either by index, description or a full server object to override with. |
| 45 | + |
| 46 | +Type: `number`, `string` or [Server Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#serverObject |
| 47 | + |
| 48 | +#### Parameter: opts.axiosConfigDefaults |
| 49 | + |
| 50 | +Optional. Default axios config for the instance. Applied when instance is created. |
| 51 | + |
| 52 | +Type: [`AxiosRequestConfig`](https://github.com/axios/axios#request-config) |
| 53 | + |
| 54 | +#### Parameter: opts.transformOperationName |
| 55 | + |
| 56 | +Optional. Override the default method name resolution strategy (default: use `operationId` as method name) |
| 57 | + |
| 58 | +Type: `(operationId: string) => string` |
| 59 | + |
| 60 | +For typegen: You can pass the `transformOperationName` using the `-t` ot `--transformOperationName` command line flag. |
| 61 | + |
| 62 | +#### Parameter: opts.transformOperationMethod |
| 63 | + |
| 64 | +Optional. Transforms the returned operation method (default: do not transform) |
| 65 | + |
| 66 | +Type: `(operationMethod: UnknownOperationMethod, operationToTransform: Operation) => UnknownOperationMethod` |
| 67 | + |
| 68 | +The `operation` is also provided to the function, such that you can also conditionally transform the method. |
| 69 | + |
| 70 | +Example: |
| 71 | + |
| 72 | +```javascript |
| 73 | +const api = new OpenAPIClientAxios({ |
| 74 | + definition: 'https://example.com/api/openapi.json', |
| 75 | + transformOperationMethod: (operationMethod, operation) => { |
| 76 | + return (params, body, config) => { |
| 77 | + // set default workspaceId for all operations |
| 78 | + params.workspaceId = '63e90965-07a7-43b3-8f5d-d2e8fa90e8d0'; |
| 79 | + return operationMethod(params, body, config); |
| 80 | + } |
| 81 | + } |
| 82 | +}); |
| 83 | +``` |
| 84 | + |
| 85 | +### .init() |
| 86 | + |
| 87 | +Initalizes OpenAPIClientAxios |
| 88 | + |
| 89 | +Returns a promise of the created member axios instance. |
| 90 | + |
| 91 | +1. Parses the input definition into a JS object. If the input definition is a URL, it will be resolved |
| 92 | +2. Dereferences the definition for use. |
| 93 | +3. Creates the member axios instance |
| 94 | +4. Sets `api.initialised = true` and returns the created axios instance |
| 95 | + |
| 96 | +Example: |
| 97 | +```javascript |
| 98 | +await api.init(); |
| 99 | +``` |
| 100 | + |
| 101 | +### .initSync() |
| 102 | + |
| 103 | +Synchronous version of [`.init()`](#init) |
| 104 | + |
| 105 | +Initalizes OpenAPIClientAxios and creates the axios client instance. |
| 106 | + |
| 107 | +Note: Only works when the input definition is a valid OpenAPI v3 object. |
| 108 | + |
| 109 | +Example: |
| 110 | +```javascript |
| 111 | +api.initSync(); |
| 112 | +``` |
| 113 | + |
| 114 | +### .getClient() |
| 115 | + |
| 116 | +Returns a promise of the member axios instance. Will run .init() if API is not initalised yet. |
| 117 | + |
| 118 | +Example: |
| 119 | +```javascript |
| 120 | +const client = await api.getClient(); |
| 121 | +``` |
| 122 | + |
| 123 | +### .withServer(server) |
| 124 | + |
| 125 | +Set the default server base url to use for client. |
| 126 | + |
| 127 | + |
| 128 | +#### Parameter: server |
| 129 | + |
| 130 | +The default server to use. Either an index, description or a full server object to override with. |
| 131 | + |
| 132 | +Example: |
| 133 | +```javascript |
| 134 | +// by index |
| 135 | +api.withServer(1); |
| 136 | +// by description property |
| 137 | +api.withServer('EU server'); |
| 138 | +// by server object (override) |
| 139 | +api.withServer({ url: 'https://example.com/api/', description: 'Eu Server' }); |
| 140 | +``` |
| 141 | + |
| 142 | +Type: `number`, `string` or [Server Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#serverObject) |
| 143 | + |
| 144 | +### .getBaseURL(operation?) |
| 145 | + |
| 146 | +Gets the API baseurl defined in the servers property |
| 147 | + |
| 148 | +Example: |
| 149 | +```javascript |
| 150 | +const baseURL = api.getBaseUrl(); |
| 151 | +const baseURLForOperation = api.getBaseUrl('operationId'); |
| 152 | +``` |
| 153 | + |
| 154 | +#### Parameter: operation |
| 155 | + |
| 156 | +Optional. The Operation object or operationId that may override the baseURL with its own or path object's servers |
| 157 | +property. |
| 158 | + |
| 159 | +Type: `Operation` or `string` (operationId) |
| 160 | + |
| 161 | +### .getRequestConfigForOperation(operation, args) |
| 162 | + |
| 163 | +Creates a generic request config object for operation + arguments top be used for calling the API. |
| 164 | + |
| 165 | +This function contains the logic that handles operation method parameters. |
| 166 | + |
| 167 | +Example: |
| 168 | +```javascript |
| 169 | +const request = api.getRequestConfigForOperation('updatePet', [1, { name: 'Odie' }]) |
| 170 | +``` |
| 171 | + |
| 172 | +#### Parameter: operation |
| 173 | + |
| 174 | +The operation to call. Either as an operation object or string (operationId). |
| 175 | + |
| 176 | +Type: `Operation` or `string` (operationId) |
| 177 | + |
| 178 | +#### Parameter: args |
| 179 | + |
| 180 | +The operation method arguments. |
| 181 | + |
| 182 | +Type: `OperationMethodArguments` |
| 183 | + |
| 184 | +### .getAxiosInstance() |
| 185 | + |
| 186 | +Creates a new axios instance, extends it and returns it. |
| 187 | + |
| 188 | +While initalising with [`.init()`](#init) or [.initSync()](#initsync) OpenAPIClientAxios calls this function to create the member client. |
| 189 | + |
| 190 | +Note: Requires the API to be initalised first if run outside of .init() methods. |
| 191 | + |
| 192 | +### .getAxiosConfigForOperation(operation, args) |
| 193 | + |
| 194 | +Creates an axios config for operation + arguments to be used for calling the API. |
| 195 | + |
| 196 | +This function calls `.getRequestConfigForOperation()` internally and maps the values to be suitable for axios. |
| 197 | + |
| 198 | +Returns an [AxiosRequestConfig](https://github.com/axios/axios#request-config) object |
| 199 | + |
| 200 | +Example: |
| 201 | +```javascript |
| 202 | +const request = api.getAxiosConfigForOperation('getPets', [{ petId: 1 }]) |
| 203 | +``` |
| 204 | + |
| 205 | +#### Parameter: operation |
| 206 | + |
| 207 | +The operation to call. Either as an operation object or string (operationId). |
| 208 | + |
| 209 | +Type: `Operation` or `string` (operationId) |
| 210 | + |
| 211 | +#### Parameter: args |
| 212 | + |
| 213 | +The operation method arguments. |
| 214 | + |
| 215 | +Type: `OperationMethodArguments` |
| 216 | + |
| 217 | +## Axios Client Instance |
| 218 | + |
| 219 | +When OpenAPIClientAxios is initalised, a member |
| 220 | +[axios client instance](https://github.com/axios/axios#creating-an-instance) is created. |
| 221 | + |
| 222 | +The client instance can be accessed either directly via `api.client` getter, or `api.getClient()`. |
| 223 | + |
| 224 | +The member axios client instance is a regular instance of axios with [Operation Methods](#operation-method) created to |
| 225 | +provide an easy JavaScript API to call API operations. |
| 226 | + |
| 227 | +In addition to operation methods, the Axios client instance baseURL is pre-configured to match the first OpenAPI |
| 228 | +definition [servers](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#serverObject) property. |
| 229 | + |
| 230 | +The parent OpenAPIClientAxios instance can also be accessed from the client via `client.api`. |
| 231 | + |
| 232 | +## Operation Method |
| 233 | + |
| 234 | +Operation methods are the main API used to call OpenAPI operations. |
| 235 | + |
| 236 | +Each method is generated during [`.init()`](#init) and is attached as a property to the axios client instance. |
| 237 | + |
| 238 | +## Operation Method Arguments |
| 239 | + |
| 240 | +Each operation method takes three arguments: |
| 241 | +```javascript |
| 242 | +client.operationId(parameters?, data?, config?) |
| 243 | +``` |
| 244 | +
|
| 245 | +### Parameters |
| 246 | +
|
| 247 | +The first argument is used to pass parameters available for the operation. |
| 248 | +
|
| 249 | +```javascript |
| 250 | +// GET /pets/{petId} |
| 251 | +client.getPet({ petId: 1 }) |
| 252 | +``` |
| 253 | +
|
| 254 | +For syntactic sugar purposes, you can also specify a single implicit parameter value, in which case OpenAPIClientAxios |
| 255 | +will look for the first required parameter for the operation. Usually this is will be a path parameter. |
| 256 | +
|
| 257 | +```javascript |
| 258 | +// GET /pets/{petId} - getPet |
| 259 | +client.getPet(1) |
| 260 | +``` |
| 261 | +
|
| 262 | +Alternatively, you can explicitly specify parameters in array form. This method allows you to set custom parameters not defined |
| 263 | +in the OpenAPI spec. |
| 264 | +
|
| 265 | +```javascript |
| 266 | +// GET /pets?search=Garfield - searchPets |
| 267 | +client.searchPets([{ name: 'search', value: 'Garfield', in: 'query' }]) |
| 268 | +``` |
| 269 | +
|
| 270 | +The type of the parameters can be any of: |
| 271 | +- query |
| 272 | +- header |
| 273 | +- path |
| 274 | +- cookie |
| 275 | +
|
| 276 | +### Data |
| 277 | +
|
| 278 | +The second argument is used to pass the requestPayload |
| 279 | +
|
| 280 | +```javascript |
| 281 | +// PUT /pets/1 - updatePet |
| 282 | +client.updatePet(1, { name: 'Odie' }) |
| 283 | +``` |
| 284 | +
|
| 285 | +More complex payloads, such as Node.js streams or FormData supported by Axios can be used. |
| 286 | +
|
| 287 | +The first argument can be set to null if there are no parameters required for the operation. |
| 288 | +
|
| 289 | +```javascript |
| 290 | +// POST /pets - createPet |
| 291 | +client.updatePet(null, { name: 'Garfield' }) |
| 292 | +``` |
| 293 | +
|
| 294 | +### Config |
| 295 | +
|
| 296 | +The last argument is the config object. |
| 297 | +
|
| 298 | +The config object is an [`AxiosRequestConfig`](https://github.com/axios/axios#request-config) object. You can use it to |
| 299 | +override axios request config parameters, such as `headers`, `timeout`, `withCredentials` and many more. |
| 300 | +
|
| 301 | +```javascript |
| 302 | +// POST /user - createUser |
| 303 | +client.createUser(null, { user: 'admin', pass: '123' }, { headers: { 'x-api-key': 'secret' } }); |
| 304 | +``` |
| 305 | +
|
| 306 | +## Request Config Object |
| 307 | +
|
| 308 | +A `RequestConfig` object gets created as part of every operation method call. |
| 309 | +
|
| 310 | +It represents a generic HTTP request to be executed. |
| 311 | +
|
| 312 | +A request config object can be created without calling an operation method using |
| 313 | +[`.getRequestConfigForOperation()`](#getrequestconfigforoperationoperation-args) |
| 314 | +
|
| 315 | +```javascript |
| 316 | +import { RequestConfig } from 'openapi-client-axios'; |
| 317 | +``` |
| 318 | +
|
| 319 | +Example object |
| 320 | +```javascript |
| 321 | +const requestConfig = { |
| 322 | + method: 'put', // HTTP method |
| 323 | + url: 'http://localhost:8000/pets/1?return=id,name', // full URL including protocol, host, path and query string |
| 324 | + path: '/pets/1', // path for the operation (relative to server base URL) |
| 325 | + pathParams: { petId: 1 }, // path parameters |
| 326 | + query: { return: ['id', 'name'] }, // query parameters |
| 327 | + queryString: 'return=id,name', // query string |
| 328 | + headers: { |
| 329 | + 'content-type': 'application/json;charset=UTF-8', |
| 330 | + 'accept': 'application/json' , |
| 331 | + 'cookie': 'x-api-key=secret', |
| 332 | + }, // HTTP headers, including cookie |
| 333 | + cookies: { 'x-api-key': 'secret' }, // cookies |
| 334 | + payload: { |
| 335 | + name: 'Garfield', |
| 336 | + age: 35, |
| 337 | + }, // the request payload passed as-is |
| 338 | +} |
| 339 | +``` |
| 340 | +
|
| 341 | +## Paths Dictionary |
| 342 | +
|
| 343 | +In addition to operationIds, OpenAPIClient also allows calling operation |
| 344 | +methods, using the operations' path and HTTP method. |
| 345 | +
|
| 346 | +The paths dictionary contains each path found in the OAS definition as keys, |
| 347 | +and an object with each registered operation method as the value. |
| 348 | +
|
| 349 | +Example: |
| 350 | +
|
| 351 | +```javascript |
| 352 | +client.paths['/pets'].get(); // GET /pets, same as calling client.getPets() |
| 353 | +client.paths['/pets'].post(); // POST /pets |
| 354 | +client.paths['/pets/{petId}'].put(1); // PUT /pets/1 |
| 355 | +client.paths['/pets/{petId}/owner/{ownerId}'].get({ petId: 1, ownerId: 2 }) ; // GET /pets/1/owner/2 |
| 356 | +``` |
| 357 | +
|
| 358 | +This allows calling operation methods without using their operationIds, which |
| 359 | +may be sometimes preferred. |
0 commit comments