Renamed id routes for API.

This commit is contained in:
Mike Cao 2024-01-31 22:08:48 -08:00
parent 53a991176b
commit 4429198397
42 changed files with 154 additions and 170 deletions

View file

@ -7,12 +7,12 @@ import { getActiveVisitors } from 'queries';
import * as yup from 'yup';
export interface WebsiteActiveRequestQuery {
id: string;
websiteId: string;
}
const schema = {
GET: yup.object().shape({
id: yup.string().uuid().required(),
websiteId: yup.string().uuid().required(),
}),
};
@ -24,14 +24,14 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
const { id: websiteId } = req.query;
const { websiteId } = req.query;
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {
if (!(await canViewWebsite(req.auth, websiteId as string))) {
return unauthorized(res);
}
const result = await getActiveVisitors(websiteId);
const result = await getActiveVisitors(websiteId as string);
return ok(res, result);
}

View file

@ -7,12 +7,12 @@ import { getWebsiteDateRange } from 'queries';
import * as yup from 'yup';
export interface WebsiteDateRangeRequestQuery {
id: string;
websiteId: string;
}
const schema = {
GET: yup.object().shape({
id: yup.string().uuid().required(),
websiteId: yup.string().uuid().required(),
}),
};
@ -24,7 +24,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
const { id: websiteId } = req.query;
const { websiteId } = req.query;
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {

View file

@ -9,7 +9,7 @@ import { getEventMetrics } from 'queries';
import * as yup from 'yup';
export interface WebsiteEventsRequestQuery {
id: string;
websiteId: string;
startAt: string;
endAt: string;
unit?: string;
@ -19,7 +19,7 @@ export interface WebsiteEventsRequestQuery {
const schema = {
GET: yup.object().shape({
id: yup.string().uuid().required(),
websiteId: yup.string().uuid().required(),
startAt: yup.number().integer().required(),
endAt: yup.number().integer().moreThan(yup.ref('startAt')).required(),
unit: UnitTypeTest,
@ -36,7 +36,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
const { id: websiteId, timezone, url } = req.query;
const { websiteId, timezone, url } = req.query;
const { startDate, endDate, unit } = await parseDateRangeQuery(req);
if (req.method === 'GET') {

View file

@ -7,7 +7,7 @@ import { deleteWebsite, getWebsite, updateWebsite } from 'queries';
import { SHARE_ID_REGEX } from 'lib/constants';
export interface WebsiteRequestQuery {
id: string;
websiteId: string;
}
export interface WebsiteRequestBody {
@ -20,10 +20,10 @@ import * as yup from 'yup';
const schema = {
GET: yup.object().shape({
id: yup.string().uuid().required(),
websiteId: yup.string().uuid().required(),
}),
POST: yup.object().shape({
id: yup.string().uuid().required(),
websiteId: yup.string().uuid().required(),
name: yup.string(),
domain: yup.string(),
shareId: yup.string().matches(SHARE_ID_REGEX, { excludeEmptyString: true }).nullable(),
@ -38,7 +38,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
const { id: websiteId } = req.query;
const { websiteId } = req.query;
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {

View file

@ -9,7 +9,7 @@ import { parseDateRangeQuery } from 'lib/query';
import * as yup from 'yup';
export interface WebsiteMetricsRequestQuery {
id: string;
websiteId: string;
type: string;
startAt: number;
endAt: number;
@ -30,7 +30,7 @@ export interface WebsiteMetricsRequestQuery {
const schema = {
GET: yup.object().shape({
id: yup.string().uuid().required(),
websiteId: yup.string().uuid().required(),
type: yup.string().required(),
startAt: yup.number().required(),
endAt: yup.number().required(),
@ -59,7 +59,7 @@ export default async (
await useValidate(schema, req, res);
const {
id: websiteId,
websiteId,
type,
url,
referrer,

View file

@ -7,7 +7,7 @@ import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getPageviewStats, getSessionStats } from 'queries';
export interface WebsitePageviewRequestQuery {
id: string;
websiteId: string;
startAt: number;
endAt: number;
unit?: string;
@ -27,7 +27,7 @@ import { TimezoneTest, UnitTypeTest } from 'lib/yup';
import * as yup from 'yup';
const schema = {
GET: yup.object().shape({
id: yup.string().uuid().required(),
websiteId: yup.string().uuid().required(),
startAt: yup.number().required(),
endAt: yup.number().required(),
unit: UnitTypeTest,
@ -52,19 +52,8 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
const {
id: websiteId,
timezone,
url,
referrer,
title,
os,
browser,
device,
country,
region,
city,
} = req.query;
const { websiteId, timezone, url, referrer, title, os, browser, device, country, region, city } =
req.query;
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {

View file

@ -8,12 +8,12 @@ import { getWebsiteReports } from 'queries';
import { pageInfo } from 'lib/schema';
export interface ReportsRequestQuery extends SearchFilter {
id: string;
websiteId: string;
}
const schema = {
GET: yup.object().shape({
id: yup.string().uuid().required(),
websiteId: yup.string().uuid().required(),
...pageInfo,
}),
};
@ -26,7 +26,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
const { id: websiteId } = req.query;
const { websiteId } = req.query;
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {

View file

@ -7,12 +7,12 @@ import { resetWebsite } from 'queries';
import * as yup from 'yup';
export interface WebsiteResetRequestQuery {
id: string;
websiteId: string;
}
const schema = {
POST: yup.object().shape({
id: yup.string().uuid().required(),
websiteId: yup.string().uuid().required(),
}),
};
@ -24,7 +24,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
const { id: websiteId } = req.query;
const { websiteId } = req.query;
if (req.method === 'POST') {
if (!(await canUpdateWebsite(req.auth, websiteId))) {

View file

@ -8,7 +8,7 @@ import { parseDateRangeQuery } from 'lib/query';
import { getWebsiteStats } from 'queries';
export interface WebsiteStatsRequestQuery {
id: string;
websiteId: string;
startAt: number;
endAt: number;
url?: string;
@ -27,7 +27,7 @@ export interface WebsiteStatsRequestQuery {
import * as yup from 'yup';
const schema = {
GET: yup.object().shape({
id: yup.string().uuid().required(),
websiteId: yup.string().uuid().required(),
startAt: yup.number().required(),
endAt: yup.number().required(),
url: yup.string(),
@ -53,7 +53,7 @@ export default async (
await useValidate(schema, req, res);
const {
id: websiteId,
websiteId,
url,
referrer,
title,
@ -65,7 +65,7 @@ export default async (
country,
region,
city,
} = req.query;
}: any & { websiteId: string } = req.query;
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {

View file

@ -8,7 +8,7 @@ import { getValues } from 'queries';
import { parseDateRangeQuery } from 'lib/query';
export interface ValuesRequestQuery {
id: string;
websiteId: string;
startAt: number;
endAt: number;
}
@ -16,7 +16,7 @@ export interface ValuesRequestQuery {
import * as yup from 'yup';
const schema = {
GET: yup.object().shape({
id: yup.string().uuid().required(),
websiteId: yup.string().uuid().required(),
startAt: yup.number().required(),
endAt: yup.number().required(),
}),
@ -27,7 +27,7 @@ export default async (req: NextApiRequestQueryBody<ValuesRequestQuery>, res: Nex
await useAuth(req, res);
await useValidate(schema, req, res);
const { id: websiteId, type } = req.query;
const { websiteId, type } = req.query;
const { startDate, endDate } = await parseDateRangeQuery(req);
if (req.method === 'GET') {

View file

@ -5,7 +5,7 @@ import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { createWebsite } from 'queries';
import userWebsites from 'pages/api/users/[id]/websites';
import userWebsites from 'pages/api/users/[userId]/websites';
import * as yup from 'yup';
import { pageInfo } from 'lib/schema';
@ -41,11 +41,11 @@ export default async (
} = req.auth;
if (req.method === 'GET') {
if (!req.query.id) {
req.query.id = userId;
if (!req.query.userId) {
req.query.userId = userId;
}
return userWebsites(req as any, res);
return userWebsites(req, res);
}
if (req.method === 'POST') {