@pulsecharterconnect/types 0.0.2 → 0.0.3

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,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ApiResultWithError = exports.ApiResultStatus = void 0;
4
+ var ApiResultStatus;
5
+ (function (ApiResultStatus) {
6
+ ApiResultStatus["Success"] = "success";
7
+ ApiResultStatus["Failed"] = "failed";
8
+ })(ApiResultStatus = exports.ApiResultStatus || (exports.ApiResultStatus = {}));
9
+ class ApiResult {
10
+ /** Create a failed API result */
11
+ static failure(message = "", data = undefined) {
12
+ return new ApiResult({
13
+ status: ApiResultStatus.Failed,
14
+ message,
15
+ data
16
+ });
17
+ }
18
+ /** Create a successful API result */
19
+ static success(data = undefined, message = undefined) {
20
+ return new ApiResult({
21
+ status: ApiResultStatus.Success,
22
+ data,
23
+ message
24
+ });
25
+ }
26
+ /** Reconstruct an API result from JSON */
27
+ static fromJson(json) {
28
+ return new ApiResult(json);
29
+ }
30
+ static isApiResult(resultData) {
31
+ if (!resultData)
32
+ return false;
33
+ return resultData.status && (resultData.status === ApiResultStatus.Success) || (resultData.status === ApiResultStatus.Failed);
34
+ }
35
+ constructor(resultJson = {}) {
36
+ this.status = ApiResultStatus.Success;
37
+ Object.assign(this, resultJson);
38
+ }
39
+ get isSuccess() {
40
+ return this.status === ApiResultStatus.Success;
41
+ }
42
+ get isFailure() {
43
+ return this.status === ApiResultStatus.Failed;
44
+ }
45
+ get asJson() {
46
+ return {
47
+ status: this.status,
48
+ data: this.data
49
+ };
50
+ }
51
+ }
52
+ exports.default = ApiResult;
53
+ /**
54
+ * Use this as a base class when the API returns a different data struct for an Error then a Success response.
55
+ */
56
+ class ApiResultWithError extends ApiResult {
57
+ constructor(resultJson = {}, error) {
58
+ super(resultJson);
59
+ this.error = error;
60
+ }
61
+ static failure(message = "", error = undefined) {
62
+ return new ApiResultWithError({
63
+ status: ApiResultStatus.Failed,
64
+ message,
65
+ }, error);
66
+ }
67
+ }
68
+ exports.ApiResultWithError = ApiResultWithError;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -14,4 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./ApiResult"), exports);
17
+ __exportStar(require("./ApiResultResponse"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pulsecharterconnect/types",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "A TypeScript library for enhanced type safety.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -19,5 +19,8 @@
19
19
  "license": "MIT",
20
20
  "devDependencies": {
21
21
  "typescript": "^4.0.0"
22
+ },
23
+ "dependencies": {
24
+ "@types/express": "^5.0.0"
22
25
  }
23
26
  }
@@ -0,0 +1,7 @@
1
+ import { Response } from "express"
2
+ import ApiResult from "../models/ApiResult"
3
+
4
+
5
+ /** alias for an express.js Response that returns an ApiResult with a given type for the data property (TData) */
6
+ type ApiResultResponse<TData = any> = Response<ApiResult<TData>>;
7
+ export default ApiResultResponse;
@@ -1 +1 @@
1
- export * from './ApiResult';
1
+ export * from './ApiResultResponse';
File without changes