express-typed-response 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.
package/Readme.md ADDED
@@ -0,0 +1,19 @@
1
+ # express-typed-response
2
+
3
+ Typed and consistent API responses for Express.js.
4
+
5
+ ## Installation
6
+ npm install express-typed-response
7
+
8
+ ## Usage
9
+
10
+ ```ts
11
+ sendSuccess(res, {
12
+ message: "User created",
13
+ data: user
14
+ });
15
+
16
+ sendError(res, {
17
+ status: 401,
18
+ message: "Unauthorized"
19
+ });
package/dist/index.cjs ADDED
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ sendError: () => sendError,
24
+ sendSuccess: () => sendSuccess
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+
28
+ // src/success.ts
29
+ function sendSuccess(res, options) {
30
+ const response = {
31
+ success: true,
32
+ message: options.message,
33
+ data: options.data,
34
+ meta: options.meta
35
+ };
36
+ return res.status(options.status ?? 200).json(response);
37
+ }
38
+
39
+ // src/error.ts
40
+ function sendError(res, options) {
41
+ const response = {
42
+ success: false,
43
+ message: options.message,
44
+ error: options.error
45
+ };
46
+ return res.status(options.status ?? 500).json(response);
47
+ }
48
+ // Annotate the CommonJS export names for ESM import in node:
49
+ 0 && (module.exports = {
50
+ sendError,
51
+ sendSuccess
52
+ });
@@ -0,0 +1,31 @@
1
+ import { Response } from 'express';
2
+
3
+ interface SuccessResponse<T> {
4
+ success: true;
5
+ message: string;
6
+ data?: T;
7
+ meta?: Record<string, any>;
8
+ }
9
+ interface ErrorResponse {
10
+ success: false;
11
+ message: string;
12
+ error?: any;
13
+ }
14
+ type ExpressResponse = Response;
15
+
16
+ interface SendSuccessOptions<T> {
17
+ status?: number;
18
+ message: string;
19
+ data?: T;
20
+ meta?: Record<string, any>;
21
+ }
22
+ declare function sendSuccess<T>(res: ExpressResponse, options: SendSuccessOptions<T>): ExpressResponse;
23
+
24
+ interface SendErrorOptions {
25
+ status?: number;
26
+ message: string;
27
+ error?: any;
28
+ }
29
+ declare function sendError(res: ExpressResponse, options: SendErrorOptions): ExpressResponse;
30
+
31
+ export { type ErrorResponse, type ExpressResponse, type SuccessResponse, sendError, sendSuccess };
@@ -0,0 +1,31 @@
1
+ import { Response } from 'express';
2
+
3
+ interface SuccessResponse<T> {
4
+ success: true;
5
+ message: string;
6
+ data?: T;
7
+ meta?: Record<string, any>;
8
+ }
9
+ interface ErrorResponse {
10
+ success: false;
11
+ message: string;
12
+ error?: any;
13
+ }
14
+ type ExpressResponse = Response;
15
+
16
+ interface SendSuccessOptions<T> {
17
+ status?: number;
18
+ message: string;
19
+ data?: T;
20
+ meta?: Record<string, any>;
21
+ }
22
+ declare function sendSuccess<T>(res: ExpressResponse, options: SendSuccessOptions<T>): ExpressResponse;
23
+
24
+ interface SendErrorOptions {
25
+ status?: number;
26
+ message: string;
27
+ error?: any;
28
+ }
29
+ declare function sendError(res: ExpressResponse, options: SendErrorOptions): ExpressResponse;
30
+
31
+ export { type ErrorResponse, type ExpressResponse, type SuccessResponse, sendError, sendSuccess };
package/dist/index.js ADDED
@@ -0,0 +1,24 @@
1
+ // src/success.ts
2
+ function sendSuccess(res, options) {
3
+ const response = {
4
+ success: true,
5
+ message: options.message,
6
+ data: options.data,
7
+ meta: options.meta
8
+ };
9
+ return res.status(options.status ?? 200).json(response);
10
+ }
11
+
12
+ // src/error.ts
13
+ function sendError(res, options) {
14
+ const response = {
15
+ success: false,
16
+ message: options.message,
17
+ error: options.error
18
+ };
19
+ return res.status(options.status ?? 500).json(response);
20
+ }
21
+ export {
22
+ sendError,
23
+ sendSuccess
24
+ };
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "express-typed-response",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1",
9
+ "build": "tsup src/index.ts --dts --format esm,cjs"
10
+ },
11
+ "keywords": [],
12
+ "author": "",
13
+ "license": "ISC",
14
+ "type": "module",
15
+ "devDependencies": {
16
+ "@types/express": "^5.0.6",
17
+ "tsup": "^8.5.1",
18
+ "typescript": "^5.9.3"
19
+ }
20
+ }