Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Typescriptify `api-service` (#1150)

Olga Bulat410b5e16bac5
diff --git a/src/data/api-service.js b/src/data/api-service.js
deleted file mode 100644
index 8e4241bf4..000000000
--- a/src/data/api-service.js
+++ /dev/null
@@ -1,137 +0,0 @@
-import axios from 'axios'
-
-import { warn } from '~/utils/console'
-import { AUDIO, IMAGE } from '~/constants/media'
-
-const DEFAULT_REQUEST_TIMEOUT = 30000
-
-/**
- * Returns a slug with trailing slash for a given resource name.
- * For media types, converts the name into resource slug when necessary (i.e. pluralizes 'image'),
- * for other resources uses the resource name as the slug.
- * @param {string} resource
- * @returns {string}
- */
-export const getResourceSlug = (resource) => {
- const slug = { [AUDIO]: 'audio', [IMAGE]: 'images' }[resource] ?? resource
- return `${slug}/`
-}
-/**
- * @param {boolean} errorCondition
- * @param {string} message
- * @param {import('axios').AxiosRequestConfig} config
- */
-const validateRequest = (errorCondition, message, config) => {
- if (errorCondition) {
- warn(
- `There is a problem with the request url: ${message}.
-Please check the url: ${config.baseURL}${config.url}`
- )
- }
-}
-
-/**
- *
- * @param {string} [baseUrl]
- */
-export const createApiService = (baseUrl = process.env.apiUrl) => {
- const client = axios.create({
- baseURL: baseUrl,
- timeout: DEFAULT_REQUEST_TIMEOUT,
- })
- client.interceptors.request.use(function (config) {
- validateRequest(
- !config.url?.endsWith('/'),
- 'API request urls should have a trailing slash',
- config
- )
- validateRequest(
- config.url?.includes('//') ?? false,
- 'API request urls should not have two slashes',
- config
- )
- return config
- })
- client.interceptors.response.use(
- (response) => response,
- (error) => {
- if (error.code === 'ECONNABORTED') {
- return Promise.reject({
- message: `timeout of ${
- DEFAULT_REQUEST_TIMEOUT / 1000
- } seconds exceeded`,
- ...error,
- })
- }
- return Promise.reject(error)
- }
- )
-
- return {
- /**
- * @template [T=unknown]
- * @param {string} resource The endpoint of the resource
- * @param {import('axios').AxiosRequestConfig['params']} params Url parameter object
- * @returns {Promise<import('axios').AxiosResponse<T>>} response The API response object
- */
- query(resource, params) {
- return client.get(`${getResourceSlug(resource)}`, { params })
- },
-
- /**
- * @template [T=unknown]
- * @param {string} resource The endpoint of the resource
- * @param {string} slug The sub-endpoint of the resource
- * @returns {Promise<import('axios').AxiosResponse<T>>} Response The API response object
- */
- get(resource, slug) {
- return client.get(`${getResourceSlug(resource)}${slug}/`)
- },
-
- /**
- * @template [T=unknown]
- * @param {string} resource The endpoint of the resource
- * @param {Parameters<typeof client['post']>[1]} data Url parameter object
- * @returns {Promise<import('axios').AxiosResponse<T>>} Response The API response object
- */
- post(resource, data) {
- return client.post(getResourceSlug(resource), data)
- },
-
- /**
- * @template [T=unknown]
- * @param {string} resource The endpoint of the resource
- * @param {string} slug The sub-endpoint of the resource
- * @param {Parameters<typeof client['put']>[1]} data Url parameter object
- * @param {import('axios').AxiosRequestConfig['headers']} headers Headers object
- * @returns {Promise<import('axios').AxiosResponse<T>>} Response The API response object
- */
- update(resource, slug, data, headers) {
- return client.put(`${getResourceSlug(resource)}${slug}`, data, {
- headers,
- })
- },
-
- /**
- * @param {string} resource The endpoint of the resource
- * @param {import('axios').AxiosRequestConfig} params Url parameter object
- * @returns {Promise<import('axios').AxiosResponse<any>>} Response The API response object
- */
- put(resource, params) {
- return client.put(getResourceSlug(resource), params)
- },
-
- /**
- * @param {string} resource The endpoint of the resource
- * @param {string} slug The sub-endpoint of the resource
- * @param {import('axios').AxiosRequestConfig['headers']} headers Headers object
- * @returns {Promise<import('axios').AxiosResponse<any>>} Response The API response object
- */
- delete(resource, slug, headers) {
- return client.delete(`${getResourceSlug(resource)}${slug}`, { headers })
- },
- }
-}
-
-export const VersionedApiService = createApiService(`${process.env.apiUrl}v1/`)
-export const NonversionedApiService = createApiService(`${process.env.apiUrl}`)
diff --git a/src/data/api-service.ts b/src/data/api-service.ts
new file mode 100644
index 000000000..75a73e8aa
--- /dev/null
+++ b/src/data/api-service.ts
@@ -0,0 +1,157 @@
+import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'
+
+import { warn } from '~/utils/console'
+import { AUDIO, IMAGE } from '~/constants/media'
+
+const DEFAULT_REQUEST_TIMEOUT = 30000
+
+/**
+ * Returns a slug with trailing slash for a given resource name.
+ * For media types, converts the name into resource slug when necessary (i.e. pluralizes 'image'),
+ * for other resources uses the resource name as the slug.
+ * @param resource - the first part of the request path
+ */
+export const getResourceSlug = (resource: string): string => {
+ const slug = { [AUDIO]: 'audio', [IMAGE]: 'images' }[resource] ?? resource
+ return `${slug}/`
+}
+/**
+ * @param errorCondition - if true, the `message` warning is logged in the console
+ * @param message - message to display if there is an error in request
+ * @param config - Axios config object that containing request url
+ */
+const validateRequest = (
+ errorCondition: boolean,
+ message: string,
+ config: AxiosRequestConfig
+): void => {
+ if (errorCondition) {
+ warn(
+ `There is a problem with the request url: ${message}.
+Please check the url: ${config.baseURL}${config.url}`
+ )
+ }
+}
+
+/**
+ *
+ * @param baseUrl - the optional base url to override the `env.apiUrl`
+ */
+export const createApiService = (baseUrl = process.env.apiUrl) => {
+ const client = axios.create({
+ baseURL: baseUrl,
+ timeout: DEFAULT_REQUEST_TIMEOUT,
+ })
+ client.interceptors.request.use(function (config) {
+ validateRequest(
+ !config.url?.endsWith('/'),
+ 'API request urls should have a trailing slash',
+ config
+ )
+ validateRequest(
+ config.url?.includes('//') ?? false,
+ 'API request urls should not have two slashes',
+ config
+ )
+ return config
+ })
+ client.interceptors.response.use(
+ (response) => response,
+ (error) => {
+ if (error.code === 'ECONNABORTED') {
+ return Promise.reject({
+ message: `timeout of ${
+ DEFAULT_REQUEST_TIMEOUT / 1000
+ } seconds exceeded`,
+ ...error,
+ })
+ }
+ return Promise.reject(error)
+ }
+ )
+
+ return {
+ /**
+ * @param resource - The endpoint of the resource
+ * @param params - Url parameter object
+ * @returns response The API response object
+ */
+ query<T = unknown>(
+ resource: string,
+ params: Record<string, string>
+ ): Promise<AxiosResponse<T>> {
+ return client.get(`${getResourceSlug(resource)}`, { params })
+ },
+
+ /**
+ * @param resource - The endpoint of the resource
+ * @param slug - The sub-endpoint of the resource
+ * @returns Response The API response object
+ */
+ get<T = unknown>(
+ resource: string,
+ slug: string
+ ): Promise<AxiosResponse<T>> {
+ return client.get(`${getResourceSlug(resource)}${slug}/`)
+ },
+
+ /**
+ * @param resource - The endpoint of the resource
+ * @param data - Url parameter object
+ * @returns Response The API response object
+ */
+ post<T = unknown>(
+ resource: string,
+ data: Parameters<typeof client['post']>[1]
+ ): Promise<AxiosResponse<T>> {
+ return client.post(getResourceSlug(resource), data)
+ },
+
+ /**
+ * @param resource - The endpoint of the resource
+ * @param slug - The sub-endpoint of the resource
+ * @param data - Url parameter object
+ * @param headers - Headers object
+ * @returns Response The API response object
+ */
+ update<T = unknown>(
+ resource: string,
+ slug: string,
+ data: Parameters<typeof client['put']>[1],
+ headers: AxiosRequestConfig['headers']
+ ): Promise<AxiosResponse<T>> {
+ return client.put(`${getResourceSlug(resource)}${slug}`, data, {
+ headers,
+ })
+ },
+
+ /**
+ * @param resource - The endpoint of the resource
+ * @param params - Url parameter object
+ * @returns Response The API response object
+ */
+ put(
+ resource: string,
+ params: AxiosRequestConfig
+ ): Promise<AxiosResponse<unknown>> {
+ return client.put(getResourceSlug(resource), params)
+ },
+
+ /**
+ * @param resource - The endpoint of the resource
+ * @param slug - The sub-endpoint of the resource
+ * @param headers - Headers object
+ * @returns Response The API response object
+ */
+ delete(
+ resource: string,
+ slug: string,
+ headers: AxiosRequestConfig['headers']
+ ): Promise<AxiosResponse<unknown>> {
+ return client.delete(`${getResourceSlug(resource)}${slug}`, { headers })
+ },
+ }
+}
+
+export const VersionedApiService = createApiService(`${process.env.apiUrl}v1/`)
+export const NonversionedApiService = createApiService(`${process.env.apiUrl}`)
diff --git a/src/store/media.js b/src/store/media.js
index 776e76ba5..39798ab8e 100644
--- a/src/store/media.js
+++ b/src/store/media.js
@@ -19,9 +19,7 @@ import {
} from '~/constants/mutation-types'
import { AUDIO, IMAGE, ALL_MEDIA, supportedMediaTypes } from '~/constants/media'
import MediaService from '~/data/media-service'
-
import { hash, rand as prng } from '~/utils/prng'
-
import { useSearchStore } from '~/stores/search'
/**
diff --git a/tsconfig.json b/tsconfig.json
index 605ba4636..4a7100fee 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -45,8 +45,6 @@
"src/composables/use-media-query.js",
"src/composables/window.js",
"src/constants/screens.js",
- "src/constants/filters",
- "src/data/api-service.js",
"src/utils/deep-freeze.js"
],
"exclude": ["node_modules"],