@mittwald/api-client-commons 4.8.1 → 4.10.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 CHANGED
@@ -2,11 +2,12 @@ Common code base used by `@mittwald/api-client-*` package.
2
2
 
3
3
  ## API
4
4
 
5
- ### assertStatus
5
+ ### assertStatus, assertOneOfStatus
6
6
 
7
7
  The API client does not validate any response status by design, to give you the
8
8
  most flexibility while handling also erroneous responses. If you want to assert
9
- some desired response status, you can use the `assertStatus` function.
9
+ some desired response status, you can use the `assertStatus` resp.
10
+ `assertOneOfStatus` function.
10
11
 
11
12
  #### assertStatus(response, expectedStatus)
12
13
 
@@ -31,3 +32,27 @@ const project = response.data;
31
32
  // Project properties can now be accessed safely
32
33
  const name = project.name;
33
34
  ```
35
+
36
+ #### assertOnOfStatus(response, expectedStatus)
37
+
38
+ Returns: void
39
+
40
+ This method throws an `ApiClientError` if the given `response` does not match
41
+ the `expectedStatus`.
42
+
43
+ When you are using TypeScript this function also asserts the correct response
44
+ type.
45
+
46
+ ```ts
47
+ const response = await client.project.getProject({
48
+ pathParameters: {
49
+ projectId: "...",
50
+ },
51
+ });
52
+
53
+ assertOneOfStatus(response, [200, 404]);
54
+
55
+ if (!response.data) {
56
+ console.log("Project not found");
57
+ }
58
+ ```
package/dist/esm/index.js CHANGED
@@ -1,4 +1,3 @@
1
1
  export * from "./core/index.js";
2
2
  export * from "./types/index.js";
3
3
  export * from "./axios.js";
4
- // trigger release publish
@@ -0,0 +1,7 @@
1
+ import ApiClientError from "../core/ApiClientError.js";
2
+ export function assertOneOfStatus(response, expectedStatus) {
3
+ if (!expectedStatus.includes(response.status)) {
4
+ throw ApiClientError.fromResponse(`Unexpected response status (expected ${expectedStatus}, got: ${response.status})`, response);
5
+ }
6
+ }
7
+ export default assertOneOfStatus;
@@ -1,7 +1,5 @@
1
- import ApiClientError from "../core/ApiClientError.js";
1
+ import assertOneOfStatus from "./assertOneOfStatus.js";
2
2
  export function assertStatus(response, expectedStatus) {
3
- if (response.status !== expectedStatus) {
4
- throw ApiClientError.fromResponse(`Unexpected response status (expected ${expectedStatus}, got: ${response.status})`, response);
5
- }
3
+ assertOneOfStatus(response, [expectedStatus]);
6
4
  }
7
5
  export default assertStatus;
@@ -5,3 +5,4 @@ export * from "./OpenAPIOperation.js";
5
5
  export * from "./http.js";
6
6
  export * from "./simplify.js";
7
7
  export * from "./assertStatus.js";
8
+ export * from "./assertOneOfStatus.js";
@@ -0,0 +1,5 @@
1
+ import { Response } from "./Response.js";
2
+ export declare function assertOneOfStatus<T extends Response, S extends T["status"]>(response: T, expectedStatus: S[]): asserts response is T & {
3
+ status: S;
4
+ };
5
+ export default assertOneOfStatus;
@@ -5,3 +5,4 @@ export * from "./OpenAPIOperation.js";
5
5
  export * from "./http.js";
6
6
  export * from "./simplify.js";
7
7
  export * from "./assertStatus.js";
8
+ export * from "./assertOneOfStatus.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mittwald/api-client-commons",
3
- "version": "4.8.1",
3
+ "version": "4.10.0",
4
4
  "author": "Mittwald CM Service GmbH & Co. KG <opensource@mittwald.de>",
5
5
  "type": "module",
6
6
  "description": "Common types and utilities for mittwald API clients",
@@ -71,5 +71,5 @@
71
71
  "optional": true
72
72
  }
73
73
  },
74
- "gitHead": "cb47b1e1602bbd7cd20f8e06acc23c5b2567f32d"
74
+ "gitHead": "2a22d83ac84633065aeae70821f6f6297c3d91e9"
75
75
  }