Skip to content

Commit

Permalink
feat: add example ava test
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Oli Evans <[email protected]>
  • Loading branch information
olizilla authored and agentofuser committed May 17, 2019
1 parent b4c181f commit cc1c34d
Show file tree
Hide file tree
Showing 5 changed files with 2,878 additions and 1,720 deletions.
10 changes: 1 addition & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,9 @@ const _ = require('lodash')
const fp = require('lodash/fp')
const neatFrame = require('neat-frame')
const { stripIndent } = require('common-tags')
const httpGatewayUrl = require('./src/gateway')

// # Pure functions
function httpGatewayUrl(hash, gatewayProvider = 'ipfs') {
const gateways = {
ipfs: 'https://ipfs.io',
infura: 'https://ipfs.infura.io',
pinata: 'https://gateway.pinata.cloud',
}
const origin = gateways[gatewayProvider] || gateways['ipfs']
return `${origin}/ipfs/${hash}`
}

function formatError(e) {
const prettierJson = obj =>
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@semantic-release/npm": "^5.1.7",
"@semantic-release/release-notes-generator": "^7.1.7",
"all-contributors-cli": "^6.4.0",
"ava": "^1.4.1",
"eslint": "^6.0.0-alpha.0",
"eslint-config-prettier": "^4.2.0",
"eslint-plugin-import": "^2.17.2",
Expand All @@ -76,7 +77,7 @@
},
"scripts": {
"format": "npx prettier --write ./**/*.{js,json,md,mdx,html,css}",
"test": "echo \"Error: no test specified\" && exit 1",
"test": "ava",
"commit": "npx sgc",
"commit:retry": "npx sgc --retry",
"semantic-release": "./semantic-release-dry-run.bash",
Expand Down
14 changes: 14 additions & 0 deletions src/gateway.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function getGatewayUrl(cid, gatewayProvider = 'ipfs') {
if (!cid) {
throw new Error('getGatewayUrl expects to be called with a valud IPFS CID')
}
const gateways = {
ipfs: 'https://ipfs.io',
infura: 'https://ipfs.infura.io',
pinata: 'https://gateway.pinata.cloud',
}
const origin = gateways[gatewayProvider] || gateways['ipfs']
return `${origin}/ipfs/${cid}`
}

module.exports = getGatewayUrl
26 changes: 26 additions & 0 deletions test/gateway.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const test = require('ava')
const httpGatewayUrl = require('../src/gateway')

test('get http gateway url for a cid', t => {
const expected = 'https://ipfs.io/ipfs/fakecid'
const actual = httpGatewayUrl('fakecid')
t.is(actual, expected)
})

test('get http gateway url for a cid on infura', t => {
const expected = 'https://ipfs.infura.io/ipfs/fakecid'
const actual = httpGatewayUrl('fakecid', 'infura')
t.is(actual, expected)
})

test('get http gateway url for a cid on pinata', t => {
const expected = 'https://gateway.pinata.cloud/ipfs/fakecid'
const actual = httpGatewayUrl('fakecid', 'pinata')
t.is(actual, expected)
})

test('throw if no cid', t => {
t.throws(() => {
httpGatewayUrl()
})
})
Loading

0 comments on commit cc1c34d

Please sign in to comment.