-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 71409b3
Showing
16 changed files
with
9,379 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
'extends': '@launchpadlab/eslint-config/react' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
lib | ||
esm | ||
/log/* | ||
/tmp/* | ||
!/log/.keep | ||
!/tmp/.keep | ||
npm-debug.log* | ||
node_modules | ||
coverage | ||
yarn-error.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": ["@launchpadlab"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = [ | ||
{ | ||
path: 'lib', | ||
limit: '5 KB' | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
language: node_js | ||
node_js: | ||
- '8' | ||
cache: | ||
- yarn | ||
- npm | ||
notifications: | ||
email: false | ||
before_script: | ||
- 'yarn lint' | ||
- 'yarn size' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 Launchpad Lab | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# use-cloudinary | ||
|
||
A hook to simplify direct upload calls to the [cloudinary API](https://cloudinary.com/documentation/upload_images#uploading_with_a_direct_call_to_the_api). | ||
|
||
<!-- TODO: add example and docs --> | ||
|
||
Implemented with [useAsyncFn](https://github.com/streamich/react-use/blob/master/docs/useAsyncFn.md) from `react-use`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
const fetch = jest.fn(function (url, options) { | ||
// Echoes back passed options | ||
return Promise.resolve({ url, ...options }) | ||
}) | ||
|
||
export default fetch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
module.exports = { | ||
"presets": [ | ||
["@launchpadlab/babel-preset/react", { | ||
"env": { | ||
// Don't transform modules in "esm" mode. | ||
"modules": process.env.BABEL_ENV === 'esm' ? false : 'auto' | ||
} | ||
}] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
'setupFiles': [ | ||
'./test/setup.js', | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"name": "use-cloudinary", | ||
"version": "1.0.0", | ||
"description": "Hook for managing cloudinary uploads", | ||
"main": "lib/index.js", | ||
"module": "esm/index.js", | ||
"sideEffects": false, | ||
"repository": "launchpadlab/use-cloudinary", | ||
"homepage": "https://github.com/launchpadlab/use-cloudinary", | ||
"author": { | ||
"name": "dpikt" | ||
}, | ||
"license": "MIT", | ||
"scripts": { | ||
"start": "yarn run build:development", | ||
"build": "yarn build:cjs && yarn build:esm", | ||
"build:cjs": "babel src --out-dir lib", | ||
"build:esm": "BABEL_ENV=esm babel src --out-dir esm", | ||
"build:development": "BABEL_ENV=esm babel src --watch --out-dir esm", | ||
"clean": "rm -rf lib && rm -rf esm", | ||
"lint": "eslint src --max-warnings=0 --fix", | ||
"precommit": "yarn lint", | ||
"prepublish": "yarn run clean && yarn run build", | ||
"test": "jest", | ||
"size": "yarn build && size-limit" | ||
}, | ||
"files": [ | ||
"README.md", | ||
"LICENSE.md", | ||
"lib/", | ||
"src/", | ||
"esm/" | ||
], | ||
"dependencies": { | ||
"isomorphic-fetch": "^2.2.1", | ||
"react-use": "^11.3.2" | ||
}, | ||
"peerDependencies": { | ||
"react": "^16.8.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.4.4", | ||
"@babel/core": "^7.4.5", | ||
"@babel/polyfill": "^7.4.4", | ||
"@launchpadlab/babel-preset": "^2.1.0", | ||
"@launchpadlab/eslint-config": "^2.7.0", | ||
"@size-limit/preset-small-lib": "^2.1.1", | ||
"@testing-library/react-hooks": "^2.0.1", | ||
"eslint": "^6.1.0", | ||
"husky": "^3.0.3", | ||
"jest": "^24.8.0", | ||
"react": "^16.9.0", | ||
"react-dom": "^16.9.0", | ||
"react-test-renderer": "^16.9.0", | ||
"size-limit": "^2.1.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './useCloudinary' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import fetch from 'isomorphic-fetch' | ||
import useAsyncFn from 'react-use/lib/useAsyncFn' | ||
|
||
function useCloudinary({ | ||
endpoint = 'https://api.cloudinary.com/v1_1', | ||
...defaults | ||
} = {}) { | ||
return useAsyncFn((file, options = {}) => { | ||
const combinedOptions = { ...defaults, ...options } | ||
const { cloudName, resourceType } = combinedOptions | ||
if (!cloudName) throw new Error('Must provide cloudName.') | ||
if (!resourceType) throw new Error('Must provide resourceType.') | ||
const url = `${endpoint}/${cloudName}/${resourceType}/upload` | ||
return fetch(url, { | ||
method: 'POST', | ||
body: JSON.stringify({ file, ...defaults, ...options }), | ||
}) | ||
}) | ||
} | ||
|
||
export default useCloudinary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Enable async/await | ||
import '@babel/polyfill' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import useCloudinary from '../src' | ||
import { renderHook, act } from '@testing-library/react-hooks' | ||
|
||
// Relies on fetch mock to return request options. | ||
// Does not test upload state updates since those are handled by react-use. | ||
|
||
describe('useCloudinary', () => { | ||
it('requires cloudName and resourceType arguments', async () => { | ||
const { result } = renderHook(() => useCloudinary()) | ||
const fetch = result.current[1] | ||
await expect(() => act(() => fetch('my file'))).toThrow() | ||
await expect(() => act(() => fetch('my file', { cloudName: 'foo' }))).toThrow() | ||
await expect(() => act(() => fetch('my file', { resourceType: 'bar' }))).toThrow() | ||
await expect(() => act(() => fetch('my file', { cloudName: 'foo', resourceType: 'bar' }))).not.toThrow() | ||
}) | ||
it('builds endpoint with cloudName and resourceType', async () => { | ||
const { result } = renderHook(() => useCloudinary({ cloudName: 'foo', resourceType: 'bar' })) | ||
const fetch = result.current[1] | ||
await act(() => fetch()) | ||
const { url } = result.current[0].value | ||
expect(url).toEqual('https://api.cloudinary.com/v1_1/foo/bar/upload') | ||
}) | ||
it('can receive custom endpoint', async () => { | ||
const { result } = renderHook(() => useCloudinary({ cloudName: 'foo', resourceType: 'bar', endpoint: 'baz' })) | ||
const fetch = result.current[1] | ||
await act(() => fetch()) | ||
const { url } = result.current[0].value | ||
expect(url).toEqual('baz/foo/bar/upload') | ||
}) | ||
it('passes file to request', async () => { | ||
const { result } = renderHook(() => useCloudinary({ cloudName: 'foo', resourceType: 'bar' })) | ||
const fetch = result.current[1] | ||
await act(() => fetch('my file')) | ||
const { body } = result.current[0].value | ||
expect(JSON.parse(body).file).toEqual('my file') | ||
}) | ||
it('passes through other options to request', async () => { | ||
const { result } = renderHook(() => useCloudinary({ cloudName: 'foo', resourceType: 'bar', folder: 'folder', otherThing: 'otherThing' })) | ||
const fetch = result.current[1] | ||
await act(() => fetch()) | ||
const { body } = result.current[0].value | ||
expect(JSON.parse(body).folder).toEqual('folder') | ||
expect(JSON.parse(body).otherThing).toEqual('otherThing') | ||
}) | ||
it('can overwrite options with fetch function', async () => { | ||
const { result } = renderHook(() => useCloudinary({ cloudName: 'foo', resourceType: 'bar', folder: 'custom' })) | ||
const fetch = result.current[1] | ||
await act(() => fetch('my file', { folder: 'custom' })) | ||
const { body } = result.current[0].value | ||
expect(JSON.parse(body).folder).toEqual('custom') | ||
}) | ||
}) |
Oops, something went wrong.