2015-04-18 00:27:04 +03:00
|
|
|
var _ = require('lodash'),
|
|
|
|
url = require('url'),
|
2014-08-22 11:58:21 +04:00
|
|
|
moment = require('moment'),
|
|
|
|
config = require('../../server/config'),
|
|
|
|
ApiRouteBase = '/ghost/api/v0.1/',
|
|
|
|
host = config.server.host,
|
|
|
|
port = config.server.port,
|
|
|
|
schema = 'http://',
|
2013-11-07 13:34:18 +04:00
|
|
|
expectedProperties = {
|
2014-08-21 01:42:34 +04:00
|
|
|
configuration: ['key', 'value'],
|
2014-04-19 19:03:20 +04:00
|
|
|
posts: ['posts', 'meta'],
|
2015-10-22 16:28:47 +03:00
|
|
|
tags: ['tags', 'meta'],
|
2014-07-20 20:42:03 +04:00
|
|
|
users: ['users', 'meta'],
|
2014-07-25 02:22:27 +04:00
|
|
|
roles: ['roles'],
|
2014-04-19 19:03:20 +04:00
|
|
|
pagination: ['page', 'limit', 'pages', 'total', 'next', 'prev'],
|
2013-11-07 13:34:18 +04:00
|
|
|
post: ['id', 'uuid', 'title', 'slug', 'markdown', 'html', 'meta_title', 'meta_description',
|
2014-04-22 05:08:11 +04:00
|
|
|
'featured', 'image', 'status', 'language', 'created_at', 'created_by', 'updated_at',
|
2014-12-10 17:03:39 +03:00
|
|
|
'updated_by', 'published_at', 'published_by', 'page', 'author', 'url'
|
2014-09-10 08:06:24 +04:00
|
|
|
],
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
settings: ['settings', 'meta'],
|
2014-04-29 00:42:38 +04:00
|
|
|
setting: ['id', 'uuid', 'key', 'value', 'type', 'created_at', 'created_by', 'updated_at', 'updated_by'],
|
2014-07-18 21:58:29 +04:00
|
|
|
tag: ['id', 'uuid', 'name', 'slug', 'description', 'parent', 'image', 'hidden',
|
2014-09-10 08:06:24 +04:00
|
|
|
'meta_title', 'meta_description', 'created_at', 'created_by', 'updated_at', 'updated_by'
|
|
|
|
],
|
2014-05-13 18:33:34 +04:00
|
|
|
theme: ['uuid', 'name', 'version', 'active'],
|
2013-11-07 13:34:18 +04:00
|
|
|
user: ['id', 'uuid', 'name', 'slug', 'email', 'image', 'cover', 'bio', 'website',
|
2015-07-11 21:56:17 +03:00
|
|
|
'location', 'accessibility', 'status', 'language', 'meta_title', 'meta_description', 'tour', 'last_login',
|
2014-09-10 08:06:24 +04:00
|
|
|
'created_at', 'created_by', 'updated_at', 'updated_by'
|
|
|
|
],
|
2014-06-30 01:45:03 +04:00
|
|
|
notification: ['type', 'message', 'status', 'id', 'dismissible', 'location'],
|
2014-05-06 02:38:05 +04:00
|
|
|
slugs: ['slugs'],
|
2014-06-30 16:58:10 +04:00
|
|
|
slug: ['slug'],
|
2014-07-08 20:00:59 +04:00
|
|
|
accesstoken: ['access_token', 'refresh_token', 'expires_in', 'token_type'],
|
|
|
|
role: ['id', 'uuid', 'name', 'description', 'created_at', 'created_by', 'updated_at', 'updated_by'],
|
|
|
|
permission: ['id', 'uuid', 'name', 'object_type', 'action_type', 'object_id', 'created_at', 'created_by',
|
2014-09-10 08:06:24 +04:00
|
|
|
'updated_at', 'updated_by'
|
|
|
|
]
|
2013-11-07 13:34:18 +04:00
|
|
|
};
|
|
|
|
|
2014-04-29 00:42:38 +04:00
|
|
|
function getApiQuery(route) {
|
2014-04-07 17:50:18 +04:00
|
|
|
return url.resolve(ApiRouteBase, route);
|
|
|
|
}
|
2013-11-03 21:13:19 +04:00
|
|
|
|
2014-04-29 00:42:38 +04:00
|
|
|
function getApiURL(route) {
|
2013-11-03 21:13:19 +04:00
|
|
|
var baseURL = url.resolve(schema + host + ':' + port, ApiRouteBase);
|
|
|
|
return url.resolve(baseURL, route);
|
2013-10-08 05:39:33 +04:00
|
|
|
}
|
2014-04-29 00:42:38 +04:00
|
|
|
function getSigninURL() {
|
2013-11-03 21:13:19 +04:00
|
|
|
return url.resolve(schema + host + ':' + port, 'ghost/signin/');
|
2013-10-08 05:39:33 +04:00
|
|
|
}
|
2014-04-29 00:42:38 +04:00
|
|
|
function getAdminURL() {
|
2013-11-24 18:29:36 +04:00
|
|
|
return url.resolve(schema + host + ':' + port, 'ghost/');
|
|
|
|
}
|
2013-10-08 05:39:33 +04:00
|
|
|
|
2013-11-03 21:13:19 +04:00
|
|
|
// make sure the API only returns expected properties only
|
2014-04-29 00:42:38 +04:00
|
|
|
function checkResponseValue(jsonResponse, properties) {
|
|
|
|
for (var i = 0; i < properties.length; i = i + 1) {
|
2014-04-28 03:28:50 +04:00
|
|
|
// For some reason, settings response objects do not have the 'hasOwnProperty' method
|
|
|
|
if (Object.prototype.hasOwnProperty.call(jsonResponse, properties[i])) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-11-07 13:34:18 +04:00
|
|
|
jsonResponse.should.have.property(properties[i]);
|
2013-10-08 05:39:33 +04:00
|
|
|
}
|
2014-07-25 02:07:29 +04:00
|
|
|
Object.keys(jsonResponse).length.should.eql(properties.length);
|
2013-10-08 05:39:33 +04:00
|
|
|
}
|
|
|
|
|
2015-04-18 00:27:04 +03:00
|
|
|
function checkResponse(jsonResponse, objectType, additionalProperties, missingProperties) {
|
2014-07-08 20:00:59 +04:00
|
|
|
var checkProperties = expectedProperties[objectType];
|
|
|
|
checkProperties = additionalProperties ? checkProperties.concat(additionalProperties) : checkProperties;
|
2015-04-18 00:27:04 +03:00
|
|
|
checkProperties = missingProperties ? _.xor(checkProperties, missingProperties) : checkProperties;
|
2014-07-08 20:00:59 +04:00
|
|
|
|
|
|
|
checkResponseValue(jsonResponse, checkProperties);
|
2014-04-29 00:42:38 +04:00
|
|
|
}
|
|
|
|
|
2014-07-05 18:57:56 +04:00
|
|
|
function isISO8601(date) {
|
|
|
|
return moment(date).parsingFlags().iso;
|
|
|
|
}
|
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
function getURL() {
|
|
|
|
return schema + host;
|
|
|
|
}
|
|
|
|
|
2013-11-07 13:34:18 +04:00
|
|
|
module.exports = {
|
|
|
|
getApiURL: getApiURL,
|
2014-04-07 17:50:18 +04:00
|
|
|
getApiQuery: getApiQuery,
|
2013-11-07 13:34:18 +04:00
|
|
|
getSigninURL: getSigninURL,
|
2013-11-24 18:29:36 +04:00
|
|
|
getAdminURL: getAdminURL,
|
2015-10-22 16:28:47 +03:00
|
|
|
getURL: getURL,
|
2013-11-07 13:34:18 +04:00
|
|
|
checkResponse: checkResponse,
|
2014-07-05 18:57:56 +04:00
|
|
|
checkResponseValue: checkResponseValue,
|
|
|
|
isISO8601: isISO8601
|
2013-11-07 13:34:18 +04:00
|
|
|
};
|