@pulsecharterconnect/types 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.
- package/README.md +25 -0
- package/dist/index.js +17 -0
- package/dist/types/ApiResult.js +68 -0
- package/dist/types/index.js +17 -0
- package/package.json +23 -0
- package/src/index.ts +1 -0
- package/src/types/ApiResult.ts +94 -0
- package/src/types/index.ts +1 -0
- package/tsconfig.json +14 -0
package/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Pulse Charter Connect Typescript Library
|
|
2
|
+
|
|
3
|
+
This is a TypeScript library that provides various types and interfaces to enhance type safety in the Pulse Charter Connect Application across the frontend and backend
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
You can install the library via npm:
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
npm install @pulsecharterconnect/types
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
To use the types and interfaces provided by this library, you can import them into your TypeScript files:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { ApiResult } from '@pulsecharterconnect/types';
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## API Reference
|
|
22
|
+
|
|
23
|
+
### Types
|
|
24
|
+
|
|
25
|
+
- **ApiResult**: Used to respond to an HTTP request.
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./types"), exports);
|
|
@@ -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,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./ApiResult"), exports);
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pulsecharterconnect/types",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A TypeScript library for enhanced type safety.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
10
|
+
"prepublishOnly": "npm run build"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"typescript",
|
|
14
|
+
"library",
|
|
15
|
+
"types",
|
|
16
|
+
"interfaces"
|
|
17
|
+
],
|
|
18
|
+
"author": "Your Name",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"typescript": "^4.0.0"
|
|
22
|
+
}
|
|
23
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './types';
|
|
@@ -0,0 +1,94 @@
|
|
|
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>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ApiResult';
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
}
|