Skip to content

Commit

Permalink
BA: updates utils package v1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
anicioalexandre committed Sep 26, 2023
1 parent 7a8f204 commit eb2186a
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 18 deletions.
8 changes: 8 additions & 0 deletions packages/utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @baseapp-frontend/utils

## 1.2.1

### Patch Changes

- Add types to the `refreshAccessToken` function.
- Fix test types taht were using implict `any`.
- Import missing types.

## 1.2.0

### Minor Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { MockAdapter } from '@baseapp-frontend/test'
import Cookies from 'js-cookie'

import { ACCESS_COOKIE_NAME, REFRESH_COOKIE_NAME } from '../../../../constants/cookie'
import { CookieType } from '../../../../types/cookie'
import { axios } from '../index'

jest.mock('js-cookie')
Expand All @@ -16,12 +17,14 @@ describe('refreshAccessToken', () => {
[ACCESS_COOKIE_NAME]: 'accessToken',
[REFRESH_COOKIE_NAME]: 'refreshToken',
}
;(Cookies.set as jest.Mock).mockImplementation((cookieName: string, cookieValue: string) => {
cookiesFakeStore[cookieName] = cookieValue
return cookieValue
})
;(Cookies.set as jest.Mock).mockImplementation(
(cookieName: CookieType, cookieValue: string) => {
cookiesFakeStore[cookieName] = cookieValue
return cookieValue
},
)
;(Cookies.get as jest.Mock).mockImplementation(
(cookieName: string) => cookiesFakeStore[cookieName],
(cookieName: CookieType) => cookiesFakeStore[cookieName],
)

let timesCalled = 0
Expand Down Expand Up @@ -49,9 +52,9 @@ describe('refreshAccessToken', () => {
[REFRESH_COOKIE_NAME]: 'refreshToken',
}
;(Cookies.get as jest.Mock).mockImplementation(
(cookieName: string) => cookiesFakeStore[cookieName],
(cookieName: CookieType) => cookiesFakeStore[cookieName],
)
;(Cookies.remove as jest.Mock).mockImplementation((cookieName: string) => {
;(Cookies.remove as jest.Mock).mockImplementation((cookieName: CookieType) => {
delete cookiesFakeStore[cookieName]
})

Expand Down
11 changes: 7 additions & 4 deletions packages/utils/functions/axios/createAxiosInstance/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _axios from 'axios'
import _axios, { AxiosRequestConfig } from 'axios'
import humps from 'humps'
import Cookies from 'js-cookie'

Expand Down Expand Up @@ -108,7 +108,7 @@ export const {
export const refreshAccessToken = async (
cookieName: string,
refreshCookieName: string,
originalRequest,
originalRequest: AxiosRequestConfig,
) => {
const refreshToken = Cookies.get(refreshCookieName)

Expand All @@ -125,8 +125,11 @@ export const refreshAccessToken = async (
secure: process.env.NODE_ENV === 'production',
})

// eslint-disable-next-line no-param-reassign
originalRequest.headers.Authorization = `Bearer ${response.access}`
if (originalRequest.headers) {
// eslint-disable-next-line no-param-reassign
originalRequest.headers.Authorization = `Bearer ${response.access}`
}

return await axios(originalRequest)
} catch (error) {
Cookies.remove(cookieName)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { setFormApiErrors } from '..'

describe('setFormApiErrors', () => {
let mockForm
let mockError
let mockForm: any
let mockError: any

beforeEach(() => {
mockForm = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import withController from '..'

jest.mock('react-hook-form', () => ({
...jest.requireActual('react-hook-form'),
Controller: ({ render: r }) => r({ field: {}, fieldState: {} }),
Controller: ({ render: r }: { render: any }) => r({ field: {}, fieldState: {} }),
}))

describe('withController', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export * from './functions/token'
export { default as useDebounce } from './hooks/useDebounce'
export * from './hooks/useEventSubscription'

export type { IDjangoPaginatedResponse, OrderingDirection } from './types/django'
export type { ValueOf } from './types/typescript'
export type * from './types/cookie'
export type * from './types/django'
export type * from './types/form'
export type * from './types/jwt'
export type * from './types/typescript'
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@baseapp-frontend/utils",
"description": "Util functions, constants and types.",
"version": "1.2.0",
"version": "1.2.1",
"main": "./dist/index.ts",
"module": "./dist/index.mjs",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions packages/utils/types/cookie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ACCESS_COOKIE_NAME, REFRESH_COOKIE_NAME } from '../constants/cookie'

export type CookieType = typeof ACCESS_COOKIE_NAME | typeof REFRESH_COOKIE_NAME

0 comments on commit eb2186a

Please sign in to comment.