diff --git a/src/api/general.ts b/src/api/general.ts index 35b971dd..29e49158 100644 --- a/src/api/general.ts +++ b/src/api/general.ts @@ -1,6 +1,6 @@ import axios from 'axios' -import Constants from 'expo-constants' import handleError, { ctx } from './handleError' +import { userAgent } from './helpers' export type Params = { method: 'get' | 'post' | 'put' | 'delete' @@ -44,8 +44,8 @@ const apiGeneral = async ({ body && body instanceof FormData ? 'multipart/form-data' : 'application/json', - 'User-Agent': `tooot/${Constants.expoConfig?.version}`, Accept: '*/*', + ...userAgent, ...headers }, ...(body && { data: body }) diff --git a/src/api/helpers/index.ts b/src/api/helpers/index.ts new file mode 100644 index 00000000..799b7d11 --- /dev/null +++ b/src/api/helpers/index.ts @@ -0,0 +1,6 @@ +import Constants from "expo-constants" +import { Platform } from "react-native" + +const userAgent = { 'User-Agent': `tooot/${Constants.expoConfig?.version} ${Platform.OS}/${Platform.Version}` } + +export { userAgent } \ No newline at end of file diff --git a/src/api/instance.ts b/src/api/instance.ts index c15d5bcc..6df9974d 100644 --- a/src/api/instance.ts +++ b/src/api/instance.ts @@ -1,8 +1,8 @@ import { RootState } from '@root/store' import axios, { AxiosRequestConfig } from 'axios' -import Constants from 'expo-constants' import li from 'li' import handleError, { ctx } from './handleError' +import { userAgent } from './helpers' export type Params = { method: 'get' | 'post' | 'put' | 'delete' | 'patch' @@ -53,13 +53,13 @@ const apiInstance = async ({ console.log( ctx.bgGreen.bold(' API instance ') + - ' ' + - domain + - ' ' + - method + - ctx.green(' -> ') + - `/${url}` + - (params ? ctx.green(' -> ') : ''), + ' ' + + domain + + ' ' + + method + + ctx.green(' -> ') + + `/${url}` + + (params ? ctx.green(' -> ') : ''), params ? params : '' ) @@ -74,8 +74,8 @@ const apiInstance = async ({ body && body instanceof FormData ? 'multipart/form-data' : 'application/json', - 'User-Agent': `tooot/${Constants.expoConfig?.version}`, Accept: '*/*', + ...userAgent, ...headers, ...(token && { Authorization: `Bearer ${token}` diff --git a/src/api/tooot.ts b/src/api/tooot.ts index 09a8e6d7..96f0ed66 100644 --- a/src/api/tooot.ts +++ b/src/api/tooot.ts @@ -1,8 +1,8 @@ import * as Sentry from '@sentry/react-native' import { mapEnvironment } from '@utils/checkEnvironment' import axios from 'axios' -import Constants from 'expo-constants' import handleError, { ctx } from './handleError' +import { userAgent } from './helpers' export type Params = { method: 'get' | 'post' | 'put' | 'delete' @@ -50,13 +50,11 @@ const apiTooot = async ({ body && body instanceof FormData ? 'multipart/form-data' : 'application/json', - 'User-Agent': `tooot/${Constants.expoConfig?.version}`, Accept: '*/*', + ...userAgent, ...headers }, - ...(body && { data: body }), - validateStatus: status => - url.includes('translate') ? status < 500 : status === 200 + ...(body && { data: body }) }) .then(response => { return Promise.resolve({