@equinor/fusion-framework-module-services
Advanced tools
Comparing version 3.1.0-next.0 to 3.1.0-next.1
@@ -13,16 +13,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import { NotificationApiClient } from './notification'; | ||
export class ApiProviderHttpError extends Error { | ||
export class ApiProviderError extends Error { | ||
constructor(msg, response, options) { | ||
super(msg, options); | ||
this.type = response.type; | ||
this.status = response.status; | ||
this.statusText = response.statusText; | ||
try { | ||
this.json = response.json(); | ||
} | ||
catch (err) { | ||
this.json = Promise.resolve({ err }); | ||
} | ||
this.response = response; | ||
this.name = 'ApiProviderError'; | ||
} | ||
} | ||
const validateResponse = (response) => __awaiter(void 0, void 0, void 0, function* () { | ||
var _a; | ||
if (!response.ok) { | ||
const { headers, status, statusText, type, url, bodyUsed } = response; | ||
const isJson = (_a = headers.get('content-type')) === null || _a === void 0 ? void 0 : _a.match(/application\/(\w*)?[+]?json/); | ||
const data = !bodyUsed && (yield (isJson ? response.json() : response.text())); | ||
throw new ApiProviderError('failed to execute request, response was not ok', { | ||
headers, | ||
status, | ||
statusText, | ||
type, | ||
url, | ||
data, | ||
}); | ||
} | ||
}); | ||
export class ApiProvider { | ||
@@ -35,2 +44,3 @@ constructor({ createClient }) { | ||
const httpClient = yield this._createClientFn('notification'); | ||
httpClient.responseHandler.add('validate_api_request', validateResponse); | ||
return new NotificationApiClient(httpClient, method); | ||
@@ -42,2 +52,3 @@ }); | ||
const httpClient = yield this._createClientFn('bookmarks'); | ||
httpClient.responseHandler.add('validate_api_request', validateResponse); | ||
return new BookmarksApiClient(httpClient, method); | ||
@@ -49,7 +60,3 @@ }); | ||
const httpClient = yield this._createClientFn('context'); | ||
httpClient.responseHandler.add('validate_api_request', (response) => { | ||
if (!response.ok) { | ||
throw new ApiProviderHttpError('ContextApiClient: response was not ok', response); | ||
} | ||
}); | ||
httpClient.responseHandler.add('validate_api_request', validateResponse); | ||
return new ContextApiClient(httpClient, method); | ||
@@ -56,0 +63,0 @@ }); |
@@ -15,8 +15,13 @@ import { IHttpClient } from '@equinor/fusion-framework-module-http'; | ||
}; | ||
export declare class ApiProviderHttpError extends Error { | ||
readonly type: ResponseType; | ||
readonly status: number; | ||
readonly statusText: string; | ||
readonly json?: Promise<unknown>; | ||
constructor(msg: string, response: Response, options?: ErrorOptions); | ||
type ApiProviderErrorResponse = { | ||
type: ResponseType; | ||
status: number; | ||
statusText: string; | ||
headers: Headers; | ||
url: string; | ||
data: unknown; | ||
}; | ||
export declare class ApiProviderError extends Error { | ||
readonly response: ApiProviderErrorResponse; | ||
constructor(msg: string, response: ApiProviderErrorResponse, options?: ErrorOptions); | ||
} | ||
@@ -23,0 +28,0 @@ export declare class ApiProvider<TClient extends IHttpClient = IHttpClient> implements IApiProvider<TClient> { |
{ | ||
"name": "@equinor/fusion-framework-module-services", | ||
"version": "3.1.0-next.0", | ||
"version": "3.1.0-next.1", | ||
"description": "", | ||
@@ -63,3 +63,3 @@ "sideEffects": false, | ||
"@equinor/fusion-framework-module-http": "^5.0.0", | ||
"@equinor/fusion-framework-module-service-discovery": "^7.0.0" | ||
"@equinor/fusion-framework-module-service-discovery": "^7.0.1-next.0" | ||
}, | ||
@@ -70,3 +70,3 @@ "peerDependencies": { | ||
}, | ||
"gitHead": "3a5344ee1e18a35158681004cb154b2522e659ed" | ||
"gitHead": "9d8ce56834f6e3076c93893b1f0e531b2c49dcf3" | ||
} |
@@ -36,21 +36,40 @@ import { IHttpClient } from '@equinor/fusion-framework-module-http'; | ||
export class ApiProviderHttpError extends Error { | ||
readonly type: ResponseType; | ||
readonly status: number; | ||
readonly statusText: string; | ||
readonly json?: Promise<unknown>; | ||
// TODO move to own file | ||
type ApiProviderErrorResponse = { | ||
type: ResponseType; | ||
status: number; | ||
statusText: string; | ||
headers: Headers; | ||
url: string; | ||
data: unknown; | ||
}; | ||
constructor(msg: string, response: Response, options?: ErrorOptions) { | ||
// TODO move to own file | ||
export class ApiProviderError extends Error { | ||
readonly response: ApiProviderErrorResponse; | ||
constructor(msg: string, response: ApiProviderErrorResponse, options?: ErrorOptions) { | ||
super(msg, options); | ||
this.type = response.type; | ||
this.status = response.status; | ||
this.statusText = response.statusText; | ||
try { | ||
this.json = response.json(); | ||
} catch (err) { | ||
this.json = Promise.resolve({ err }); | ||
} | ||
this.response = response; | ||
this.name = 'ApiProviderError'; | ||
} | ||
} | ||
// TODO move to own file | ||
const validateResponse = async (response: Response) => { | ||
if (!response.ok) { | ||
const { headers, status, statusText, type, url, bodyUsed } = response; | ||
const isJson = headers.get('content-type')?.match(/application\/(\w*)?[+]?json/); | ||
const data = !bodyUsed && (await (isJson ? response.json() : response.text())); | ||
throw new ApiProviderError('failed to execute request, response was not ok', { | ||
headers, | ||
status, | ||
statusText, | ||
type, | ||
url, | ||
data, | ||
}); | ||
} | ||
}; | ||
export class ApiProvider<TClient extends IHttpClient = IHttpClient> | ||
@@ -68,2 +87,3 @@ implements IApiProvider<TClient> | ||
const httpClient = await this._createClientFn('notification'); | ||
httpClient.responseHandler.add('validate_api_request', validateResponse); | ||
return new NotificationApiClient(httpClient, method); | ||
@@ -76,2 +96,3 @@ } | ||
const httpClient = await this._createClientFn('bookmarks'); | ||
httpClient.responseHandler.add('validate_api_request', validateResponse); | ||
return new BookmarksApiClient(httpClient, method); | ||
@@ -84,9 +105,5 @@ } | ||
const httpClient = await this._createClientFn('context'); | ||
httpClient.responseHandler.add('validate_api_request', (response) => { | ||
if (!response.ok) { | ||
throw new ApiProviderHttpError('ContextApiClient: response was not ok', response); | ||
} | ||
}); | ||
httpClient.responseHandler.add('validate_api_request', validateResponse); | ||
return new ContextApiClient(httpClient, method); | ||
} | ||
} |
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
438169
4848