Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

kinvey-flex-sdk

Package Overview
Dependencies
Maintainers
1
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kinvey-flex-sdk - npm Package Compare versions

Comparing version 1.2.0 to 2.0.0-b.1

lib/service/auth.js

7

CHANGELOG.md
## Changelog
### 2.0.0
* Add Auth support to flex under flex.auth
* Change request to context
* Fix completion handlers to properly handle pre/post step context
* Added `setBody` and `setQuery` to data/function completion hanlder
* DEPRECATED: Passing the entity directly in the `complete()` method. Will be removed in future version.
### 1.2.0

@@ -4,0 +11,0 @@ * Append flex SDK version to task response

27

lib/flex.js

@@ -19,4 +19,5 @@ /**

const functions = require('./service/functions');
const auth = require('./service/auth');
const moduleGenerator = require('./service/moduleGenerator');
const logger = require('./service/logger');
const moduleGenerator = require('./service/moduleGenerator');
const receiver = require('kinvey-code-task-runner');

@@ -39,4 +40,5 @@

this.data = data;
this.functions = functions;
this.data = this.dataLink = data;
this.functions = this.businessLogic = functions;
this.auth = auth;
this.logger = logger;

@@ -47,7 +49,9 @@ this.moduleGenerator = moduleGenerator;

// TODO Remove legacy taskType values
const taskReceivedCallback = (task, completionCallback) => {
const taskReceivedCallback = ((task, completionCallback) => {
task.sdkVersion = this.version;
if (task.taskType === 'data' || task.taskType === 'dataLink') {
return this.data.process(task, this.moduleGenerator.generate(task), completionCallback);
} else if (task.taskType === 'serviceDiscovery') {
if (!this[task.taskType] || task.taskType === 'logger' || task.taskType === 'moduleGenerator') {
return new Error('Invalid task Type');
}
if (task.taskType === 'serviceDiscovery') {
task.discoveryObjects = {

@@ -59,11 +63,12 @@ dataLink: {

handlers: this.functions.getHandlers()
},
auth: {
handlers: this.auth.getHandlers()
}
};
return completionCallback(null, task);
} else if (task.taskType === 'functions' || task.taskType === 'businessLogic') {
return this.functions.process(task, this.moduleGenerator.generate(task), completionCallback);
}
return new Error('Invalid task Type');
};
return this[task.taskType].process(task, this.moduleGenerator.generate(task), completionCallback);
});

@@ -70,0 +75,0 @@ receiver.start(options, taskReceivedCallback, (err) => {

@@ -136,3 +136,2 @@ /**

let dataOp = '';
const dataCompletionHandler = kinveyCompletionHandler(task, callback);

@@ -202,2 +201,3 @@ try {

const dataCompletionHandler = kinveyCompletionHandler(task, false, callback);
const operationHandler = serviceObjectToProcess.resolve(dataOp);

@@ -204,0 +204,0 @@ return operationHandler(task.request, dataCompletionHandler, modules);

@@ -43,8 +43,20 @@ /**

const functionCompletionHandler = kinveyCompletionHandler(task, callback);
const context = {};
const currentContext = task.hookType === 'post' ? task.response : task.request;
context.method = task.request.method;
context.headers = currentContext.headers;
context.username = task.request.username;
context.userId = task.request.userId;
context.objectName = task.request.objectName || task.request.collectionNamemo;
context.hookType = task.hookType;
if (task.request.entityId) {
context.entityId = task.request.entityId;
}
try {
task.request.body = JSON.parse(task.request.body);
context.body = JSON.parse(currentContext.body);
} catch (error) {
if ((task.request.body) && typeof task.request.body !== 'object') {
if ((currentContext.body) && typeof currentContext.body !== 'object') {
const result = task.response;

@@ -56,6 +68,23 @@ result.body = kinveyErrors.generateKinveyError('BadRequest', 'Request body is not JSON');

}
context.body = currentContext.body;
}
if (task.request.query != null) {
try {
task.request.query = JSON.parse(task.request.query);
} catch (error) {
if ((task.request.query) && typeof task.request.query !== 'object') {
const result = task.response;
result.body = kinveyErrors.generateKinveyError('BadRequest', 'Request body is not JSON');
result.statusCode = result.body.statusCode;
delete result.body.statusCode;
return callback(task);
}
context.query = task.request.query;
}
}
const functionCompletionHandler = kinveyCompletionHandler(task, task.hookType === 'pre', callback);
const functionHandler = resolve(task.taskName);
return functionHandler(task.request, functionCompletionHandler, modules);
return functionHandler(context, functionCompletionHandler, modules);
}

@@ -62,0 +91,0 @@

@@ -15,12 +15,42 @@ /**

function createCompletionHandler(task, callback) {
function completionHandler(entity) {
if (!entity) entity = {};
function createCompletionHandler(task, updateRequestBody, callback) {
function completionHandler(body) {
if (typeof updateRequestBody === 'function') {
callback = updateRequestBody;
updateRequestBody = false;
}
const responseCallback = callback;
const result = task.response;
const result = {};
result.body = entity;
function normalizeError(error) {
if (error instanceof Error) {
return {
name: error.name,
message: error.message,
stack: error.stack
};
}
return error;
}
function updateBody(body) {
if (body != null) {
result.body = body;
}
}
updateBody(body);
const api = {
setBody(body) {
updateBody(body);
return this;
},
setQuery(query) {
if (query) {
result.query = query;
}
return this;
},
created() {

@@ -43,3 +73,3 @@ result.statusCode = 201;

description: 'The requested entity or entities were not found in the serviceObject',
debug: debug || result.body || {}
debug: normalizeError(debug) || normalizeError(result.body) || {}
};

@@ -53,3 +83,3 @@ return this;

description: 'Unable to understand request',
debug: debug || result.body || {}
debug: normalizeError(debug) || normalizeError(result.body) || {}
};

@@ -63,3 +93,3 @@ return this;

description: 'Invalid credentials. Please retry your request with correct credentials',
debug: debug || result.body || {}
debug: normalizeError(debug) || normalizeError(result.body) || {}
};

@@ -73,3 +103,3 @@ return this;

description: 'The request is forbidden',
debug: debug || result.body || {}
debug: normalizeError(debug) || normalizeError(result.body) || {}
};

@@ -83,3 +113,3 @@ return this;

description: 'The request is not allowed',
debug: debug || result.body || {}
debug: normalizeError(debug) || normalizeError(result.body) || {}
};

@@ -93,3 +123,3 @@ return this;

description: 'The request invoked a method that is not implemented',
debug: debug || result.body || {}
debug: normalizeError(debug) || normalizeError(result.body) || {}
};

@@ -103,3 +133,3 @@ return this;

description: 'The Flex Service had a runtime error. See debug message for details',
debug: debug || result.body || {}
debug: normalizeError(debug) || normalizeError(result.body) || {}
};

@@ -112,4 +142,9 @@ return this;

}
result.body = JSON.stringify(result.body);
task.response.body = JSON.stringify(result.body || task.response.body);
task.response.statusCode = result.statusCode;
task.request.query = JSON.stringify(task.request.query);
task.request.body = JSON.stringify(task.request.body);
// TODO: Ensure that the result is a kinveyEntity or array of kinveyEntities or {count} object

@@ -121,3 +156,3 @@ //

result.continue = false;
task.response.continue = false;
return responseCallback(null, task);

@@ -129,7 +164,18 @@ },

}
result.body = JSON.stringify(result.body);
if (updateRequestBody) {
task.request.body = JSON.stringify(result.body || task.request.body);
task.request.query = JSON.stringify(result.query || task.request.query);
task.response.body = JSON.stringify(task.response.body);
} else {
task.request.body = JSON.stringify(task.request.body);
task.request.query = JSON.stringify(task.request.query);
task.response.body = JSON.stringify(result.body || task.response.body);
}
task.response.statusCode = result.statusCode;
// TODO: Ensure that the result is a kinveyEntity or array of kinveyEntities or {count} object
result.continue = true;
task.response.continue = true;
return responseCallback(null, task);

@@ -136,0 +182,0 @@ }

@@ -101,3 +101,3 @@ /**

requestOptions.url += entityId;
_makeRequest(requestOptions, callback);
return _makeRequest(requestOptions, callback);
}

@@ -119,3 +119,3 @@

_makeRequest(requestOptions, callback);
return _makeRequest(requestOptions, callback);
}

@@ -136,3 +136,3 @@

requestOptions.url += entityId;
_makeRequest(requestOptions, callback);
return _makeRequest(requestOptions, callback);
}

@@ -154,3 +154,3 @@

_makeRequest(requestOptions, callback);
return _makeRequest(requestOptions, callback);
}

@@ -173,3 +173,3 @@

_makeRequest(requestOptions, callback);
return _makeRequest(requestOptions, callback);
}

@@ -176,0 +176,0 @@

@@ -23,3 +23,3 @@ /**

const skipBl = this.skipBl = storeOptions.skipBl || false;
const useMasterSecret = this.useMasterSecret = storeOptions.useMasterSecret || false;
this.useMasterSecret = storeOptions.useMasterSecret || false;

@@ -95,3 +95,3 @@ function _buildKinveyRequest(options = {}) {

_makeRequest(requestOptions, callback);
return _makeRequest(requestOptions, callback);
}

@@ -123,3 +123,3 @@

_makeRequest(requestOptions, callback);
return _makeRequest(requestOptions, callback);
}

@@ -140,3 +140,3 @@

requestOptions.url += userId;
_makeRequest(requestOptions, callback);
return _makeRequest(requestOptions, callback);
}

@@ -152,3 +152,3 @@

_makeRequest(requestOptions, callback);
return _makeRequest(requestOptions, callback);
}

@@ -170,3 +170,3 @@

_makeRequest(requestOptions, callback);
return _makeRequest(requestOptions, callback);
}

@@ -192,3 +192,3 @@

_makeRequest(requestOptions, callback);
return _makeRequest(requestOptions, callback);
}

@@ -214,3 +214,3 @@

_makeRequest(requestOptions, callback);
return _makeRequest(requestOptions, callback);
}

@@ -232,3 +232,3 @@

_makeRequest(requestOptions, callback);
return _makeRequest(requestOptions, callback);
}

@@ -251,3 +251,3 @@

_makeRequest(requestOptions, callback);
return _makeRequest(requestOptions, callback);
}

@@ -254,0 +254,0 @@

{
"name": "kinvey-flex-sdk",
"version": "1.2.0",
"version": "2.0.0-b.1",
"description": "SDK for creating Kinvey Flex Services",

@@ -20,3 +20,3 @@ "engines": {

"bson": "0.4.23",
"kinvey-code-task-runner": "1.2.1",
"kinvey-code-task-runner": "2.0.0",
"kinvey-datalink-errors": "0.2.2",

@@ -23,0 +23,0 @@ "lodash.forown": "4.2.0",

@@ -64,4 +64,5 @@ # Kinvey Flex SDK (beta)

| query | The query string |
| id | The id of the entity if applicable |
| entityId | The id of the entity if applicable |
| method | The original http method |
| hookType | The type of event hook. Valid values are `pre` for a pre-data hook, `post` for a post-data hook, and `customEndpoint` for an endpoint hook. Defaults to `customEndpoint` |

@@ -116,7 +117,7 @@ For service discovery

The data events take a handler function, which takes three arguments: `request`, `complete`, and `modules`. `request` represents the request made to Kinvey, and `complete` is a completion handler for completing the data request. The `modules` argument is an object containing several libraries for accessing Kinvey functionality via your service (for more information, see the section on [modules](#modules)).
The data events take a handler function, which takes three arguments: `context`, `complete`, and `modules`. `context` represents the current context of the request made to Kinvey, and `complete` is a completion handler for completing the data request. The `modules` argument is an object containing several libraries for accessing Kinvey functionality via your service (for more information, see the section on [modules](#modules)).
#### request Object
#### context Object
The request object contains the following properties
The `context` object contains the following properties

@@ -134,3 +135,3 @@ | property | description |

The completion handlers object follows a builder pattern for creating the handler's response. The pattern for the completion handler is `complete(<entity>).<status>.<done|next>`
The completion handlers object follows a builder pattern for creating the handler's response. The pattern for the completion handler is `complete()[.setBody(<entity>)].<status>.<done|next>`

@@ -145,18 +146,36 @@ For example, a sample completion handler is:

The `complete` handler takes either an entity, an array of entities, or an error description. The result of the `complete` handler is an object of status functions.
The `complete` handler initiates the complete process. It prodives a set of methods for altering the context of the request, and a series of status functions. For the data step, the only relevant context-altering method is `setBody`, which takes either an entity, an array of entities, an error description or Error Object.
Note that a body must be explicitly set using `setBody`. The entity must be a JSON object or a JSON array for successful responses. For example:
```
// Sets the response to include an entity.
complete({"foo", "bar"});
complete().setBody({"foo", "bar"});
// Sets the response to include an array of entities
complete([{"foo":"bar"}, {"abc":"123}]);
complete().setBody([{"foo":"bar"}, {"abc":"123}]);
```
// Sets the response to an error string, to be used with error status codes
complete("Record 123 was not found");
The `setBody` method is not required. If you want to return an empty object, simply call `complete()`. If you need to pass an empty array, you *must* call setBody with the empty Array.
```
// empty body
complete();
//empty array
complete().setBody([]);
```
For errors, you can either pass a string as the error message, or a JavaScript `Error` object.
```
// Sets the response to an error string, to be used with error status functions
complete().setBody("Record 123 was not found");
// Sets the response to an error object, to be used with error status functions
complete().setBody(new Error("Record 123 was not found");
```
##### Status Functions
Status functions set the valid status codes for a Data Link Connector. The status function also sets the body to a Kinvey-formatted error, and uses the value passed into the `complete` function as the debug property, if it is present.
Status functions set the valid status codes for a Flex Data operation. The status function also sets the body to a Kinvey-formatted error, and uses the value passed into the `setData` function as the debug property, if it is present.

@@ -182,6 +201,6 @@ The available status functions are:

// Return that the record has been created
complete(myRecord).created();
complete().setBody(myRecord).created();
// Entity wasn't found
complete("The given entity wasn't found").notFound();
complete().setBody("The given entity wasn't found").notFound();
```

@@ -195,6 +214,6 @@

// This will continue the request chain
complete(myEntity).ok().next();
complete().setBody(myEntity).ok().next();
// This will end the request chain with no further processing
complete(myEntity).ok().done();
complete().ok().done();
```

@@ -223,3 +242,3 @@

// return the entity
return complete(entity).ok().next();
return complete().setBody(entity).ok().next();
}

@@ -246,2 +265,4 @@ }

Note: In previous versions of the Flex-SDK, entities were passed in the `complete` method. This functionality is deprecated, and will be removed in a future version of the SDK.
## [FlexFunctions](#flex-functions)

@@ -268,7 +289,7 @@

Like the Data handlers, FlexFunctions take a handler functions with three arguments: `request`, `complete`, and `modules`. `request` represents the handler request's current state, and `complete` is a completion handler for completing the function. The `modules` argument is an object containing several libraries for accessing Kinvey functionality via your service (for more information, see the section on [modules](#modules)).
Like the Data handlers, FlexFunctions take a handler functions with three arguments: `context`, `complete`, and `modules`. `context` represents the current context state of the Kinvey request, and `complete` is a completion handler for completing the function. The `modules` argument is an object containing several libraries for accessing Kinvey functionality via your service (for more information, see the section on [modules](#modules)).
#### request Object
#### context Object
The request object contains the following properties:
The `context` object contains the following properties:

@@ -281,8 +302,8 @@ | property | description |

| collectionName | the name of the collection |
| body | the HTTP body |
| body | the data entity, entities, or error message associated with the request |
| query | the query object |
#### completion Handler
The `context` object will contain the appropriate state for the stage of the Kinvey request pipeline that the request is currently in. For example, for events that are executed before a data request (pre-hooks), the context will contain the request body and query.
The completion handlers object follows a builder pattern for creating the FlexFunctions' response. The pattern for the completion handler is `complete(<entity>).<status>.<done|next>`
The completion handlers object follows a builder pattern for creating the handler's response. The pattern for the completion handler is `complete().[setBody(<entity>).setQuery(<query>)].<status>.<done|next>`

@@ -292,26 +313,43 @@ For example, a sample completion handler is:

```
complete(myEntity).ok().next()
complete().setBody(myEntity).setQuery(myQuery).ok().next()
```
The entity is optional, as it will not always be returned.
##### complete
You can also alter the request object by making changes to the query, body, or headers objects. If the request body is modified, it will be persisted back for subsequent steps in the request pipeline.
The `complete` handler initiates the complete process. It prodives a set of methods for altering the context of the request, and a series of status functions. For the data step, the only relevant context-altering method is `setBody`, which takes either an entity, an array of entities, an error description or Error Object.
##### complete
The context-altering methods are:
| method | description |
| ------- | -------|
| setBody | Sets the data entity or entities to be passed to the next step of the pipeline or as the final result. Also used to pass error messages/objects.
| setQuery | Replaces the query object with an altered query object. This is only useful in pre-hook functions. |
The `complete` handler takes either an entity, an array of entities, or an error description. The result of the `complete` handler is an object of status functions.
Note that a body must be explicitly set using `setBody`. The entity must be a JSON object or a JSON array for successful responses. For example:
```
// Sets the context to include an entity and altered query.
complete().setBody({"foo", "bar"}).setQuery({query: {foo: 'bar'});
// No entity as part of the response
// Sets the response to include an array of entities
complete().setBody([{"foo":"bar"}, {"abc":"123}]);
```
The `setBody` method is not required. If you want to return an empty object, simply call `complete()`. If you need to pass an empty array, you *must* call setBody with the empty Array.
```
// empty body
complete();
// Sets the response to include an entity.
complete({"foo", "bar"});
//empty array
complete().setBody([]);
```
For errors, you can either pass a string as the error message, or a JavaScript `Error` object.
// Sets the response to include an array of entities
complete([{"foo":"bar"}, {"abc":"123}]);
```
// Sets the response to an error string, to be used with error status functions
complete().setBody("Record 123 was not found");
// Sets the response to an error string, to be used with error status codes
complete("Record 123 was not found");
// Sets the response to an error object, to be used with error status functions
complete().setBody(new Error("Record 123 was not found");
```

@@ -321,3 +359,3 @@

Status functions set the valid status codes for the request. The status function also sets the body to a Kinvey-formatted error, and uses the value passed into the `complete` function as the debug property, if it is present.
Status functions set the valid status codes for a Flex Function operation. The status function also sets the body to a Kinvey-formatted error, and uses the value passed into the `setData` function as the debug property, if it is present.

@@ -343,6 +381,6 @@ The available status functions are:

// Return that the record has been created
complete(myRecord).created();
complete().setBody(myRecord).created();
// Entity wasn't found
complete("The given entity wasn't found").notFound();
complete().setBody("The given entity wasn't found").notFound();
```

@@ -352,12 +390,12 @@

Once the status is set, you can end the processing of the handler request with either `done` or `next`. Most requests should normally end with `next`, which will continue the Kinvey request pipeline. `done` will return the response that was set in the handler, and end request processing without executing any further part of the kinvey request pipeline.
Once the status is set, you can end the processing of the handler request with either `done` or `next`. Most requests should normally end with `next`, which will continue the Kinvey request pipeline. `done` will return the response that was set in the completion handler, and end request processing without executing any further functions.
```
// This will continue the request chain
complete(myEntity).ok().next();
complete().setBody(myEntity).ok().next();
// This will end the request chain with no further processing
complete(myEntity).ok().done();
complete().ok().done();
```
### Example

@@ -374,11 +412,11 @@

function getRedLineSchedule(req, complete, modules) {
function getRedLineSchedule(context, complete, modules) {
request.get('http://developer.mbta.com/Data/Red.json', (err, response, body) => {
// if error, return an error
if (err) {
return complete("Could not complete request").runtimeError().done();
return complete().setBody("Could not complete request").runtimeError().done();
}
//otherwise, return the results
return complete(body).ok().done();
return complete().setBody(body).ok().done();
});

@@ -395,2 +433,4 @@

Note: In previous versions of the Flex-SDK, entities were passed in the `complete` method. This functionality is deprecated, and will be removed in a future version of the SDK.
## [Executing a Long-running Script](#long-running-scripts)

@@ -442,3 +482,3 @@

function initiateCalcAndPost(req, complete, modules) {
function initiateCalcAndPost(context, complete, modules) {
// Since the calc and post data function may take a long time, execute it asynchronously

@@ -464,3 +504,3 @@ setImmediate(calcAndPostData);

// data handler
function onGetById(request, complete, modules) {
function onGetById(context, complete, modules) {
const appKey = modules.backendContext.getAppKey();

@@ -467,0 +507,0 @@ // etc...

@@ -38,3 +38,3 @@ /**

describe('data', () => {
describe('FlexData', () => {
afterEach((done) => {

@@ -179,6 +179,19 @@ data.clearAll();

});
it('can explicitly set a body', (done) => {
const task = sampleTask();
data.serviceObject(serviceObjectName).onInsert((context, complete) => complete()
.setBody({ foo: 'bar' })
.ok()
.done());
return data.process(task, {}, (err, result) => {
console.log(result);
should.not.exist(err);
result.response.body.should.eql(JSON.stringify({ foo: 'bar' }));
done();
});
});
it('can process an insert', (done) => {
const task = sampleTask();
data.serviceObject(serviceObjectName).onInsert((request) => {
request.entityId = task.request.entityId;
data.serviceObject(serviceObjectName).onInsert((context) => {
context.entityId = task.request.entityId;
return done();

@@ -190,4 +203,4 @@ });

const task = sampleTask();
data.serviceObject(serviceObjectName).onInsert((request, complete, modules) => {
request.should.be.an.Object();
data.serviceObject(serviceObjectName).onInsert((context, complete, modules) => {
context.should.be.an.Object();
complete.should.be.a.Function();

@@ -200,7 +213,6 @@ modules.should.be.an.Object();

it('can process an update', (done) => {
let task = sampleTask();
const task = sampleTask();
task.method = 'PUT';
data.serviceObject(serviceObjectName).onUpdate((request) => {
task = sampleTask();
request.entityId = task.request.entityId;
data.serviceObject(serviceObjectName).onUpdate((context) => {
context.entityId.should.eql(task.request.entityId);
return done();

@@ -210,7 +222,7 @@ });

});
it('can process an update and include request, complete, and modules', (done) => {
it('can process an update and include context, complete, and modules', (done) => {
const task = sampleTask();
task.method = 'PUT';
data.serviceObject(serviceObjectName).onUpdate((request, complete, modules) => {
request.should.be.an.Object();
data.serviceObject(serviceObjectName).onUpdate((context, complete, modules) => {
context.should.be.an.Object();
complete.should.be.a.Function();

@@ -223,3 +235,3 @@ modules.should.be.an.Object();

it('can process a get all', (done) => {
let task = sampleTask();
const task = sampleTask();
task.method = 'GET';

@@ -230,5 +242,5 @@ delete task.entityId;

delete task.request.query;
data.serviceObject(serviceObjectName).onGetAll((request) => {
task = sampleTask();
request.entityId = task.request.entityId;
data.serviceObject(serviceObjectName).onGetAll((context) => {
should.not.exist(context.entityId);
should.not.exist(context.query);
return done();

@@ -238,3 +250,3 @@ });

});
it('can process a getAll and include request, complete, and modules', (done) => {
it('can process a getAll and include context, complete, and modules', (done) => {
const task = sampleTask();

@@ -246,4 +258,4 @@ task.method = 'GET';

delete task.request.query;
data.serviceObject(serviceObjectName).onGetAll((request, complete, modules) => {
request.should.be.an.Object();
data.serviceObject(serviceObjectName).onGetAll((context, complete, modules) => {
context.should.be.an.Object();
complete.should.be.a.Function();

@@ -256,9 +268,8 @@ modules.should.be.an.Object();

it('can process a get by Id', (done) => {
let task = sampleTask();
const task = sampleTask();
task.method = 'GET';
delete task.query;
delete task.request.query;
data.serviceObject(serviceObjectName).onGetById((request) => {
task = sampleTask();
request.entityId = task.request.entityId;
data.serviceObject(serviceObjectName).onGetById((context) => {
context.entityId.should.eql(task.request.entityId);
return done();

@@ -268,3 +279,3 @@ });

});
it('can process a getById and include request, complete, and modules', (done) => {
it('can process a getById and include context, complete, and modules', (done) => {
const task = sampleTask();

@@ -274,4 +285,4 @@ task.method = 'GET';

delete task.request.query;
data.serviceObject(serviceObjectName).onGetById((request, complete, modules) => {
request.should.be.an.Object();
data.serviceObject(serviceObjectName).onGetById((context, complete, modules) => {
context.should.be.an.Object();
complete.should.be.a.Function();

@@ -284,3 +295,3 @@ modules.should.be.an.Object();

it('can process a get by query', (done) => {
let task = sampleTask();
const task = sampleTask();
task.method = 'GET';

@@ -291,5 +302,5 @@ delete task.entityId;

task.query = {};
data.serviceObject(serviceObjectName).onGetByQuery((request) => {
task = sampleTask();
request.entityId = task.request.entityId;
data.serviceObject(serviceObjectName).onGetByQuery((context) => {
should.not.exist(context.entityId);
context.query.should.eql(task.request.query);
return done();

@@ -299,3 +310,3 @@ });

});
it('can process a getByQuery and include request, complete, and modules', (done) => {
it('can process a getByQuery and include context, complete, and modules', (done) => {
const task = sampleTask();

@@ -307,4 +318,4 @@ task.method = 'GET';

task.query = {};
data.serviceObject(serviceObjectName).onGetByQuery((request, complete, modules) => {
request.should.be.an.Object();
data.serviceObject(serviceObjectName).onGetByQuery((context, complete, modules) => {
context.should.be.an.Object();
complete.should.be.a.Function();

@@ -320,3 +331,3 @@ modules.should.be.an.Object();

};
let task = sampleTask();
const task = sampleTask();
task.method = 'GET';

@@ -326,5 +337,4 @@ delete task.entityId;

task.query = query;
data.serviceObject(serviceObjectName).onGetByQuery((request) => {
task = sampleTask();
request.query.should.eql(query);
data.serviceObject(serviceObjectName).onGetByQuery((context) => {
context.query.should.eql(query);
return done();

@@ -334,7 +344,7 @@ });

});
it('can process a get by query with a query just in the request', (done) => {
it('can process a get by query with a query just in the context', (done) => {
const query = {
foo: 'bar'
};
let task = sampleTask();
const task = sampleTask();
task.method = 'GET';

@@ -344,5 +354,4 @@ delete task.entityId;

task.request.query = query;
data.serviceObject(serviceObjectName).onGetByQuery((request) => {
task = sampleTask();
request.query.should.eql(query);
data.serviceObject(serviceObjectName).onGetByQuery((context) => {
context.query.should.eql(query);
return done();

@@ -353,3 +362,3 @@ });

it('can process a delete all', (done) => {
let task = sampleTask();
const task = sampleTask();
task.method = 'DELETE';

@@ -360,5 +369,5 @@ delete task.entityId;

delete task.request.query;
data.serviceObject(serviceObjectName).onDeleteAll((request) => {
task = sampleTask();
request.entityId = task.request.entityId;
data.serviceObject(serviceObjectName).onDeleteAll((context) => {
should.not.exist(context.entityId);
should.not.exist(context.query);
return done();

@@ -368,3 +377,3 @@ });

});
it('can process a deleteAll and include request, complete, and modules', (done) => {
it('can process a deleteAll and include context, complete, and modules', (done) => {
const task = sampleTask();

@@ -376,4 +385,4 @@ task.method = 'DELETE';

delete task.request.query;
data.serviceObject(serviceObjectName).onDeleteAll((request, complete, modules) => {
request.should.be.an.Object();
data.serviceObject(serviceObjectName).onDeleteAll((context, complete, modules) => {
context.should.be.an.Object();
complete.should.be.a.Function();

@@ -386,9 +395,8 @@ modules.should.be.an.Object();

it('can process a delete by Id', (done) => {
let task = sampleTask();
const task = sampleTask();
task.method = 'DELETE';
delete task.query;
delete task.request.query;
data.serviceObject(serviceObjectName).onDeleteById((request) => {
task = sampleTask();
request.entityId = task.request.entityId;
data.serviceObject(serviceObjectName).onDeleteById((context) => {
context.entityId.should.eql(task.request.entityId);
return done();

@@ -398,3 +406,3 @@ });

});
it('can process a deleteById and include request, complete, and modules', (done) => {
it('can process a deleteById and include context, complete, and modules', (done) => {
const task = sampleTask();

@@ -404,4 +412,4 @@ task.method = 'DELETE';

delete task.request.query;
data.serviceObject(serviceObjectName).onDeleteById((request, complete, modules) => {
request.should.be.an.Object();
data.serviceObject(serviceObjectName).onDeleteById((context, complete, modules) => {
context.should.be.an.Object();
complete.should.be.a.Function();

@@ -414,3 +422,3 @@ modules.should.be.an.Object();

it('can process a delete by query', (done) => {
let task = sampleTask();
const task = sampleTask();
task.method = 'DELETE';

@@ -421,5 +429,5 @@ delete task.entityId;

task.query = {};
data.serviceObject(serviceObjectName).onDeleteByQuery((request) => {
task = sampleTask();
request.entityId = task.request.entityId;
data.serviceObject(serviceObjectName).onDeleteByQuery((context) => {
should.not.exist(context.entityId);
context.query.should.eql(task.request.query);
return done();

@@ -436,4 +444,4 @@ });

task.query = {};
data.serviceObject(serviceObjectName).onDeleteByQuery((request, complete, modules) => {
request.should.be.an.Object();
data.serviceObject(serviceObjectName).onDeleteByQuery((context, complete, modules) => {
context.should.be.an.Object();
complete.should.be.a.Function();

@@ -446,3 +454,3 @@ modules.should.be.an.Object();

it('can process a count all', (done) => {
let task = sampleTask();
const task = sampleTask();
task.method = 'GET';

@@ -454,5 +462,5 @@ task.endpoint = '_count';

delete task.request.query;
data.serviceObject(serviceObjectName).onGetCount((request) => {
task = sampleTask();
request.entityId = task.request.entityId;
data.serviceObject(serviceObjectName).onGetCount((context) => {
should.not.exist(context.entityId);
should.not.exist(context.query);
return done();

@@ -462,3 +470,3 @@ });

});
it('can process a count all and include request, complete, and modules', (done) => {
it('can process a count all and include context, complete, and modules', (done) => {
const task = sampleTask();

@@ -471,4 +479,4 @@ task.method = 'GET';

delete task.request.query;
data.serviceObject(serviceObjectName).onGetCount((request, complete, modules) => {
request.should.be.an.Object();
data.serviceObject(serviceObjectName).onGetCount((context, complete, modules) => {
context.should.be.an.Object();
complete.should.be.a.Function();

@@ -481,3 +489,3 @@ modules.should.be.an.Object();

it('can process a count by query', (done) => {
let task = sampleTask();
const task = sampleTask();
task.method = 'GET';

@@ -489,5 +497,5 @@ task.endpoint = '_count';

task.query = {};
data.serviceObject(serviceObjectName).onGetCountByQuery((request) => {
task = sampleTask();
request.entityId = task.request.entityId;
data.serviceObject(serviceObjectName).onGetCountByQuery((context) => {
should.not.exist(context.entityId);
context.query.should.eql(task.request.query);
return done();

@@ -505,4 +513,4 @@ });

task.query = {};
data.serviceObject(serviceObjectName).onGetCountByQuery((request, complete, modules) => {
request.should.be.an.Object();
data.serviceObject(serviceObjectName).onGetCountByQuery((context, complete, modules) => {
context.should.be.an.Object();
complete.should.be.a.Function();

@@ -518,3 +526,3 @@ modules.should.be.an.Object();

const task = sampleTask();
data.serviceObject(serviceObjectName).onInsert((request, complete) => complete().ok().next());
data.serviceObject(serviceObjectName).onInsert((context, complete) => complete().ok().next());
return data.process(task, {}, (err, result) => {

@@ -529,3 +537,3 @@ should.not.exist(err);

const task = sampleTask();
data.serviceObject(serviceObjectName).onInsert((request, complete) => complete({ foo: 'bar' }).ok().next());
data.serviceObject(serviceObjectName).onInsert((context, complete) => complete({ foo: 'bar' }).ok().next());
return data.process(task, {}, (err, result) => {

@@ -542,3 +550,3 @@ should.not.exist(err);

const task = sampleTask();
data.serviceObject(serviceObjectName).onInsert((request, complete) => complete({ foo: 'bar' }).created().next());
data.serviceObject(serviceObjectName).onInsert((context, complete) => complete({ foo: 'bar' }).created().next());
return data.process(task, {}, (err, result) => {

@@ -555,3 +563,3 @@ should.not.exist(err);

const task = sampleTask();
data.serviceObject(serviceObjectName).onInsert((request, complete) => complete({ foo: 'bar' }).accepted().next());
data.serviceObject(serviceObjectName).onInsert((context, complete) => complete({ foo: 'bar' }).accepted().next());
return data.process(task, {}, (err, result) => {

@@ -568,3 +576,3 @@ should.not.exist(err);

const task = sampleTask();
data.serviceObject(serviceObjectName).onInsert((request, complete) => complete('This is a bad request')
data.serviceObject(serviceObjectName).onInsert((context, complete) => complete('This is a bad request')
.badRequest().next());

@@ -583,3 +591,3 @@ return data.process(task, {}, (err, result) => {

const task = sampleTask();
data.serviceObject(serviceObjectName).onInsert((request, complete) => complete('You are not authorized!')
data.serviceObject(serviceObjectName).onInsert((context, complete) => complete('You are not authorized!')
.unauthorized().next());

@@ -599,3 +607,3 @@ return data.process(task, {}, (err, result) => {

const task = sampleTask();
data.serviceObject(serviceObjectName).onInsert((request, complete) => complete('Forbidden!').forbidden().next());
data.serviceObject(serviceObjectName).onInsert((context, complete) => complete('Forbidden!').forbidden().next());
return data.process(task, {}, (err, result) => {

@@ -613,3 +621,3 @@ should.not.exist(err);

const task = sampleTask();
data.serviceObject(serviceObjectName).onInsert((request, complete) => complete('The request is not found!')
data.serviceObject(serviceObjectName).onInsert((context, complete) => complete('The request is not found!')
.notFound().next());

@@ -629,3 +637,3 @@ return data.process(task, {}, (err, result) => {

const task = sampleTask();
data.serviceObject(serviceObjectName).onInsert((request, complete) => complete('The request is not allowed!')
data.serviceObject(serviceObjectName).onInsert((context, complete) => complete('The request is not allowed!')
.notAllowed().next());

@@ -644,3 +652,3 @@ return data.process(task, {}, (err, result) => {

const task = sampleTask();
data.serviceObject(serviceObjectName).onInsert((request, complete) => complete('This isn\'t implemented')
data.serviceObject(serviceObjectName).onInsert((context, complete) => complete('This isn\'t implemented')
.notImplemented().next());

@@ -659,3 +667,3 @@ return data.process(task, {}, (err, result) => {

const task = sampleTask();
data.serviceObject(serviceObjectName).onInsert((request, complete) => complete('There was some error in the app!')
data.serviceObject(serviceObjectName).onInsert((context, complete) => complete('There was some error in the app!')
.runtimeError().next());

@@ -674,3 +682,3 @@ return data.process(task, {}, (err, result) => {

const task = sampleTask();
data.serviceObject(serviceObjectName).onInsert((request, complete) => complete({ foo: 'bar' }).ok().next());
data.serviceObject(serviceObjectName).onInsert((context, complete) => complete({ foo: 'bar' }).ok().next());
return data.process(task, {}, (err, result) => {

@@ -688,3 +696,3 @@ should.not.exist(err);

const task = sampleTask();
data.serviceObject(serviceObjectName).onInsert((request, complete) => complete({ foo: 'bar' }).ok().done());
data.serviceObject(serviceObjectName).onInsert((context, complete) => complete({ foo: 'bar' }).ok().done());
return data.process(task, {}, (err, result) => {

@@ -691,0 +699,0 @@ should.not.exist(err);

@@ -36,2 +36,3 @@ /**

should.exist(flex.version);
should.exist(flex.auth);
flex.version.should.eql(flexPackageJson.version);

@@ -38,0 +39,0 @@ return done();

@@ -15,3 +15,3 @@ /**

const logic = require('../../lib/service/functions');
const functions = require('../../lib/service/functions');
const should = require('should');

@@ -30,7 +30,8 @@ const testTaskName = 'myTaskName';

endpoint: null,
hookType: 'pre',
request: {
method: 'POST',
headers: {},
headers: { },
entityId: '12345',
serviceObjectName: null
objectName: null
},

@@ -45,2 +46,68 @@ response: {

function samplePostTask(name) {
return {
taskType: 'functions',
taskName: name,
method: 'POST',
endpoint: null,
hookType: 'post',
request: {
method: 'POST',
headers: { requestHeader: 'foo' },
entityId: '12345',
objectName: null,
body: { bar: 'foo' }
},
response: {
status: 0,
headers: { responseHeader: 'bar' },
body: [{ id: quickRandom() }, { id: quickRandom() }, { id: quickRandom() }]
}
};
}
function samplePreTask(name) {
return {
taskType: 'functions',
taskName: name,
method: 'POST',
endpoint: null,
hookType: 'pre',
request: {
method: 'POST',
headers: { requestHeader: 'foo' },
entityId: '12345',
objectName: null,
body: { bar: 'foo' }
},
response: {
status: 0,
headers: { responseHeader: 'bar' },
body: [{ id: quickRandom() }, { id: quickRandom() }, { id: quickRandom() }]
}
};
}
function sampleCustomEndpoint(name) {
return {
taskType: 'functions',
taskName: name,
method: 'POST',
endpoint: null,
hookType: 'customEndpoint',
request: {
method: 'POST',
headers: { requestHeader: 'foo' },
entityId: '12345',
objectName: null,
body: { bar: 'foo' }
},
response: {
status: 0,
headers: { responseHeader: 'bar' },
body: [{ id: quickRandom() }, { id: quickRandom() }, { id: quickRandom() }]
}
};
}
function sampleBadTask() {

@@ -59,11 +126,11 @@ return {

describe('business logic', () => {
describe('logic registration', () => {
describe('FlexFunctions', () => {
describe('function registration', () => {
afterEach((done) => {
logic.clearAll();
functions.clearAll();
return done();
});
it('can register a logic task', (done) => {
logic.register(testTaskName, () => done());
const fn = logic.resolve(testTaskName);
it('can register a functions task', (done) => {
functions.register(testTaskName, () => done());
const fn = functions.resolve(testTaskName);
return fn();

@@ -74,9 +141,9 @@ });

afterEach((done) => {
logic.clearAll();
functions.clearAll();
return done();
});
it('returns an array of all registered business logic handlers', (done) => {
it('returns an array of all registered function handlers', (done) => {
const testHandlerName = 'testObj';
logic.register(testHandlerName, () => {});
const discoveredHandlers = logic.getHandlers();
functions.register(testHandlerName, () => {});
const discoveredHandlers = functions.getHandlers();
should.exist(discoveredHandlers[0]);

@@ -87,20 +154,27 @@ discoveredHandlers.length.should.eql(1);

});
it('returns an empty array if no function handlers have been registered', (done) => {
const discoveredHandlers = functions.getHandlers();
Array.isArray(discoveredHandlers).should.eql(true);
discoveredHandlers.length.should.eql(0);
done();
});
});
describe('logic processing', () => {
describe('functions processing', () => {
afterEach((done) => {
logic.clearAll();
functions.clearAll();
return done();
});
it('can process a logic task', (done) => {
it('can process a functions task', (done) => {
const taskName = quickRandom();
const task = sampleTask(taskName);
logic.register(taskName, (request) => done());
logic.process(task, {}, () => {});
functions.register(taskName, () => done());
functions.process(task, {}, () => {});
});
it('includes request, completion, and module handlers in a logic task', (done) => {
it('includes context, completion, and module handlers in a functions task', (done) => {
const taskName = quickRandom();
const task = sampleTask(taskName);
logic.register(taskName, (request, complete, modules) => {
request.should.be.an.Object();
functions.register(taskName, (context, complete, modules) => {
context.should.be.an.Object();
complete.should.be.a.Function();

@@ -110,3 +184,3 @@ modules.should.be.an.Object();

});
logic.process(task, {}, () => {});
functions.process(task, {}, () => {});
});

@@ -116,3 +190,3 @@ });

afterEach((done) => {
logic.clearAll();
functions.clearAll();
return done();

@@ -122,3 +196,3 @@ });

const task = sampleTask(null);
return logic.process(task, null, (err) => {
return functions.process(task, null, (err) => {
err.response.statusCode.should.eql(400);

@@ -131,3 +205,3 @@ err.response.body.debug.should.eql('No task name to execute');

const task = sampleBadTask(null);
return logic.process(task, null, (err) => {
return functions.process(task, null, (err) => {
err.response.statusCode.should.eql(400);

@@ -141,4 +215,4 @@ err.response.body.debug.should.eql('Request body is not JSON');

const task = sampleTask(taskName);
logic.register(taskName, (request, complete) => complete().ok().next());
return logic.process(task, null, (err, result) => {
functions.register(taskName, (context, complete) => complete().ok().next());
return functions.process(task, null, (err, result) => {
should.not.exist(err);

@@ -153,4 +227,4 @@ result.response.statusCode.should.eql(200);

const task = sampleTask(taskName);
logic.register(taskName, (request, complete) => complete({ foo: 'bar' }).ok().next());
return logic.process(task, null, (err, result) => {
functions.register(taskName, (context, complete) => complete({ foo: 'bar' }).ok().done());
return functions.process(task, null, (err, result) => {
result.response.statusCode.should.eql(200);

@@ -163,7 +237,276 @@ result.response.body.should.eql(JSON.stringify({

});
it('should explicitly set a body', (done) => {
const taskName = quickRandom();
const task = samplePreTask(taskName);
functions.register(taskName, (context, complete) => complete()
.setBody({ foo: 'bar' })
.ok()
.done());
return functions.process(task, null, (err, result) => {
result.response.statusCode.should.eql(200);
result.response.body.should.eql(JSON.stringify({
foo: 'bar'
}));
return done();
});
});
it('should explicitly set a query', (done) => {
const taskName = quickRandom();
const task = samplePreTask(taskName);
functions.register(taskName, (context, complete) => complete()
.setQuery({ foo: 'bar' })
.ok()
.next());
return functions.process(task, null, (err, result) => {
result.response.statusCode.should.eql(200);
result.request.query.should.eql(JSON.stringify({
foo: 'bar'
}));
return done();
});
});
it('should include the response body and headers when task is a post task', (done) => {
const taskName = quickRandom();
const task = samplePostTask(taskName);
functions.register(taskName, (context, complete) => {
context.body.should.eql(task.response.body);
context.body.should.not.eql(task.request.body);
context.headers.should.eql(task.response.headers);
context.headers.should.not.eql(task.request.headers);
return complete(task.response.body).ok().next();
});
return functions.process(task, null, (err, result) => {
return done();
});
});
it('should include the request body and headers when task is a pre task', (done) => {
const taskName = quickRandom();
const task = samplePreTask(taskName);
functions.register(taskName, (context, complete) => {
context.body.should.eql(task.request.body);
context.body.should.not.eql(task.response.body);
context.headers.should.eql(task.request.headers);
context.headers.should.not.eql(task.response.headers);
return complete(task.response.body).ok().next();
});
return functions.process(task, null, (err, result) => {
return done();
});
});
it('should include the request body and headers when task is a custom endpoint task', (done) => {
const taskName = quickRandom();
const task = sampleCustomEndpoint(taskName);
functions.register(taskName, (context, complete) => {
context.body.should.eql(task.request.body);
context.body.should.not.eql(task.response.body);
context.headers.should.eql(task.request.headers);
context.headers.should.not.eql(task.response.headers);
return complete(task.response.body).ok().next();
});
return functions.process(task, null, (err, result) => {
return done();
});
});
it('should set the response body when task is a post task and the request is ended', (done) => {
const taskName = quickRandom();
const task = samplePostTask(taskName);
functions.register(taskName, (context, complete) => complete({ foo: 'bar' }).ok().done());
return functions.process(task, null, (err, result) => {
should.not.exist(err);
result.response.statusCode.should.eql(200);
result.response.body.should.eql(JSON.stringify({
foo: 'bar'
}));
return done();
});
});
it('should set the response body when task is a post task and the request is continued', (done) => {
const taskName = quickRandom();
const task = samplePostTask(taskName);
functions.register(taskName, (context, complete) => complete({ foo: 'bar' }).ok().next());
return functions.process(task, null, (err, result) => {
should.not.exist(err);
result.response.statusCode.should.eql(200);
result.response.body.should.eql(JSON.stringify({
foo: 'bar'
}));
return done();
});
});
it('should keep existing response body when task is a post task and no body is set and the request is ended', (done) => {
const taskName = quickRandom();
const task = samplePostTask(taskName);
functions.register(taskName, (context, complete) => complete({ foo: 'bar' }).ok().done());
return functions.process(task, null, (err, result) => {
should.not.exist(err);
result.response.statusCode.should.eql(200);
result.response.body.should.eql(task.response.body);
return done();
});
});
it('should keep existing response body when task is a post task and no body is set and the request is continued', (done) => {
const taskName = quickRandom();
const task = samplePostTask(taskName);
functions.register(taskName, (context, complete) => complete({ foo: 'bar' }).ok().next());
return functions.process(task, null, (err, result) => {
should.not.exist(err);
result.response.statusCode.should.eql(200);
result.response.body.should.eql(task.response.body);
return done();
});
});
it('should include the request body and headers when task is a pre task', (done) => {
const taskName = quickRandom();
const task = samplePreTask(taskName);
functions.register(taskName, (context, complete) => {
context.body.should.eql(task.request.body);
context.body.should.not.eql(task.response.body);
context.headers.should.eql(task.request.headers);
context.headers.should.not.eql(task.response.headers);
return complete(task.response.body).ok().next();
});
return functions.process(task, null, (err, result) => {
return done();
});
});
it('should set the response body when task is a pre task and the request is ended', (done) => {
const taskName = quickRandom();
const task = samplePreTask(taskName);
functions.register(taskName, (context, complete) => complete({ foo: 'bar' }).ok().done());
return functions.process(task, null, (err, result) => {
should.not.exist(err);
result.response.statusCode.should.eql(200);
result.response.body.should.eql(JSON.stringify({
foo: 'bar'
}));
result.response.body.should.not.eql(result.request.body);
return done();
});
});
it('should set the request body when task is a pre task and the request is continued', (done) => {
const taskName = quickRandom();
const task = samplePreTask(taskName);
functions.register(taskName, (context, complete) => complete({ foo: 'bar' }).ok().next());
return functions.process(task, null, (err, result) => {
should.not.exist(err);
result.response.statusCode.should.eql(200);
result.request.body.should.eql(JSON.stringify({
foo: 'bar'
}));
result.request.body.should.not.eql(result.response.body);
return done();
});
});
it('should keep empty response body when task is a pre task and no body is set and the request is ended', (done) => {
const taskName = quickRandom();
const task = samplePreTask(taskName);
functions.register(taskName, (context, complete) => complete({ foo: 'bar' }).ok().done());
return functions.process(task, null, (err, result) => {
should.not.exist(err);
result.response.statusCode.should.eql(200);
result.response.body.should.eql(task.response.body);
return done();
});
});
it('should keep existing response body when task is a pre task and no body is set and the request is continued', (done) => {
const taskName = quickRandom();
const task = samplePreTask(taskName);
functions.register(taskName, (context, complete) => complete({ foo: 'bar' }).ok().next());
return functions.process(task, null, (err, result) => {
should.not.exist(err);
result.response.statusCode.should.eql(200);
result.request.body.should.eql(JSON.stringify({
foo: 'bar'
}));
result.request.body.should.not.eql(result.response.body);
return done();
});
});
it('should include the request body and headers when task is a custom endpoint task', (done) => {
const taskName = quickRandom();
const task = sampleCustomEndpoint(taskName);
functions.register(taskName, (context, complete) => {
context.body.should.eql(task.request.body);
context.body.should.not.eql(task.response.body);
context.headers.should.eql(task.request.headers);
context.headers.should.not.eql(task.response.headers);
return complete(task.response.body).ok().next();
});
return functions.process(task, null, (err, result) => {
return done();
});
});
it('should set the response body when task is a custom endpoint task and the request is ended', (done) => {
const taskName = quickRandom();
const task = sampleCustomEndpoint(taskName);
functions.register(taskName, (context, complete) => complete({ foo: 'bar' }).ok().done());
return functions.process(task, null, (err, result) => {
should.not.exist(err);
result.response.statusCode.should.eql(200);
result.response.body.should.eql(JSON.stringify({
foo: 'bar'
}));
result.response.body.should.not.eql(result.request.body);
return done();
});
});
it('should set the response body when task is a custom endpoint task and the request is continued', (done) => {
const taskName = quickRandom();
const task = sampleCustomEndpoint(taskName);
functions.register(taskName, (context, complete) => complete({ foo: 'bar' }).ok().next());
return functions.process(task, null, (err, result) => {
should.not.exist(err);
result.response.statusCode.should.eql(200);
result.response.body.should.eql(JSON.stringify({
foo: 'bar'
}));
result.response.body.should.not.eql(result.request.body);
return done();
});
});
it('should keep empty response body when task is a custom endpoint and no body is set and the request is ended', (done) => {
const taskName = quickRandom();
const task = sampleCustomEndpoint(taskName);
functions.register(taskName, (context, complete) => complete({ foo: 'bar' }).ok().done());
return functions.process(task, null, (err, result) => {
should.not.exist(err);
result.response.statusCode.should.eql(200);
result.response.body.should.eql(task.response.body);
return done();
});
});
it('should keep existing response body when task is a custom endpoint and no body is set and the request is continued', (done) => {
const taskName = quickRandom();
const task = sampleCustomEndpoint(taskName);
functions.register(taskName, (context, complete) => complete({ foo: 'bar' }).ok().next());
return functions.process(task, null, (err, result) => {
should.not.exist(err);
result.response.statusCode.should.eql(200);
result.response.body.should.eql(task.response.body);
result.request.body.should.not.eql(result.response.body);
return done();
});
});
it('should return a 201 created', (done) => {
const taskName = quickRandom();
const task = sampleTask(taskName);
logic.register(taskName, (request, complete) => complete({ foo: 'bar' }).created().next());
return logic.process(task, null, (err, result) => {
functions.register(taskName, (context, complete) => complete({ foo: 'bar' }).created().done());
return functions.process(task, null, (err, result) => {
should.not.exist(err);

@@ -180,4 +523,4 @@ result.response.statusCode.should.eql(201);

const task = sampleTask(taskName);
logic.register(taskName, (request, complete) => complete({ foo: 'bar' }).accepted().next());
return logic.process(task, null, (err, result) => {
functions.register(taskName, (context, complete) => complete({ foo: 'bar' }).accepted().done());
return functions.process(task, null, (err, result) => {
should.not.exist(err);

@@ -194,7 +537,8 @@ result.response.statusCode.should.eql(202);

const task = sampleTask(taskName);
logic.register(taskName, (request, complete) => complete('This is a bad request').badRequest().next());
return logic.process(task, null, (err, result) => {
functions.register(taskName, (context, complete) => complete('This is a bad request').badRequest().done());
return functions.process(task, null, (err, result) => {
should.not.exist(err);
result.response.statusCode.should.eql(400);
result.response.body = JSON.parse(result.response.body);
result.response.body.error.should.eql('BadRequest');

@@ -209,4 +553,4 @@ result.response.body.description.should.eql('Unable to understand request');

const task = sampleTask(taskName);
logic.register(taskName, (request, complete) => complete('You are not authorized!').unauthorized().next());
return logic.process(task, null, (err, result) => {
functions.register(taskName, (context, complete) => complete('You are not authorized!').unauthorized().done());
return functions.process(task, null, (err, result) => {
should.not.exist(err);

@@ -225,4 +569,4 @@ result.response.statusCode.should.eql(401);

const task = sampleTask(taskName);
logic.register(taskName, (request, complete) => complete('Forbidden!').forbidden().next());
return logic.process(task, null, (err, result) => {
functions.register(taskName, (context, complete) => complete('Forbidden!').forbidden().done());
return functions.process(task, null, (err, result) => {
should.not.exist(err);

@@ -240,4 +584,4 @@ result.response.statusCode.should.eql(403);

const task = sampleTask(taskName);
logic.register(taskName, (request, complete) => complete('The request is not found!').notFound().next());
return logic.process(task, null, (err, result) => {
functions.register(taskName, (context, complete) => complete('The request is not found!').notFound().done());
return functions.process(task, null, (err, result) => {
should.not.exist(err);

@@ -256,4 +600,4 @@ result.response.statusCode.should.eql(404);

const task = sampleTask(taskName);
logic.register(taskName, (request, complete) => complete('The request is not allowed!').notAllowed().next());
return logic.process(task, null, (err, result) => {
functions.register(taskName, (context, complete) => complete('The request is not allowed!').notAllowed().done());
return functions.process(task, null, (err, result) => {
should.not.exist(err);

@@ -271,4 +615,4 @@ result.response.statusCode.should.eql(405);

const task = sampleTask(taskName);
logic.register(taskName, (request, complete) => complete('This isn\'t implemented').notImplemented().next());
return logic.process(task, null, (err, result) => {
functions.register(taskName, (context, complete) => complete('This isn\'t implemented').notImplemented().done());
return functions.process(task, null, (err, result) => {
should.not.exist(err);

@@ -286,5 +630,5 @@ result.response.statusCode.should.eql(501);

const task = sampleTask(taskName);
logic.register(taskName, (request, complete) => complete('There was some error in the app!')
.runtimeError().next());
return logic.process(task, null, (err, result) => {
functions.register(taskName, (context, complete) => complete('There was some error in the app!')
.runtimeError().done());
return functions.process(task, null, (err, result) => {
should.not.exist(err);

@@ -301,5 +645,5 @@ result.response.statusCode.should.eql(550);

const taskName = quickRandom();
const task = sampleTask(taskName);
logic.register(taskName, (request, complete) => complete({ foo: 'bar' }).ok().next());
return logic.process(task, null, (err, result) => {
const task = samplePostTask(taskName);
functions.register(taskName, (context, complete) => complete({ foo: 'bar' }).ok().next());
return functions.process(task, null, (err, result) => {
should.not.exist(err);

@@ -317,4 +661,4 @@ result.response.statusCode.should.eql(200);

const task = sampleTask(taskName);
logic.register(taskName, (request, complete) => complete({ foo: 'bar' }).ok().done());
return logic.process(task, null, (err, result) => {
functions.register(taskName, (context, complete) => complete({ foo: 'bar' }).ok().done());
return functions.process(task, null, (err, result) => {
should.not.exist(err);

@@ -321,0 +665,0 @@ result.response.statusCode.should.eql(200);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc