@xube/kit-request 0.0.21 → 0.0.22
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/constants.d.ts +1 -0
- package/dist/constants.js +4 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/request/headers.d.ts +3 -0
- package/dist/request/headers.js +6 -0
- package/dist/request/request.d.ts +2 -0
- package/dist/request/request.js +33 -0
- package/dist/response/response.d.ts +3 -2
- package/dist/response/response.js +5 -1
- package/package.json +5 -3
- package/src/constants.ts +1 -0
- package/src/index.ts +3 -0
- package/src/request/headers.ts +3 -0
- package/src/request/request.ts +36 -0
- package/src/response/response.ts +50 -47
- package/tsconfig.json +3 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const XUBE_BASE_API_URL = "https://api.xube.io";
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -14,5 +14,8 @@ 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("./constants"), exports);
|
|
17
18
|
__exportStar(require("./helpers"), exports);
|
|
19
|
+
__exportStar(require("./request/headers"), exports);
|
|
20
|
+
__exportStar(require("./request/request"), exports);
|
|
18
21
|
__exportStar(require("./response/response"), exports);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.xubePostRequest = void 0;
|
|
4
|
+
const kit_constants_1 = require("@xube/kit-constants");
|
|
5
|
+
const response_1 = require("../response/response");
|
|
6
|
+
const xubePostRequest = async (body, path, headers) => {
|
|
7
|
+
try {
|
|
8
|
+
const response = await fetch(path, {
|
|
9
|
+
method: "POST",
|
|
10
|
+
headers,
|
|
11
|
+
body: JSON.stringify(body),
|
|
12
|
+
});
|
|
13
|
+
const responseBody = await response.json();
|
|
14
|
+
if (response.ok) {
|
|
15
|
+
console.log("Xube request successful");
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
console.log("Xube Request unsuccessful");
|
|
19
|
+
}
|
|
20
|
+
return new response_1.XubeResponse({
|
|
21
|
+
statusCode: response.status,
|
|
22
|
+
error: responseBody,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
catch (e) {
|
|
26
|
+
console.log("Failed to complete request", e);
|
|
27
|
+
return new response_1.XubeResponse({
|
|
28
|
+
statusCode: kit_constants_1.StatusCode.BadRequest,
|
|
29
|
+
error: "Failed to complete request",
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
exports.xubePostRequest = xubePostRequest;
|
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import { XubeLog } from "@xube/kit-log";
|
|
2
2
|
import { DATA, ERROR, STATUS_CODE, StatusCode } from "@xube/kit-constants";
|
|
3
3
|
export interface XubeResponseProps<TData> {
|
|
4
|
-
[STATUS_CODE]: StatusCode;
|
|
4
|
+
[STATUS_CODE]: StatusCode | number;
|
|
5
5
|
[ERROR]?: string;
|
|
6
6
|
[DATA]?: TData;
|
|
7
7
|
}
|
|
8
8
|
export declare class XubeResponse<TData> implements XubeResponseProps<TData> {
|
|
9
|
-
statusCode: StatusCode;
|
|
9
|
+
statusCode: StatusCode | number;
|
|
10
10
|
error?: string;
|
|
11
11
|
data?: TData;
|
|
12
12
|
constructor(props: XubeResponseProps<TData>);
|
|
13
13
|
isSuccessful: () => boolean;
|
|
14
14
|
hasFailed: () => boolean;
|
|
15
|
+
switchXubeResponseType: <TDATA_IN, TDATA_OUT>() => XubeResponse<TDATA_OUT>;
|
|
15
16
|
}
|
|
16
17
|
export declare const xubeResponseSucceeded: <TData>(xubeResponse: XubeResponse<TData>) => boolean;
|
|
17
18
|
export declare const xubeResponseFailed: <TData>(xubeResponse: XubeResponse<TData>) => boolean;
|
|
@@ -10,11 +10,15 @@ class XubeResponse {
|
|
|
10
10
|
data;
|
|
11
11
|
constructor(props) {
|
|
12
12
|
this.statusCode = props[kit_constants_1.STATUS_CODE];
|
|
13
|
-
this.error =
|
|
13
|
+
this.error =
|
|
14
|
+
props[kit_constants_1.ERROR] && props[kit_constants_1.STATUS_CODE] >= 500
|
|
15
|
+
? (0, helpers_1.suggestSupportRequest)(props[kit_constants_1.ERROR])
|
|
16
|
+
: undefined;
|
|
14
17
|
this.data = props[kit_constants_1.DATA];
|
|
15
18
|
}
|
|
16
19
|
isSuccessful = () => (0, exports.xubeResponseSucceeded)(this);
|
|
17
20
|
hasFailed = () => (0, exports.xubeResponseFailed)(this);
|
|
21
|
+
switchXubeResponseType = () => (0, exports.switchXubeResponseType)(this);
|
|
18
22
|
}
|
|
19
23
|
exports.XubeResponse = XubeResponse;
|
|
20
24
|
const xubeResponseSucceeded = (xubeResponse) => xubeResponse.statusCode <= 300;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xube/kit-request",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.22",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -17,9 +17,11 @@
|
|
|
17
17
|
},
|
|
18
18
|
"homepage": "https://github.com/XubeLtd/data-logger#readme",
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@xube/kit-constants": "^0.0.
|
|
20
|
+
"@xube/kit-constants": "^0.0.22",
|
|
21
|
+
"@xube/kit-generator": "^0.0.22",
|
|
22
|
+
"@xube/kit-log": "^0.0.22"
|
|
21
23
|
},
|
|
22
24
|
"devDependencies": {
|
|
23
|
-
"@xube/kit-build": "^0.0.
|
|
25
|
+
"@xube/kit-build": "^0.0.22"
|
|
24
26
|
}
|
|
25
27
|
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const XUBE_BASE_API_URL = "https://api.xube.io";
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { StatusCode } from "@xube/kit-constants";
|
|
2
|
+
import { XubeResponse } from "../response/response";
|
|
3
|
+
|
|
4
|
+
export const xubePostRequest = async <TResponse = any>(
|
|
5
|
+
body: Record<string, any> | string,
|
|
6
|
+
path: string,
|
|
7
|
+
headers?: Record<string, string>
|
|
8
|
+
): Promise<XubeResponse<TResponse>> => {
|
|
9
|
+
try {
|
|
10
|
+
const response: Response = await fetch(path, {
|
|
11
|
+
method: "POST",
|
|
12
|
+
headers,
|
|
13
|
+
body: JSON.stringify(body),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const responseBody = await response.json();
|
|
17
|
+
|
|
18
|
+
if (response.ok) {
|
|
19
|
+
console.log("Xube request successful");
|
|
20
|
+
} else {
|
|
21
|
+
console.log("Xube Request unsuccessful");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return new XubeResponse({
|
|
25
|
+
statusCode: response.status,
|
|
26
|
+
error: responseBody,
|
|
27
|
+
});
|
|
28
|
+
} catch (e) {
|
|
29
|
+
console.log("Failed to complete request", e);
|
|
30
|
+
|
|
31
|
+
return new XubeResponse({
|
|
32
|
+
statusCode: StatusCode.BadRequest,
|
|
33
|
+
error: "Failed to complete request",
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
};
|
package/src/response/response.ts
CHANGED
|
@@ -1,50 +1,53 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { DATA, ERROR, STATUS_CODE, StatusCode } from "@xube/kit-constants";
|
|
1
|
+
import { XubeLog } from "@xube/kit-log";
|
|
2
|
+
import { DATA, ERROR, STATUS_CODE, StatusCode } from "@xube/kit-constants";
|
|
4
3
|
import { suggestSupportRequest } from "../helpers";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
|
|
5
|
+
export interface XubeResponseProps<TData> {
|
|
6
|
+
[STATUS_CODE]: StatusCode | number;
|
|
7
|
+
[ERROR]?: string;
|
|
8
|
+
[DATA]?: TData;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class XubeResponse<TData> implements XubeResponseProps<TData> {
|
|
12
|
+
public statusCode: StatusCode | number;
|
|
13
|
+
public error?: string;
|
|
14
|
+
public data?: TData;
|
|
15
|
+
|
|
16
|
+
constructor(props: XubeResponseProps<TData>) {
|
|
17
|
+
this.statusCode = props[STATUS_CODE];
|
|
18
|
+
this.error =
|
|
19
|
+
props[ERROR] && props[STATUS_CODE] >= 500
|
|
20
|
+
? suggestSupportRequest(props[ERROR])
|
|
21
|
+
: undefined;
|
|
22
|
+
this.data = props[DATA];
|
|
10
23
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
|
|
25
|
+
isSuccessful = (): boolean => xubeResponseSucceeded(this);
|
|
26
|
+
hasFailed = (): boolean => xubeResponseFailed(this);
|
|
27
|
+
switchXubeResponseType = <TDATA_IN, TDATA_OUT>() =>
|
|
28
|
+
switchXubeResponseType<TDATA_IN, TDATA_OUT>(this);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const xubeResponseSucceeded = <TData>(
|
|
32
|
+
xubeResponse: XubeResponse<TData>
|
|
33
|
+
): boolean => xubeResponse.statusCode <= 300;
|
|
34
|
+
|
|
35
|
+
export const xubeResponseFailed = <TData>(
|
|
36
|
+
xubeResponse: XubeResponse<TData>
|
|
37
|
+
): boolean => !xubeResponseSucceeded(xubeResponse);
|
|
38
|
+
|
|
39
|
+
export const switchXubeResponseType = <TDATA_IN, TDATA_OUT>(
|
|
40
|
+
inXubeResponse: XubeResponse<TDATA_IN>,
|
|
41
|
+
log: XubeLog = XubeLog.getInstance()
|
|
42
|
+
): XubeResponse<TDATA_OUT> => {
|
|
43
|
+
if (inXubeResponse.data) {
|
|
44
|
+
log.warn(
|
|
45
|
+
"Requester Response had data. Removing, but this could be undesired"
|
|
46
|
+
);
|
|
47
|
+
delete inXubeResponse.data;
|
|
25
48
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
)
|
|
30
|
-
|
|
31
|
-
export const xubeResponseFailed = <TData>(
|
|
32
|
-
xubeResponse: XubeResponse<TData>
|
|
33
|
-
): boolean => !xubeResponseSucceeded(xubeResponse);
|
|
34
|
-
|
|
35
|
-
export const switchXubeResponseType = <TDATA_IN, TDATA_OUT>(
|
|
36
|
-
inXubeResponse: XubeResponse<TDATA_IN>,
|
|
37
|
-
log: XubeLog = XubeLog.getInstance()
|
|
38
|
-
): XubeResponse<TDATA_OUT> => {
|
|
39
|
-
if (inXubeResponse.data) {
|
|
40
|
-
log.warn(
|
|
41
|
-
"Requester Response had data. Removing, but this could be undesired"
|
|
42
|
-
);
|
|
43
|
-
delete inXubeResponse.data;
|
|
44
|
-
}
|
|
45
|
-
return new XubeResponse<TDATA_OUT>({
|
|
46
|
-
statusCode: inXubeResponse.statusCode,
|
|
47
|
-
error: inXubeResponse.error,
|
|
48
|
-
});
|
|
49
|
-
};
|
|
50
|
-
|
|
49
|
+
return new XubeResponse<TDATA_OUT>({
|
|
50
|
+
statusCode: inXubeResponse.statusCode,
|
|
51
|
+
error: inXubeResponse.error,
|
|
52
|
+
});
|
|
53
|
+
};
|