Skip to content

Commit

Permalink
fix: do not execute manual requests on server
Browse files Browse the repository at this point in the history
  • Loading branch information
simoneb committed Oct 19, 2019
1 parent 9f5a6ce commit 88829b0
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules
npm-debug.log*
src/index.d.ts
*.test.ts
*.test.ssr.ts
34 changes: 20 additions & 14 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ import {
AxiosStatic,
AxiosInstance,
AxiosResponse
} from "axios";
import LRUCache from "lru-cache";
} from 'axios'
import LRUCache from 'lru-cache'

interface ResponseValues<T> {
data: T;
loading: boolean;
error?: AxiosError;
response?: AxiosResponse;
data: T
loading: boolean
error?: AxiosError
response?: AxiosResponse
}

interface Options {
manual?: boolean;
useCache?: boolean;
manual?: boolean
useCache?: boolean
}

interface RefetchOptions {
useCache?: boolean;
useCache?: boolean
}

interface ConfigureOptions {
axios?: AxiosInstance | AxiosStatic | any;
cache?: LRUCache<any, any>;
axios?: AxiosInstance | AxiosStatic | any
cache?: LRUCache<any, any>
}

export default function useAxios<T = any>(
Expand All @@ -35,7 +35,13 @@ export default function useAxios<T = any>(
): [
ResponseValues<T>,
(config?: AxiosRequestConfig, options?: RefetchOptions) => void
];
]

export function configure(options: ConfigureOptions): void;
export function resetConfigure(): void;
export function loadCache(data: any[]): void
export function serializeCache(): any[]

export function configure(options: ConfigureOptions): void
export function resetConfigure(): void

// private
export const __ssrPromises: Promise<any>[]
13 changes: 13 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ const projects = [
coverageDirectory: 'coverage',
testMatch: ['**/?(*.)+(spec|test).ts?(x)'],
preset: 'ts-jest/presets/js-with-ts'
},
{
clearMocks: true,
coverageDirectory: 'coverage',
testMatch: ['**/?(*.)+(spec|test).ssr.js?(x)'],
testEnvironment: 'node'
},
{
clearMocks: true,
coverageDirectory: 'coverage',
testMatch: ['**/?(*.)+(spec|test).ssr.ts?(x)'],
preset: 'ts-jest/presets/js-with-ts',
testEnvironment: 'node'
}
]

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"prepare": "npm run clean && npm run build",
"prepublishOnly": "npm run build",
"release": "standard-version",
"pretest": "cp ./src/index.test.js ./src/index.test.ts && cp ./index.d.ts ./src",
"pretest": "cp ./src/index.test.js ./src/index.test.ts && cp ./src/index.test.ssr.js ./src/index.test.ssr.ts && cp ./index.d.ts ./src",
"test": "jest --no-cache"
},
"dependencies": {
Expand Down
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const actions = {
REQUEST_END: 'REQUEST_END'
}

const ssrPromises = []
export const __ssrPromises = []

let cache
let axiosInstance
Expand All @@ -34,9 +34,9 @@ export function loadCache(data) {
}

export async function serializeCache() {
const ssrPromisesCopy = [...ssrPromises]
const ssrPromisesCopy = [...__ssrPromises]

ssrPromises.length = 0
__ssrPromises.length = 0

await Promise.all(ssrPromisesCopy)

Expand Down Expand Up @@ -130,8 +130,8 @@ export default function useAxios(config, options) {
createInitialState(options)
)

if (typeof window === 'undefined') {
ssrPromises.push(axiosInstance({ ...config, adapter: cacheAdapter }))
if (typeof window === 'undefined' && !options.manual) {
__ssrPromises.push(axiosInstance({ ...config, adapter: cacheAdapter }))
}

React.useEffect(() => {
Expand Down
11 changes: 11 additions & 0 deletions src/index.test.ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { renderHook } from '@testing-library/react-hooks'

import useAxios, { __ssrPromises } from './'

jest.mock('axios')

it('should not populate promises on server when manual', () => {
renderHook(() => useAxios('', { manual: true }))

expect(__ssrPromises.length).toBe(0)
})

0 comments on commit 88829b0

Please sign in to comment.