@pogodisco/response 0.0.1

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,2 @@
1
+ export * from "./types/response";
2
+ export * from "./utils/response";
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./types/response";
2
+ export * from "./utils/response";
@@ -0,0 +1,23 @@
1
+ export interface BaseResponse {
2
+ ok: boolean;
3
+ message: string;
4
+ status: number;
5
+ level: ResponseLevel;
6
+ }
7
+ export declare enum ResponseLevel {
8
+ SUCCESS = "success",
9
+ WARNING = "warning",
10
+ ERROR = "error"
11
+ }
12
+ export interface SuccessResponse<T> extends BaseResponse {
13
+ ok: true;
14
+ level: ResponseLevel.SUCCESS;
15
+ data: T;
16
+ }
17
+ export interface FailureResponse extends BaseResponse {
18
+ ok: false;
19
+ level: ResponseLevel.WARNING | ResponseLevel.ERROR;
20
+ error: string;
21
+ errors: Record<string, string[]> | null;
22
+ }
23
+ export type TResponse<T> = SuccessResponse<T> | FailureResponse;
@@ -0,0 +1,6 @@
1
+ export var ResponseLevel;
2
+ (function (ResponseLevel) {
3
+ ResponseLevel["SUCCESS"] = "success";
4
+ ResponseLevel["WARNING"] = "warning";
5
+ ResponseLevel["ERROR"] = "error";
6
+ })(ResponseLevel || (ResponseLevel = {}));
@@ -0,0 +1,7 @@
1
+ import { type BaseResponse, type FailureResponse, type SuccessResponse, TResponse } from "../types/response";
2
+ export declare function newSuccessResponse<T>(data: T, opts?: Partial<Pick<BaseResponse, "message" | "status">>): SuccessResponse<T>;
3
+ export declare function newFailureResponse(error: string, opts?: Partial<Pick<FailureResponse, "message" | "status" | "level" | "errors">>): FailureResponse;
4
+ export declare function isFailure<T>(resp: TResponse<T>): resp is FailureResponse;
5
+ export declare function isSuccess<T>(resp: TResponse<T>): resp is SuccessResponse<T>;
6
+ export declare function assertResponse<T>(res: TResponse<T>, msg?: string): T;
7
+ export declare function withResponse<Args extends any[], Return>(fn: (...args: Args) => Promise<Return> | Return, validate?: (result: Return) => boolean, failureMessage?: string): (...args: Args) => Promise<TResponse<Return>>;
@@ -0,0 +1,44 @@
1
+ import { ResponseLevel, } from "../types/response";
2
+ export function newSuccessResponse(data, opts) {
3
+ const { message = "", status = 200 } = opts ?? {};
4
+ return { ok: true, data, message, status, level: ResponseLevel.SUCCESS };
5
+ }
6
+ export function newFailureResponse(error, opts) {
7
+ const { message = "", errors = null, status = 400, level = ResponseLevel.ERROR, } = opts ?? {};
8
+ return { ok: false, error, errors, message, status, level };
9
+ }
10
+ export function isFailure(resp) {
11
+ return !resp.ok;
12
+ }
13
+ export function isSuccess(resp) {
14
+ return resp.ok;
15
+ }
16
+ export function assertResponse(res, msg) {
17
+ if (isFailure(res))
18
+ throw new Error(res.error || msg || "Unknown error");
19
+ return res.data;
20
+ }
21
+ export function withResponse(fn, validate, failureMessage) {
22
+ return async (...args) => {
23
+ try {
24
+ const result = await fn(...args);
25
+ const isOk = validate ? validate(result) : true;
26
+ return isOk
27
+ ? newSuccessResponse(result)
28
+ : newFailureResponse(failureMessage || "Operation failed", {
29
+ status: 400,
30
+ });
31
+ }
32
+ catch (err) {
33
+ if (typeof err === "object" && err !== null) {
34
+ if ("ok" in err && err.ok === false && "error" in err) {
35
+ return err;
36
+ }
37
+ return newFailureResponse(err.message || "Unknown error", {
38
+ ...err,
39
+ });
40
+ }
41
+ return newFailureResponse(err?.toString() || "Unknown error");
42
+ }
43
+ };
44
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@pogodisco/response",
3
+ "version": "0.0.1",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "author": "Jakub Bystroński",
8
+ "description": "unified response type across consumers",
9
+ "keywords": [
10
+ "typescript"
11
+ ],
12
+ "license": "MIT",
13
+ "type": "module",
14
+ "main": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "import": "./dist/index.js",
19
+ "require": "./dist/index.cjs",
20
+ "types": "./dist/index.d.ts"
21
+ }
22
+ },
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "scripts": {
27
+ "build": "tsc",
28
+ "prepublishOnly": "npm run build"
29
+ },
30
+ "peerDependencies": {
31
+ "typescript": "^5.0.0"
32
+ }
33
+ }