express-endpoints-collection 1.0.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.
Files changed (3) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +219 -0
  3. package/package.json +34 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 pilotpirxie
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,219 @@
1
+ # express-endpoints-collection
2
+
3
+ ## Description
4
+
5
+ This package provides easy to use helper for creating API endpoints in Express with TypeScript inference, validation and OpenAPI 3 schema out of the box.
6
+
7
+ No need to duplicate OpenAPI definitions in your codebase. Just define your API endpoints and automatically generate OpenAPI 3 schema.
8
+
9
+ You can configure exposed endpoints, request and response schemas, and validation rules.
10
+
11
+ ![output](./img/output1.png)
12
+
13
+ ## Features
14
+
15
+ * Fully typed endpoints (TypeScript hints and checks)
16
+ * Request body
17
+ * Response body
18
+ * Query parameters
19
+ * Path parameters
20
+ * Headers
21
+ * Automatic OpenAPI 3 schema generation
22
+ * Request and response validation using Zod
23
+ * Middleware support
24
+ * Minimal setup
25
+
26
+ ## Installation
27
+
28
+ ```shell
29
+ npm install express-endpoints-collection
30
+
31
+ # or
32
+
33
+ yarn add express-endpoints-collection
34
+
35
+ # or
36
+
37
+ pnpm add express-endpoints-collection
38
+ ```
39
+
40
+ ## Usage
41
+
42
+ ```typescript
43
+ import express, { Express } from "express";
44
+ import bodyParser from "body-parser";
45
+ import { z } from "zod";
46
+ import { EndpointsCollection } from "express-endpoints-collection";
47
+ import { generateOpenAPI } from "express-endpoints-collection/generator";
48
+
49
+ // 1. Create express app
50
+ const app: Express = express();
51
+ app.use(bodyParser.json());
52
+
53
+ // 2. Create endpoints collection, this will store all your endpoints
54
+ const endpointsCollection = new EndpointsCollection();
55
+
56
+ // 3. Add new endpoint
57
+ endpointsCollection.post(
58
+ "/add",
59
+ {
60
+ inputSchema: {
61
+ body: z.object({
62
+ a: z.number(),
63
+ b: z.number(),
64
+ }),
65
+ },
66
+ outputSchema: [
67
+ {
68
+ status: 200,
69
+ body: z.object({
70
+ result: z.number(),
71
+ }),
72
+ },
73
+ ],
74
+ summary: "Add two numbers",
75
+ },
76
+ // 4. req and res are fully typed!
77
+ (req, res) => {
78
+ const { a, b } = req.body;
79
+ res.json({ result: a + b });
80
+ },
81
+ );
82
+
83
+ // 5. Collection creates its own router, to use it just add it to your app
84
+ app.use(endpointsCollection.getRouter());
85
+
86
+ // 6. Expose OpenAPI 3 schema
87
+ app.get("/openapi", (req, res) => {
88
+ res.json(
89
+ generateOpenAPI({
90
+ title: "Minimal demo",
91
+ version: "1.0.0",
92
+ endpoints: endpointsCollection.getEndpoints(),
93
+ }),
94
+ );
95
+ });
96
+
97
+ // 7. Start the server and done!
98
+ app.listen(3000, () => {
99
+ console.info(`Server is running on port http://localhost:3000`);
100
+ });
101
+ ```
102
+
103
+ it will generate OpenAPI 3 definition as follow:
104
+
105
+ ```yaml
106
+ openapi: 3.0.0
107
+ info:
108
+ title: Minimal demo
109
+ version: 1.0.0
110
+ components:
111
+ schemas: {}
112
+ parameters: {}
113
+ paths:
114
+ /add:
115
+ post:
116
+ summary: Add two numbers
117
+ requestBody:
118
+ content:
119
+ application/json:
120
+ schema:
121
+ type: object
122
+ properties:
123
+ a:
124
+ type: number
125
+ b:
126
+ type: number
127
+ required:
128
+ - a
129
+ - b
130
+ responses:
131
+ '200':
132
+ description: Response for status code 200
133
+ content:
134
+ application/json:
135
+ schema:
136
+ type: object
137
+ properties:
138
+ result:
139
+ type: number
140
+ required:
141
+ - result
142
+ ```
143
+
144
+ or as JSON
145
+
146
+ ```json
147
+ {
148
+ "openapi": "3.0.0",
149
+ "info": {
150
+ "title": "Minimal demo",
151
+ "version": "1.0.0"
152
+ },
153
+ "components": {
154
+ "schemas": {},
155
+ "parameters": {}
156
+ },
157
+ "paths": {
158
+ "/add": {
159
+ "post": {
160
+ "summary": "Add two numbers",
161
+ "requestBody": {
162
+ "content": {
163
+ "application/json": {
164
+ "schema": {
165
+ "type": "object",
166
+ "properties": {
167
+ "a": {
168
+ "type": "number"
169
+ },
170
+ "b": {
171
+ "type": "number"
172
+ }
173
+ },
174
+ "required": [
175
+ "a",
176
+ "b"
177
+ ]
178
+ }
179
+ }
180
+ }
181
+ },
182
+ "responses": {
183
+ "200": {
184
+ "description": "Response for status code 200",
185
+ "content": {
186
+ "application/json": {
187
+ "schema": {
188
+ "type": "object",
189
+ "properties": {
190
+ "result": {
191
+ "type": "number"
192
+ }
193
+ },
194
+ "required": [
195
+ "result"
196
+ ]
197
+ }
198
+ }
199
+ }
200
+ }
201
+ }
202
+ }
203
+ }
204
+ }
205
+ }
206
+ ```
207
+
208
+ ## Example
209
+
210
+ ![output](./img/output0.png)
211
+
212
+ Type inference and checks:
213
+
214
+ ![infer0](./img/infer0.png)
215
+
216
+ ![infer1](./img/infer1.png)
217
+
218
+ ### License
219
+ MIT
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "express-endpoints-collection",
3
+ "version": "1.0.0",
4
+ "main": "./dist/index.js",
5
+ "repository": "https://github.com/pilotpirxie/express-endpoints-collection.git",
6
+ "author": "pilotpirxie <10637666+pilotpirxie@users.noreply.github.com>",
7
+ "license": "MIT",
8
+ "types": "./dist/index.d.ts",
9
+ "devDependencies": {
10
+ "@types/express": "^4.17.21",
11
+ "@types/jsonwebtoken": "^9.0.6",
12
+ "@types/node": "^20.14.9",
13
+ "body-parser": "^1.20.2",
14
+ "jsonwebtoken": "^9.0.2",
15
+ "node-cache": "^5.1.2",
16
+ "prettier": "3.3.2",
17
+ "ts-node": "^10.9.2",
18
+ "typescript": "^5.5.2"
19
+ },
20
+ "scripts": {
21
+ "prettier": "prettier --write .",
22
+ "dev": "ts-node ./example/app.ts",
23
+ "minimal": "ts-node ./example/minimal.ts",
24
+ "build": "tsc"
25
+ },
26
+ "dependencies": {
27
+ "@asteasolutions/zod-to-openapi": "^7.1.1",
28
+ "express": "^4.19.2",
29
+ "zod": "^3.23.8"
30
+ },
31
+ "files": [
32
+ "dist"
33
+ ]
34
+ }