mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-25 02:31:59 -05:00
Added Collections Content API
The only usecases we need to support at the moment are reading individual collections by ID and by Slug. We can extend this API as we get more usescases in future.
This commit is contained in:
parent
b0cf1f949a
commit
517c406e17
5 changed files with 131 additions and 0 deletions
53
ghost/core/core/server/api/endpoints/collections-public.js
Normal file
53
ghost/core/core/server/api/endpoints/collections-public.js
Normal file
|
@ -0,0 +1,53 @@
|
|||
const errors = require('@tryghost/errors');
|
||||
const tpl = require('@tryghost/tpl');
|
||||
const collectionsService = require('../../services/collections');
|
||||
|
||||
const messages = {
|
||||
collectionNotFound: 'Collection not found.'
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
docName: 'collections',
|
||||
|
||||
readBySlug: {
|
||||
headers: {
|
||||
cacheInvalidate: false
|
||||
},
|
||||
data: [
|
||||
'slug'
|
||||
],
|
||||
permissions: true,
|
||||
async query(frame) {
|
||||
const model = await collectionsService.api.getBySlug(frame.data.slug);
|
||||
|
||||
if (!model) {
|
||||
throw new errors.NotFoundError({
|
||||
message: tpl(messages.collectionNotFound)
|
||||
});
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
},
|
||||
|
||||
readById: {
|
||||
headers: {
|
||||
cacheInvalidate: false
|
||||
},
|
||||
data: [
|
||||
'id'
|
||||
],
|
||||
permissions: true,
|
||||
async query(frame) {
|
||||
const model = await collectionsService.api.getById(frame.data.id);
|
||||
|
||||
if (!model) {
|
||||
throw new errors.NotFoundError({
|
||||
message: tpl(messages.collectionNotFound)
|
||||
});
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
}
|
||||
};
|
|
@ -217,6 +217,10 @@ module.exports = {
|
|||
return apiFramework.pipeline(require('./pages-public'), localUtils, 'content');
|
||||
},
|
||||
|
||||
get collectionsPublic() {
|
||||
return apiFramework.pipeline(require('./collections-public'), localUtils);
|
||||
},
|
||||
|
||||
get tagsPublic() {
|
||||
return apiFramework.pipeline(require('./tags-public'), localUtils, 'content');
|
||||
},
|
||||
|
|
|
@ -39,5 +39,8 @@ module.exports = function apiRoutes() {
|
|||
router.get('/tiers', mw.authenticatePublic, http(api.tiersPublic.browse));
|
||||
router.get('/offers/:id', mw.authenticatePublic, http(api.offersPublic.read));
|
||||
|
||||
router.get('/collections/:id', mw.authenticatePublic, http(api.collectionsPublic.readById));
|
||||
router.get('/collections/slug/:slug', mw.authenticatePublic, http(api.collectionsPublic.readBySlug));
|
||||
|
||||
return router;
|
||||
};
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`Collections Content API Can request a collection by slug and id 1: [body] 1`] = `
|
||||
Object {
|
||||
"collections": Array [
|
||||
Object {
|
||||
"created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
|
||||
"description": "Featured posts",
|
||||
"feature_image": null,
|
||||
"filter": "featured:true",
|
||||
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||
"slug": "featured",
|
||||
"title": "Featured",
|
||||
"type": "automatic",
|
||||
"updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
|
||||
},
|
||||
],
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`Collections Content API Can request a collection by slug and id 2: [body] 1`] = `
|
||||
Object {
|
||||
"collections": Array [
|
||||
Object {
|
||||
"created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
|
||||
"description": "Featured posts",
|
||||
"feature_image": null,
|
||||
"filter": "featured:true",
|
||||
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||
"slug": "featured",
|
||||
"title": "Featured",
|
||||
"type": "automatic",
|
||||
"updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
|
||||
},
|
||||
],
|
||||
}
|
||||
`;
|
34
ghost/core/test/e2e-api/content/collections.test.js
Normal file
34
ghost/core/test/e2e-api/content/collections.test.js
Normal file
|
@ -0,0 +1,34 @@
|
|||
const {agentProvider, fixtureManager, matchers} = require('../../utils/e2e-framework');
|
||||
const {anyISODateTime, anyObjectId} = matchers;
|
||||
|
||||
const collectionMatcher = {
|
||||
id: anyObjectId,
|
||||
created_at: anyISODateTime,
|
||||
updated_at: anyISODateTime
|
||||
};
|
||||
|
||||
describe('Collections Content API', function () {
|
||||
let agent;
|
||||
|
||||
before(async function () {
|
||||
agent = await agentProvider.getContentAPIAgent();
|
||||
await fixtureManager.init('users', 'api_keys');
|
||||
await agent.authenticate();
|
||||
});
|
||||
|
||||
it('Can request a collection by slug and id', async function () {
|
||||
const {body: {collections: [collection]}} = await agent
|
||||
.get(`collections/slug/featured`)
|
||||
.expectStatus(200)
|
||||
.matchBodySnapshot({
|
||||
collections: [collectionMatcher]
|
||||
});
|
||||
|
||||
await agent
|
||||
.get(`collections/${collection.id}`)
|
||||
.expectStatus(200)
|
||||
.matchBodySnapshot({
|
||||
collections: [collectionMatcher]
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue