@timetac/js-client-library
Advanced tools
Comparing version 0.13.0 to 0.14.0
@@ -14,2 +14,3 @@ "use strict"; | ||
const baseApi_1 = require("../baseApi"); | ||
const responseHandlers = require("../utils/response/responseHandlers"); | ||
class ServerCommunicationEndpoint extends baseApi_1.default { | ||
@@ -23,7 +24,4 @@ constructor() { | ||
this.setAccount(account); | ||
const response = yield this._get(`${this.resourceName}/read`, { withCredentials: false }); | ||
if (response.data.Success && response.data.Results !== undefined) { | ||
return response.data.Results; | ||
} | ||
throw 'Connection problem'; | ||
const response = this._get(`${this.resourceName}/read`, { withCredentials: false }); | ||
return responseHandlers.requiredSingle(response); | ||
}); | ||
@@ -30,0 +28,0 @@ } |
@@ -7,6 +7,2 @@ export declare type ApiResponseOnSuccess<T> = { | ||
}; | ||
export declare type ApiResponseErrorPage = { | ||
Success: true; | ||
Results: undefined; | ||
}; | ||
export declare type ApiResponseOnFailure = { | ||
@@ -26,2 +22,2 @@ Success: false; | ||
}; | ||
export declare type ApiResponse<T> = ApiResponseOnSuccess<T> | ApiResponseOnFailure | ApiResponseErrorPage; | ||
export declare type ApiResponse<T> = ApiResponseOnSuccess<T> | ApiResponseOnFailure; |
@@ -14,2 +14,3 @@ "use strict"; | ||
function handleResponse(axiosResponse) { | ||
var _a, _b; | ||
if (isRawApiResponse(axiosResponse.data) && axiosResponse.data.Success) { | ||
@@ -19,3 +20,10 @@ axiosResponse.data._ignoreTypeGuard = undefined; | ||
} | ||
throw axiosResponse; | ||
const optionalResponse = axiosResponse.data; | ||
throw { | ||
response: optionalResponse, | ||
_plainError: axiosResponse, | ||
message: (_a = optionalResponse === null || optionalResponse === void 0 ? void 0 : optionalResponse.ErrorMessage) !== null && _a !== void 0 ? _a : (axiosResponse.status != 200 ? axiosResponse.statusText : 'Unsuccessful response'), | ||
code: (_b = optionalResponse === null || optionalResponse === void 0 ? void 0 : optionalResponse.Error) !== null && _b !== void 0 ? _b : axiosResponse.status, | ||
stack: new Error().stack, | ||
}; | ||
} | ||
@@ -22,0 +30,0 @@ function isRawApiResponse(response) { |
import { AxiosResponse } from 'axios'; | ||
import { ApiResponse } from './apiResponse'; | ||
import { ApiResponse, ApiResponseOnSuccess } from './apiResponse'; | ||
import { ResourceResponse } from './resourceResponse'; | ||
export declare type RequestPromise<T> = Promise<AxiosResponse<ApiResponse<T>>>; | ||
export declare function toApiResponse<T>(promise: RequestPromise<T>): Promise<import("./apiResponse").ApiResponseErrorPage | import("./apiResponse").ApiResponseOnSuccess<T>>; | ||
export declare function toApiResponse<T>(promise: RequestPromise<T>): Promise<ApiResponseOnSuccess<T>>; | ||
export declare function required<T>(promise: RequestPromise<T[]>): Promise<T>; | ||
export declare function requiredSingle<T>(promise: RequestPromise<T>): Promise<T>; | ||
export declare function optional<T>(promise: RequestPromise<T[]>): Promise<T | undefined>; | ||
export declare function list<T>(promise: RequestPromise<T[]>): Promise<T[]>; | ||
export declare function toResourceResponse<T>(promise: RequestPromise<T[]>): Promise<ResourceResponse<T>>; |
@@ -12,16 +12,36 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toResourceResponse = exports.list = exports.optional = exports.required = exports.toApiResponse = void 0; | ||
exports.toResourceResponse = exports.list = exports.optional = exports.requiredSingle = exports.required = exports.toApiResponse = void 0; | ||
const rawApiResponse_1 = require("./rawApiResponse"); | ||
const resourceResponse_1 = require("./resourceResponse"); | ||
function toApiResponse(promise) { | ||
var _a, _b, _c; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const resolved = yield promise; | ||
if (resolved === undefined) { | ||
throw new Error('Promise resolve is undefined, please contact administrator.'); | ||
let resolved; | ||
try { | ||
resolved = yield promise; | ||
} | ||
catch (e) { | ||
const error = e; | ||
throw { | ||
response: resolved === null || resolved === void 0 ? void 0 : resolved.data, | ||
_plainError: error, | ||
message: error.message, | ||
code: resolved === null || resolved === void 0 ? void 0 : resolved.status, | ||
stack: (_a = error.stack) !== null && _a !== void 0 ? _a : new Error().stack, | ||
}; | ||
} | ||
const apiResponse = resolved.data; | ||
if (apiResponse.Success && apiResponse.Results == null) { | ||
apiResponse.Success = false; | ||
} | ||
if (apiResponse.Success) { | ||
return apiResponse; | ||
} | ||
throw apiResponse; | ||
throw { | ||
response: apiResponse, | ||
_plainError: resolved, | ||
message: (_b = apiResponse.ErrorMessage) !== null && _b !== void 0 ? _b : (resolved.status != 200 ? resolved.statusText : 'Unsuccessful response'), | ||
code: (_c = apiResponse.Error) !== null && _c !== void 0 ? _c : resolved.status, | ||
stack: new Error().stack, | ||
}; | ||
}); | ||
@@ -33,3 +53,3 @@ } | ||
const response = yield toApiResponse(promise); | ||
if (response.Results && response.NumResults > 0) { | ||
if (response.NumResults > 0) { | ||
return response.Results[0]; | ||
@@ -43,6 +63,18 @@ } | ||
exports.required = required; | ||
function requiredSingle(promise) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const response = yield toApiResponse(promise); | ||
if (response.NumResults > 0) { | ||
return response.Results; | ||
} | ||
else { | ||
throw new Error('There are no results.'); | ||
} | ||
}); | ||
} | ||
exports.requiredSingle = requiredSingle; | ||
function optional(promise) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const response = yield toApiResponse(promise); | ||
return (response.Results && response.NumResults > 0 && response.Results[0]) || undefined; | ||
return (response.NumResults > 0 && response.Results[0]) || undefined; | ||
}); | ||
@@ -49,0 +81,0 @@ } |
{ | ||
"name": "@timetac/js-client-library", | ||
"version": "0.13.0", | ||
"version": "0.14.0", | ||
"description": "TimeTac API JS client library", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/TimeTac/js-client-library#readme", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
211273
3108