Skip to content

Commit 9b27ba5

Browse files
BKNDLSS-26312 Add ability to set headers to Backendless.BL.CustomServices.invoke (#219)
1 parent 08869e9 commit 9b27ba5

File tree

4 files changed

+145
-9
lines changed

4 files changed

+145
-9
lines changed

backendless.d.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,11 +485,15 @@ declare module Backendless {
485485
}
486486

487487
export interface CustomServicesI {
488-
invoke(serviceName: string, method: string, parameters: Object): Promise<any>;
488+
invoke(serviceName: string, method: string, parameters?: object): Promise<any>;
489489

490-
invoke(serviceName: string, method: string, parameters: Object, executionType: string): Promise<any>;
490+
invoke(serviceName: string, method: string, parameters: object|null, executionType?: string): Promise<any>;
491491

492-
invoke(serviceName: string, method: string, executionType: string): Promise<any>;
492+
invoke(serviceName: string, method: string, executionType?: string): Promise<any>;
493+
494+
invoke(serviceName: string, method: string, parameters: object|null, executionType?: string): Promise<any>;
495+
496+
invoke(serviceName: string, method: string, parameters: object|null, options?: { executionType?: string, httpRequestHeaders?: object }): Promise<any>;
493497
}
494498

495499
/**

src/bl/custom-services.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default class CustomServices {
55
this.app = app
66
}
77

8-
async invoke(serviceName, methodName, parameters, executionType) {
8+
async invoke(serviceName, methodName, parameters, options) {
99
if (!serviceName || typeof serviceName !== 'string') {
1010
throw new Error('Service Name must be provided and must be a string.')
1111
}
@@ -14,15 +14,26 @@ export default class CustomServices {
1414
throw new Error('Method Name must be provided and must be a string.')
1515
}
1616

17+
if (typeof options === 'string') {
18+
options = {
19+
executionType: options
20+
}
21+
}
22+
1723
if (typeof parameters === 'string' && isExecutionType(parameters)) {
18-
executionType = parameters
24+
options = {
25+
executionType: parameters
26+
}
27+
1928
parameters = undefined
2029
}
2130

22-
const headers = {}
31+
options = options || {}
32+
33+
const headers = { ...options.httpRequestHeaders }
2334

24-
if (executionType) {
25-
headers[EXECUTION_TYPE_HEADER] = executionType
35+
if (options.executionType) {
36+
headers[EXECUTION_TYPE_HEADER] = options.executionType
2637
}
2738

2839
return this.app.request.post({

test/tsd.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1247,9 +1247,15 @@ function testCounters() {
12471247
function testCustomServices() {
12481248
const serviceName: string = 'str';
12491249
const method: string = 'str';
1250-
const parameters: Object = {};
1250+
const parameters: object = {};
12511251
let resultObj: any
12521252
let promiseAny: Promise<any>
1253+
let executionType: string = Backendless.BL.ExecutionTypes.SYNC
1254+
let httpRequestHeaders: object = { 'custom-header': 'value' }
1255+
let options1: object = {}
1256+
let options2: object = { httpRequestHeaders }
1257+
let options3: object = { executionType }
1258+
let options4: object = { executionType, httpRequestHeaders }
12531259

12541260
promiseAny = Backendless.CustomServices.invoke(serviceName, method, parameters);
12551261
promiseAny = Backendless.CustomServices.invoke(serviceName, method, parameters);
@@ -1264,6 +1270,15 @@ function testCustomServices() {
12641270
promiseAny = Backendless.APIServices.invoke(serviceName, method, parameters, Backendless.BL.ExecutionTypes.ASYNC);
12651271
promiseAny = Backendless.APIServices.invoke(serviceName, method, parameters, Backendless.BL.ExecutionTypes.ASYNC_LOW_PRIORITY);
12661272
promiseAny = Backendless.APIServices.invoke(serviceName, method, Backendless.BL.ExecutionTypes.ASYNC_LOW_PRIORITY);
1273+
1274+
promiseAny = Backendless.APIServices.invoke(serviceName, method, parameters, options1);
1275+
promiseAny = Backendless.APIServices.invoke(serviceName, method, parameters, options2);
1276+
promiseAny = Backendless.APIServices.invoke(serviceName, method, parameters, options3);
1277+
promiseAny = Backendless.APIServices.invoke(serviceName, method, parameters, options4);
1278+
promiseAny = Backendless.APIServices.invoke(serviceName, method, null, options1);
1279+
promiseAny = Backendless.APIServices.invoke(serviceName, method, null, options2);
1280+
promiseAny = Backendless.APIServices.invoke(serviceName, method, null, options3);
1281+
promiseAny = Backendless.APIServices.invoke(serviceName, method, null, options4);
12671282
}
12681283

12691284
function testLogging() {

test/unit/specs/bl/api-services.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,110 @@ describe('<BusinessLogic> API Services', function() {
156156

157157
})
158158
})
159+
160+
describe('Options and Custom Headers', function() {
161+
it('should run with options', async () => {
162+
const req1 = prepareMockRequest()
163+
const req2 = prepareMockRequest()
164+
const req3 = prepareMockRequest()
165+
166+
const options1 = {
167+
executionType : Backendless.BL.ExecutionTypes.ASYNC,
168+
httpRequestHeaders: { 'custom-header': 'headerValue' }
169+
}
170+
171+
const options2 = {
172+
executionType : Backendless.BL.ExecutionTypes.ASYNC_LOW_PRIORITY,
173+
httpRequestHeaders: { 'custom-header': 'headerValue' }
174+
}
175+
176+
const options3 = {
177+
executionType : Backendless.BL.ExecutionTypes.SYNC,
178+
httpRequestHeaders: { 'custom-header': 'headerValue' }
179+
}
180+
181+
await Backendless.BL.CustomServices.invoke(serviceName, methodName, args, options1)
182+
183+
expect(req1).to.deep.include({
184+
method : 'POST',
185+
path : `${APP_PATH}/services/${serviceName}/${methodName}`,
186+
body : args,
187+
headers: {
188+
'Content-Type' : 'application/json',
189+
'bl-execution-type': 'async',
190+
'custom-header' : 'headerValue',
191+
},
192+
})
193+
194+
await Backendless.BL.CustomServices.invoke(serviceName, methodName, null, options2)
195+
196+
expect(req2).to.deep.include({
197+
method : 'POST',
198+
path : `${APP_PATH}/services/${serviceName}/${methodName}`,
199+
body : null,
200+
headers: {
201+
'custom-header' : 'headerValue',
202+
'bl-execution-type': 'async-low-priority'
203+
},
204+
})
205+
206+
await Backendless.BL.CustomServices.invoke(serviceName, methodName, null, options3)
207+
208+
expect(req3).to.deep.include({
209+
method : 'POST',
210+
path : `${APP_PATH}/services/${serviceName}/${methodName}`,
211+
body : null,
212+
headers: {
213+
'custom-header' : 'headerValue',
214+
'bl-execution-type': 'sync'
215+
},
216+
})
217+
})
218+
219+
it('should ignore options if parameters specified as string (execution type)', async function() {
220+
const req = prepareMockRequest()
221+
222+
const options = {
223+
executionType : Backendless.BL.ExecutionTypes.ASYNC,
224+
httpRequestHeaders: { 'custom-header': 'headerValue' }
225+
}
226+
227+
await Backendless.BL.CustomServices.invoke(serviceName, methodName, Backendless.BL.ExecutionTypes.SYNC, options)
228+
229+
expect(req).to.deep.include({
230+
method : 'POST',
231+
path : `${APP_PATH}/services/${serviceName}/${methodName}`,
232+
body : undefined,
233+
headers: {
234+
'bl-execution-type': 'sync'
235+
},
236+
})
237+
})
238+
239+
it('should not override httpRequestHeaders if executionType is specified', async function() {
240+
const req = prepareMockRequest()
241+
242+
const options = {
243+
executionType : Backendless.BL.ExecutionTypes.ASYNC,
244+
httpRequestHeaders: { 'custom-header': 'headerValue' }
245+
}
246+
247+
await Backendless.BL.CustomServices.invoke(serviceName, methodName, args, options)
248+
249+
expect(req).to.deep.include({
250+
method : 'POST',
251+
path : `${APP_PATH}/services/${serviceName}/${methodName}`,
252+
body : args,
253+
headers: {
254+
'Content-Type' : 'application/json',
255+
'bl-execution-type': 'async',
256+
'custom-header' : 'headerValue',
257+
},
258+
})
259+
260+
expect(options.httpRequestHeaders).to.deep.equal({
261+
'custom-header': 'headerValue'
262+
})
263+
})
264+
})
159265
})

0 commit comments

Comments
 (0)