Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Models package #22

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
356 changes: 354 additions & 2 deletions .pnp.cjs

Large diffs are not rendered by default.

29 changes: 27 additions & 2 deletions packages/commons/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ Common code base used by `@mittwald/api-client-*` package.

## API

### assertStatus
### assertStatus, assertOneOfStatus

The API client does not validate any response status by design, to give you the
most flexibility while handling also erroneous responses. If you want to assert
some desired response status, you can use the `assertStatus` function.
some desired response status, you can use the `assertStatus` resp.
`assertOneOfStatus` function.

#### assertStatus(response, expectedStatus)

Expand All @@ -31,3 +32,27 @@ const project = response.data;
// Project properties can now be accessed safely
const name = project.name;
```

#### assertOnOfStatus(response, expectedStatus)

Returns: void

This method throws an `ApiClientError` if the given `response` does not match
the `expectedStatus`.

When you are using TypeScript this function also asserts the correct response
type.

```ts
const response = await client.project.getProject({
pathParameters: {
projectId: "...",
},
});

assertOneOfStatus(response, [200, 404]);

if (!response.data) {
console.log("Project not found");
}
```
16 changes: 16 additions & 0 deletions packages/commons/src/types/assertOneOfStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ApiClientError from "../core/ApiClientError.js";
import { Response } from "./Response.js";

export function assertOneOfStatus<T extends Response, S extends T["status"]>(
response: T,
expectedStatus: S[],
): asserts response is T & { status: S } {
if (!expectedStatus.includes(response.status as S)) {
throw ApiClientError.fromResponse(
`Unexpected response status (expected ${expectedStatus}, got: ${response.status})`,
response,
);
}
}

export default assertOneOfStatus;
9 changes: 2 additions & 7 deletions packages/commons/src/types/assertStatus.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { Response } from "./Response.js";
import ApiClientError from "../core/ApiClientError.js";
import assertOneOfStatus from "./assertOneOfStatus.js";

export function assertStatus<T extends Response, S extends T["status"]>(
response: T,
expectedStatus: S,
): asserts response is T & { status: S } {
if (response.status !== expectedStatus) {
throw ApiClientError.fromResponse(
`Unexpected response status (expected ${expectedStatus}, got: ${response.status})`,
response,
);
}
assertOneOfStatus(response, [expectedStatus]);
}

export default assertStatus;
1 change: 1 addition & 0 deletions packages/commons/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from "./OpenAPIOperation.js";
export * from "./http.js";
export * from "./simplify.js";
export * from "./assertStatus.js";
export * from "./assertOneOfStatus.js";
2 changes: 1 addition & 1 deletion packages/mittwald/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { assertStatus } from "@mittwald/api-client-commons";
export { assertStatus, assertOneOfStatus } from "@mittwald/api-client-commons";
export { MittwaldAPIClient as MittwaldAPIV2Client } from "./v2/default.js";
export type { MittwaldAPIV2 } from "./generated/v2/types.js";
2 changes: 2 additions & 0 deletions packages/models/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/

2 changes: 2 additions & 0 deletions packages/models/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extends:
- "../../config/.eslintrc.yml"
1 change: 1 addition & 0 deletions packages/models/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
3 changes: 3 additions & 0 deletions packages/models/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import config from "../../config/.prettierrc.js";

export default config;
Loading