Skip to content

Commit

Permalink
Always split response type (#2225)
Browse files Browse the repository at this point in the history
Partially based on discussion
#2219
Needed for #2226 
But generally should simplify the flow and enable further improvements
of the Integration class
  • Loading branch information
RobinTail authored Dec 2, 2024
1 parent fea1f88 commit d8b105e
Show file tree
Hide file tree
Showing 8 changed files with 566 additions and 498 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

## Version 21

### v21.2.1
### v21.3.0

- Fixed `provide()` method usage example in the code of the generated client.
- Fixed `provide()` method usage example in the code of the generated client;
- Always splitting the response in the generated client:
- This will print the positive and negative response types separately;
- The `splitResponse` property on the `Integration` class constructor argument is deprecated.

### v21.2.0

Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,6 @@ const client = new Integration({
routing,
variant: "client", // <— optional, see also "types" for a DIY solution
optionalPropStyle: { withQuestionMark: true, withUndefined: true }, // optional
splitResponse: false, // optional, prints the positive and negative response types separately
});

const prettierFormattedTypescriptCode = await client.printFormatted(); // or just .print() for unformatted
Expand Down
225 changes: 145 additions & 80 deletions example/example.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,41 @@ type GetV1UserRetrieveInput = {
id: string;
};

type GetV1UserRetrievePositiveResponse = {
status: "success";
data: {
id: number;
name: string;
features: {
title: string;
features: Type1;
}[];
};
};

type GetV1UserRetrieveNegativeResponse = {
status: "error";
error: {
message: string;
};
};

type GetV1UserRetrieveResponse =
| {
status: "success";
data: {
id: number;
name: string;
features: {
title: string;
features: Type1;
}[];
};
}
| {
status: "error";
error: {
message: string;
};
};
| GetV1UserRetrievePositiveResponse
| GetV1UserRetrieveNegativeResponse;

type DeleteV1UserIdRemoveInput = {
/** numeric string */
id: string;
};

type DeleteV1UserIdRemoveResponse = undefined;
type DeleteV1UserIdRemovePositiveResponse = undefined;

type DeleteV1UserIdRemoveNegativeResponse = undefined;

type DeleteV1UserIdRemoveResponse =
| DeleteV1UserIdRemovePositiveResponse
| DeleteV1UserIdRemoveNegativeResponse;

type PatchV1UserIdInput = {
key: string;
Expand All @@ -41,100 +51,131 @@ type PatchV1UserIdInput = {
birthday: string;
};

type PatchV1UserIdPositiveResponse = {
status: "success";
data: {
name: string;
createdAt: string;
};
};

type PatchV1UserIdNegativeResponse = {
status: "error";
error: {
message: string;
};
};

type PatchV1UserIdResponse =
| {
status: "success";
data: {
name: string;
createdAt: string;
};
}
| {
status: "error";
error: {
message: string;
};
};
| PatchV1UserIdPositiveResponse
| PatchV1UserIdNegativeResponse;

type PostV1UserCreateInput = {
name: string;
};

type PostV1UserCreateResponse =
type PostV1UserCreatePositiveResponse = {
status: "created";
data: {
id: number;
};
};

type PostV1UserCreateNegativeResponse =
| {
status: "created";
data: {
id: number;
};
status: "exists";
id: number;
}
| (
| {
status: "exists";
id: number;
}
| {
status: "error";
reason: string;
}
);
| {
status: "error";
reason: string;
};

type PostV1UserCreateResponse =
| PostV1UserCreatePositiveResponse
| PostV1UserCreateNegativeResponse;

type GetV1UserListInput = {};

type GetV1UserListPositiveResponse = {
name: string;
}[];

type GetV1UserListNegativeResponse = string;

type GetV1UserListResponse =
| {
name: string;
}[]
| string;
| GetV1UserListPositiveResponse
| GetV1UserListNegativeResponse;

type GetV1AvatarSendInput = {
userId: string;
};

type GetV1AvatarSendResponse = string;
type GetV1AvatarSendPositiveResponse = string;

type GetV1AvatarSendNegativeResponse = string;

type GetV1AvatarSendResponse =
| GetV1AvatarSendPositiveResponse
| GetV1AvatarSendNegativeResponse;

type GetV1AvatarStreamInput = {
userId: string;
};

type GetV1AvatarStreamResponse = Buffer | string;
type GetV1AvatarStreamPositiveResponse = Buffer;

type GetV1AvatarStreamNegativeResponse = string;

type GetV1AvatarStreamResponse =
| GetV1AvatarStreamPositiveResponse
| GetV1AvatarStreamNegativeResponse;

type PostV1AvatarUploadInput = {
avatar: any;
};

type PostV1AvatarUploadPositiveResponse = {
status: "success";
data: {
name: string;
size: number;
mime: string;
hash: string;
otherInputs: Record<string, any>;
};
};

type PostV1AvatarUploadNegativeResponse = {
status: "error";
error: {
message: string;
};
};

type PostV1AvatarUploadResponse =
| {
status: "success";
data: {
name: string;
size: number;
mime: string;
hash: string;
otherInputs: Record<string, any>;
};
}
| {
status: "error";
error: {
message: string;
};
};
| PostV1AvatarUploadPositiveResponse
| PostV1AvatarUploadNegativeResponse;

type PostV1AvatarRawInput = Buffer;

type PostV1AvatarRawPositiveResponse = {
status: "success";
data: {
length: number;
};
};

type PostV1AvatarRawNegativeResponse = {
status: "error";
error: {
message: string;
};
};

type PostV1AvatarRawResponse =
| {
status: "success";
data: {
length: number;
};
}
| {
status: "error";
error: {
message: string;
};
};
| PostV1AvatarRawPositiveResponse
| PostV1AvatarRawNegativeResponse;

export type Path =
| "/v1/user/retrieve"
Expand All @@ -161,6 +202,30 @@ export interface Input {
"post /v1/avatar/raw": PostV1AvatarRawInput;
}

export interface PositiveResponse {
"get /v1/user/retrieve": GetV1UserRetrievePositiveResponse;
"delete /v1/user/:id/remove": DeleteV1UserIdRemovePositiveResponse;
"patch /v1/user/:id": PatchV1UserIdPositiveResponse;
"post /v1/user/create": PostV1UserCreatePositiveResponse;
"get /v1/user/list": GetV1UserListPositiveResponse;
"get /v1/avatar/send": GetV1AvatarSendPositiveResponse;
"get /v1/avatar/stream": GetV1AvatarStreamPositiveResponse;
"post /v1/avatar/upload": PostV1AvatarUploadPositiveResponse;
"post /v1/avatar/raw": PostV1AvatarRawPositiveResponse;
}

export interface NegativeResponse {
"get /v1/user/retrieve": GetV1UserRetrieveNegativeResponse;
"delete /v1/user/:id/remove": DeleteV1UserIdRemoveNegativeResponse;
"patch /v1/user/:id": PatchV1UserIdNegativeResponse;
"post /v1/user/create": PostV1UserCreateNegativeResponse;
"get /v1/user/list": GetV1UserListNegativeResponse;
"get /v1/avatar/send": GetV1AvatarSendNegativeResponse;
"get /v1/avatar/stream": GetV1AvatarStreamNegativeResponse;
"post /v1/avatar/upload": PostV1AvatarUploadNegativeResponse;
"post /v1/avatar/raw": PostV1AvatarRawNegativeResponse;
}

export interface Response {
"get /v1/user/retrieve": GetV1UserRetrieveResponse;
"delete /v1/user/:id/remove": DeleteV1UserIdRemoveResponse;
Expand Down
2 changes: 1 addition & 1 deletion example/example.documentation.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
openapi: 3.1.0
info:
title: Example API
version: 21.2.0
version: 21.3.0-beta.0
paths:
/v1/user/retrieve:
get:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-zod-api",
"version": "21.2.0",
"version": "21.3.0-beta.0",
"description": "A Typescript framework to help you get an API server up and running with I/O schema validation and custom middlewares in minutes.",
"license": "MIT",
"repository": {
Expand Down
Loading

0 comments on commit d8b105e

Please sign in to comment.