@equinor/fusion
Advanced tools
Comparing version 0.1.61 to 0.1.62
@@ -14,2 +14,3 @@ import { MutableRefObject } from "react"; | ||
import AbortControllerManager from "../utils/AbortControllerManager"; | ||
import TasksContainer from "./TasksContainer"; | ||
export declare type Auth = { | ||
@@ -49,2 +50,3 @@ container: IAuthContainer; | ||
contextManager: ContextManager; | ||
tasksContainer: TasksContainer; | ||
abortControllerManager: AbortControllerManager; | ||
@@ -51,0 +53,0 @@ } |
@@ -13,2 +13,3 @@ import { createContext, useContext } from "react"; | ||
import AbortControllerManager from "../utils/AbortControllerManager"; | ||
import TasksContainer from "./TasksContainer"; | ||
export const defaultSettings = { | ||
@@ -44,2 +45,3 @@ componentDisplayType: ComponentDisplayType.Comfortable, | ||
const contextManager = new ContextManager(apiClients, contextId); | ||
const tasksContainer = new TasksContainer(apiClients); | ||
return { | ||
@@ -63,2 +65,3 @@ auth: { container: authContainer }, | ||
contextManager, | ||
tasksContainer, | ||
abortControllerManager, | ||
@@ -65,0 +68,0 @@ }; |
@@ -13,3 +13,3 @@ import TaskType, { TaskTypes } from "./TaskType"; | ||
priority: number; | ||
taskTypeKey: string; | ||
taskTypeKey: TaskTypes; | ||
title: string; | ||
@@ -16,0 +16,0 @@ category: string; |
@@ -10,2 +10,3 @@ import BaseApiClient from "./BaseApiClient"; | ||
setTaskPriorityAsync(id: string, priority: number): Promise<void>; | ||
refreshTasksAsync(type: TaskTypes, refreshRequest: RequestInit): Promise<import("../HttpClient").HttpResponse<Task[]>>; | ||
} |
@@ -28,2 +28,6 @@ import BaseApiClient from "./BaseApiClient"; | ||
} | ||
async refreshTasksAsync(type, refreshRequest) { | ||
const url = this.resourceCollections.tasks.tasks(type); | ||
return this.httpClient.getAsync(url, refreshRequest); | ||
} | ||
} |
@@ -5,3 +5,3 @@ export declare type HttpResponse<T> = { | ||
status: number; | ||
refreshPromise?: Promise<HttpResponse<T>> | null; | ||
refreshRequest: RequestInit | null; | ||
}; |
@@ -19,4 +19,9 @@ import { IAuthContainer } from "../../auth/AuthContainer"; | ||
putAsync<TBody, TResponse, TExpectedErrorResponse>(url: string, body: TBody, init?: RequestInit): Promise<HttpResponse<TResponse>>; | ||
private transformHeaders; | ||
private getRequestInProgress; | ||
private performFetchAsync; | ||
private parseResponseJSONAsync; | ||
private parseResponseStringAsync; | ||
private parseResponseAsync; | ||
private createHttpResponse; | ||
private responseIsRefreshable; | ||
private transformRequestAsync; | ||
private addSessionIdHeader; | ||
@@ -27,9 +32,5 @@ private addAcceptJsonHeader; | ||
private addAbortSignal; | ||
private transformRequestAsync; | ||
private parseResponseJSONAsync; | ||
private parseResponseStringAsync; | ||
private parseResponseAsync; | ||
private responseIsRefreshable; | ||
private performFetchAsync; | ||
private transformHeaders; | ||
private getRequestInProgress; | ||
} | ||
export declare const useHttpClient: () => IHttpClient; |
@@ -18,7 +18,4 @@ import uuid from "uuid/v1"; | ||
const data = JSON.parse(result.data); | ||
return { | ||
data, | ||
headers: result.headers, | ||
status: result.status, | ||
}; | ||
const response = Object.assign({}, result, { data }); | ||
return response; | ||
} | ||
@@ -32,9 +29,9 @@ async getStringAsync(url, init) { | ||
await this.resourceCache.setIsFetchingAsync(url); | ||
init = ensureRequestInit(init, init => (Object.assign({}, init, { method: "GET" }))); | ||
const requestInit = ensureRequestInit(init, init => (Object.assign({}, init, { method: "GET" }))); | ||
try { | ||
const request = this.performFetchAsync(url, init); | ||
const request = this.performFetchAsync(url, requestInit); | ||
this.requestsInProgress[url] = new Promise(async (resolve, reject) => { | ||
try { | ||
const response = await request; | ||
const data = await this.parseResponseStringAsync(response); | ||
const data = await this.parseResponseStringAsync(requestInit, response); | ||
delete this.requestsInProgress[url]; | ||
@@ -58,3 +55,3 @@ await this.resourceCache.updateAsync(url, data); | ||
const response = await this.performFetchAsync(url, init); | ||
return await this.parseResponseAsync(response); | ||
return await this.parseResponseAsync(init, response); | ||
} | ||
@@ -64,39 +61,27 @@ async putAsync(url, body, init) { | ||
const response = await this.performFetchAsync(url, init); | ||
return await this.parseResponseAsync(response); | ||
return await this.parseResponseAsync(init, response); | ||
} | ||
transformHeaders(init, transform) { | ||
const headers = new Headers(init.headers); | ||
transform(headers); | ||
return Object.assign({}, init, { headers }); | ||
} | ||
getRequestInProgress(url) { | ||
return this.requestsInProgress[url]; | ||
} | ||
addSessionIdHeader(init) { | ||
return this.transformHeaders(init, headers => headers.append("X-Session-Id", this.sessionId)); | ||
} | ||
addAcceptJsonHeader(init) { | ||
return this.transformHeaders(init, headers => headers.append("Accept", "application/json")); | ||
} | ||
addRefreshHeader(init) { | ||
return this.transformHeaders(init, headers => headers.append("x-pp-refresh", "true")); | ||
} | ||
async addAuthHeaderAsync(url, init) { | ||
const token = await this.authContainer.acquireTokenAsync(url); | ||
return this.transformHeaders(init, headers => headers.append("Authorization", "Bearer " + token)); | ||
} | ||
addAbortSignal(init) { | ||
const signal = this.abortControllerManager.getCurrentSignal(); | ||
if (signal !== null) { | ||
init.signal = signal; | ||
async performFetchAsync(url, init) { | ||
try { | ||
const options = await this.transformRequestAsync(url, init); | ||
const response = await fetch(url, options); | ||
// TODO: Track dependency with app insight | ||
if (!response.ok) { | ||
// Add more info | ||
const errorResponse = await this.parseResponseJSONAsync(response); | ||
throw new HttpClientRequestFailedError(url, response.status, errorResponse); | ||
} | ||
return response; | ||
} | ||
return init; | ||
catch (error) { | ||
if (error instanceof HttpClientRequestFailedError) { | ||
// TODO: Add to notification center? | ||
// TODO: Update cache status? | ||
throw error; | ||
} | ||
// Add more info | ||
throw error; | ||
} | ||
} | ||
async transformRequestAsync(url, init) { | ||
const requestWithSessionId = this.addSessionIdHeader(init); | ||
const requestWithAcceptJson = this.addAcceptJsonHeader(requestWithSessionId); | ||
const requestWithAuthToken = await this.addAuthHeaderAsync(url, requestWithAcceptJson); | ||
const requestWithAbortSignal = this.addAbortSignal(requestWithAuthToken); | ||
return requestWithAbortSignal; | ||
} | ||
// Response parsers | ||
async parseResponseJSONAsync(response) { | ||
@@ -112,19 +97,24 @@ try { | ||
} | ||
async parseResponseStringAsync(response) { | ||
async parseResponseStringAsync(request, response) { | ||
const data = await response.text(); | ||
// TODO: Update cache status? | ||
return { | ||
data, | ||
status: response.status, | ||
headers: response.headers, | ||
}; | ||
return this.createHttpResponse(request, response, data); | ||
} | ||
async parseResponseAsync(response) { | ||
async parseResponseAsync(request, response) { | ||
const data = await this.parseResponseJSONAsync(response); | ||
// TODO: Update cache status? | ||
return { | ||
return this.createHttpResponse(request, response, data); | ||
} | ||
createHttpResponse(request, response, data) { | ||
const httpResponse = { | ||
data, | ||
status: response.status, | ||
headers: response.headers, | ||
refreshRequest: null, | ||
}; | ||
if (this.responseIsRefreshable(response)) { | ||
const refreshRequest = this.addRefreshHeader(request); | ||
return Object.assign({}, httpResponse, { refreshRequest }); | ||
} | ||
return httpResponse; | ||
} | ||
@@ -134,24 +124,39 @@ responseIsRefreshable(response) { | ||
} | ||
async performFetchAsync(url, init) { | ||
try { | ||
const options = await this.transformRequestAsync(url, init); | ||
const response = await fetch(url, options); | ||
// TODO: Track dependency with app insight | ||
if (!response.ok) { | ||
// Add more info | ||
const errorResponse = await this.parseResponseJSONAsync(response); | ||
throw new HttpClientRequestFailedError(url, response.status, errorResponse); | ||
} | ||
return response; | ||
// Request transformers | ||
async transformRequestAsync(url, init) { | ||
const requestWithSessionId = this.addSessionIdHeader(init); | ||
const requestWithAcceptJson = this.addAcceptJsonHeader(requestWithSessionId); | ||
const requestWithAuthToken = await this.addAuthHeaderAsync(url, requestWithAcceptJson); | ||
const requestWithAbortSignal = this.addAbortSignal(requestWithAuthToken); | ||
return requestWithAbortSignal; | ||
} | ||
addSessionIdHeader(init) { | ||
return this.transformHeaders(init, headers => headers.append("X-Session-Id", this.sessionId)); | ||
} | ||
addAcceptJsonHeader(init) { | ||
return this.transformHeaders(init, headers => headers.append("Accept", "application/json")); | ||
} | ||
addRefreshHeader(init) { | ||
return this.transformHeaders(init, headers => headers.append("x-pp-refresh", "true")); | ||
} | ||
async addAuthHeaderAsync(url, init) { | ||
const token = await this.authContainer.acquireTokenAsync(url); | ||
return this.transformHeaders(init, headers => headers.append("Authorization", "Bearer " + token)); | ||
} | ||
addAbortSignal(init) { | ||
const signal = this.abortControllerManager.getCurrentSignal(); | ||
if (signal !== null) { | ||
init.signal = signal; | ||
} | ||
catch (error) { | ||
if (error instanceof HttpClientRequestFailedError) { | ||
// TODO: Add to notification center? | ||
// TODO: Update cache status? | ||
throw error; | ||
} | ||
// Add more info | ||
throw error; | ||
} | ||
return init; | ||
} | ||
transformHeaders(init, transform) { | ||
const headers = new Headers(init.headers); | ||
transform(headers); | ||
return Object.assign({}, init, { headers }); | ||
} | ||
// Utils | ||
getRequestInProgress(url) { | ||
return this.requestsInProgress[url]; | ||
} | ||
} | ||
@@ -158,0 +163,0 @@ export const useHttpClient = () => { |
@@ -17,3 +17,3 @@ export { IAuthContainer, default as AuthContainer } from "./auth/AuthContainer"; | ||
export { default as useHistory, HistoryContext, IHistoryContext } from "./hooks/useHistory"; | ||
export { default as useTasks, useTaskSourceSystems, useTaskTypes, useTaskPrioritySetter } from "./http/hooks/useTasks"; | ||
export { useTasksContainer, useTasks, useTaskSourceSystems, useTaskTypes, useTaskPrioritySetter } from "./core/TasksContainer"; | ||
export * from "./http/hooks/dataProxy/useHandover"; |
@@ -16,3 +16,3 @@ export { default as AuthContainer } from "./auth/AuthContainer"; | ||
export { default as useHistory, HistoryContext } from "./hooks/useHistory"; | ||
export { default as useTasks, useTaskSourceSystems, useTaskTypes, useTaskPrioritySetter } from "./http/hooks/useTasks"; | ||
export { useTasksContainer, useTasks, useTaskSourceSystems, useTaskTypes, useTaskPrioritySetter } from "./core/TasksContainer"; | ||
export * from "./http/hooks/dataProxy/useHandover"; |
import { Dispatch, SetStateAction } from "react"; | ||
declare type Parameter<T> = T extends (arg: infer T) => any ? T : never; | ||
export declare type Parameter<T> = T extends (arg: infer T) => any ? T : never; | ||
declare type Events = { | ||
@@ -11,3 +11,3 @@ [key: string]: (arg: any) => void; | ||
} | ||
export declare const useEventEmitterValue: <TEvents extends Events, TKey extends keyof TEvents>(emitter: EventEmitter<TEvents>, event: TKey, transform?: (value: Parameter<TEvents[TKey]>) => Parameter<TEvents[TKey]> | null) => [Parameter<TEvents[TKey]> | null, Dispatch<SetStateAction<Parameter<TEvents[TKey]> | null>>]; | ||
export declare const useEventEmitterValue: <TEvents extends Events, TKey extends keyof TEvents, TData = Parameter<TEvents[TKey]>>(emitter: EventEmitter<TEvents>, event: TKey, transform?: (value: TData) => TData | null) => [TData | null, Dispatch<SetStateAction<TData | null>>]; | ||
export {}; |
{ | ||
"name": "@equinor/fusion", | ||
"version": "0.1.61", | ||
"version": "0.1.62", | ||
"description": "Everything a Fusion app needs to communicate with the core", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
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
136584
162
3198
2