apis-builder
Advanced tools
Comparing version 1.0.0 to 1.1.0
65
index.js
@@ -1,4 +0,5 @@ | ||
const inflection = require('path-proxy/node_modules/inflection') | ||
const pathProxy = require('path-proxy') | ||
const promisifyCall = require('promisify-call') | ||
const ajv = require('./ajv') | ||
const _ = require('lodash') | ||
@@ -24,3 +25,2 @@ class Builder { | ||
const properties = action.properties | ||
const requiredProps = action.required | ||
@@ -43,3 +43,2 @@ // HACKY special case for members bulk add and send MIME endpoints | ||
err = new Error(`Invalid number of params in path (expected ${pathParams.length}, got ${this.params.length}).`) | ||
return fn(err) | ||
@@ -52,18 +51,5 @@ } | ||
// check required payload properties | ||
if (requiredProps && requiredProps.length > 0) { | ||
if (!data) { | ||
err = new Error('Missing parameters.') | ||
} else { | ||
for (let i = 0; i < requiredProps.length; i++) { | ||
const prop = requiredProps[i] | ||
if (typeof data[prop] === 'undefined') { | ||
err = new Error(`Missing parameter '${prop}'`) | ||
break | ||
} | ||
} | ||
} | ||
} | ||
let schema = { properties } | ||
if (action.required) { schema.required = action.required } | ||
err = ajv(schema, data) | ||
if (err) { | ||
@@ -73,30 +59,7 @@ return fn(err) | ||
// check payload property types | ||
for (const key in properties) { | ||
if (data && data[key]) { | ||
const type = properties[key].type | ||
let dataType = typeof data[key] | ||
if (Array.isArray(data[key])) { | ||
dataType = 'array' | ||
} | ||
if (Array.isArray(type)) { | ||
if (type.indexOf(dataType) === -1) { | ||
err = new Error(`Invalid parameter type. ${key} must be of type: ${type}.`) | ||
break | ||
} | ||
} else if (dataType !== type) { | ||
err = new Error(`Invalid parameter type. ${key} must be of type: ${type}.`) | ||
break | ||
} | ||
} | ||
this.client = this.base | ||
if (typeof this.client[action.method.toLowerCase()] !== 'function') { | ||
console.error(`WARNING: 'this.client.${action.method.toLowerCase()}' is not a function`) | ||
return | ||
} | ||
if (err) { | ||
return fn(err) | ||
} | ||
this.client = this.base | ||
return this.client[action.method.toLowerCase()](requestPath, data, fn) | ||
@@ -109,14 +72,6 @@ } | ||
constructor.prototype[getName(actionName)] = promisifed | ||
constructor.prototype[_.camelCase(actionName)] = promisifed | ||
} | ||
} | ||
function getName (name) { | ||
name = name.toLowerCase() | ||
name = inflection.dasherize(name).replace(/-/g, '_') | ||
name = inflection.camelize(name, true) | ||
return name | ||
} | ||
function build (baseObj, resources) { | ||
@@ -123,0 +78,0 @@ const b = new Builder(baseObj, resources) |
{ | ||
"name": "apis-builder", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "./node_modules/mocha/bin/_mocha test/ --bail" | ||
"test": "mocha test" | ||
}, | ||
@@ -12,3 +12,4 @@ "author": "leeqiang <leesumiir@gmail.com>", | ||
"dependencies": { | ||
"inflection": "^1.12.0", | ||
"ajv": "^6.4.0", | ||
"lodash": "^4.17.5", | ||
"path-proxy": "^1.0.0", | ||
@@ -15,0 +16,0 @@ "promisify-call": "^2.0.4" |
@@ -22,6 +22,19 @@ /* global describe, before, it */ | ||
}, { | ||
description: '测试2', | ||
description: '测试3', | ||
href: '/tests/{id}/hello/path', | ||
method: 'POST', | ||
title: 'say' | ||
}, { | ||
description: '测试4', | ||
href: '/tests/{id}:a', | ||
method: 'GET', | ||
title: 'get-by-id' | ||
}, { | ||
description: '测试5: ajv', | ||
href: '/tests/{_id}:ajv', | ||
method: 'GET', | ||
title: 'ajv', | ||
properties: { | ||
_otherId: { type: 'string', format: 'objectid' } | ||
} | ||
}] | ||
@@ -51,2 +64,19 @@ } | ||
}) | ||
it('should ok get by id', function (done) { | ||
let sdk = new SDK() | ||
expect(typeof sdk.tests('123').getById === 'function').toBe(true) | ||
done() | ||
}) | ||
it('should ok ajv', async function () { | ||
let sdk = new SDK() | ||
try { | ||
await sdk.tests('123').ajv({ | ||
_otherId: '123' | ||
}) | ||
} catch (err) { | ||
expect(err.message.includes('_otherId')).toBe(true) | ||
} | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
24524
7
187
1
34
4
+ Addedajv@^6.4.0
+ Addedlodash@^4.17.5
+ Addedajv@6.12.6(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addeduri-js@4.4.1(transitive)
- Removedinflection@^1.12.0
- Removedinflection@1.13.4(transitive)