From d9cdd730806e98b5138477ccd240906c7e29ab65 Mon Sep 17 00:00:00 2001 From: Maksim Sukharev Date: Sun, 25 Feb 2024 20:17:53 +0100 Subject: [PATCH] refactor(bots): migrate bots to Typescript Signed-off-by: Maksim Sukharev --- src/components/AdminSettings/BotsSettings.vue | 2 +- .../ConversationSettings/BotsSettings.vue | 2 +- .../{botsService.js => botsService.ts} | 25 ++++++++----------- src/stores/__tests__/bots.spec.js | 6 ++--- src/stores/{bots.js => bots.ts} | 24 ++++++++++-------- src/types/index.ts | 16 ++++++++++++ 6 files changed, 45 insertions(+), 30 deletions(-) rename src/services/{botsService.js => botsService.ts} (72%) rename src/stores/{bots.js => bots.ts} (76%) create mode 100644 src/types/index.ts diff --git a/src/components/AdminSettings/BotsSettings.vue b/src/components/AdminSettings/BotsSettings.vue index 3cd7926eaa70..d87ea7a7d94d 100644 --- a/src/components/AdminSettings/BotsSettings.vue +++ b/src/components/AdminSettings/BotsSettings.vue @@ -106,7 +106,7 @@ import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadi import NcPopover from '@nextcloud/vue/dist/Components/NcPopover.js' import { BOT } from '../../constants.js' -import { getAllBots } from '../../services/botsService.js' +import { getAllBots } from '../../services/botsService.ts' export default { name: 'BotsSettings', diff --git a/src/components/ConversationSettings/BotsSettings.vue b/src/components/ConversationSettings/BotsSettings.vue index dba9ae55cafb..eba196de104f 100644 --- a/src/components/ConversationSettings/BotsSettings.vue +++ b/src/components/ConversationSettings/BotsSettings.vue @@ -56,7 +56,7 @@ import Vue from 'vue' import NcButton from '@nextcloud/vue/dist/Components/NcButton.js' import { BOT } from '../../constants.js' -import { useBotsStore } from '../../stores/bots.js' +import { useBotsStore } from '../../stores/bots.ts' export default { name: 'BotsSettings', diff --git a/src/services/botsService.js b/src/services/botsService.ts similarity index 72% rename from src/services/botsService.js rename to src/services/botsService.ts index 09c653256f8c..adb73d87cd3c 100644 --- a/src/services/botsService.js +++ b/src/services/botsService.ts @@ -23,44 +23,41 @@ import axios from '@nextcloud/axios' import { generateOcsUrl } from '@nextcloud/router' +import type { Bot, getBotsResponse, getBotsAdminResponse, enableBotResponse, disableBotResponse } from '../types' + /** * Get information about available bots for this instance - * - * @return {object} The axios response */ -const getAllBots = async function() { +const getAllBots = async function(): getBotsAdminResponse { return axios.get(generateOcsUrl('/apps/spreed/api/v1/bot/admin')) } /** * Get information about available bots for this conversation * - * @param {string} token The conversation token - * @return {object} The axios response + * @param token The conversation token */ -const getConversationBots = async function(token) { +const getConversationBots = async function(token: string): getBotsResponse { return axios.get(generateOcsUrl('/apps/spreed/api/v1/bot/{token}', { token })) } /** * Enable bot for conversation * - * @param {string} token The conversation token - * @param {number} id The bot id - * @return {object} The axios response + * @param token The conversation token + * @param id The bot id */ -const enableBotForConversation = async function(token, id) { +const enableBotForConversation = async function(token: string, id: Bot['id']): enableBotResponse { return axios.post(generateOcsUrl('/apps/spreed/api/v1/bot/{token}/{id}', { token, id })) } /** * Disable bot for conversation * - * @param {string} token The conversation token - * @param {number} id The bot id - * @return {object} The axios response + * @param token The conversation token + * @param id The bot id */ -const disableBotForConversation = async function(token, id) { +const disableBotForConversation = async function(token: string, id: Bot['id']): disableBotResponse { return axios.delete(generateOcsUrl('/apps/spreed/api/v1/bot/{token}/{id}', { token, id })) } diff --git a/src/stores/__tests__/bots.spec.js b/src/stores/__tests__/bots.spec.js index 9da1aba92567..94ed216dfdea 100644 --- a/src/stores/__tests__/bots.spec.js +++ b/src/stores/__tests__/bots.spec.js @@ -4,11 +4,11 @@ import { getConversationBots, disableBotForConversation, enableBotForConversation, -} from '../../services/botsService.js' +} from '../../services/botsService.ts' import { generateOCSResponse } from '../../test-helpers.js' -import { useBotsStore } from '../bots.js' +import { useBotsStore } from '../bots.ts' -jest.mock('../../services/botsService', () => ({ +jest.mock('../../services/botsService.ts', () => ({ getConversationBots: jest.fn(), disableBotForConversation: jest.fn(), enableBotForConversation: jest.fn(), diff --git a/src/stores/bots.js b/src/stores/bots.ts similarity index 76% rename from src/stores/bots.js rename to src/stores/bots.ts index 2d82a380c1ac..66427d3cb3db 100644 --- a/src/stores/bots.js +++ b/src/stores/bots.ts @@ -24,32 +24,35 @@ import { defineStore } from 'pinia' import Vue from 'vue' import { BOT } from '../constants.js' -import { disableBotForConversation, enableBotForConversation, getConversationBots } from '../services/botsService.js' +import { disableBotForConversation, enableBotForConversation, getConversationBots } from '../services/botsService.ts' +import type { Bot } from '../types' +type State = { + bots: Record> +} export const useBotsStore = defineStore('bots', { - state: () => ({ + state: (): State => ({ bots: {}, }), actions: { - getConversationBots(token) { + getConversationBots(token: string): Bot[] { return this.bots[token] ? Object.values(this.bots[token]) : [] }, /** * Fetch a list of available bots for conversation and save them to store * - * @param {string} token The conversation token - * @return {Array} An array of bots ids + * @param token The conversation token */ - async loadConversationBots(token) { + async loadConversationBots(token: string): Promise { if (!this.bots[token]) { Vue.set(this.bots, token, {}) } const response = await getConversationBots(token) - return response.data.ocs.data.map((bot) => { + return response.data.ocs.data.map((bot: Bot) => { Vue.set(this.bots[token], bot.id, bot) return bot.id }) @@ -58,16 +61,15 @@ export const useBotsStore = defineStore('bots', { /** * Enable or disable a bot for conversation * - * @param {string} token The conversation token - * @param {object} bot The bot to toggle state + * @param token The conversation token + * @param bot The bot to toggle state */ - async toggleBotState(token, bot) { + async toggleBotState(token: string, bot: Bot): Promise { const response = bot.state === BOT.STATE.ENABLED ? await disableBotForConversation(token, bot.id) : await enableBotForConversation(token, bot.id) Vue.set(this.bots[token], bot.id, response.data.ocs.data) }, - }, }) diff --git a/src/types/index.ts b/src/types/index.ts new file mode 100644 index 000000000000..6061db30b8f6 --- /dev/null +++ b/src/types/index.ts @@ -0,0 +1,16 @@ +import type { components, paths } from './openapi/openapi-full.ts' + +// General +type ApiResponse = Promise<{ data: T }> + +// Conversations +export type Conversation = components['schemas']['Room'] + +// Bots +export type Bot = components['schemas']['Bot'] +export type BotWithDetails = components['schemas']['BotWithDetails'] + +export type getBotsResponse = ApiResponse +export type getBotsAdminResponse = ApiResponse +export type enableBotResponse = ApiResponse +export type disableBotResponse = ApiResponse