@timetac/js-client-library
Advanced tools
Comparing version 0.10.0 to 0.11.0
@@ -23,4 +23,5 @@ import { AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
onTokenRefreshedCallback?: onTokenRefreshedCallback; | ||
onTokenRefreshedFailed?: () => void; | ||
autoRefreshToken?: boolean; | ||
onTokenRefreshFailed?: () => void; | ||
shouldAutoRefreshToken?: boolean; | ||
timeout?: number; | ||
}; | ||
@@ -27,0 +28,0 @@ export default abstract class BaseApi { |
@@ -1,9 +0,5 @@ | ||
export declare enum ErrorReason { | ||
Unkown = "Unknow Error", | ||
ReponseFailed = "Response Failed" | ||
} | ||
export declare type TimeTacApiError = { | ||
reason: ErrorReason; | ||
response?: unknown; | ||
_plainError?: string; | ||
export declare type ErrorFormat = { | ||
statusCode?: number; | ||
message?: string; | ||
raw: unknown; | ||
}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ErrorReason = void 0; | ||
var ErrorReason; | ||
(function (ErrorReason) { | ||
ErrorReason["Unkown"] = "Unknow Error"; | ||
ErrorReason["ReponseFailed"] = "Response Failed"; | ||
})(ErrorReason = exports.ErrorReason || (exports.ErrorReason = {})); | ||
//# sourceMappingURL=index.js.map |
@@ -37,3 +37,3 @@ import { AbsenceDaysEndpoint } from './absenceDays'; | ||
export * from './enums'; | ||
export * from './errors'; | ||
export { ErrorFormat } from './errors'; | ||
export { FavouriteTask, FavouriteTaskCreate } from './favouriteTasks/types'; | ||
@@ -61,2 +61,3 @@ export { GeneralSetting } from './generalSettings/types'; | ||
export { RequestParams, RequestParamsBuilder } from './utils/params/requestParams'; | ||
export { ApiResponse, ApiResponseOnFailure, ApiResponseOnSuccess } from './utils/response/apiResponse'; | ||
export { DeltaSyncResponse } from './utils/response/deltaSyncResponse'; | ||
@@ -97,3 +98,4 @@ export { RawApiResponse } from './utils/response/rawApiResponse'; | ||
setBaseUrl(): void; | ||
setTimeout(timeout: number): void; | ||
setAccount(account: string): void; | ||
} |
@@ -42,3 +42,2 @@ "use strict"; | ||
__exportStar(require("./enums"), exports); | ||
__exportStar(require("./errors"), exports); | ||
var axiosSetup_2 = require("./utils/axiosSetup"); | ||
@@ -53,5 +52,6 @@ Object.defineProperty(exports, "testAxiosObject", { enumerable: true, get: function () { return axiosSetup_2.testAxiosObject; } }); | ||
constructor(config) { | ||
var _a, _b; | ||
this.config = new utils_1.ConfigProvider(Object.assign(Object.assign({}, config), { autoRefreshToken: (_a = config.autoRefreshToken) !== null && _a !== void 0 ? _a : true, https: (_b = config.https) !== null && _b !== void 0 ? _b : true })); | ||
var _a, _b, _c; | ||
this.config = new utils_1.ConfigProvider(Object.assign(Object.assign({}, config), { shouldAutoRefreshToken: (_a = config.shouldAutoRefreshToken) !== null && _a !== void 0 ? _a : true, https: (_b = config.https) !== null && _b !== void 0 ? _b : true })); | ||
this.setBaseUrl(); | ||
this.setTimeout((_c = config.timeout) !== null && _c !== void 0 ? _c : 30000); | ||
this.state = { | ||
@@ -85,3 +85,3 @@ refreshingToken: false, | ||
this.timezones = new timezones_1.TimezonesEndpoint(this.config); | ||
axiosSetup_1.interceptor({ state: this.state, config: this.config, authentication: this.authentication }); | ||
axiosSetup_1.useInterceptors({ state: this.state, config: this.config, authentication: this.authentication }); | ||
} | ||
@@ -94,2 +94,7 @@ setBaseUrl() { | ||
} | ||
setTimeout(timeout) { | ||
axiosSetup_1.setAxiosDefaults({ | ||
timeout: timeout, | ||
}); | ||
} | ||
setAccount(account) { | ||
@@ -96,0 +101,0 @@ this.config.settings.account = account; |
@@ -1,6 +0,6 @@ | ||
import { AxiosRequestConfig } from 'axios'; | ||
import { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
import { AuthenticationEndpoint } from '../authentication'; | ||
import { ApiState } from '../baseApi'; | ||
import { ConfigProvider } from '.'; | ||
declare type interceptorParams = { | ||
export declare type InterceptorParams = { | ||
state: ApiState; | ||
@@ -10,5 +10,5 @@ config: ConfigProvider; | ||
}; | ||
export declare const interceptor: (apiInstanceData: interceptorParams) => void; | ||
export declare const createResponseRejectedInterceptor: (interceptorParams: InterceptorParams) => (error: AxiosError) => Promise<AxiosResponse<any>>; | ||
export declare const useInterceptors: (interceptorParams: InterceptorParams) => void; | ||
export declare const setAxiosDefaults: (defaults: AxiosRequestConfig) => void; | ||
export declare const testAxiosObject: import("axios").AxiosStatic; | ||
export {}; |
@@ -12,41 +12,66 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.testAxiosObject = exports.setAxiosDefaults = exports.interceptor = void 0; | ||
exports.testAxiosObject = exports.setAxiosDefaults = exports.useInterceptors = exports.createResponseRejectedInterceptor = void 0; | ||
const axios_1 = require("axios"); | ||
const interceptor = (apiInstanceData) => { | ||
axios_1.default.interceptors.response.use((res) => { | ||
return res; | ||
}, (error) => __awaiter(void 0, void 0, void 0, function* () { | ||
if (apiInstanceData.config.settings.autoRefreshToken && error.response) { | ||
const untouchedRequest = error.config; | ||
if (error.response.status === 497 && untouchedRequest.url.includes('auth/oauth2/token')) { | ||
if (apiInstanceData.config.settings.onTokenRefreshedFailed) { | ||
apiInstanceData.config.settings.onTokenRefreshedFailed(); | ||
const requestInterceptor = (config) => { | ||
config.timeout = axios_1.default.defaults.timeout; | ||
return config; | ||
}; | ||
const responseFulfilledInterceptor = (res) => res; | ||
const createResponseRejectedInterceptor = (interceptorParams) => (error) => __awaiter(void 0, void 0, void 0, function* () { | ||
var _a, _b, _c; | ||
if (interceptorParams.config.settings.shouldAutoRefreshToken && error.response) { | ||
const untouchedRequest = error.config; | ||
if (error.response.status === 401 && !untouchedRequest._shouldRetry && !((_a = error.response.config.url) === null || _a === void 0 ? void 0 : _a.includes('oauth2'))) { | ||
untouchedRequest._shouldRetry = true; | ||
if (!interceptorParams.state.refreshingToken) { | ||
interceptorParams.state.refreshingToken = interceptorParams.authentication.refreshToken(); | ||
} | ||
let res; | ||
try { | ||
res = yield interceptorParams.state.refreshingToken; | ||
} | ||
catch (error) { | ||
const status = (_c = (_b = error.raw) === null || _b === void 0 ? void 0 : _b.response) === null || _c === void 0 ? void 0 : _c.status; | ||
if (status === 497) { | ||
if (interceptorParams.config.settings.onTokenRefreshFailed != null) { | ||
interceptorParams.config.settings.onTokenRefreshFailed(); | ||
} | ||
} | ||
throw error; | ||
} | ||
if (error.response.status === 401 && !untouchedRequest._retry) { | ||
untouchedRequest._retry = true; | ||
if (!apiInstanceData.state.refreshingToken) { | ||
apiInstanceData.state.refreshingToken = apiInstanceData.authentication.refreshToken(); | ||
interceptorParams.state.refreshingToken = false; | ||
if (res.status === 200 && res.data.access_token) { | ||
const { access_token: accessToken, refresh_token: refreshToken } = res.data; | ||
interceptorParams.authentication.setTokens({ accessToken, refreshToken }); | ||
untouchedRequest.headers.Authorization = `Bearer ${accessToken}`; | ||
if (interceptorParams.config.settings.onTokenRefreshedCallback) { | ||
interceptorParams.config.settings.onTokenRefreshedCallback({ accessToken, refreshToken }); | ||
} | ||
const res = yield apiInstanceData.state.refreshingToken; | ||
apiInstanceData.state.refreshingToken = false; | ||
if (res.status === 200 && res.data.access_token) { | ||
const { access_token: accessToken, refresh_token: refreshToken } = res.data; | ||
apiInstanceData.authentication.setTokens({ accessToken, refreshToken }); | ||
untouchedRequest.headers.Authorization = `Bearer ${accessToken}`; | ||
if (apiInstanceData.config.settings.onTokenRefreshedCallback) { | ||
apiInstanceData.config.settings.onTokenRefreshedCallback({ accessToken, refreshToken }); | ||
} | ||
return axios_1.default(untouchedRequest); | ||
} | ||
else if (apiInstanceData.config.settings.onTokenRefreshedFailed) { | ||
apiInstanceData.config.settings.onTokenRefreshedFailed(); | ||
} | ||
return axios_1.default(untouchedRequest); | ||
} | ||
} | ||
throw error.response || error; | ||
})); | ||
} | ||
let toThrow; | ||
if (error.code != null && error.code.length > 0) { | ||
toThrow = { | ||
statusCode: parseInt(error.code), | ||
message: error.message, | ||
raw: error, | ||
}; | ||
} | ||
else { | ||
toThrow = { | ||
statusCode: undefined, | ||
message: error.message, | ||
raw: error, | ||
}; | ||
} | ||
throw toThrow; | ||
}); | ||
exports.createResponseRejectedInterceptor = createResponseRejectedInterceptor; | ||
const useInterceptors = (interceptorParams) => { | ||
axios_1.default.interceptors.request.use(requestInterceptor); | ||
axios_1.default.interceptors.response.use(responseFulfilledInterceptor, exports.createResponseRejectedInterceptor(interceptorParams)); | ||
}; | ||
exports.interceptor = interceptor; | ||
exports.useInterceptors = useInterceptors; | ||
const setAxiosDefaults = (defaults) => { | ||
@@ -53,0 +78,0 @@ axios_1.default.defaults = Object.assign(Object.assign({}, axios_1.default.defaults), defaults); |
@@ -14,2 +14,3 @@ export declare type ApiResponseOnSuccess<T> = { | ||
RequestStartTime: string; | ||
Error?: number; | ||
ErrorMessage: string; | ||
@@ -16,0 +17,0 @@ ErrorExtended?: { |
@@ -13,3 +13,2 @@ "use strict"; | ||
exports.createRawApiResponse = void 0; | ||
const index_1 = require("../../errors/index"); | ||
function handleResponse(axiosResponse) { | ||
@@ -22,17 +21,2 @@ if (isRawApiResponse(axiosResponse.data) && axiosResponse.data.Success) { | ||
} | ||
function handleError(error) { | ||
var _a; | ||
const apiResponseError = { | ||
reason: index_1.ErrorReason.ReponseFailed, | ||
_plainError: JSON.stringify(error), | ||
response: undefined, | ||
}; | ||
if ('data' in error) { | ||
apiResponseError.response = error.data; | ||
} | ||
if ('response' in error) { | ||
apiResponseError.response = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data; | ||
} | ||
return Promise.reject(apiResponseError); | ||
} | ||
function isRawApiResponse(response) { | ||
@@ -49,3 +33,3 @@ const hasHost = 'Host' in response; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return promise.then(handleResponse).catch(handleError); | ||
return promise.then(handleResponse); | ||
}); | ||
@@ -52,0 +36,0 @@ } |
@@ -7,3 +7,3 @@ "use strict"; | ||
return { | ||
success: (_a = rawApiResponse.Success) !== null && _a !== void 0 ? _a : false, | ||
success: (_a = rawApiResponse === null || rawApiResponse === void 0 ? void 0 : rawApiResponse.Success) !== null && _a !== void 0 ? _a : false, | ||
apiResponse: rawApiResponse, | ||
@@ -10,0 +10,0 @@ results: (_b = rawApiResponse.Results) !== null && _b !== void 0 ? _b : [], |
{ | ||
"name": "@timetac/js-client-library", | ||
"version": "0.10.0", | ||
"version": "0.11.0", | ||
"description": "TimeTac API JS client library", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/TimeTac/js-client-library#readme", |
@@ -51,3 +51,3 @@ # TimeTac Client Library | ||
//Callback called when refresh of the token fails. | ||
onTokenRefreshedFailed: () => { console.log('Intended action, such as logout')}, | ||
onTokenRefreshFailed: () => { console.log('Intended action, such as logout')}, | ||
//If true, it tries to refresh token on failed request. Default true. | ||
@@ -54,0 +54,0 @@ autoRefreshToken: <BOOLEAN> |
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
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
206644
3038