From d9e56b87b120fc5e3b90d3e6677a35afea1c3cf2 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Fri, 1 Jun 2018 15:49:54 +0100 Subject: [PATCH] feat: export resolver --- README.md | 26 +++++++++++++++++++++++--- src/index.js | 7 ++++++- test/index.spec.js | 8 ++++---- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index b98fe363b6..8764b19719 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,30 @@ This project consists on creating a HTTP response from an IPFS Hash. This response can be a file, a directory list view or the entry point of a web page. ```js -const ipfsHttpResponse = require('ipfs-http-response') +const { getResponse } = require('ipfs-http-response') -ipfsHttpResponse(ipfsNode, ipfsPath) - .then((response) => { +getResponse(ipfsNode, ipfsPath) + .then((result) => { + ... + }) +``` + +This module also exports the used ipfs resolver, which should be used when the response needs to be customized. + +```js +const { resolver } = require('ipfs-http-response') + +resolver.multihash(ipfsNode, ipfsPath) + .then((result) => { + ... + }) +``` + +```js +const { resolver } = require('ipfs-http-response') + +resolver.directory(node, path, multihash) + .then((result) => { ... }) ``` diff --git a/src/index.js b/src/index.js index 37f01ae278..dec9d2ab12 100644 --- a/src/index.js +++ b/src/index.js @@ -16,7 +16,7 @@ const header = (status = 200, statusText = 'OK', headers = {}) => ({ headers }) -module.exports = (ipfsNode, ipfsPath) => { +const response = (ipfsNode, ipfsPath) => { // handle hash resolve error (simple hash, test for directory now) const handleResolveError = (node, path, error) => { if (error) { @@ -100,3 +100,8 @@ module.exports = (ipfsNode, ipfsPath) => { }) }) } + +module.exports = { + getResponse: response, + resolver: resolver +} diff --git a/test/index.spec.js b/test/index.spec.js index 38a44d847b..0fa0faa3dc 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -10,7 +10,7 @@ const loadFixture = require('aegir/fixtures') const ipfs = require('ipfs') const DaemonFactory = require('ipfsd-ctl') -const ipfsHttpResponse = require('../src') +const { getResponse } = require('../src') const makeWebResponseEnv = require('./utils/web-response-env') const df = DaemonFactory.create({ type: 'proc', exec: ipfs }) @@ -46,7 +46,7 @@ describe('resolve file', function () { }) it('should resolve a multihash', async () => { - const res = await ipfsHttpResponse(ipfs, `/ipfs/${file.cid}`) + const res = await getResponse(ipfs, `/ipfs/${file.cid}`) expect(res).to.exist() expect(res).to.deep.include({ @@ -98,7 +98,7 @@ describe('resolve directory', function () { }) it('should return the list of files of a directory', async () => { - const res = await ipfsHttpResponse(ipfs, `/ipfs/${directory.cid}`, directory.cid) + const res = await getResponse(ipfs, `/ipfs/${directory.cid}`, directory.cid) expect(res).to.exist() }) @@ -149,7 +149,7 @@ describe('resolve web page', function () { }) it('should return the entry point of a web page when a trying to fetch a directory containing a web page', async () => { - const res = await ipfsHttpResponse(ipfs, `/ipfs/${webpage.cid}`, webpage.cid) + const res = await getResponse(ipfs, `/ipfs/${webpage.cid}`, webpage.cid) expect(res).to.exist() expect(res).to.equal(`/ipfs/${webpage.cid}/index.html`)