Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/components/UploadWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const UploadWidget = ({
folder = null,
cropping = true,
generateSignatureUrl,
getCustomHeaders = null,
onSuccess = null,
onFailure = null,
logging = true,
Expand Down Expand Up @@ -42,6 +43,7 @@ const UploadWidget = ({
folder,
cropping,
generateSignatureUrl,
getCustomHeaders,
onSuccess,
onFailure,
logging,
Expand Down
8 changes: 5 additions & 3 deletions src/functions/generateSignature.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const generateSignature = (
params,
{
generateSignatureUrl,
getCustomHeaders,
accepts,
contentType,
withCredentials,
Expand Down Expand Up @@ -47,15 +48,16 @@ const generateSignature = (
},
accepts: accepts,
contentType: contentType,
withCredentials: withCredentials
withCredentials: withCredentials,
getCustomHeaders
}).then((response) => {
logging && console.log(response, 'Signature Response')
return Object.assign(
{
...(eager && { eager: eager }),
...(customPublicId && { public_id: customPublicId }),
signature: response,
api_key: apiKey,
signature: response.signature,
api_key: response.api_key,
resource_type: resourceType
},
uploadParams
Expand Down
36 changes: 24 additions & 12 deletions src/functions/getterFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,33 @@ const instance = axios.create()

instance.interceptors.response.use((response) => response.data)

export const getterFunction = ({
export const getterFunction = async ({
url,
data,
accepts,
contentType,
withCredentials
getCustomHeaders
}) => {
instance.defaults.headers.common['Accepts'] = accepts
instance.defaults.headers.common['Content-Type'] = contentType
instance.defaults.withCredentials = withCredentials
const options = {
url: url + '?nocache=' + new Date().getTime(),
method: 'post',
data: data
const headers = { 'Content-Type': 'application/json' }

if (getCustomHeaders) {
const customHeaders = await getCustomHeaders()

for (const [key, value] of Object.entries(customHeaders)) {
headers[key] = value
}
}

const endpoint = url + '?nocache=' + new Date().getTime()

// eslint-disable-next-line no-undef
const response = await fetch(endpoint, {
method: 'POST',
body: JSON.stringify(data),
headers
})

if (!response.ok) {
throw new Error('Error fetching media upload signature.')
}

return instance(options)
return response.json()
}
6 changes: 4 additions & 2 deletions src/functions/myWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const myWidget = (
folder,
cropping,
generateSignatureUrl,
getCustomHeaders,
onSuccess,
onFailure,
logging,
Expand Down Expand Up @@ -52,15 +53,16 @@ const myWidget = (
params,
{
generateSignatureUrl,
getCustomHeaders,
accepts,
contentType,
withCredentials,
customPublicId,
eager,
apiKey,
resourceType,
unique_filename,
use_filename
use_filename,
resourceType
},
logging
)
Expand Down
20 changes: 10 additions & 10 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { useMyHook } from './'
import { renderHook, act } from "@testing-library/react-hooks";
import { renderHook, act } from '@testing-library/react-hooks'

// mock timer using jest
jest.useFakeTimers();
jest.useFakeTimers()

describe('useMyHook', () => {
it('updates every second', () => {
const { result } = renderHook(() => useMyHook());
const { result } = renderHook(() => useMyHook())

expect(result.current).toBe(0);
expect(result.current).toBe(0)

// Fast-forward 1sec
act(() => {
jest.advanceTimersByTime(1000);
});
jest.advanceTimersByTime(1000)
})

// Check after total 1 sec
expect(result.current).toBe(1);
expect(result.current).toBe(1)

// Fast-forward 1 more sec
act(() => {
jest.advanceTimersByTime(1000);
});
jest.advanceTimersByTime(1000)
})

// Check after total 2 sec
expect(result.current).toBe(2);
expect(result.current).toBe(2)
})
})
Loading