{"id":15046230,"url":"https://github.com/zihantype/poem-extensions","last_synced_at":"2025-07-13T07:37:49.205Z","repository":{"id":38537052,"uuid":"497759017","full_name":"ZihanType/poem-extensions","owner":"ZihanType","description":"为Poem框架添加扩展功能 -- Add some extensions to Poem web framework","archived":false,"fork":false,"pushed_at":"2024-05-06T07:10:23.000Z","size":59,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-09-28T13:22:05.749Z","etag":null,"topics":["openapi","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ZihanType.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-30T02:08:36.000Z","updated_at":"2024-05-23T08:07:58.000Z","dependencies_parsed_at":"2023-02-18T14:31:23.228Z","dependency_job_id":"3fad9bec-76f2-4460-ac69-d0057153897a","html_url":"https://github.com/ZihanType/poem-extensions","commit_stats":{"total_commits":52,"total_committers":2,"mean_commits":26.0,"dds":"0.019230769230769273","last_synced_commit":"8e81bb1ec4c38f1e4080d0e9435204d8032d0d7d"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZihanType%2Fpoem-extensions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZihanType%2Fpoem-extensions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZihanType%2Fpoem-extensions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZihanType%2Fpoem-extensions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ZihanType","download_url":"https://codeload.github.com/ZihanType/poem-extensions/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219862887,"owners_count":16555951,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["openapi","rust"],"created_at":"2024-09-24T20:52:53.196Z","updated_at":"2024-10-11T04:40:47.591Z","avatar_url":"https://github.com/ZihanType.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# poem-extensions\n\n[![Crates.io version](https://img.shields.io/crates/v/poem-extensions.svg?style=flat-square)](https://crates.io/crates/poem-extensions)\n\nAdd some extensions to Poem web framework.\n\n## `UniOpenApi`, `api`\n\n`UniOpenApi` unifies multiple `struct`s that implement [`OpenApi`](https://docs.rs/poem-openapi/latest/poem_openapi/attr.OpenApi.html) into one `struct`. Because using the [`OpenApiService::new()`](https://docs.rs/poem-openapi/latest/poem_openapi/struct.OpenApiService.html#method.new) method can only convert a tuple with at most 16 elements into an [`Endpoint`](https://docs.rs/poem/latest/poem/endpoint/trait.Endpoint.html#), `UniOpenApi` is available to facilitate developers to define an unlimited number of `OpenApi` implementations. `api` is a simplified version of `UniOpenApi`, combining declaration and invocation into one.\n\n### Example\n\n#### before\n\n```rust\nuse poem_openapi::{OpenApi, OpenApiService};\n\nstruct Api1;\n\n#[OpenApi]\nimpl Api1 {}\n\nstruct Api2;\n\n#[OpenApi]\nimpl Api2 {}\n\nstruct Api3;\n\n#[OpenApi]\nimpl Api3 {}\n\n/// only put a maximum of 16 OpenApi struct\nlet api = (Api1, Api2, Api3);\n\nlet api_service = OpenApiService::new(api, \"Combined APIs\", \"1.0\")\n        .server(\"http://localhost:3000/api\");\n```\n\n#### after\n\n```rust\nuse poem_extensions::{api, UniOpenApi};\nuse poem_openapi::{OpenApi, OpenApiService};\n\nstruct Api1;\n\n#[OpenApi]\nimpl Api1 {}\n\nstruct Api2;\n\n#[OpenApi]\nimpl Api2 {}\n\nstruct Api3;\n\n#[OpenApi]\nimpl Api3 {}\n\n/// struct mode, support generics\n#[derive(UniOpenApi)]\nstruct Union(Api1, Api2, Api3);\n\nlet api = Union(Api1, Api2, Api3);\n\n/// ... or tuple mode, not support generics\nlet api = api!(Api1, Api2, Api3);\n\nlet api_service = OpenApiService::new(api, \"Combined APIs\", \"1.0\")\n        .server(\"http://localhost:3000/api\");\n```\n\n## `OneResponse`, `UniResponse`, `response`\n\nThe response type defined by [ApiResponse](https://docs.rs/poem-openapi/latest/poem_openapi/derive.ApiResponse.html) has too much control granularity and is less reusable. Either one request defines one response, which is too much code, or it defines a response that contains all possible responses, which can obscure the really important ones.\n\nBecause of such shortcomings, 3 helpers are provided in this repository.\n\n- `OneResponse` is a simplification of `ApiResponse`, where only one response type corresponding to one status code can be defined.\n- `UniResponse` is an `enum` with 60 generic type slots corresponding to 60 response status codes.\n- `response` is a functional macro for insert response type that defined by `OneResponse` into `UniResponse` type slots.\n\n### Example\n\n#### before\n\n```rust\nuse poem_openapi::{param::Query, ApiResponse, OpenApi};\n\n#[derive(ApiResponse)]\nenum FirstResp {\n    #[oai(status = 200)]\n    Ok,\n    #[oai(status = 400)]\n    BadRequest,\n}\n\n#[derive(ApiResponse)]\nenum SecondResp {\n    #[oai(status = 200)]\n    Ok,\n    #[oai(status = 400)]\n    BadRequest,\n    #[oai(status = 404)]\n    NotFound,\n}\n\nstruct Api;\n\n#[OpenApi]\nimpl Api {\n    #[oai(path = \"/first\", method = \"get\")]\n    async fn first(\u0026self, name: Query\u003cOption\u003cu64\u003e\u003e) -\u003e FirstResp {\n        match name.0 {\n            Some(_) =\u003e FirstResp::Ok,\n            None =\u003e FirstResp::BadRequest,\n        }\n    }\n\n    #[oai(path = \"/second\", method = \"get\")]\n    async fn second(\u0026self, name: Query\u003cOption\u003cu64\u003e\u003e) -\u003e SecondResp {\n        match name.0 {\n            Some(a) if a \u003e 100 =\u003e SecondResp::NotFound,\n            Some(_) =\u003e SecondResp::Ok,\n            None =\u003e SecondResp::BadRequest,\n        }\n    }\n}\n```\n\n#### after\n\n```rust\nuse poem::IntoResponse;\nuse poem_extensions::{\n    response, OneResponse,\n    UniResponse::{T200, T400, T404},\n};\nuse poem_openapi::{\n    param::Query,\n    payload::{Payload, PlainText},\n    OpenApi,\n};\n\n#[derive(OneResponse)]\n#[oai(status = 400)]\nstruct BadRequest\u003cT: IntoResponse + Payload\u003e(T);\n\n#[derive(OneResponse)]\n#[oai(status = 404)]\nstruct NotFound;\n\nstruct Api;\n\n#[OpenApi]\nimpl Api {\n    #[oai(path = \"/first\", method = \"get\")]\n    async fn first(\n        \u0026self,\n        name: Query\u003cOption\u003cu64\u003e\u003e,\n    ) -\u003e response! {\n           200: PlainText\u003cString\u003e,\n           400: BadRequest\u003cPlainText\u003cString\u003e\u003e,\n       } {\n        match name.0 {\n            Some(a) =\u003e T200(PlainText(format!(\"{}\", a))),\n            None =\u003e T400(BadRequest(PlainText(\"name is required\".to_string()))),\n        }\n    }\n\n    #[oai(path = \"/second\", method = \"get\")]\n    async fn second(\n        \u0026self,\n        name: Query\u003cOption\u003cu64\u003e\u003e,\n    ) -\u003e response! {\n           200: (),\n           400: BadRequest\u003cPlainText\u003cString\u003e\u003e,\n           404: NotFound,\n       } {\n        match name.0 {\n            Some(a) if a \u003e 100 =\u003e T404(NotFound),\n            Some(_) =\u003e T200(()),\n            None =\u003e T400(BadRequest(PlainText(\"name is required\".to_string()))),\n        }\n    }\n}\n```\n\n## Contributing\n\nThanks for your help improving the project! We are so happy to have you!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzihantype%2Fpoem-extensions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzihantype%2Fpoem-extensions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzihantype%2Fpoem-extensions/lists"}