@janiscommerce/api
Advanced tools
Comparing version 1.1.0 to 1.2.0
{ | ||
"exclude": [ | ||
"tests/", | ||
"coverage/", | ||
@@ -4,0 +5,0 @@ ".eslintrc.js" |
@@ -76,7 +76,5 @@ 'use strict'; | ||
} catch(err) { | ||
// devuelve un 500 | ||
return { | ||
code: 500, | ||
message: err.message || 'internal server error' | ||
code: 500, // returns a 500 http code | ||
message: err.message | ||
}; | ||
@@ -97,9 +95,6 @@ } | ||
} catch(err) { | ||
// devuelve un 400 | ||
return { | ||
code: 400, | ||
code: 400, // returns a 400 http code | ||
message: err.message || 'data invalid' | ||
}; | ||
} | ||
@@ -115,12 +110,10 @@ | ||
} catch(err) { | ||
return { | ||
code: 500, | ||
code: 500, // returns a 500 http code | ||
message: err.message || 'internal server error' | ||
}; | ||
} | ||
return { | ||
code: 200, | ||
code: 200, // returns a 200 http code | ||
body: result | ||
@@ -127,0 +120,0 @@ }; |
@@ -14,3 +14,4 @@ 'use strict'; | ||
API_NOT_FOUND: 6, | ||
PROCESS_METHOD_NOT_FOUND: 7 | ||
INVALID_API: 7, | ||
PROCESS_METHOD_NOT_FOUND: 8 | ||
}; | ||
@@ -17,0 +18,0 @@ } |
@@ -61,5 +61,11 @@ 'use strict'; | ||
const controller = new APIController(); | ||
let apiController; | ||
return controller; | ||
try { | ||
apiController = new APIController(); | ||
} catch(err) { | ||
throw new APIError(`API Controller '${filePath}' is not a api class`, APIError.codes.INVALID_API); | ||
} | ||
return apiController; | ||
} | ||
@@ -66,0 +72,0 @@ |
@@ -7,2 +7,7 @@ # Changelog | ||
## [1.2.0] - 2019-05-29 | ||
### Added | ||
- Api tests | ||
- Excluded tests folder from coverage | ||
## [1.1.0] - 2019-05-29 | ||
@@ -9,0 +14,0 @@ ### Added |
{ | ||
"name": "@janiscommerce/api", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A package for managing API from any origin", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -15,2 +15,50 @@ 'use strict'; | ||
class ValidProcessClass { | ||
async process() { | ||
return 1; | ||
} | ||
} | ||
class ValidateOkClass extends ValidProcessClass { | ||
async validate() { | ||
return true; | ||
} | ||
} | ||
class ValidateRejectsDefaultClass extends ValidProcessClass { | ||
async validate() { | ||
throw new Error(); | ||
} | ||
} | ||
class ValidateRejectsClass extends ValidProcessClass { | ||
async validate() { | ||
throw new Error('some data invalid'); | ||
} | ||
} | ||
class StructClass extends ValidProcessClass { | ||
get struct() { | ||
return { foo: 'string' }; | ||
} | ||
} | ||
class StructMultipleClass extends ValidProcessClass { | ||
get struct() { | ||
return [{ foo: 'string', bar: 'number' }]; | ||
} | ||
} | ||
class ProcessRejectsClass { | ||
async process() { | ||
throw new Error('some internal error'); | ||
} | ||
} | ||
class ProcessRejectsDefaultClass { | ||
async process() { | ||
throw new Error(); | ||
} | ||
} | ||
const mock = (endpoint, classContent) => { | ||
@@ -21,40 +69,12 @@ mockRequire(path.join(Fetcher.apiPath, endpoint), classContent); | ||
before(() => { | ||
mock('invalid-api-class-endpoint/list', { foo: 'bar' }); | ||
mock('no-process-endpoint/list', class {}); | ||
mock('process-throws-endpoint/post', class { | ||
async process() { | ||
throw new Error('some internal error'); | ||
} | ||
}); | ||
mock('validate-throws-endpoint/put', class { | ||
async validate() { | ||
throw new Error('some data invalid'); | ||
} | ||
async process() { | ||
return 1; | ||
} | ||
}); | ||
mock('struct-endpoint/list', class { | ||
get struct() { | ||
return [{ foo: 'string' }]; | ||
} | ||
async process() { | ||
return 1; | ||
} | ||
}); | ||
mock('validate-correctly-endpoint/list', class { | ||
async validate() { | ||
return true; | ||
} | ||
async process() { | ||
return 1; | ||
} | ||
}); | ||
mock('valid-endpoint/list', class { | ||
async process() { | ||
return 1; | ||
} | ||
}); | ||
mock('process-rejects-endpoint/post', ProcessRejectsClass); | ||
mock('process-rejects-default-message-endpoint/post', ProcessRejectsDefaultClass); | ||
mock('validate-rejects-endpoint/put', ValidateRejectsClass); | ||
mock('validate-rejects-default-message-endpoint/post', ValidateRejectsDefaultClass); | ||
mock('struct-endpoint/list', StructClass); | ||
mock('struct-multiple-endpoint/list', StructMultipleClass); | ||
mock('validate-correctly-endpoint/list', ValidateOkClass); | ||
mock('valid-endpoint/list', ValidProcessClass); | ||
}); | ||
@@ -66,2 +86,7 @@ | ||
const test = async(myApi, code) => { | ||
const result = await myApi.dispatch(); | ||
assert.deepEqual(result.code, code); | ||
}; | ||
describe('should reject', function() { | ||
@@ -133,17 +158,18 @@ const testConstructorReject = (APIErrorCode, requestData) => { | ||
const test = async myApi => { | ||
const result = await myApi.dispatch(); | ||
assert.deepEqual(result.code, 500); | ||
}; | ||
it('when api file not found', async function() { | ||
await test(new API({ | ||
endpoint: 'api/unknown-endpoint' | ||
})); | ||
}), 500); | ||
}); | ||
it('when api file hasn\'t a class', async function() { | ||
await test(new API({ | ||
endpoint: 'api/invalid-api-class-endpoint' | ||
}), 500); | ||
}); | ||
it('when api file found but api object has not a process method', async function() { | ||
await test(new API({ | ||
endpoint: 'api/no-process-endpoint' | ||
})); | ||
}), 500); | ||
}); | ||
@@ -153,6 +179,13 @@ | ||
await test(new API({ | ||
endpoint: 'api/process-throws-endpoint', | ||
endpoint: 'api/process-rejects-endpoint', | ||
method: 'post' | ||
})); | ||
}), 500); | ||
}); | ||
it('when api process method throw an internal server error - default message', async function() { | ||
await test(new API({ | ||
endpoint: 'api/process-rejects-default-message-endpoint', | ||
method: 'post' | ||
}), 500); | ||
}); | ||
}); | ||
@@ -162,20 +195,39 @@ | ||
const test = async myApi => { | ||
const result = await myApi.dispatch(); | ||
assert.deepEqual(result.code, 400); | ||
}; | ||
it('when api validate method throw a data invalid error', async function() { | ||
it('when api validate method throw a data invalid', async function() { | ||
await test(new API({ | ||
endpoint: 'api/validate-throws-endpoint', | ||
endpoint: 'api/validate-rejects-endpoint', | ||
method: 'put' | ||
})); | ||
}), 400); | ||
}); | ||
it('when api validate method throw a data invalid - default message', async function() { | ||
await test(new API({ | ||
endpoint: 'api/validate-rejects-default-message-endpoint', | ||
method: 'post' | ||
}), 400); | ||
}); | ||
it('when api data is invlaid against struct', async function() { | ||
await test(new API({ | ||
endpoint: 'api/struct-endpoint' | ||
})); | ||
}), 400); | ||
await test(new API({ | ||
endpoint: 'api/struct-endpoint', | ||
data: { unknownField: '123' } | ||
}), 400); | ||
}); | ||
it('when api data is invlaid against struct multiple', async function() { | ||
await test(new API({ | ||
endpoint: 'api/struct-multiple-endpoint', | ||
data: { foo: '123' } | ||
}), 400); | ||
await test(new API({ | ||
endpoint: 'api/struct-multiple-endpoint', | ||
data: { bar: 123 } | ||
}), 400); | ||
}); | ||
}); | ||
@@ -185,11 +237,6 @@ | ||
const test = async myApi => { | ||
const result = await myApi.dispatch(); | ||
assert.deepEqual(result.code, 200); | ||
}; | ||
it('when api validates correctly', async function() { | ||
await test(new API({ | ||
endpoint: 'api/validate-correctly-endpoint' | ||
})); | ||
}), 200); | ||
}); | ||
@@ -201,3 +248,3 @@ | ||
data: { foo: 'bar' } | ||
})); | ||
}), 200); | ||
}); | ||
@@ -208,3 +255,3 @@ | ||
endpoint: 'api/valid-endpoint' | ||
})); | ||
}), 200); | ||
}); | ||
@@ -211,0 +258,0 @@ }); |
17677
535