// (C) 2007-2019 GoodData Corporation import { Application } from "express"; import { Promise } from "es6-promise"; import { IMockProject, getAttributes } from "../../model/MockProject"; import { ILink } from "../../model/Link"; import { IError } from "../../model/Error"; import * as HttpStatusCodes from "http-status-codes"; import { IObjectMeta } from "../../model/ObjectMeta"; const PENDING_IMPLEMENTATIONS = ["projectdashboards"]; const KNOWN_TYPES = ["attributes", "analyticaldashboard", ...PENDING_IMPLEMENTATIONS]; interface IQueryResponse { query: { entries: ILink[]; meta: { category: string; summary: string; title: string; }; }; } function getAnalyticalDashboards(project: IMockProject) { return project.analyticalDashboards; } function queryObjects(project: IMockProject, type: string): Promise { if (isUnknownType(type)) { return Promise.reject(createError(type)); } if (PENDING_IMPLEMENTATIONS.indexOf(type) >= 0) { return Promise.resolve([]); } // TODO: We need more generic system to get different kinds of metadata objects if (type === "analyticaldashboard") { const links: ILink[] = getAnalyticalDashboards(project).map(dashboard => createLink(dashboard.meta)); return Promise.resolve(links); } return Promise.resolve(getAttributes(project).map(attribute => createLink(attribute.meta))); } function createLink(meta: IObjectMeta): ILink { const link = { ...meta, link: meta.uri }; delete link.uri; return link; } function isUnknownType(objectType: string): boolean { return KNOWN_TYPES.indexOf(objectType) === -1; } function createError(objectType: string): IError { return { error: { component: "metadata", errorClass: "foo.bar", message: "Unknown type '%s'", parameters: [objectType], }, }; } function createQuery(entries: ILink[], project: string, type: string): IQueryResponse { return { query: { entries, meta: { category: "query", summary: `Meta Query Resources for project '${project}'`, title: `List of ${type}`, }, }, }; } export const query = { register(app: Application, project: IMockProject) { app.get(`/gdc/md/${project.project.meta.identifier}/query/:type`, (req, res) => { queryObjects(project, req.params.type) .then(entries => [ HttpStatusCodes.OK, createQuery(entries, project.project.meta.identifier, req.params.type), ]) .catch(error => [HttpStatusCodes.NOT_FOUND, error]) .then(([status, body]) => res.status(status).json(body)); }); return app; }, };