@hedystia/swagger 1.2.3 → 1.2.5

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.
@@ -0,0 +1,55 @@
1
+ import { Hedystia } from 'hedystia';
2
+ import { OpenAPI } from 'openapi-types';
3
+
4
+ interface SwaggerOptions {
5
+ title?: string;
6
+ description?: string;
7
+ version?: string;
8
+ basePath?: string;
9
+ schemes?: string[];
10
+ consumes?: string[];
11
+ produces?: string[];
12
+ tags?: {
13
+ name: string;
14
+ description?: string;
15
+ }[];
16
+ securityDefinitions?: Record<string, any>;
17
+ externalDocs?: {
18
+ description: string;
19
+ url: string;
20
+ };
21
+ host?: string;
22
+ }
23
+ declare class Swagger {
24
+ private spec;
25
+ constructor(options?: SwaggerOptions);
26
+ addRoute(method: string, path: string, schema: any, summary?: string, description?: string, tags?: string[]): void;
27
+ private buildParameters;
28
+ private buildRequestBody;
29
+ private buildResponses;
30
+ validate(): Promise<boolean>;
31
+ getSpec(): OpenAPI.Document<{}>;
32
+ generateHTML(): string;
33
+ }
34
+
35
+ declare function swagger(options?: SwaggerOptions): {
36
+ plugin: Hedystia<[{
37
+ method: "GET";
38
+ path: "/";
39
+ params: any;
40
+ query: any;
41
+ headers: any;
42
+ response: any;
43
+ }, {
44
+ method: "GET";
45
+ path: "/json";
46
+ params: any;
47
+ query: any;
48
+ headers: any;
49
+ response: any;
50
+ }], {}>;
51
+ swagger: Swagger;
52
+ captureRoutes: (app: any) => void;
53
+ };
54
+
55
+ export { swagger };
package/dist/index.js ADDED
@@ -0,0 +1,204 @@
1
+ "use strict";var h=Object.create;var a=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var b=Object.getPrototypeOf,w=Object.prototype.hasOwnProperty;var x=(i,e)=>{for(var s in e)a(i,s,{get:e[s],enumerable:!0})},g=(i,e,s,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of y(e))!w.call(i,r)&&r!==s&&a(i,r,{get:()=>e[r],enumerable:!(t=f(e,r))||t.enumerable});return i};var v=(i,e,s)=>(s=i!=null?h(b(i)):{},g(e||!i||!i.__esModule?a(s,"default",{value:i,enumerable:!0}):s,i)),S=i=>g(a({},"__esModule",{value:!0}),i);var j={};x(j,{swagger:()=>k});module.exports=S(j);var u=require("hedystia");var l=v(require("@apidevtools/swagger-parser")),o=class{spec={openapi:"3.0.0",info:{title:"API Documentation",version:"1.0.0"},paths:{},components:{schemas:{}}};constructor(e={}){this.spec.info.title=e.title||"API Documentation",this.spec.info.description=e.description,this.spec.info.version=e.version||"1.0.0",e.tags&&(this.spec.tags=e.tags),e.externalDocs&&(this.spec.externalDocs=e.externalDocs),e.securityDefinitions&&(this.spec.components=this.spec.components||{},this.spec.components.securitySchemes=e.securityDefinitions)}addRoute(e,s,t,r,n,d){if(!t)return;let c=s.replace(/:([^/]+)/g,"{$1}");this.spec.paths||(this.spec.paths={}),this.spec.paths[c]||(this.spec.paths[c]={});let m=e.toLowerCase(),p={summary:r||`${e} ${s}`,parameters:this.buildParameters(t),requestBody:this.buildRequestBody(t),responses:this.buildResponses(t)};n&&(p.description=n),d&&(p.tags=d),this.spec.paths[c][m]=p}buildParameters(e){let s=[];if(e.params)try{let t=e.params;t.properties&&Object.entries(t.properties).forEach(([r,n])=>{s.push({name:r,in:"path",required:t.required?.includes(r)??!0,schema:n})})}catch(t){console.error("Failed to convert params schema:",t)}if(e.query)try{let t=e.query;t.properties&&Object.entries(t.properties).forEach(([r,n])=>{s.push({name:r,in:"query",required:t.required?.includes(r)??!1,schema:n})})}catch(t){console.error("Failed to convert query schema:",t)}return s.length>0?s:void 0}buildRequestBody(e){if(e.body)try{return{required:!0,content:{"application/json":{schema:e.body}}}}catch(s){console.error("Failed to convert body schema:",s);return}}buildResponses(e){let s={200:{description:"Successful response"}};if(e.response)try{let t=e.response;s[200].content={"application/json":{schema:t}}}catch(t){console.error("Failed to convert response schema:",t)}return s}async validate(){try{return await l.default.validate(this.spec),!0}catch(e){return console.error("Swagger validation error:",e),!1}}getSpec(){return this.spec}generateHTML(){return`
2
+ <!DOCTYPE html>
3
+ <html lang="en">
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <title>${this.spec.info.title}</title>
8
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.9.0/swagger-ui.css">
9
+ <style>
10
+ :root {
11
+ --primary-color: #3b82f6;
12
+ --primary-dark: #1d4ed8;
13
+ --secondary-color: #64748b;
14
+ --background-color: #f8fafc;
15
+ --card-background: #ffffff;
16
+ --text-color: #1e293b;
17
+ --border-color: #e2e8f0;
18
+ }
19
+
20
+ body {
21
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
22
+ margin: 0;
23
+ padding: 0;
24
+ background-color: var(--background-color);
25
+ color: var(--text-color);
26
+ }
27
+
28
+ .header {
29
+ background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
30
+ color: white;
31
+ padding: 2rem;
32
+ box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
33
+ }
34
+
35
+ .container {
36
+ width: 100%;
37
+ max-width: 1200px;
38
+ margin: 0 auto;
39
+ padding: 0 1rem;
40
+ }
41
+
42
+ .header-content {
43
+ max-width: 1200px;
44
+ margin: 0 auto;
45
+ }
46
+
47
+ .title {
48
+ font-size: 2.25rem;
49
+ font-weight: 700;
50
+ margin: 0;
51
+ line-height: 1.2;
52
+ }
53
+
54
+ .description {
55
+ margin-top: 0.75rem;
56
+ font-size: 1.125rem;
57
+ opacity: 0.9;
58
+ }
59
+
60
+ .version {
61
+ display: inline-block;
62
+ background-color: rgba(255, 255, 255, 0.2);
63
+ padding: 0.25rem 0.75rem;
64
+ border-radius: 9999px;
65
+ font-size: 0.875rem;
66
+ margin-top: 0.75rem;
67
+ }
68
+
69
+ .main {
70
+ padding: 2rem 0;
71
+ }
72
+
73
+ .swagger-container {
74
+ background-color: var(--card-background);
75
+ border-radius: 0.5rem;
76
+ box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
77
+ padding: 1rem;
78
+ margin-bottom: 2rem;
79
+ }
80
+
81
+ .swagger-ui .topbar {
82
+ display: none;
83
+ }
84
+
85
+ .swagger-ui .info {
86
+ margin: 20px 0;
87
+ }
88
+
89
+ .swagger-ui .opblock-tag {
90
+ font-size: 1.25rem;
91
+ border-bottom: 1px solid var(--border-color);
92
+ padding: 10px 0;
93
+ }
94
+
95
+ .swagger-ui .opblock {
96
+ border-radius: 8px;
97
+ box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
98
+ margin: 0 0 15px;
99
+ }
100
+
101
+ .swagger-ui .opblock .opblock-summary {
102
+ padding: 8px 20px;
103
+ }
104
+
105
+ .swagger-ui .btn {
106
+ border-radius: 4px;
107
+ }
108
+
109
+ .footer {
110
+ background-color: #1e293b;
111
+ color: white;
112
+ padding: 1.5rem 0;
113
+ text-align: center;
114
+ margin-top: 2rem;
115
+ }
116
+
117
+ .footer-content {
118
+ font-size: 0.875rem;
119
+ opacity: 0.8;
120
+ }
121
+
122
+ .footer-link {
123
+ color: white;
124
+ text-decoration: underline;
125
+ margin: 0 0.5rem;
126
+ }
127
+
128
+ /* Responsive adjustments */
129
+ @media (max-width: 768px) {
130
+ .header {
131
+ padding: 1.5rem 1rem;
132
+ }
133
+
134
+ .title {
135
+ font-size: 1.75rem;
136
+ }
137
+
138
+ .swagger-ui .wrapper {
139
+ padding: 0;
140
+ }
141
+
142
+ .swagger-ui .opblock .opblock-summary {
143
+ padding: 8px 10px;
144
+ }
145
+ }
146
+ </style>
147
+ </head>
148
+ <body>
149
+ <header class="header">
150
+ <div class="header-content">
151
+ <h1 class="title">${this.spec.info.title}</h1>
152
+ ${this.spec.info.description?`<p class="description">${this.spec.info.description}</p>`:""}
153
+ <span class="version">Version ${this.spec.info.version}</span>
154
+ </div>
155
+ </header>
156
+
157
+ <main class="main">
158
+ <div class="container">
159
+ <div class="swagger-container">
160
+ <div id="swagger-ui"></div>
161
+ </div>
162
+ </div>
163
+ </main>
164
+
165
+ <footer class="footer">
166
+ <div class="container">
167
+ <div class="footer-content">
168
+ <p>Powered by Hedystia</p>
169
+ <div>
170
+ <a href="https://github.com/Hedystia/Framework" class="footer-link" target="_blank">GitHub</a>
171
+ <a href="/swagger/json" class="footer-link" target="_blank">Raw JSON</a>
172
+ </div>
173
+ </div>
174
+ </div>
175
+ </footer>
176
+
177
+ <script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.9.0/swagger-ui-bundle.js"></script>
178
+ <script>
179
+ window.onload = function() {
180
+ const ui = SwaggerUIBundle({
181
+ spec: ${JSON.stringify(this.spec)},
182
+ dom_id: '#swagger-ui',
183
+ deepLinking: true,
184
+ presets: [
185
+ SwaggerUIBundle.presets.apis,
186
+ SwaggerUIBundle.SwaggerUIStandalonePreset
187
+ ],
188
+ layout: "BaseLayout",
189
+ docExpansion: "list",
190
+ defaultModelsExpandDepth: 1,
191
+ defaultModelExpandDepth: 1,
192
+ syntaxHighlight: {
193
+ activated: true,
194
+ theme: "agate"
195
+ },
196
+ filter: true,
197
+ withCredentials: true,
198
+ persistAuthorization: true
199
+ });
200
+ };
201
+ </script>
202
+ </body>
203
+ </html>
204
+ `}};function k(i={}){let e=new o(i);return{plugin:new u.Hedystia().get("/",()=>new Response(e.generateHTML(),{headers:{"Content-Type":"text/html"}})).get("/json",()=>Response.json(e.getSpec())),swagger:e,captureRoutes:t=>{if(t&&t.routes)for(let r of t.routes){let n={params:r.schema.params?r.schema.params.jsonSchema:void 0,query:r.schema.query?r.schema.query.jsonSchema:void 0,body:r.schema.body?r.schema.body.jsonSchema:void 0,response:r.schema.response?r.schema.response.jsonSchema:void 0};e.addRoute(r.method,r.path,n,r.schema.description||`${r.method} ${r.path}`,r.schema.description,r.schema.tags)}}}}0&&(module.exports={swagger});
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@hedystia/swagger",
3
- "version": "1.2.3",
3
+ "version": "1.2.5",
4
4
  "devDependencies": {
5
5
  "@types/bun": "latest",
6
6
  "tsup": "^8.3.5"
7
7
  },
8
8
  "dependencies": {
9
9
  "@apidevtools/swagger-parser": "^10.1.1",
10
- "hedystia": "^1.2.3",
10
+ "hedystia": "^1.2.5",
11
11
  "openapi-types": "^12.1.3"
12
12
  },
13
13
  "peerDependencies": {
@@ -47,5 +47,6 @@
47
47
  },
48
48
  "type": "commonjs",
49
49
  "types": "./dist/index.d.ts",
50
- "main": "./dist/index.js"
50
+ "main": "./dist/index.js",
51
+ "module": "./dist/index.js"
51
52
  }
package/readme.md CHANGED
@@ -131,10 +131,9 @@ app.use("/swagger", swaggerPlugin.plugin).listen(3000);
131
131
  - ✅ Router Groups & Middleware
132
132
  - ✅ Type-safe Client Generation
133
133
  - ✅ WebSocket Support
134
- - ✅ Standard Schema Compatibility
135
134
 
136
135
  ### Advanced Capabilities
137
- - ✅ Zod Validation - Changed to native validation plugin
136
+ - ✅ Standard Schema Compatibility
138
137
  - ✅ Hooks System (onRequest, onError, etc)
139
138
  - ✅ Macro System for Auth/Rate Limiting
140
139
  - ✅ OpenAPI - Swagger Integration