Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | 3x 2x 2x 2x | /** * @fileoverview Http helper functions. * @module common * @description Helper functions for http requests * @requires react react-native */ import { NativeModules, Platform } from 'react-native' import { API_URL } from '@env' import { Header } from '@global/types/httpClient/Header' /** * @constant defaultHeaders * @description Default headers to send with requests. * @type {Header} * @default * @property {string} Accept - Accept header. * @property {string} ContentType - Content-Type header. * @property {string} Locale - Locale header. */ export const defaultHeaders: Header = { Accept: '*/*', ContentType: 'application/json', Locale: Platform.OS === 'ios' ? NativeModules.SettingsManager.settings.AppleLocale || NativeModules.SettingsManager.settings.AppleLanguages[0] : NativeModules.I18nManager.localeIdentifier, } interface PostProps { url?: string endpoint: `/${string}` body: string headers?: Header authorizationToken?: string } /** * @function post * @description Send a POST request to the server. * @param props Object containing the url, endpoint, body, and headers. * @param props.url The url to send the request to. Defaults to defaultApiURL * @param props.endpoint The endpoint to send the request to. Must start with a '/'. * @param props.body The body to send with the request. * @param props.headers The headers to send with the request. * @returns Promise of a Response object */ export function post({ url = API_URL, endpoint, body, headers = defaultHeaders, authorizationToken = '', }: PostProps): Promise<Response> { return requestServer({ url, endpoint, method: 'POST', headers, body, authorizationToken, }) } /** * @function put * @description Send a PUT request to the server. * @param props Object containing the url, endpoint, body, and headers. * @param props.url The url to send the request to. Defaults to defaultApiURL * @param props.endpoint The endpoint to send the request to. Must start with a '/'. * @param props.body The body to send with the request. * @param props.headers The headers to send with the request. * @returns Promise of a Response object */ export function put({ url = API_URL, endpoint, body, headers = defaultHeaders, authorizationToken = '', }: PostProps): Promise<Response> { return requestServer({ url, endpoint, method: 'PUT', headers, body, authorizationToken, }) } interface DeleteProps { url?: string endpoint: `/${string}` headers?: Header authorizationToken?: string } /** * @function delete * @description Send a DELETE request to the server. * @param props Object containing the url, endpoint, body, and headers. * @param props.url The url to send the request to. Defaults to defaultApiUrl * @param props.endpoint The endpoint to send the request to. Must start with a '/'. * @param props.body The body to send with the request. * @param props.headers The headers to send with the request. * @returns Promise of a Response object */ export function deleteRequest({ url = API_URL, endpoint, headers = defaultHeaders, authorizationToken = '', }: DeleteProps): Promise<Response> { return requestServer({ url, endpoint, method: 'DELETE', headers, authorizationToken, }) } interface GetProps { url?: string endpoint: `/${string}` headers?: Header authorizationToken?: string } /** * @function get * @description Send a GET request to the server. * @param props Object containing the url, endpoint, and headers. * @param props.url The url to send the request to. Defaults to defaultApiURL * @param props.endpoint The endpoint to send the request to. Must start with a '/'. * @param props.headers The headers to send with the request. * @returns Promise of a Response object */ export function get({ url = API_URL, endpoint, headers = defaultHeaders, authorizationToken = '', }: GetProps): Promise<Response> { return requestServer({ url, endpoint, method: 'GET', headers, authorizationToken, }) } interface RequestServerProps { url: string endpoint: `/${string}` method: 'POST' | 'GET' | 'PUT' | 'DELETE' headers: Header body?: string authorizationToken?: string } /** * @function requestServer * @description Send a request to the server. * @param Props Object containing the url, endpoint, method, headers, and body. * @param Props.url The url to send the request to. Defaults to defaultApiURL * @param Props.endpoint The endpoint to send the request to. Must start with a '/'. * @param Props.method The method to use for the request. * @param Props.headers The headers to send with the request. * @param Props.body The body to send with the request. * @returns Promise of a Response object */ export function requestServer({ url = API_URL, endpoint, method, headers, body, authorizationToken, }: RequestServerProps): Promise<Response> { Eif (__DEV__) console.log('finalURL:', url + endpoint) return fetch(url + endpoint, { method, headers: { Accept: headers.Accept, 'Content-Type': headers.ContentType, Locale: headers.Locale, Authorization: `Bearer ${authorizationToken}`, }, body, }) } |