From 7eb8d5449a68e2564aa9d6178edd5e4b42ca4a57 Mon Sep 17 00:00:00 2001 From: Chaim Lev-Ari Date: Thu, 23 Jul 2020 10:45:01 +0300 Subject: [PATCH] refactor(agent): refactor rest factories to es6 (#4090) * refactor(agent): replace v1 browse with es6 module * refactor(agent): refactor agentv1 to es6 * refactor(agent): replace agent factory with es6 * refactor(agent): refactor browse response to es6 * refactor(agent): refactor browse to es6 * refactor(agent): import angular * refactor(agent): refactor host to es6 * refactor(agent): refactor ping to es6 --- app/agent/rest/agent.js | 35 +++++++--------- app/agent/rest/browse.js | 69 +++++++++++++++---------------- app/agent/rest/host.js | 35 +++++++--------- app/agent/rest/ping.js | 63 ++++++++++++++-------------- app/agent/rest/response/browse.js | 2 +- app/agent/rest/v1/agent.js | 32 +++++++------- app/agent/rest/v1/browse.js | 66 ++++++++++++++--------------- 7 files changed, 143 insertions(+), 159 deletions(-) diff --git a/app/agent/rest/agent.js b/app/agent/rest/agent.js index d7d0c6a2f..00267ce00 100644 --- a/app/agent/rest/agent.js +++ b/app/agent/rest/agent.js @@ -1,19 +1,16 @@ -angular.module('portainer.agent').factory('Agent', [ - '$resource', - 'API_ENDPOINT_ENDPOINTS', - 'EndpointProvider', - 'StateManager', - function AgentFactory($resource, API_ENDPOINT_ENDPOINTS, EndpointProvider, StateManager) { - 'use strict'; - return $resource( - API_ENDPOINT_ENDPOINTS + '/:endpointId/docker/v:version/agents', - { - endpointId: EndpointProvider.endpointID, - version: StateManager.getAgentApiVersion, - }, - { - query: { method: 'GET', isArray: true }, - } - ); - }, -]); +import angular from 'angular'; + +angular.module('portainer.agent').factory('Agent', AgentFactory); + +function AgentFactory($resource, API_ENDPOINT_ENDPOINTS, EndpointProvider, StateManager) { + return $resource( + `${API_ENDPOINT_ENDPOINTS}/:endpointId/docker/v:version/agents`, + { + endpointId: EndpointProvider.endpointID, + version: StateManager.getAgentApiVersion, + }, + { + query: { method: 'GET', isArray: true }, + } + ); +} diff --git a/app/agent/rest/browse.js b/app/agent/rest/browse.js index 852ee0535..66f8329d5 100644 --- a/app/agent/rest/browse.js +++ b/app/agent/rest/browse.js @@ -1,39 +1,36 @@ +import angular from 'angular'; + import { browseGetResponse } from './response/browse'; -angular.module('portainer.agent').factory('Browse', [ - '$resource', - 'API_ENDPOINT_ENDPOINTS', - 'EndpointProvider', - 'StateManager', - function BrowseFactory($resource, API_ENDPOINT_ENDPOINTS, EndpointProvider, StateManager) { - 'use strict'; - return $resource( - API_ENDPOINT_ENDPOINTS + '/:endpointId/docker/v:version/browse/:action', - { - endpointId: EndpointProvider.endpointID, - version: StateManager.getAgentApiVersion, +angular.module('portainer.agent').factory('Browse', BrowseFactory); + +function BrowseFactory($resource, API_ENDPOINT_ENDPOINTS, EndpointProvider, StateManager) { + return $resource( + `${API_ENDPOINT_ENDPOINTS}/:endpointId/docker/v:version/browse/:action`, + { + endpointId: EndpointProvider.endpointID, + version: StateManager.getAgentApiVersion, + }, + { + ls: { + method: 'GET', + isArray: true, + params: { action: 'ls' }, }, - { - ls: { - method: 'GET', - isArray: true, - params: { action: 'ls' }, - }, - get: { - method: 'GET', - params: { action: 'get' }, - transformResponse: browseGetResponse, - responseType: 'arraybuffer', - }, - delete: { - method: 'DELETE', - params: { action: 'delete' }, - }, - rename: { - method: 'PUT', - params: { action: 'rename' }, - }, - } - ); - }, -]); + get: { + method: 'GET', + params: { action: 'get' }, + transformResponse: browseGetResponse, + responseType: 'arraybuffer', + }, + delete: { + method: 'DELETE', + params: { action: 'delete' }, + }, + rename: { + method: 'PUT', + params: { action: 'rename' }, + }, + } + ); +} diff --git a/app/agent/rest/host.js b/app/agent/rest/host.js index a5fb198ae..cd79b9763 100644 --- a/app/agent/rest/host.js +++ b/app/agent/rest/host.js @@ -1,19 +1,16 @@ -angular.module('portainer.agent').factory('Host', [ - '$resource', - 'API_ENDPOINT_ENDPOINTS', - 'EndpointProvider', - 'StateManager', - function AgentFactory($resource, API_ENDPOINT_ENDPOINTS, EndpointProvider, StateManager) { - 'use strict'; - return $resource( - API_ENDPOINT_ENDPOINTS + '/:endpointId/docker/v:version/host/:action', - { - endpointId: EndpointProvider.endpointID, - version: StateManager.getAgentApiVersion, - }, - { - info: { method: 'GET', params: { action: 'info' } }, - } - ); - }, -]); +import angular from 'angular'; + +angular.module('portainer.agent').factory('Host', HostFactory); + +function HostFactory($resource, API_ENDPOINT_ENDPOINTS, EndpointProvider, StateManager) { + return $resource( + `${API_ENDPOINT_ENDPOINTS}/:endpointId/docker/v:version/host/:action`, + { + endpointId: EndpointProvider.endpointID, + version: StateManager.getAgentApiVersion, + }, + { + info: { method: 'GET', params: { action: 'info' } }, + } + ); +} diff --git a/app/agent/rest/ping.js b/app/agent/rest/ping.js index b6527dfb4..8726cd8d6 100644 --- a/app/agent/rest/ping.js +++ b/app/agent/rest/ping.js @@ -1,35 +1,32 @@ -angular.module('portainer.agent').factory('AgentPing', [ - '$resource', - 'API_ENDPOINT_ENDPOINTS', - 'EndpointProvider', - '$q', - function AgentPingFactory($resource, API_ENDPOINT_ENDPOINTS, EndpointProvider, $q) { - 'use strict'; - return $resource( - API_ENDPOINT_ENDPOINTS + '/:endpointId/docker/ping', - { - endpointId: EndpointProvider.endpointID, - }, - { - ping: { - method: 'GET', - interceptor: { - response: function versionInterceptor(response) { - var instance = response.resource; - var version = response.headers('Portainer-Agent-Api-Version') || 1; - instance.version = Number(version); - return instance; - }, - responseError: function versionResponseError(error) { - // 404 - agent is up - set version to 1 - if (error.status === 404) { - return { version: 1 }; - } - return $q.reject(error); - }, +import angular from 'angular'; + +angular.module('portainer.agent').factory('AgentPing', AgentPingFactory); + +function AgentPingFactory($resource, API_ENDPOINT_ENDPOINTS, EndpointProvider, $q) { + return $resource( + `${API_ENDPOINT_ENDPOINTS}/:endpointId/docker/ping`, + { + endpointId: EndpointProvider.endpointID, + }, + { + ping: { + method: 'GET', + interceptor: { + response: function versionInterceptor(response) { + const instance = response.resource; + const version = response.headers('Portainer-Agent-Api-Version') || 1; + instance.version = Number(version); + return instance; + }, + responseError: function versionResponseError(error) { + // 404 - agent is up - set version to 1 + if (error.status === 404) { + return { version: 1 }; + } + return $q.reject(error); }, }, - } - ); - }, -]); + }, + } + ); +} diff --git a/app/agent/rest/response/browse.js b/app/agent/rest/response/browse.js index 32e454305..b88f3212c 100644 --- a/app/agent/rest/response/browse.js +++ b/app/agent/rest/response/browse.js @@ -3,7 +3,7 @@ // This functions simply creates a response object and assign // the data to a field. export function browseGetResponse(data) { - var response = {}; + const response = {}; response.file = data; return response; } diff --git a/app/agent/rest/v1/agent.js b/app/agent/rest/v1/agent.js index 3d9e8d606..d42a3fb44 100644 --- a/app/agent/rest/v1/agent.js +++ b/app/agent/rest/v1/agent.js @@ -1,17 +1,15 @@ -angular.module('portainer.agent').factory('AgentVersion1', [ - '$resource', - 'API_ENDPOINT_ENDPOINTS', - 'EndpointProvider', - function AgentFactory($resource, API_ENDPOINT_ENDPOINTS, EndpointProvider) { - 'use strict'; - return $resource( - API_ENDPOINT_ENDPOINTS + '/:endpointId/docker/agents', - { - endpointId: EndpointProvider.endpointID, - }, - { - query: { method: 'GET', isArray: true }, - } - ); - }, -]); +import angular from 'angular'; + +angular.module('portainer.agent').factory('AgentVersion1', AgentFactory); + +function AgentFactory($resource, API_ENDPOINT_ENDPOINTS, EndpointProvider) { + return $resource( + `${API_ENDPOINT_ENDPOINTS}/:endpointId/docker/agents`, + { + endpointId: EndpointProvider.endpointID, + }, + { + query: { method: 'GET', isArray: true }, + } + ); +} diff --git a/app/agent/rest/v1/browse.js b/app/agent/rest/v1/browse.js index 89c18b384..cfc06128c 100644 --- a/app/agent/rest/v1/browse.js +++ b/app/agent/rest/v1/browse.js @@ -1,37 +1,35 @@ +import angular from 'angular'; + import { browseGetResponse } from '../response/browse'; -angular.module('portainer.agent').factory('BrowseVersion1', [ - '$resource', - 'API_ENDPOINT_ENDPOINTS', - 'EndpointProvider', - function BrowseFactory($resource, API_ENDPOINT_ENDPOINTS, EndpointProvider) { - 'use strict'; - return $resource( - API_ENDPOINT_ENDPOINTS + '/:endpointId/docker/browse/:volumeID/:action', - { - endpointId: EndpointProvider.endpointID, +angular.module('portainer.agent').factory('BrowseVersion1', BrowseFactory); + +function BrowseFactory($resource, API_ENDPOINT_ENDPOINTS, EndpointProvider) { + return $resource( + `${API_ENDPOINT_ENDPOINTS}/:endpointId/docker/browse/:volumeID/:action`, + { + endpointId: EndpointProvider.endpointID, + }, + { + ls: { + method: 'GET', + isArray: true, + params: { action: 'ls' }, }, - { - ls: { - method: 'GET', - isArray: true, - params: { action: 'ls' }, - }, - get: { - method: 'GET', - params: { action: 'get' }, - transformResponse: browseGetResponse, - responseType: 'arraybuffer', - }, - delete: { - method: 'DELETE', - params: { action: 'delete' }, - }, - rename: { - method: 'PUT', - params: { action: 'rename' }, - }, - } - ); - }, -]); + get: { + method: 'GET', + params: { action: 'get' }, + transformResponse: browseGetResponse, + responseType: 'arraybuffer', + }, + delete: { + method: 'DELETE', + params: { action: 'delete' }, + }, + rename: { + method: 'PUT', + params: { action: 'rename' }, + }, + } + ); +}