Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 3028688

Browse files
committed
Simplify send.withTransform API.
1 parent 09cebb9 commit 3028688

File tree

2 files changed

+41
-38
lines changed

2 files changed

+41
-38
lines changed

src/api/add.js

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ module.exports = (send) => {
1111
opts = {}
1212
}
1313

14-
var sendBackDagNode = sendWithTransform(send, transform)
14+
send = send.withTransform(transform)
1515

1616
if (typeof files === 'string' && files.startsWith('http')) {
1717
return Wreck.request('GET', files, null, (err, res) => {
1818
if (err) return cb(err)
1919

20-
sendBackDagNode('add', null, opts, res, cb)
20+
send('add', null, opts, res, cb)
2121
})
2222
}
2323

24-
return sendBackDagNode('add', null, opts, files, cb)
24+
return send('add', null, opts, files, cb)
2525

2626
// transform returned objects into DAGNodes
2727
function transform (err, res, done) {
@@ -41,37 +41,3 @@ module.exports = (send) => {
4141
}
4242
}
4343
}
44-
45-
// Wraps the 'send' function such that an asynchronous transform may be applied
46-
// to its result before passing it on to either its callback or promise.
47-
function sendWithTransform (send, transform) {
48-
return function (path, args, qs, files, buffer, cb) {
49-
if (typeof buffer === 'function') {
50-
cb = buffer
51-
buffer = false
52-
}
53-
54-
var p = send(path, args, qs, files, buffer, wrap(cb))
55-
56-
if (p instanceof Promise) {
57-
return p.then((res) => {
58-
return new Promise(function (resolve, reject) {
59-
transform(null, res, function (err, res) {
60-
if (err) reject(err)
61-
else resolve(res)
62-
})
63-
})
64-
})
65-
} else {
66-
return p
67-
}
68-
69-
function wrap (done) {
70-
if (done) {
71-
return function (err, res) {
72-
transform(err, res, done)
73-
}
74-
}
75-
}
76-
}
77-
}

src/request-api.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function requestAPI (config, path, args, qs, files, buffer, cb) {
110110
// -- Interface
111111

112112
exports = module.exports = function getRequestAPI (config) {
113-
return function (path, args, qs, files, buffer, cb) {
113+
var send = function (path, args, qs, files, buffer, cb) {
114114
if (typeof buffer === 'function') {
115115
cb = buffer
116116
buffer = false
@@ -127,4 +127,41 @@ exports = module.exports = function getRequestAPI (config) {
127127

128128
return requestAPI(config, path, args, qs, files, buffer, cb)
129129
}
130+
131+
// Wraps the 'send' function such that an asynchronous transform may be
132+
// applied to its result before passing it on to either its callback or
133+
// promise.
134+
send.withTransform = function (transform) {
135+
return function (path, args, qs, files, buffer, cb) {
136+
if (typeof buffer === 'function') {
137+
cb = buffer
138+
buffer = false
139+
}
140+
141+
var p = send(path, args, qs, files, buffer, wrap(cb))
142+
143+
if (p instanceof Promise) {
144+
return p.then((res) => {
145+
return new Promise(function (resolve, reject) {
146+
transform(null, res, function (err, res) {
147+
if (err) reject(err)
148+
else resolve(res)
149+
})
150+
})
151+
})
152+
} else {
153+
return p
154+
}
155+
156+
function wrap (done) {
157+
if (done) {
158+
return function (err, res) {
159+
transform(err, res, done)
160+
}
161+
}
162+
}
163+
}
164+
}
165+
166+
return send
130167
}

0 commit comments

Comments
 (0)