@pulsecharterconnect/types 0.0.3 → 0.0.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.
- package/dist/index.d.ts +1 -0
- package/dist/types/ApiResult.d.ts +39 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +1 -1
- package/package.json +4 -1
- package/dist/models/ApiResult.js +0 -68
- package/dist/types/ApiResultResponse.js +0 -2
- package/src/index.ts +0 -1
- package/src/models/ApiResult.ts +0 -94
- package/src/types/ApiResultResponse.ts +0 -7
- package/src/types/index.ts +0 -1
- package/tsconfig.json +0 -14
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './types';
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export declare enum ApiResultStatus {
|
|
2
|
+
Success = "success",
|
|
3
|
+
Failed = "failed"
|
|
4
|
+
}
|
|
5
|
+
export interface IApiResult<T = any> {
|
|
6
|
+
status: ApiResultStatus | `${ApiResultStatus}`;
|
|
7
|
+
data?: T;
|
|
8
|
+
message?: string;
|
|
9
|
+
}
|
|
10
|
+
export default class ApiResult<TData = any> implements IApiResult<TData> {
|
|
11
|
+
/** Create a failed API result */
|
|
12
|
+
static failure<TData = any>(message?: string, data?: TData | undefined): ApiResult<TData>;
|
|
13
|
+
/** Create a successful API result */
|
|
14
|
+
static success<TData = any>(data?: TData | undefined, message?: string | undefined): ApiResult<TData>;
|
|
15
|
+
/** Reconstruct an API result from JSON */
|
|
16
|
+
static fromJson<TData = any>(json: IApiResult<TData>): ApiResult<TData>;
|
|
17
|
+
static isApiResult(resultData: any): resultData is IApiResult;
|
|
18
|
+
status: ApiResultStatus | `${ApiResultStatus}`;
|
|
19
|
+
data?: TData extends object ? {
|
|
20
|
+
[Property in keyof TData]: TData[Property];
|
|
21
|
+
} : TData;
|
|
22
|
+
message?: string;
|
|
23
|
+
protected constructor(resultJson?: Partial<IApiResult<TData>>);
|
|
24
|
+
get isSuccess(): boolean;
|
|
25
|
+
get isFailure(): boolean;
|
|
26
|
+
get asJson(): IApiResult;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Use this as a base class when the API returns a different data struct for an Error then a Success response.
|
|
30
|
+
*/
|
|
31
|
+
export declare class ApiResultWithError<T = any, E = any> extends ApiResult<T> {
|
|
32
|
+
error?: E;
|
|
33
|
+
protected constructor(resultJson: Partial<IApiResult<T>> | undefined, error: E);
|
|
34
|
+
static failure<E = any>(message?: string, error?: E | undefined): ApiResultWithError<any, E | undefined>;
|
|
35
|
+
}
|
|
36
|
+
/** Alias for a promise that resolves to an `ApiResult<T>` */
|
|
37
|
+
export type AsyncApiResult<T = any> = Promise<ApiResult<T>>;
|
|
38
|
+
/** Alias for a promise that resolves to an `ApiResultWithError<T, TErr>` */
|
|
39
|
+
export type AsyncApiResultWithError<T = any, TErr = any> = Promise<ApiResultWithError<T, TErr>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ApiResult';
|
package/dist/types/index.js
CHANGED
|
@@ -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("./
|
|
17
|
+
__exportStar(require("./ApiResult"), exports);
|
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pulsecharterconnect/types",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "A TypeScript library for enhanced type safety.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist/"
|
|
9
|
+
],
|
|
7
10
|
"scripts": {
|
|
8
11
|
"build": "tsc",
|
|
9
12
|
"test": "echo \"Error: no test specified\" && exit 1",
|
package/dist/models/ApiResult.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
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;
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './types';
|
package/src/models/ApiResult.ts
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
export enum ApiResultStatus {
|
|
2
|
-
Success = "success",
|
|
3
|
-
Failed = "failed"
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export interface IApiResult<T = any> {
|
|
7
|
-
status: ApiResultStatus | `${ApiResultStatus}`,
|
|
8
|
-
data?: T,
|
|
9
|
-
message?: string
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export default class ApiResult<TData = any> implements IApiResult<TData> {
|
|
13
|
-
|
|
14
|
-
/** Create a failed API result */
|
|
15
|
-
static failure<TData = any>(message: string = "", data: TData | undefined = undefined) {
|
|
16
|
-
return new ApiResult({
|
|
17
|
-
status: ApiResultStatus.Failed,
|
|
18
|
-
message,
|
|
19
|
-
data
|
|
20
|
-
})
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/** Create a successful API result */
|
|
24
|
-
static success<TData = any>(data: TData | undefined = undefined, message: string | undefined = undefined) {
|
|
25
|
-
return new ApiResult({
|
|
26
|
-
status: ApiResultStatus.Success,
|
|
27
|
-
data,
|
|
28
|
-
message
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** Reconstruct an API result from JSON */
|
|
33
|
-
static fromJson<TData = any>(json: IApiResult<TData>): ApiResult<TData> {
|
|
34
|
-
return new ApiResult(json);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
static isApiResult(resultData: any): resultData is IApiResult {
|
|
38
|
-
if (!resultData) return false;
|
|
39
|
-
|
|
40
|
-
return resultData.status && (resultData.status === ApiResultStatus.Success) || (resultData.status === ApiResultStatus.Failed);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
status: ApiResultStatus | `${ApiResultStatus}`;
|
|
44
|
-
data?: TData extends object ? { [Property in keyof TData]: TData[Property] } : TData;
|
|
45
|
-
message?: string;
|
|
46
|
-
|
|
47
|
-
protected constructor(resultJson: Partial<IApiResult<TData>> = {}) {
|
|
48
|
-
this.status = ApiResultStatus.Success;
|
|
49
|
-
Object.assign(this, resultJson)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
get isSuccess() {
|
|
53
|
-
return this.status === ApiResultStatus.Success;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
get isFailure() {
|
|
57
|
-
return this.status === ApiResultStatus.Failed;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
get asJson(): IApiResult {
|
|
61
|
-
return {
|
|
62
|
-
status: this.status,
|
|
63
|
-
data: this.data
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Use this as a base class when the API returns a different data struct for an Error then a Success response.
|
|
70
|
-
*/
|
|
71
|
-
export class ApiResultWithError<T = any, E = any> extends ApiResult<T> {
|
|
72
|
-
error?: E
|
|
73
|
-
protected constructor(resultJson: Partial<IApiResult<T>> = {}, error: E) {
|
|
74
|
-
super(resultJson);
|
|
75
|
-
this.error = error;
|
|
76
|
-
}
|
|
77
|
-
static override failure<E = any>(message: string = "", error: E | undefined = undefined) {
|
|
78
|
-
return new ApiResultWithError({
|
|
79
|
-
status: ApiResultStatus.Failed,
|
|
80
|
-
message,
|
|
81
|
-
}, error)
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
// #######################
|
|
87
|
-
// # Convenience Types
|
|
88
|
-
// #######################
|
|
89
|
-
|
|
90
|
-
/** Alias for a promise that resolves to an `ApiResult<T>` */
|
|
91
|
-
export type AsyncApiResult<T = any> = Promise<ApiResult<T>>;
|
|
92
|
-
|
|
93
|
-
/** Alias for a promise that resolves to an `ApiResultWithError<T, TErr>` */
|
|
94
|
-
export type AsyncApiResultWithError<T = any, TErr = any> = Promise<ApiResultWithError<T, TErr>>;
|
|
@@ -1,7 +0,0 @@
|
|
|
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;
|
package/src/types/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './ApiResultResponse';
|
package/tsconfig.json
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES6",
|
|
4
|
-
"module": "commonjs",
|
|
5
|
-
"strict": true,
|
|
6
|
-
"esModuleInterop": true,
|
|
7
|
-
"skipLibCheck": true,
|
|
8
|
-
"forceConsistentCasingInFileNames": true,
|
|
9
|
-
"outDir": "./dist",
|
|
10
|
-
"rootDir": "./src"
|
|
11
|
-
},
|
|
12
|
-
"include": ["src/**/*"],
|
|
13
|
-
"exclude": ["node_modules", "**/*.spec.ts"]
|
|
14
|
-
}
|