Skip to content

Commit

Permalink
guess params / data (Fix #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
bumpmann committed Feb 25, 2016
1 parent 18584db commit 734757e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 206 deletions.
122 changes: 64 additions & 58 deletions lib/api/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,48 +22,54 @@ class Endpoint {
constructor(app, endpointConfig) {
this.app = app
this.history = app.history
this.method = endpointConfig.method || 'GET'
this.method = (endpointConfig.method && endpointConfig.method.toLowerCase()) || 'get'

this.name = endpointConfig.name
this.desc = endpointConfig.desc
this.url = endpointConfig.url

this.base = endpointConfig.base

this.params = []
this.data = []
this.params = []
this.data = []
this.permissions = endpointConfig.permissions || []

if (typeof endpointConfig.query == 'function') {
this.params = endpointConfig.params || []
this.data = endpointConfig.data || []
this.params = endpointConfig.params || []
this.data = endpointConfig.data || []
this.query = endpointConfig.query
}
else {
if (typeof endpointConfig.query.entity == 'string') {
this.entity = this.app.entities.get(endpointConfig.query.entity)
}
else {
this.entity = this.app.entities.get(endpointConfig.query.entity.name)
}
//console.log(this.entity)
if (typeof endpointConfig.query.entity == 'string') {
this.entity = this.app.entities.get(endpointConfig.query.entity)
}
else {
this.entity = this.app.entities.get(endpointConfig.query.entity.name)
}

//console.log(this.entity)
this.query = this.entity.getQuery(endpointConfig.query.id)

let params = this.query.getAllParams()
for (let p of params) {
if (this.method.toLowerCase() == 'get' || this.method.toLowerCase() == 'delete') {
this.params.push(p)
}
else {
if (this.url.indexOf('/:' + p.name) != -1) {
this.params.push(p)
}
else {
this.data.push(p)
}
}
}

if (this.method == 'post' || this.method == 'put' || this.method == 'patch') {
for (let param of this.query.params) {
this.data.push(param)
}

let re = /\:([a-zA-Z_][a-zA-Z0-9_-]*)/g
let matchParam
while(matchParam = re.exec(this.url)) {
for (let i of this.data) {
if (this.data[i].name == matchParam[1]) {
this.params.push(this.data.splice(i, 1))
}
}
}
}
else {
for (let param of this.query.params) {
this.params.push(param)
}
}
}
//this.queryType = endpointConfig.queryType || 'findAll'
//this.query = new Query[this.queryType](this, endpointConfig.query)
Expand Down Expand Up @@ -210,34 +216,34 @@ class Endpoint {
}
}

getMergedParams(onlyRequired) {
let res = []
getMergedParams(onlyRequired) {
let res = []
this.params.forEach((param) => {
if (param.required && onlyRequired || ! onlyRequired) {
res.push(param)
}
})
this.data.forEach((data) => {
if (data.required && onlyRequired || ! onlyRequired) {
res.push(data)
}
})
return res;
}

getRequiredMergedParams() {
return this.getmergedParams(true)
}
getAllData(onlyRequired) {
let res = []
this.data.forEach((data) => {
if (data.required && onlyRequired || ! onlyRequired) {
res.push(data)
}
})
return res;
}
this.data.forEach((data) => {
if (data.required && onlyRequired || ! onlyRequired) {
res.push(data)
}
})
return res;
}

getRequiredMergedParams() {
return this.getmergedParams(true)
}

getAllData(onlyRequired) {
let res = []
this.data.forEach((data) => {
if (data.required && onlyRequired || ! onlyRequired) {
res.push(data)
}
})
return res;
}


getAllParams(onlyRequired) {
Expand All @@ -252,9 +258,9 @@ class Endpoint {
getRequiredParams() {
return this.getAllParams(true)
}
getRequiredData() {
return this.getAllData(true)
}
getRequiredData() {
return this.getAllData(true)
}

handle(req, res) {
if ( ! this.entity && typeof this.query == 'function') {
Expand Down Expand Up @@ -284,8 +290,8 @@ class Endpoint {
} else if (req[param.name] != null) {
v = req[param.name]
} else if (req.query[param.name] != null) {
v = req.query[param.name]
} else if (param.required) {
v = req.query[param.name]
} else if (param.required) {
return res.status(500).json({
error: true,
message: 'Missing required parameter:' + param.name
Expand Down Expand Up @@ -341,9 +347,9 @@ class Endpoint {
id: this.query.id
}
}
if (this.params.length)
if (this.params.length && ! this.query)
res.params = this.params
if (this.data.length)
if (this.data.length && ! this.query)
res.data = this.data
if (this.permissions.length)
res.permissions = this.permissions
Expand Down
3 changes: 2 additions & 1 deletion lib/entities/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ class Entity {
let queriesJson = []
if (this.queries) {
this.queries.forEach((query) => {
queriesJson.push(query.toJson())
if (['get', 'list', 'update', 'create', 'delete'].indexOf(query.id) == -1)
queriesJson.push(query.toJson())
})
}

Expand Down
19 changes: 1 addition & 18 deletions test/apidays.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,23 +370,6 @@ describe('Apidays', () => {
"read": true,
"write": true
}).then(() => {
eventEntity.getQuery('update').addParam({
"name": "testField",
"type": "text",
"required": false
})
eventEntity.getQuery('update').updateValue('testField', '=')
eventEntity.getQuery('create').addParam({
"name": "testField",
"type": "text",
"required": false
})
eventEntity.getQuery('create').updateValue('testField', '=')
app.api.get('post', '/events').addData({
"name": "testField",
"type": "text",
"required": false
})
return eventEntity.getQuery('update').run({
testField: 'test field',
slug: 'global-2015'
Expand Down Expand Up @@ -429,7 +412,7 @@ describe('Apidays', () => {
catch (e) {
return done(new Error('Failed to parse commit file: ' + e.message))
}
expect(commitobj.length).to.equal(5) // depends on previous tests
expect(commitobj.length).to.equal(2) // depends on previous tests
done()
}).catch((err) => {
done(err)
Expand Down
123 changes: 1 addition & 122 deletions test/samples/apidays/tpl_entities/event.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@
}
],
"queries": [
{
"id": "list",
"type": "findAll",
"params": [],
"opts": {
"conditions": []
}
},
{
"id": "getBySlug",
"type": "findOne",
Expand All @@ -93,118 +85,6 @@
]
}
},
{
"id": "create",
"type": "create",
"params": [
{
"name": "slug",
"type": "text",
"required": true
},
{
"name": "title",
"type": "text",
"required": true
},
{
"name": "baseline",
"type": "text",
"required": false
},
{
"name": "date_start",
"type": "date",
"required": true
},
{
"name": "date_end",
"type": "date",
"required": true
}
],
"opts": {
"values": {
"slug": "=",
"title": "=",
"baseline": "=",
"date_start": "=",
"date_end": "="
}
}
},
{
"id": "update",
"type": "update",
"params": [
{
"name": "slug",
"type": "text",
"required": "true"
},
{
"name": "newslug",
"type": "text",
"required": "true"
},
{
"name": "title",
"type": "text",
"required": "false"
},
{
"name": "baseline",
"type": "text",
"required": "false"
},
{
"name": "date_start",
"type": "date",
"required": "false"
},
{
"name": "date_end",
"type": "date",
"required": "false"
}
],
"opts": {
"values": {
"slug": "=newslug",
"title": "=",
"baseline": "=",
"date_start": "=",
"date_end": "="
},
"conditions": [
{
"name": "slug",
"operator": "=",
"value": "="
}
]
}
},
{
"id": "delete",
"type": "delete",
"params": [
{
"name": "slug",
"type": "text",
"required": true
}
],
"opts": {
"conditions": [
{
"name": "slug",
"operator": "=",
"value": "="
}
]
}
},
{
"id": "companiesSince",
"type": "sql",
Expand All @@ -222,6 +102,5 @@
"query": "SELECT speaker.company FROM speaker, event WHERE event.date_start >= :date AND event.slug = speaker.slug_event GROUP BY speaker.company"
}
}
],
"type": "db"
]
}
7 changes: 0 additions & 7 deletions test/samples/apidays/tpl_entities/speaker.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,6 @@
"value": "=slug"
}
}
},
{
"id": "create",
"type": "create",
"opts": {
"default": true
}
}
],
"relations": [
Expand Down

0 comments on commit 734757e

Please sign in to comment.