mirror of
https://github.com/umami-software/umami.git
synced 2026-02-17 02:55:38 +01:00
Renamed id routes for API.
This commit is contained in:
parent
53a991176b
commit
4429198397
42 changed files with 154 additions and 170 deletions
40
src/pages/api/websites/[websiteId]/active.ts
Normal file
40
src/pages/api/websites/[websiteId]/active.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import { WebsiteActive, NextApiRequestQueryBody } from 'lib/types';
|
||||
import { canViewWebsite } from 'lib/auth';
|
||||
import { useAuth, useCors, useValidate } from 'lib/middleware';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { getActiveVisitors } from 'queries';
|
||||
import * as yup from 'yup';
|
||||
|
||||
export interface WebsiteActiveRequestQuery {
|
||||
websiteId: string;
|
||||
}
|
||||
|
||||
const schema = {
|
||||
GET: yup.object().shape({
|
||||
websiteId: yup.string().uuid().required(),
|
||||
}),
|
||||
};
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<WebsiteActiveRequestQuery>,
|
||||
res: NextApiResponse<WebsiteActive>,
|
||||
) => {
|
||||
await useCors(req, res);
|
||||
await useAuth(req, res);
|
||||
await useValidate(schema, req, res);
|
||||
|
||||
const { websiteId } = req.query;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
if (!(await canViewWebsite(req.auth, websiteId as string))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const result = await getActiveVisitors(websiteId as string);
|
||||
|
||||
return ok(res, result);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
40
src/pages/api/websites/[websiteId]/daterange.ts
Normal file
40
src/pages/api/websites/[websiteId]/daterange.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import { WebsiteActive, NextApiRequestQueryBody } from 'lib/types';
|
||||
import { canViewWebsite } from 'lib/auth';
|
||||
import { useAuth, useCors, useValidate } from 'lib/middleware';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { getWebsiteDateRange } from 'queries';
|
||||
import * as yup from 'yup';
|
||||
|
||||
export interface WebsiteDateRangeRequestQuery {
|
||||
websiteId: string;
|
||||
}
|
||||
|
||||
const schema = {
|
||||
GET: yup.object().shape({
|
||||
websiteId: yup.string().uuid().required(),
|
||||
}),
|
||||
};
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<WebsiteDateRangeRequestQuery>,
|
||||
res: NextApiResponse<WebsiteActive>,
|
||||
) => {
|
||||
await useCors(req, res);
|
||||
await useAuth(req, res);
|
||||
await useValidate(schema, req, res);
|
||||
|
||||
const { websiteId } = req.query;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
if (!(await canViewWebsite(req.auth, websiteId))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const result = await getWebsiteDateRange(websiteId);
|
||||
|
||||
return ok(res, result);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
59
src/pages/api/websites/[websiteId]/events.ts
Normal file
59
src/pages/api/websites/[websiteId]/events.ts
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import { canViewWebsite } from 'lib/auth';
|
||||
import { useAuth, useCors, useValidate } from 'lib/middleware';
|
||||
import { parseDateRangeQuery } from 'lib/query';
|
||||
import { NextApiRequestQueryBody, WebsiteMetric } from 'lib/types';
|
||||
import { TimezoneTest, UnitTypeTest } from 'lib/yup';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { getEventMetrics } from 'queries';
|
||||
import * as yup from 'yup';
|
||||
|
||||
export interface WebsiteEventsRequestQuery {
|
||||
websiteId: string;
|
||||
startAt: string;
|
||||
endAt: string;
|
||||
unit?: string;
|
||||
timezone?: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
const schema = {
|
||||
GET: yup.object().shape({
|
||||
websiteId: yup.string().uuid().required(),
|
||||
startAt: yup.number().integer().required(),
|
||||
endAt: yup.number().integer().moreThan(yup.ref('startAt')).required(),
|
||||
unit: UnitTypeTest,
|
||||
timezone: TimezoneTest,
|
||||
url: yup.string(),
|
||||
}),
|
||||
};
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<WebsiteEventsRequestQuery>,
|
||||
res: NextApiResponse<WebsiteMetric>,
|
||||
) => {
|
||||
await useCors(req, res);
|
||||
await useAuth(req, res);
|
||||
await useValidate(schema, req, res);
|
||||
|
||||
const { websiteId, timezone, url } = req.query;
|
||||
const { startDate, endDate, unit } = await parseDateRangeQuery(req);
|
||||
|
||||
if (req.method === 'GET') {
|
||||
if (!(await canViewWebsite(req.auth, websiteId))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const events = await getEventMetrics(websiteId, {
|
||||
startDate,
|
||||
endDate,
|
||||
timezone,
|
||||
unit,
|
||||
url,
|
||||
});
|
||||
|
||||
return ok(res, events);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
84
src/pages/api/websites/[websiteId]/index.ts
Normal file
84
src/pages/api/websites/[websiteId]/index.ts
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, ok, serverError, unauthorized } from 'next-basics';
|
||||
import { Website, NextApiRequestQueryBody } from 'lib/types';
|
||||
import { canViewWebsite, canUpdateWebsite, canDeleteWebsite } from 'lib/auth';
|
||||
import { useAuth, useCors, useValidate } from 'lib/middleware';
|
||||
import { deleteWebsite, getWebsite, updateWebsite } from 'queries';
|
||||
import { SHARE_ID_REGEX } from 'lib/constants';
|
||||
|
||||
export interface WebsiteRequestQuery {
|
||||
websiteId: string;
|
||||
}
|
||||
|
||||
export interface WebsiteRequestBody {
|
||||
name: string;
|
||||
domain: string;
|
||||
shareId: string;
|
||||
}
|
||||
|
||||
import * as yup from 'yup';
|
||||
|
||||
const schema = {
|
||||
GET: yup.object().shape({
|
||||
websiteId: yup.string().uuid().required(),
|
||||
}),
|
||||
POST: yup.object().shape({
|
||||
websiteId: yup.string().uuid().required(),
|
||||
name: yup.string(),
|
||||
domain: yup.string(),
|
||||
shareId: yup.string().matches(SHARE_ID_REGEX, { excludeEmptyString: true }).nullable(),
|
||||
}),
|
||||
};
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<WebsiteRequestQuery, WebsiteRequestBody>,
|
||||
res: NextApiResponse<Website>,
|
||||
) => {
|
||||
await useCors(req, res);
|
||||
await useAuth(req, res);
|
||||
await useValidate(schema, req, res);
|
||||
|
||||
const { websiteId } = req.query;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
if (!(await canViewWebsite(req.auth, websiteId))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const website = await getWebsite(websiteId);
|
||||
|
||||
return ok(res, website);
|
||||
}
|
||||
|
||||
if (req.method === 'POST') {
|
||||
if (!(await canUpdateWebsite(req.auth, websiteId))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const { name, domain, shareId } = req.body;
|
||||
|
||||
let website;
|
||||
|
||||
try {
|
||||
website = await updateWebsite(websiteId, { name, domain, shareId });
|
||||
} catch (e: any) {
|
||||
if (e.message.includes('Unique constraint') && e.message.includes('share_id')) {
|
||||
return serverError(res, 'That share ID is already taken.');
|
||||
}
|
||||
}
|
||||
|
||||
return ok(res, website);
|
||||
}
|
||||
|
||||
if (req.method === 'DELETE') {
|
||||
if (!(await canDeleteWebsite(req.auth, websiteId))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
await deleteWebsite(websiteId);
|
||||
|
||||
return ok(res);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
137
src/pages/api/websites/[websiteId]/metrics.ts
Normal file
137
src/pages/api/websites/[websiteId]/metrics.ts
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
import { NextApiResponse } from 'next';
|
||||
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { WebsiteMetric, NextApiRequestQueryBody } from 'lib/types';
|
||||
import { canViewWebsite } from 'lib/auth';
|
||||
import { useAuth, useCors, useValidate } from 'lib/middleware';
|
||||
import { SESSION_COLUMNS, EVENT_COLUMNS, FILTER_COLUMNS } from 'lib/constants';
|
||||
import { getPageviewMetrics, getSessionMetrics } from 'queries';
|
||||
import { parseDateRangeQuery } from 'lib/query';
|
||||
import * as yup from 'yup';
|
||||
|
||||
export interface WebsiteMetricsRequestQuery {
|
||||
websiteId: string;
|
||||
type: string;
|
||||
startAt: number;
|
||||
endAt: number;
|
||||
url?: string;
|
||||
referrer?: string;
|
||||
title?: string;
|
||||
query?: string;
|
||||
os?: string;
|
||||
browser?: string;
|
||||
device?: string;
|
||||
country?: string;
|
||||
region?: string;
|
||||
city?: string;
|
||||
language?: string;
|
||||
event?: string;
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
const schema = {
|
||||
GET: yup.object().shape({
|
||||
websiteId: yup.string().uuid().required(),
|
||||
type: yup.string().required(),
|
||||
startAt: yup.number().required(),
|
||||
endAt: yup.number().required(),
|
||||
url: yup.string(),
|
||||
referrer: yup.string(),
|
||||
title: yup.string(),
|
||||
query: yup.string(),
|
||||
os: yup.string(),
|
||||
browser: yup.string(),
|
||||
device: yup.string(),
|
||||
country: yup.string(),
|
||||
region: yup.string(),
|
||||
city: yup.string(),
|
||||
language: yup.string(),
|
||||
event: yup.string(),
|
||||
limit: yup.number(),
|
||||
}),
|
||||
};
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<WebsiteMetricsRequestQuery>,
|
||||
res: NextApiResponse<WebsiteMetric[]>,
|
||||
) => {
|
||||
await useCors(req, res);
|
||||
await useAuth(req, res);
|
||||
await useValidate(schema, req, res);
|
||||
|
||||
const {
|
||||
websiteId,
|
||||
type,
|
||||
url,
|
||||
referrer,
|
||||
title,
|
||||
query,
|
||||
os,
|
||||
browser,
|
||||
device,
|
||||
country,
|
||||
region,
|
||||
city,
|
||||
language,
|
||||
event,
|
||||
limit,
|
||||
} = req.query;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
if (!(await canViewWebsite(req.auth, websiteId))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const { startDate, endDate } = await parseDateRangeQuery(req);
|
||||
|
||||
const filters = {
|
||||
startDate,
|
||||
endDate,
|
||||
url,
|
||||
referrer,
|
||||
title,
|
||||
query,
|
||||
os,
|
||||
browser,
|
||||
device,
|
||||
country,
|
||||
region,
|
||||
city,
|
||||
language,
|
||||
event,
|
||||
};
|
||||
|
||||
const column = FILTER_COLUMNS[type] || type;
|
||||
|
||||
if (SESSION_COLUMNS.includes(type)) {
|
||||
const data = await getSessionMetrics(websiteId, column, filters, limit);
|
||||
|
||||
if (type === 'language') {
|
||||
const combined = {};
|
||||
|
||||
for (const { x, y } of data) {
|
||||
const key = String(x).toLowerCase().split('-')[0];
|
||||
|
||||
if (combined[key] === undefined) {
|
||||
combined[key] = { x: key, y };
|
||||
} else {
|
||||
combined[key].y += y;
|
||||
}
|
||||
}
|
||||
|
||||
return ok(res, Object.values(combined));
|
||||
}
|
||||
|
||||
return ok(res, data);
|
||||
}
|
||||
|
||||
if (EVENT_COLUMNS.includes(type)) {
|
||||
const data = await getPageviewMetrics(websiteId, column, filters, limit);
|
||||
|
||||
return ok(res, data);
|
||||
}
|
||||
|
||||
return badRequest(res);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
90
src/pages/api/websites/[websiteId]/pageviews.ts
Normal file
90
src/pages/api/websites/[websiteId]/pageviews.ts
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
import { canViewWebsite } from 'lib/auth';
|
||||
import { useAuth, useCors, useValidate } from 'lib/middleware';
|
||||
import { parseDateRangeQuery } from 'lib/query';
|
||||
import { NextApiRequestQueryBody, WebsitePageviews } from 'lib/types';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { getPageviewStats, getSessionStats } from 'queries';
|
||||
|
||||
export interface WebsitePageviewRequestQuery {
|
||||
websiteId: string;
|
||||
startAt: number;
|
||||
endAt: number;
|
||||
unit?: string;
|
||||
timezone?: string;
|
||||
url?: string;
|
||||
referrer?: string;
|
||||
title?: string;
|
||||
os?: string;
|
||||
browser?: string;
|
||||
device?: string;
|
||||
country?: string;
|
||||
region: string;
|
||||
city?: string;
|
||||
}
|
||||
|
||||
import { TimezoneTest, UnitTypeTest } from 'lib/yup';
|
||||
import * as yup from 'yup';
|
||||
const schema = {
|
||||
GET: yup.object().shape({
|
||||
websiteId: yup.string().uuid().required(),
|
||||
startAt: yup.number().required(),
|
||||
endAt: yup.number().required(),
|
||||
unit: UnitTypeTest,
|
||||
timezone: TimezoneTest,
|
||||
url: yup.string(),
|
||||
referrer: yup.string(),
|
||||
title: yup.string(),
|
||||
os: yup.string(),
|
||||
browser: yup.string(),
|
||||
device: yup.string(),
|
||||
country: yup.string(),
|
||||
region: yup.string(),
|
||||
city: yup.string(),
|
||||
}),
|
||||
};
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<WebsitePageviewRequestQuery>,
|
||||
res: NextApiResponse<WebsitePageviews>,
|
||||
) => {
|
||||
await useCors(req, res);
|
||||
await useAuth(req, res);
|
||||
await useValidate(schema, req, res);
|
||||
|
||||
const { websiteId, timezone, url, referrer, title, os, browser, device, country, region, city } =
|
||||
req.query;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
if (!(await canViewWebsite(req.auth, websiteId))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const { startDate, endDate, unit } = await parseDateRangeQuery(req);
|
||||
|
||||
const filters = {
|
||||
startDate,
|
||||
endDate,
|
||||
timezone,
|
||||
unit,
|
||||
url,
|
||||
referrer,
|
||||
title,
|
||||
os,
|
||||
browser,
|
||||
device,
|
||||
country,
|
||||
region,
|
||||
city,
|
||||
};
|
||||
|
||||
const [pageviews, sessions] = await Promise.all([
|
||||
getPageviewStats(websiteId, filters),
|
||||
getSessionStats(websiteId, filters),
|
||||
]);
|
||||
|
||||
return ok(res, { pageviews, sessions });
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
48
src/pages/api/websites/[websiteId]/reports.ts
Normal file
48
src/pages/api/websites/[websiteId]/reports.ts
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import * as yup from 'yup';
|
||||
import { canViewWebsite } from 'lib/auth';
|
||||
import { useAuth, useCors, useValidate } from 'lib/middleware';
|
||||
import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { getWebsiteReports } from 'queries';
|
||||
import { pageInfo } from 'lib/schema';
|
||||
|
||||
export interface ReportsRequestQuery extends SearchFilter {
|
||||
websiteId: string;
|
||||
}
|
||||
|
||||
const schema = {
|
||||
GET: yup.object().shape({
|
||||
websiteId: yup.string().uuid().required(),
|
||||
...pageInfo,
|
||||
}),
|
||||
};
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<ReportsRequestQuery, any>,
|
||||
res: NextApiResponse,
|
||||
) => {
|
||||
await useCors(req, res);
|
||||
await useAuth(req, res);
|
||||
await useValidate(schema, req, res);
|
||||
|
||||
const { websiteId } = req.query;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
if (!(await canViewWebsite(req.auth, websiteId))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const { page, query, pageSize } = req.query;
|
||||
|
||||
const data = await getWebsiteReports(websiteId, {
|
||||
page,
|
||||
pageSize: +pageSize || undefined,
|
||||
query,
|
||||
});
|
||||
|
||||
return ok(res, data);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
40
src/pages/api/websites/[websiteId]/reset.ts
Normal file
40
src/pages/api/websites/[websiteId]/reset.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import { NextApiRequestQueryBody } from 'lib/types';
|
||||
import { canUpdateWebsite } from 'lib/auth';
|
||||
import { useAuth, useCors, useValidate } from 'lib/middleware';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { resetWebsite } from 'queries';
|
||||
import * as yup from 'yup';
|
||||
|
||||
export interface WebsiteResetRequestQuery {
|
||||
websiteId: string;
|
||||
}
|
||||
|
||||
const schema = {
|
||||
POST: yup.object().shape({
|
||||
websiteId: yup.string().uuid().required(),
|
||||
}),
|
||||
};
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<WebsiteResetRequestQuery>,
|
||||
res: NextApiResponse,
|
||||
) => {
|
||||
await useCors(req, res);
|
||||
await useAuth(req, res);
|
||||
await useValidate(schema, req, res);
|
||||
|
||||
const { websiteId } = req.query;
|
||||
|
||||
if (req.method === 'POST') {
|
||||
if (!(await canUpdateWebsite(req.auth, websiteId))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
await resetWebsite(websiteId);
|
||||
|
||||
return ok(res);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
114
src/pages/api/websites/[websiteId]/stats.ts
Normal file
114
src/pages/api/websites/[websiteId]/stats.ts
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
import { subMinutes, differenceInMinutes } from 'date-fns';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { canViewWebsite } from 'lib/auth';
|
||||
import { useAuth, useCors, useValidate } from 'lib/middleware';
|
||||
import { NextApiRequestQueryBody, WebsiteStats } from 'lib/types';
|
||||
import { parseDateRangeQuery } from 'lib/query';
|
||||
import { getWebsiteStats } from 'queries';
|
||||
|
||||
export interface WebsiteStatsRequestQuery {
|
||||
websiteId: string;
|
||||
startAt: number;
|
||||
endAt: number;
|
||||
url?: string;
|
||||
referrer?: string;
|
||||
title?: string;
|
||||
query?: string;
|
||||
event?: string;
|
||||
os?: string;
|
||||
browser?: string;
|
||||
device?: string;
|
||||
country?: string;
|
||||
region?: string;
|
||||
city?: string;
|
||||
}
|
||||
|
||||
import * as yup from 'yup';
|
||||
const schema = {
|
||||
GET: yup.object().shape({
|
||||
websiteId: yup.string().uuid().required(),
|
||||
startAt: yup.number().required(),
|
||||
endAt: yup.number().required(),
|
||||
url: yup.string(),
|
||||
referrer: yup.string(),
|
||||
title: yup.string(),
|
||||
query: yup.string(),
|
||||
event: yup.string(),
|
||||
os: yup.string(),
|
||||
browser: yup.string(),
|
||||
device: yup.string(),
|
||||
country: yup.string(),
|
||||
region: yup.string(),
|
||||
city: yup.string(),
|
||||
}),
|
||||
};
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<WebsiteStatsRequestQuery>,
|
||||
res: NextApiResponse<WebsiteStats>,
|
||||
) => {
|
||||
await useCors(req, res);
|
||||
await useAuth(req, res);
|
||||
await useValidate(schema, req, res);
|
||||
|
||||
const {
|
||||
websiteId,
|
||||
url,
|
||||
referrer,
|
||||
title,
|
||||
query,
|
||||
event,
|
||||
os,
|
||||
browser,
|
||||
device,
|
||||
country,
|
||||
region,
|
||||
city,
|
||||
}: any & { websiteId: string } = req.query;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
if (!(await canViewWebsite(req.auth, websiteId))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const { startDate, endDate } = await parseDateRangeQuery(req);
|
||||
const diff = differenceInMinutes(endDate, startDate);
|
||||
const prevStartDate = subMinutes(startDate, diff);
|
||||
const prevEndDate = subMinutes(endDate, diff);
|
||||
|
||||
const filters = {
|
||||
url,
|
||||
referrer,
|
||||
title,
|
||||
query,
|
||||
event,
|
||||
os,
|
||||
browser,
|
||||
device,
|
||||
country,
|
||||
region,
|
||||
city,
|
||||
};
|
||||
|
||||
const metrics = await getWebsiteStats(websiteId, { ...filters, startDate, endDate });
|
||||
|
||||
const prevPeriod = await getWebsiteStats(websiteId, {
|
||||
...filters,
|
||||
startDate: prevStartDate,
|
||||
endDate: prevEndDate,
|
||||
});
|
||||
|
||||
const stats = Object.keys(metrics[0]).reduce((obj, key) => {
|
||||
obj[key] = {
|
||||
value: Number(metrics[0][key]) || 0,
|
||||
change: Number(metrics[0][key]) - Number(prevPeriod[0][key]) || 0,
|
||||
};
|
||||
return obj;
|
||||
}, {});
|
||||
|
||||
return ok(res, stats);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
54
src/pages/api/websites/[websiteId]/values.ts
Normal file
54
src/pages/api/websites/[websiteId]/values.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import { NextApiRequestQueryBody } from 'lib/types';
|
||||
import { canViewWebsite } from 'lib/auth';
|
||||
import { useAuth, useCors, useValidate } from 'lib/middleware';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { EVENT_COLUMNS, FILTER_COLUMNS, SESSION_COLUMNS } from 'lib/constants';
|
||||
import { getValues } from 'queries';
|
||||
import { parseDateRangeQuery } from 'lib/query';
|
||||
|
||||
export interface ValuesRequestQuery {
|
||||
websiteId: string;
|
||||
startAt: number;
|
||||
endAt: number;
|
||||
}
|
||||
|
||||
import * as yup from 'yup';
|
||||
const schema = {
|
||||
GET: yup.object().shape({
|
||||
websiteId: yup.string().uuid().required(),
|
||||
startAt: yup.number().required(),
|
||||
endAt: yup.number().required(),
|
||||
}),
|
||||
};
|
||||
|
||||
export default async (req: NextApiRequestQueryBody<ValuesRequestQuery>, res: NextApiResponse) => {
|
||||
await useCors(req, res);
|
||||
await useAuth(req, res);
|
||||
await useValidate(schema, req, res);
|
||||
|
||||
const { websiteId, type } = req.query;
|
||||
const { startDate, endDate } = await parseDateRangeQuery(req);
|
||||
|
||||
if (req.method === 'GET') {
|
||||
if (!SESSION_COLUMNS.includes(type as string) && !EVENT_COLUMNS.includes(type as string)) {
|
||||
return badRequest(res);
|
||||
}
|
||||
|
||||
if (!(await canViewWebsite(req.auth, websiteId))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const values = await getValues(websiteId, FILTER_COLUMNS[type as string], startDate, endDate);
|
||||
|
||||
return ok(
|
||||
res,
|
||||
values
|
||||
.map(({ value }) => value)
|
||||
.filter(n => n)
|
||||
.sort(),
|
||||
);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue