diff options
Diffstat (limited to 'app/assets/javascripts/api')
-rw-r--r-- | app/assets/javascripts/api/api_utils.js | 5 | ||||
-rw-r--r-- | app/assets/javascripts/api/constants.js | 1 | ||||
-rw-r--r-- | app/assets/javascripts/api/groups_api.js | 22 | ||||
-rw-r--r-- | app/assets/javascripts/api/projects_api.js | 27 | ||||
-rw-r--r-- | app/assets/javascripts/api/user_api.js | 66 |
5 files changed, 121 insertions, 0 deletions
diff --git a/app/assets/javascripts/api/api_utils.js b/app/assets/javascripts/api/api_utils.js new file mode 100644 index 00000000000..42eb5e7aaf3 --- /dev/null +++ b/app/assets/javascripts/api/api_utils.js @@ -0,0 +1,5 @@ +import { joinPaths } from '../lib/utils/url_utility'; + +export function buildApiUrl(url) { + return joinPaths('/', gon.relative_url_root || '', url.replace(':version', gon.api_version)); +} diff --git a/app/assets/javascripts/api/constants.js b/app/assets/javascripts/api/constants.js new file mode 100644 index 00000000000..b6c720a85f3 --- /dev/null +++ b/app/assets/javascripts/api/constants.js @@ -0,0 +1 @@ +export const DEFAULT_PER_PAGE = 20; diff --git a/app/assets/javascripts/api/groups_api.js b/app/assets/javascripts/api/groups_api.js new file mode 100644 index 00000000000..d4ba46656e6 --- /dev/null +++ b/app/assets/javascripts/api/groups_api.js @@ -0,0 +1,22 @@ +import axios from '../lib/utils/axios_utils'; +import { buildApiUrl } from './api_utils'; +import { DEFAULT_PER_PAGE } from './constants'; + +const GROUPS_PATH = '/api/:version/groups.json'; + +export function getGroups(query, options, callback = () => {}) { + const url = buildApiUrl(GROUPS_PATH); + return axios + .get(url, { + params: { + search: query, + per_page: DEFAULT_PER_PAGE, + ...options, + }, + }) + .then(({ data }) => { + callback(data); + + return data; + }); +} diff --git a/app/assets/javascripts/api/projects_api.js b/app/assets/javascripts/api/projects_api.js new file mode 100644 index 00000000000..d9a2467cff3 --- /dev/null +++ b/app/assets/javascripts/api/projects_api.js @@ -0,0 +1,27 @@ +import axios from '../lib/utils/axios_utils'; +import { buildApiUrl } from './api_utils'; +import { DEFAULT_PER_PAGE } from './constants'; + +const PROJECTS_PATH = '/api/:version/projects.json'; + +export function getProjects(query, options, callback = () => {}) { + const url = buildApiUrl(PROJECTS_PATH); + const defaults = { + search: query, + per_page: DEFAULT_PER_PAGE, + simple: true, + }; + + if (gon.current_user_id) { + defaults.membership = true; + } + + return axios + .get(url, { + params: Object.assign(defaults, options), + }) + .then(({ data, headers }) => { + callback(data); + return { data, headers }; + }); +} diff --git a/app/assets/javascripts/api/user_api.js b/app/assets/javascripts/api/user_api.js new file mode 100644 index 00000000000..e5983ec3c58 --- /dev/null +++ b/app/assets/javascripts/api/user_api.js @@ -0,0 +1,66 @@ +import axios from '../lib/utils/axios_utils'; +import { buildApiUrl } from './api_utils'; +import { DEFAULT_PER_PAGE } from './constants'; +import { deprecatedCreateFlash as flash } from '~/flash'; +import { __ } from '~/locale'; + +const USER_COUNTS_PATH = '/api/:version/user_counts'; +const USERS_PATH = '/api/:version/users.json'; +const USER_PATH = '/api/:version/users/:id'; +const USER_STATUS_PATH = '/api/:version/users/:id/status'; +const USER_PROJECTS_PATH = '/api/:version/users/:id/projects'; +const USER_POST_STATUS_PATH = '/api/:version/user/status'; + +export function getUsers(query, options) { + const url = buildApiUrl(USERS_PATH); + return axios.get(url, { + params: { + search: query, + per_page: DEFAULT_PER_PAGE, + ...options, + }, + }); +} + +export function getUser(id, options) { + const url = buildApiUrl(USER_PATH).replace(':id', encodeURIComponent(id)); + return axios.get(url, { + params: options, + }); +} + +export function getUserCounts() { + const url = buildApiUrl(USER_COUNTS_PATH); + return axios.get(url); +} + +export function getUserStatus(id, options) { + const url = buildApiUrl(USER_STATUS_PATH).replace(':id', encodeURIComponent(id)); + return axios.get(url, { + params: options, + }); +} + +export function getUserProjects(userId, query, options, callback) { + const url = buildApiUrl(USER_PROJECTS_PATH).replace(':id', userId); + const defaults = { + search: query, + per_page: DEFAULT_PER_PAGE, + }; + return axios + .get(url, { + params: { ...defaults, ...options }, + }) + .then(({ data }) => callback(data)) + .catch(() => flash(__('Something went wrong while fetching projects'))); +} + +export function updateUserStatus({ emoji, message, availability }) { + const url = buildApiUrl(USER_POST_STATUS_PATH); + + return axios.put(url, { + emoji, + message, + availability, + }); +} |