Skip to content

Commit ba759a6

Browse files
committed
Pass through context data to before
1 parent 94523a7 commit ba759a6

File tree

5 files changed

+70
-14
lines changed

5 files changed

+70
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file. Dates are d
77
> 2022-04-21
88
99
- Add `throttle` option
10+
- Pass `contextData` through to `before` function call
1011

1112
### [1.8.1](https://github.com/doesdev/get-scrud/compare/1.8.0...1.8.1)
1213

index.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ var bodyToQuery = function bodyToQuery() {
9797
}).join('&');
9898
};
9999

100+
var ensurePromise = function ensurePromise(fn) {
101+
return new Promise(function (resolve, reject) {
102+
try {
103+
return resolve(fn());
104+
} catch (ex) {
105+
return reject(ex);
106+
}
107+
});
108+
};
109+
100110
var actions = {
101111
search: function search(id, body) {
102112
return ['GET', "?".concat(bodyToQuery(body))];
@@ -173,7 +183,7 @@ var source = (function () {
173183
});
174184
};
175185

176-
var getScrud = function getScrud(api, action, id, body, jwt) {
186+
var getScrud = function getScrud(api, action, id, body, jwt, contextData) {
177187
if (api && _typeof(api) === 'object') return setOpts(api);
178188
return new Promise(function (resolve, reject) {
179189
if (!Number.isInteger(id) && typeof id !== 'string') {
@@ -221,7 +231,11 @@ var source = (function () {
221231
if (jwt) options.headers.Authorization = "Bearer ".concat(jwt);
222232

223233
if (opts.before) {
224-
return opts.before(api, action, options).then(function () {
234+
var before = function before() {
235+
return opts.before(api, action, options, contextData);
236+
};
237+
238+
return ensurePromise(before).then(function () {
225239
return sendRequest(options).then(resolve).catch(handleError);
226240
}).catch(handleError);
227241
}
@@ -230,9 +244,13 @@ var source = (function () {
230244
});
231245
};
232246

233-
actionList.forEach(function (a) {
234-
getScrud[a] = function (api, id, body, jwt) {
235-
return getScrud(api, a, id, body, jwt);
247+
actionList.forEach(function (action) {
248+
getScrud[action] = function (api) {
249+
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
250+
args[_key - 1] = arguments[_key];
251+
}
252+
253+
return getScrud.apply(void 0, [api, action].concat(args));
236254
};
237255
});
238256
if (opts.cache) cached = getScrud;

module.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ var bodyToQuery = function bodyToQuery() {
8989
}).join('&');
9090
};
9191

92+
var ensurePromise = function ensurePromise(fn) {
93+
return new Promise(function (resolve, reject) {
94+
try {
95+
return resolve(fn());
96+
} catch (ex) {
97+
return reject(ex);
98+
}
99+
});
100+
};
101+
92102
var actions = {
93103
search: function search(id, body) {
94104
return ['GET', "?".concat(bodyToQuery(body))];
@@ -165,7 +175,7 @@ var source = (function () {
165175
});
166176
};
167177

168-
var getScrud = function getScrud(api, action, id, body, jwt) {
178+
var getScrud = function getScrud(api, action, id, body, jwt, contextData) {
169179
if (api && _typeof(api) === 'object') return setOpts(api);
170180
return new Promise(function (resolve, reject) {
171181
if (!Number.isInteger(id) && typeof id !== 'string') {
@@ -213,7 +223,11 @@ var source = (function () {
213223
if (jwt) options.headers.Authorization = "Bearer ".concat(jwt);
214224

215225
if (opts.before) {
216-
return opts.before(api, action, options).then(function () {
226+
var before = function before() {
227+
return opts.before(api, action, options, contextData);
228+
};
229+
230+
return ensurePromise(before).then(function () {
217231
return sendRequest(options).then(resolve).catch(handleError);
218232
}).catch(handleError);
219233
}
@@ -222,9 +236,13 @@ var source = (function () {
222236
});
223237
};
224238

225-
actionList.forEach(function (a) {
226-
getScrud[a] = function (api, id, body, jwt) {
227-
return getScrud(api, a, id, body, jwt);
239+
actionList.forEach(function (action) {
240+
getScrud[action] = function (api) {
241+
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
242+
args[_key - 1] = arguments[_key];
243+
}
244+
245+
return getScrud.apply(void 0, [api, action].concat(args));
228246
};
229247
});
230248
if (opts.cache) cached = getScrud;

source.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ const bodyToQuery = (body = {}) => {
2121
}).join('&')
2222
}
2323

24+
const ensurePromise = (fn) => new Promise((resolve, reject) => {
25+
try {
26+
return resolve(fn())
27+
} catch (ex) {
28+
return reject(ex)
29+
}
30+
})
31+
2432
const actions = {
2533
search: (id, body) => ['GET', `?${bodyToQuery(body)}`],
2634
create: (id, body) => ['POST', null],
@@ -87,7 +95,7 @@ export default (opts = {}) => {
8795
})
8896
}
8997

90-
const getScrud = (api, action, id, body, jwt) => {
98+
const getScrud = (api, action, id, body, jwt, contextData) => {
9199
if (api && typeof api === 'object') return setOpts(api)
92100

93101
return new Promise((resolve, reject) => {
@@ -135,7 +143,9 @@ export default (opts = {}) => {
135143
if (jwt) options.headers.Authorization = `Bearer ${jwt}`
136144

137145
if (opts.before) {
138-
return opts.before(api, action, options).then(() => {
146+
const before = () => opts.before(api, action, options, contextData)
147+
148+
return ensurePromise(before).then(() => {
139149
return sendRequest(options).then(resolve).catch(handleError)
140150
}).catch(handleError)
141151
}
@@ -144,8 +154,8 @@ export default (opts = {}) => {
144154
})
145155
}
146156

147-
actionList.forEach((a) => {
148-
getScrud[a] = (api, id, body, jwt) => getScrud(api, a, id, body, jwt)
157+
actionList.forEach((action) => {
158+
getScrud[action] = (api, ...args) => getScrud(api, action, ...args)
149159
})
150160

151161
if (opts.cache) cached = getScrud

test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,15 @@ test('before hook is called', async (assert) => {
210210

211211
const optsB = { host: 'localhost', port, jwt, before: noError }
212212
await assert.notThrowsAsync(() => getScrud(optsB)('a', 'create', 1, { a: 1 }))
213+
214+
let contextData
215+
216+
const setContext = (_0, _1, _3, ctx) => { contextData = ctx }
217+
const optsC = { host: 'localhost', port, jwt, before: setContext }
218+
219+
assert.falsy(contextData)
220+
await getScrud(optsC)('a', 'create', 1, { a: 1 }, undefined, true)
221+
assert.truthy(contextData)
213222
})
214223

215224
test('throttle options apply as expected', async (assert) => {

0 commit comments

Comments
 (0)