Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dpikt committed Aug 28, 2019
0 parents commit 71409b3
Show file tree
Hide file tree
Showing 16 changed files with 9,379 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'extends': '@launchpadlab/eslint-config/react'
}
10 changes: 10 additions & 0 deletions .gitignore
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
3 changes: 3 additions & 0 deletions .renovaterc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@launchpadlab"]
}
6 changes: 6 additions & 0 deletions .size-limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = [
{
path: 'lib',
limit: '5 KB'
}
]
11 changes: 11 additions & 0 deletions .travis.yml
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'
9 changes: 9 additions & 0 deletions LICENSE.md
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.
7 changes: 7 additions & 0 deletions README.md
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`.
7 changes: 7 additions & 0 deletions __mocks__/isomorphic-fetch.js
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
11 changes: 11 additions & 0 deletions babel.config.js
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'
}
}]
]
}
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
'setupFiles': [
'./test/setup.js',
]
}
57 changes: 57 additions & 0 deletions package.json
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"
}
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './useCloudinary'
21 changes: 21 additions & 0 deletions src/useCloudinary.js
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
2 changes: 2 additions & 0 deletions test/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Enable async/await
import '@babel/polyfill'
52 changes: 52 additions & 0 deletions test/useCloudinary.test.js
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')
})
})
Loading

0 comments on commit 71409b3

Please sign in to comment.