Fixed queries again.

This commit is contained in:
Mike Cao 2025-02-06 11:22:00 -08:00
parent fc640ff394
commit 1ba28ece8b
8 changed files with 26 additions and 21 deletions

View file

@ -2,7 +2,7 @@ import { z } from 'zod';
import { canViewWebsite } from '@/lib/auth'; import { canViewWebsite } from '@/lib/auth';
import { unauthorized, json } from '@/lib/response'; import { unauthorized, json } from '@/lib/response';
import { parseRequest } from '@/lib/request'; import { parseRequest } from '@/lib/request';
import { getGoals } from '@/queries/analytics/reports/getGoals'; import { getGoals } from '@/queries/sql/reports/getGoals';
import { reportParms } from '@/lib/schema'; import { reportParms } from '@/lib/schema';
export async function POST(request: Request) { export async function POST(request: Request) {

View file

@ -3,8 +3,8 @@ import { canViewWebsite } from '@/lib/auth';
import { unauthorized, json } from '@/lib/response'; import { unauthorized, json } from '@/lib/response';
import { parseRequest } from '@/lib/request'; import { parseRequest } from '@/lib/request';
import { reportParms, timezoneParam } from '@/lib/schema'; import { reportParms, timezoneParam } from '@/lib/schema';
import { getRevenue } from '@/queries/analytics/reports/getRevenue'; import { getRevenue } from '@/queries/sql/reports/getRevenue';
import { getRevenueValues } from '@/queries/analytics/reports/getRevenueValues'; import { getRevenueValues } from '@/queries/sql/reports/getRevenueValues';
export async function GET(request: Request) { export async function GET(request: Request) {
const { auth, query, error } = await parseRequest(request); const { auth, query, error } = await parseRequest(request);

View file

@ -1,8 +1,8 @@
import { z } from 'zod'; import { z } from 'zod';
import { json, unauthorized } from '@/lib/response'; import { json, unauthorized } from '@/lib/response';
import { getAllUserWebsitesIncludingTeamOwner } from '@/queries/prisma/website'; import { getAllUserWebsitesIncludingTeamOwner } from '@/queries/prisma/website';
import { getEventUsage } from '@/queries/analytics/events/getEventUsage'; import { getEventUsage } from '@/queries/sql/events/getEventUsage';
import { getEventDataUsage } from '@/queries/analytics/events/getEventDataUsage'; import { getEventDataUsage } from '@/queries/sql/events/getEventDataUsage';
import { parseRequest } from '@/lib/request'; import { parseRequest } from '@/lib/request';
export async function GET(request: Request, { params }: { params: Promise<{ userId: string }> }) { export async function GET(request: Request, { params }: { params: Promise<{ userId: string }> }) {

View file

@ -2,7 +2,7 @@ import { z } from 'zod';
import { parseRequest } from '@/lib/request'; import { parseRequest } from '@/lib/request';
import { unauthorized, json } from '@/lib/response'; import { unauthorized, json } from '@/lib/response';
import { canViewWebsite } from '@/lib/auth'; import { canViewWebsite } from '@/lib/auth';
import { getEventDataEvents } from '@/queries/analytics/events/getEventDataEvents'; import { getEventDataEvents } from '@/queries/sql/events/getEventDataEvents';
export async function GET( export async function GET(
request: Request, request: Request,

View file

@ -3,7 +3,7 @@ import { canViewWebsite } from '@/lib/auth';
import { SESSION_COLUMNS, EVENT_COLUMNS, FILTER_COLUMNS, OPERATORS } from '@/lib/constants'; import { SESSION_COLUMNS, EVENT_COLUMNS, FILTER_COLUMNS, OPERATORS } from '@/lib/constants';
import { getRequestFilters, getRequestDateRange, parseRequest } from '@/lib/request'; import { getRequestFilters, getRequestDateRange, parseRequest } from '@/lib/request';
import { json, unauthorized, badRequest } from '@/lib/response'; import { json, unauthorized, badRequest } from '@/lib/response';
import { getPageviewMetrics, getSessionMetrics } from '@/queries'; import { getPageviewMetrics, getSessionMetrics, getChannelMetrics } from '@/queries';
import { filterParams } from '@/lib/schema'; import { filterParams } from '@/lib/schema';
export async function GET( export async function GET(
@ -78,5 +78,11 @@ export async function GET(
return json(data); return json(data);
} }
if (type === 'channel') {
const data = await getChannelMetrics(websiteId, filters);
return json(data);
}
return badRequest(); return badRequest();
} }

View file

@ -2,7 +2,7 @@ import { z } from 'zod';
import { parseRequest } from '@/lib/request'; import { parseRequest } from '@/lib/request';
import { unauthorized, json } from '@/lib/response'; import { unauthorized, json } from '@/lib/response';
import { canViewWebsite } from '@/lib/auth'; import { canViewWebsite } from '@/lib/auth';
import { getEventDataEvents } from '@/queries/analytics/events/getEventDataEvents'; import { getEventDataEvents } from '@/queries/sql/events/getEventDataEvents';
export async function GET( export async function GET(
request: Request, request: Request,

View file

@ -27,8 +27,8 @@ export function useApi() {
}; };
const basePath = process.env.basePath; const basePath = process.env.basePath;
const getUrl = (url: string, basePath = '') => { const getUrl = (url: string) => {
return url.startsWith('http') ? url : `${basePath}/api${url}`; return url.startsWith('http') ? url : `${basePath || ''}/api${url}`;
}; };
const getHeaders = (headers: any = {}) => { const getHeaders = (headers: any = {}) => {
@ -38,7 +38,7 @@ export function useApi() {
return { return {
get: useCallback( get: useCallback(
async (url: string, params: object = {}, headers: object = {}) => { async (url: string, params: object = {}, headers: object = {}) => {
return httpGet(getUrl(url, basePath), params, getHeaders(headers)) return httpGet(getUrl(url), params, getHeaders(headers))
.then(handleResponse) .then(handleResponse)
.catch(handleError); .catch(handleError);
}, },
@ -47,7 +47,7 @@ export function useApi() {
post: useCallback( post: useCallback(
async (url: string, params: object = {}, headers: object = {}) => { async (url: string, params: object = {}, headers: object = {}) => {
return httpPost(getUrl(url, basePath), params, getHeaders(headers)) return httpPost(getUrl(url), params, getHeaders(headers))
.then(handleResponse) .then(handleResponse)
.catch(handleError); .catch(handleError);
}, },
@ -56,7 +56,7 @@ export function useApi() {
put: useCallback( put: useCallback(
async (url: string, params: object = {}, headers: object = {}) => { async (url: string, params: object = {}, headers: object = {}) => {
return httpPut(getUrl(url, basePath), params, getHeaders(headers)) return httpPut(getUrl(url), params, getHeaders(headers))
.then(handleResponse) .then(handleResponse)
.catch(handleError); .catch(handleError);
}, },
@ -65,7 +65,7 @@ export function useApi() {
del: useCallback( del: useCallback(
async (url: string, params: object = {}, headers: object = {}) => { async (url: string, params: object = {}, headers: object = {}) => {
return httpDelete(getUrl(url, basePath), params, getHeaders(headers)) return httpDelete(getUrl(url), params, getHeaders(headers))
.then(handleResponse) .then(handleResponse)
.catch(handleError); .catch(handleError);
}, },

View file

@ -1,7 +1,7 @@
import { buildUrl } from '@/lib/url'; import { buildUrl } from '@/lib/url';
export async function request(method: string, url: string, body?: string, headers: object = {}) { export async function request(method: string, url: string, body?: string, headers: object = {}) {
const res = await fetch(url, { return fetch(url, {
method, method,
cache: 'no-cache', cache: 'no-cache',
headers: { headers: {
@ -10,22 +10,21 @@ export async function request(method: string, url: string, body?: string, header
...headers, ...headers,
}, },
body, body,
}); }).then(res => res.json());
return res.json();
} }
export function httpGet(url: string, params: object = {}, headers: object = {}) { export async function httpGet(url: string, params: object = {}, headers: object = {}) {
return request('GET', buildUrl(url, params), undefined, headers); return request('GET', buildUrl(url, params), undefined, headers);
} }
export function httpDelete(url: string, params: object = {}, headers: object = {}) { export async function httpDelete(url: string, params: object = {}, headers: object = {}) {
return request('DELETE', buildUrl(url, params), undefined, headers); return request('DELETE', buildUrl(url, params), undefined, headers);
} }
export function httpPost(url: string, params: object = {}, headers: object = {}) { export async function httpPost(url: string, params: object = {}, headers: object = {}) {
return request('POST', url, JSON.stringify(params), headers); return request('POST', url, JSON.stringify(params), headers);
} }
export function httpPut(url: string, params: object = {}, headers: object = {}) { export async function httpPut(url: string, params: object = {}, headers: object = {}) {
return request('PUT', url, JSON.stringify(params), headers); return request('PUT', url, JSON.stringify(params), headers);
} }