From 3b0011c6f2312bd6517cf470a80b921bd65e86fe Mon Sep 17 00:00:00 2001 From: Michal Klobukowski Date: Mon, 13 Aug 2018 09:04:30 +0200 Subject: [PATCH] Remvoe static create method, update webdav methods signatures, change Config props name case Feature #13989 Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski --- src/common/config.ts | 11 +++--- src/common/webdav.test.ts | 40 ++++++++++---------- src/common/webdav.ts | 58 ++++++++++------------------- src/services/services.ts | 8 ++-- src/store/auth/auth-actions.test.ts | 4 +- src/store/auth/auth-reducer.test.ts | 2 +- 6 files changed, 53 insertions(+), 70 deletions(-) diff --git a/src/common/config.ts b/src/common/config.ts index 5391927e..b898d2b1 100644 --- a/src/common/config.ts +++ b/src/common/config.ts @@ -7,8 +7,8 @@ import Axios from "../../node_modules/axios"; export const CONFIG_URL = process.env.REACT_APP_ARVADOS_CONFIG_URL || "/config.json"; export interface Config { - API_HOST: string; - KEEP_WEB_HOST: string; + apiHost: string; + keepWebHost: string; } export const fetchConfig = () => { @@ -21,12 +21,13 @@ export const fetchConfig = () => { const mapConfig = (config: Config): Config => ({ ...config, - API_HOST: addProtocol(config.API_HOST) + apiHost: addProtocol(config.apiHost), + keepWebHost: addProtocol(config.keepWebHost) }); const getDefaultConfig = (): Config => ({ - API_HOST: process.env.REACT_APP_ARVADOS_API_HOST || "", - KEEP_WEB_HOST: process.env.REACT_APP_ARVADOS_KEEP_WEB_HOST || "" + apiHost: process.env.REACT_APP_ARVADOS_API_HOST || "", + keepWebHost: process.env.REACT_APP_ARVADOS_KEEP_WEB_HOST || "" }); const addProtocol = (url: string) => `${window.location.protocol}//${url}`; diff --git a/src/common/webdav.test.ts b/src/common/webdav.test.ts index 455cef18..d96465ba 100644 --- a/src/common/webdav.test.ts +++ b/src/common/webdav.test.ts @@ -7,7 +7,7 @@ import { WebDAV } from "./webdav"; describe('WebDAV', () => { it('makes use of provided config', async () => { const { open, load, setRequestHeader, createRequest } = mockCreateRequest(); - const webdav = WebDAV.create({ baseUrl: 'http://foo.com/', headers: { Authorization: 'Basic' } }, createRequest); + const webdav = new WebDAV({ baseURL: 'http://foo.com/', headers: { Authorization: 'Basic' } }, createRequest); const promise = webdav.propfind('foo'); load(); const request = await promise; @@ -18,8 +18,8 @@ describe('WebDAV', () => { it('allows to modify defaults after instantiation', async () => { const { open, load, setRequestHeader, createRequest } = mockCreateRequest(); - const webdav = WebDAV.create(undefined, createRequest); - webdav.defaults.baseUrl = 'http://foo.com/'; + const webdav = new WebDAV(undefined, createRequest); + webdav.defaults.baseURL = 'http://foo.com/'; webdav.defaults.headers = { Authorization: 'Basic' }; const promise = webdav.propfind('foo'); load(); @@ -31,7 +31,7 @@ describe('WebDAV', () => { it('PROPFIND', async () => { const { open, load, createRequest } = mockCreateRequest(); - const webdav = WebDAV.create(undefined, createRequest); + const webdav = new WebDAV(undefined, createRequest); const promise = webdav.propfind('foo'); load(); const request = await promise; @@ -42,8 +42,8 @@ describe('WebDAV', () => { it('PUT', async () => { const { open, send, load, progress, createRequest } = mockCreateRequest(); const onProgress = jest.fn(); - const webdav = WebDAV.create(undefined, createRequest); - const promise = webdav.put('foo', { data: 'Test data', onProgress }); + const webdav = new WebDAV(undefined, createRequest); + const promise = webdav.put('foo', 'Test data', { onProgress }); progress(); load(); const request = await promise; @@ -55,20 +55,20 @@ describe('WebDAV', () => { it('COPY', async () => { const { open, setRequestHeader, load, createRequest } = mockCreateRequest(); - const webdav = WebDAV.create(undefined, createRequest); - const promise = webdav.copy('foo', { destination: 'foo-copy' }); + const webdav = new WebDAV(undefined, createRequest); + const promise = webdav.copy('foo', 'foo-copy'); load(); const request = await promise; expect(open).toHaveBeenCalledWith('COPY', 'foo'); expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'foo-copy'); expect(request).toBeInstanceOf(XMLHttpRequest); }); - - it('COPY - adds baseUrl to Destination header', async () => { + + it('COPY - adds baseURL to Destination header', async () => { const { open, setRequestHeader, load, createRequest } = mockCreateRequest(); - const webdav = WebDAV.create(undefined, createRequest); - webdav.defaults.baseUrl = 'base/'; - const promise = webdav.copy('foo', { destination: 'foo-copy' }); + const webdav = new WebDAV(undefined, createRequest); + webdav.defaults.baseURL = 'base/'; + const promise = webdav.copy('foo', 'foo-copy'); load(); const request = await promise; expect(open).toHaveBeenCalledWith('COPY', 'base/foo'); @@ -78,8 +78,8 @@ describe('WebDAV', () => { it('MOVE', async () => { const { open, setRequestHeader, load, createRequest } = mockCreateRequest(); - const webdav = WebDAV.create(undefined, createRequest); - const promise = webdav.move('foo', { destination: 'foo-copy' }); + const webdav = new WebDAV(undefined, createRequest); + const promise = webdav.move('foo', 'foo-copy'); load(); const request = await promise; expect(open).toHaveBeenCalledWith('MOVE', 'foo'); @@ -87,11 +87,11 @@ describe('WebDAV', () => { expect(request).toBeInstanceOf(XMLHttpRequest); }); - it('MOVE - adds baseUrl to Destination header', async () => { + it('MOVE - adds baseURL to Destination header', async () => { const { open, setRequestHeader, load, createRequest } = mockCreateRequest(); - const webdav = WebDAV.create(undefined, createRequest); - webdav.defaults.baseUrl = 'base/'; - const promise = webdav.move('foo', { destination: 'foo-moved' }); + const webdav = new WebDAV(undefined, createRequest); + webdav.defaults.baseURL = 'base/'; + const promise = webdav.move('foo', 'foo-moved'); load(); const request = await promise; expect(open).toHaveBeenCalledWith('MOVE', 'base/foo'); @@ -101,7 +101,7 @@ describe('WebDAV', () => { it('DELETE', async () => { const { open, load, createRequest } = mockCreateRequest(); - const webdav = WebDAV.create(undefined, createRequest); + const webdav = new WebDAV(undefined, createRequest); const promise = webdav.delete('foo'); load(); const request = await promise; diff --git a/src/common/webdav.ts b/src/common/webdav.ts index 6b56f124..671c0e36 100644 --- a/src/common/webdav.ts +++ b/src/common/webdav.ts @@ -3,57 +3,56 @@ // SPDX-License-Identifier: AGPL-3.0 export class WebDAV { - static create(config?: Partial, createRequest?: () => XMLHttpRequest) { - return new WebDAV(config, createRequest); - } defaults: WebDAVDefaults = { - baseUrl: '', + baseURL: '', headers: {}, }; - propfind = (url: string, config: PropfindConfig = {}) => + constructor(config?: Partial, private createRequest = () => new XMLHttpRequest()) { + if (config) { + this.defaults = { ...this.defaults, ...config }; + } + } + + propfind = (url: string, config: WebDAVRequestConfig = {}) => this.request({ ...config, url, method: 'PROPFIND' }) - put = (url: string, config: PutConfig = {}) => + put = (url: string, data?: any, config: WebDAVRequestConfig = {}) => this.request({ ...config, url, - method: 'PUT' + method: 'PUT', + data, }) - copy = (url: string, { destination, ...config }: CopyConfig) => + copy = (url: string, destination: string, config: WebDAVRequestConfig = {}) => this.request({ ...config, url, method: 'COPY', - headers: { ...config.headers, Destination: this.defaults.baseUrl + destination } + headers: { ...config.headers, Destination: this.defaults.baseURL + destination } }) - move = (url: string, { destination, ...config }: MoveConfig) => + move = (url: string, destination: string, config: WebDAVRequestConfig = {}) => this.request({ ...config, url, method: 'MOVE', - headers: { ...config.headers, Destination: this.defaults.baseUrl + destination } + headers: { ...config.headers, Destination: this.defaults.baseURL + destination } }) - delete = (url: string, config: DeleteConfig = {}) => + delete = (url: string, config: WebDAVRequestConfig = {}) => this.request({ ...config, url, method: 'DELETE' }) - private constructor(config?: Partial, private createRequest = () => new XMLHttpRequest()) { - if (config) { - this.defaults = { ...this.defaults, ...config }; - } - } private request = (config: RequestConfig) => { return new Promise((resolve, reject) => { const r = this.createRequest(); - r.open(config.method, this.defaults.baseUrl + config.url); + r.open(config.method, this.defaults.baseURL + config.url); const headers = { ...this.defaults.headers, ...config.headers }; Object @@ -72,32 +71,15 @@ export class WebDAV { } } - -export interface PropfindConfig extends BaseConfig { } - -export interface PutConfig extends BaseConfig { - data?: any; - onProgress?: (event: ProgressEvent) => void; -} - -export interface CopyConfig extends BaseConfig { - destination: string; -} - -export interface MoveConfig extends BaseConfig { - destination: string; -} - -export interface DeleteConfig extends BaseConfig { } - -interface BaseConfig { +export interface WebDAVRequestConfig { headers?: { [key: string]: string; }; + onProgress?: (event: ProgressEvent) => void; } interface WebDAVDefaults { - baseUrl: string; + baseURL: string; headers: { [key: string]: string }; } diff --git a/src/services/services.ts b/src/services/services.ts index d2a8ce43..45d360c1 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -19,12 +19,12 @@ export type ServiceRepository = ReturnType; export const createServices = (config: Config) => { const apiClient = Axios.create(); - apiClient.defaults.baseURL = `${config.API_HOST}/arvados/v1`; + apiClient.defaults.baseURL = `${config.apiHost}/arvados/v1`; - const webdavClient = WebDAV.create(); - webdavClient.defaults.baseUrl = config.KEEP_WEB_HOST; + const webdavClient = new WebDAV(); + webdavClient.defaults.baseURL = config.keepWebHost; - const authService = new AuthService(apiClient, config.API_HOST); + const authService = new AuthService(apiClient, config.apiHost); const keepService = new KeepService(apiClient); const groupsService = new GroupsService(apiClient); const projectService = new ProjectService(apiClient); diff --git a/src/store/auth/auth-actions.test.ts b/src/store/auth/auth-actions.test.ts index 2b28cd08..18bb3ff3 100644 --- a/src/store/auth/auth-actions.test.ts +++ b/src/store/auth/auth-actions.test.ts @@ -23,9 +23,9 @@ describe('auth-actions', () => { let store: RootStore; beforeEach(() => { - store = configureStore(createBrowserHistory(), createServices({ API_HOST: "/arvados/v1", KEEP_WEB_HOST: "" })); + store = configureStore(createBrowserHistory(), createServices({ apiHost: "/arvados/v1", keepWebHost: "" })); localStorage.clear(); - reducer = authReducer(createServices({ API_HOST: "/arvados/v1", KEEP_WEB_HOST: "" })); + reducer = authReducer(createServices({ apiHost: "/arvados/v1", keepWebHost: "" })); }); it('should initialise state with user and api token from local storage', () => { diff --git a/src/store/auth/auth-reducer.test.ts b/src/store/auth/auth-reducer.test.ts index 28cfbb2e..aaedd40d 100644 --- a/src/store/auth/auth-reducer.test.ts +++ b/src/store/auth/auth-reducer.test.ts @@ -13,7 +13,7 @@ describe('auth-reducer', () => { beforeAll(() => { localStorage.clear(); - reducer = authReducer(createServices({ API_HOST: "/arvados/v1", KEEP_WEB_HOST: "" })); + reducer = authReducer(createServices({ apiHost: "/arvados/v1", keepWebHost: "" })); }); it('should correctly initialise state', () => { -- 2.30.2