Skip to content

Commit b0e7056

Browse files
BA-1284: baseapp fetch
1 parent ca7ab04 commit b0e7056

File tree

18 files changed

+2958
-2179
lines changed

18 files changed

+2958
-2179
lines changed

packages/graphql/CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# @baseapp-frontend/graphql
22

3+
## 1.1.0
4+
5+
### Minor Changes
6+
7+
- `httpFetch` function now uses the `baseAppFetch` as its fetch function.
8+
9+
### Patch Changes
10+
11+
- Updated dependencies
12+
- @baseapp-frontend/utils@2.2.0
13+
314
## 1.0.4
415

516
### Patch Changes

packages/graphql/config/environment.ts

+23-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useMemo } from 'react'
22

3-
import { getToken } from '@baseapp-frontend/utils/functions/token'
3+
import { baseAppFetch } from '@baseapp-frontend/utils/functions/fetch/baseAppFetch'
4+
import { getToken } from '@baseapp-frontend/utils/functions/token/getToken'
45

56
import { createClient } from 'graphql-ws'
67
import WebSocket from 'isomorphic-ws'
@@ -20,26 +21,17 @@ import {
2021

2122
const CACHE_TTL = 5 * 1000 // 5 seconds, to resolve preloaded results
2223

23-
type RequestVariables = {
24-
method: string
25-
headers: {
26-
Accept: string
27-
'Content-Type'?: string
28-
Authorization: string
29-
}
24+
type GetFetchOptions = {
25+
request: RequestParameters
26+
variables: Variables
27+
uploadables?: UploadableMap | null
3028
}
3129

32-
const getFetchOptions = async (
33-
request: RequestParameters,
34-
variables: Variables,
35-
uploadables?: UploadableMap | null,
36-
) => {
37-
const authToken = await getToken()
38-
const requestVariables: RequestVariables = {
30+
const getFetchOptions = ({ request, variables, uploadables }: GetFetchOptions) => {
31+
const requestVariables = {
3932
method: 'POST',
4033
headers: {
4134
Accept: 'application/json',
42-
Authorization: authToken ? `Token ${authToken}` : '',
4335
},
4436
}
4537

@@ -56,15 +48,14 @@ const getFetchOptions = async (
5648
}
5749
}
5850

59-
requestVariables.headers['Content-Type'] = 'application/json'
6051
return {
6152
...requestVariables,
62-
body: JSON.stringify({
53+
body: {
6354
query: request.text,
6455
operationName: request.name,
6556
operationKind: request.operationKind,
6657
variables,
67-
}),
58+
},
6859
}
6960
}
7061

@@ -74,24 +65,29 @@ export async function httpFetch(
7465
cacheConfig?: CacheConfig,
7566
uploadables?: UploadableMap | null,
7667
): Promise<GraphQLResponse> {
77-
const fetchOptions = await getFetchOptions(request, variables, uploadables)
78-
79-
const response = await fetch(process.env.NEXT_PUBLIC_RELAY_ENDPOINT as string, fetchOptions)
80-
81-
const json = await response.json()
68+
const fetchOptions = getFetchOptions({ request, variables, uploadables })
69+
const response = await baseAppFetch('', {
70+
baseUrl: process.env.NEXT_PUBLIC_RELAY_ENDPOINT,
71+
decamelizeRequestBodyKeys: false,
72+
decamelizeRequestParamsKeys: false,
73+
camelizeResponseDataKeys: false,
74+
stringifyBody: !uploadables,
75+
setContentType: !uploadables,
76+
...fetchOptions,
77+
})
8278

8379
// GraphQL returns exceptions (for example, a missing required variable) in the "errors"
8480
// property of the response. If any exceptions occurred when processing the request,
8581
// throw an error to indicate to the developer what went wrong.
86-
if (Array.isArray(json.errors)) {
82+
if (Array.isArray(response.errors)) {
8783
throw new Error(
8884
`Error fetching GraphQL query '${request.name}' with variables '${JSON.stringify(
8985
variables,
90-
)}': ${JSON.stringify(json.errors)}`,
86+
)}': ${JSON.stringify(response.errors)}`,
9187
)
9288
}
9389

94-
return json
90+
return response
9591
}
9692

9793
const wsClient = createClient({

packages/graphql/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@baseapp-frontend/graphql",
33
"description": "GraphQL configurations and utilities",
4-
"version": "1.0.4",
4+
"version": "1.1.0",
55
"main": "./dist/index.ts",
66
"module": "./dist/index.mjs",
77
"scripts": {

packages/utils/CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# @baseapp-frontend/utils
22

3+
## 2.2.0
4+
5+
### Minor Changes
6+
7+
- Implement the `baseAppFetch` fetch function.
8+
- Minor changes on `createAxiosInstance` to ensure it won't try to refresh the token for api routes included in `servicesWithoutToken`.
9+
- Implement the `getAccessToken` to abstract only the access token refresh fetch, without setting cookies.
10+
- Minor changes on `refreshAccessToken` to reuse the `getAccessToken` function.
11+
312
## 2.1.0
413

514
### Minor Changes

packages/utils/functions/axios/createAxiosInstance/index.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ export const createAxiosInstance = ({
3535
instance.defaults.headers.put['Content-Type'] = contentType
3636

3737
const requestInterceptorId = instance.interceptors.request.use(async (request) => {
38+
const isAuthTokenRequired = !servicesWithoutToken.some((regex) => regex.test(request.url || ''))
3839
let authToken = Cookies.get(cookieName)
3940

40-
if (authToken) {
41-
const isTokenValid =
42-
tokenType === TokenTypes.jwt ? isUserTokenValid(decodeJWT(authToken)) : true
41+
if (authToken && isAuthTokenRequired && tokenType === TokenTypes.jwt) {
42+
const isTokenValid = isUserTokenValid(decodeJWT(authToken))
4343
if (!isTokenValid) {
4444
try {
4545
authToken = await refreshAccessToken(cookieName, refreshCookieName)
@@ -50,15 +50,11 @@ export const createAxiosInstance = ({
5050
return Promise.reject(error)
5151
}
5252
}
53+
}
5354

54-
if (
55-
request.headers &&
56-
!request.headers.Authorization &&
57-
!servicesWithoutToken.some((regex) => regex.test(request.url || ''))
58-
) {
59-
request.headers.Authorization =
60-
tokenType === TokenTypes.jwt ? `Bearer ${authToken}` : `Token ${authToken}`
61-
}
55+
if (request.headers && !request.headers.Authorization && authToken && isAuthTokenRequired) {
56+
request.headers.Authorization =
57+
tokenType === TokenTypes.jwt ? `Bearer ${authToken}` : `Token ${authToken}`
6258
}
6359

6460
if (request.data && !file) {
@@ -101,6 +97,8 @@ export const createAxiosInstance = ({
10197
) {
10298
const newError = { response: { data: {} } }
10399
newError.response.data = humps.camelizeKeys(error.response.data)
100+
101+
return Promise.reject(newError)
104102
}
105103
return Promise.reject(error)
106104
},

0 commit comments

Comments
 (0)