mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 18:57:03 +02:00
feat: remove api
This commit is contained in:
1 parent
aa8bb1c040
commit
da8025a0ad
12 files changed
-372
No files matched your search
Vendored
-5
@@ -1,5 +0,0 @@
|
||||
/// <reference types="next" />
|
||||
/// <reference types="next/image-types/global" />
|
||||
|
||||
// NOTE: This file should not be edited
|
||||
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
|
||||
@@ -1,4 +0,0 @@
|
||||
/** @type {import('next').NextConfig} */
|
||||
const nextConfig = {};
|
||||
|
||||
export default nextConfig;
|
||||
@@ -1,18 +0,0 @@
|
||||
{
|
||||
"name": "@blitzkit/api",
|
||||
"private": true,
|
||||
"main": "src/index.ts",
|
||||
"scripts": {
|
||||
"dev": "next dev",
|
||||
"build": "next build"
|
||||
},
|
||||
"dependencies": {
|
||||
"react": "^18",
|
||||
"react-dom": "^18",
|
||||
"next": "14.2.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "^18",
|
||||
"@types/react-dom": "^18"
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
import { getRatingInfo, Region } from '@blitzkit/core';
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: { region: Region } },
|
||||
) {
|
||||
return NextResponse.json(await getRatingInfo(params.region));
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
import { getRatingLeague, Region } from '@blitzkit/core';
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: { region: Region; league: number } },
|
||||
) {
|
||||
return NextResponse.json(
|
||||
await getRatingLeague(params.region, params.league, true),
|
||||
);
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
import { Region, searchCurrentRatingPlayers } from '@blitzkit/core';
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: { region: Region; username: string } },
|
||||
) {
|
||||
return NextResponse.json(
|
||||
await searchCurrentRatingPlayers(params.region, params.username),
|
||||
);
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
import { assertSecret } from '@blitzkit/core';
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: { code: string } },
|
||||
) {
|
||||
const response = await fetch(
|
||||
`https://www.patreon.com/api/oauth2/token?code=${
|
||||
params.code
|
||||
}&grant_type=authorization_code&client_id=${assertSecret(
|
||||
process.env.PUBLIC_PATREON_CLIENT_ID,
|
||||
)}&client_secret=${assertSecret(
|
||||
process.env.PATREON_CLIENT_SECRET,
|
||||
)}&redirect_uri=${assertSecret(process.env.PUBLIC_PATREON_REDIRECT_URI)}`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||||
},
|
||||
);
|
||||
const json = await response.json();
|
||||
|
||||
return NextResponse.json(json);
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
import { assertSecret } from '@blitzkit/core';
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
interface PatreonMembershipResponse {
|
||||
data: {
|
||||
attributes: unknown;
|
||||
id: `${number}`;
|
||||
relationships: {
|
||||
memberships: {
|
||||
data: {
|
||||
id: string; // uuid
|
||||
type: 'member';
|
||||
}[];
|
||||
};
|
||||
};
|
||||
type: 'user';
|
||||
};
|
||||
included: (
|
||||
| {
|
||||
attributes: {
|
||||
patron_status:
|
||||
| 'active_patron'
|
||||
| 'declined_patron'
|
||||
| 'former_patron'
|
||||
| null; // null = never a paid member
|
||||
};
|
||||
id: string; // uuid
|
||||
relationships: {
|
||||
campaign: {
|
||||
data: {
|
||||
id: `${number}`; // id of my campaign to check against
|
||||
type: 'campaign';
|
||||
};
|
||||
links: {
|
||||
related: string; // url
|
||||
};
|
||||
};
|
||||
};
|
||||
type: 'member';
|
||||
}
|
||||
| {
|
||||
attributes: {};
|
||||
id: `${number}`;
|
||||
type: 'campaign'; // ignore this discriminator
|
||||
}
|
||||
)[];
|
||||
links: {
|
||||
self: 'https://www.patreon.com/api/oauth2/v2/user/124201389';
|
||||
};
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: { token: string } },
|
||||
) {
|
||||
const response = await fetch(
|
||||
encodeURI(
|
||||
'https://www.patreon.com/api/oauth2/v2/identity?include=memberships.campaign&fields[member]=patron_status',
|
||||
),
|
||||
{ headers: { Authorization: `Bearer ${params.token}` } },
|
||||
);
|
||||
|
||||
if (!response.ok) return NextResponse.json(false);
|
||||
|
||||
const json = (await response.json()) as PatreonMembershipResponse;
|
||||
|
||||
return NextResponse.json(
|
||||
json.included.some(
|
||||
(item) =>
|
||||
item.type === 'member' &&
|
||||
item.relationships.campaign.data.id ===
|
||||
assertSecret(process.env.PATREON_CAMPAIGN_ID) &&
|
||||
item.attributes.patron_status === 'active_patron',
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
import { assertSecret } from '@blitzkit/core';
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: { token: string } },
|
||||
) {
|
||||
const response = await fetch(
|
||||
`https://www.patreon.com/api/oauth2/token?grant_type=refresh_token&refresh_token=${
|
||||
params.token
|
||||
}&client_id=${assertSecret(
|
||||
process.env.PUBLIC_PATREON_CLIENT_ID,
|
||||
)}&client_secret=${assertSecret(process.env.PATREON_CLIENT_SECRET)}`,
|
||||
{ method: 'POST' },
|
||||
);
|
||||
const json = await response.json();
|
||||
|
||||
return NextResponse.json(json);
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
import {
|
||||
BlitzkitResponse,
|
||||
BlitzkitResponseError,
|
||||
fetchTankDefinitions,
|
||||
isValidBlitzId,
|
||||
TankVotes,
|
||||
usersDatabase,
|
||||
} from '@blitzkit/core';
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } },
|
||||
) {
|
||||
const id = Number(params.id);
|
||||
const tankDefinitions = await fetchTankDefinitions();
|
||||
|
||||
if (isNaN(id) || !tankDefinitions.tanks[id]) {
|
||||
return NextResponse.json({
|
||||
status: 'error',
|
||||
error: 'INVALID_TANK_ID',
|
||||
} satisfies BlitzkitResponseError);
|
||||
}
|
||||
|
||||
const blitzIdString = request.nextUrl.searchParams.get('player');
|
||||
const blitzId = Number(blitzIdString);
|
||||
const blitzToken = request.nextUrl.searchParams.get('token');
|
||||
|
||||
if ((blitzIdString === null) !== (blitzToken === null)) {
|
||||
return NextResponse.json({
|
||||
status: 'error',
|
||||
error: 'UNDEFINED_TOKEN_OR_PLAYER_ID',
|
||||
} satisfies BlitzkitResponseError);
|
||||
}
|
||||
|
||||
if (
|
||||
blitzIdString !== null &&
|
||||
(!(await isValidBlitzId(blitzId, blitzToken ?? undefined)) ||
|
||||
isNaN(blitzId))
|
||||
) {
|
||||
return NextResponse.json({
|
||||
status: 'error',
|
||||
error: 'INVALID_TOKEN_OR_PLAYER_ID',
|
||||
} satisfies BlitzkitResponseError);
|
||||
}
|
||||
|
||||
try {
|
||||
const aggregation = await usersDatabase.tankVote.aggregate({
|
||||
where: { id },
|
||||
_count: { id: true },
|
||||
_avg: {
|
||||
easiness: true,
|
||||
firepower: true,
|
||||
maneuverability: true,
|
||||
survivability: true,
|
||||
},
|
||||
});
|
||||
const userVote = await usersDatabase.tankVote.findUnique({
|
||||
where: { id, user_blitz_id: blitzId },
|
||||
select: { last_updated: true },
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
status: 'ok',
|
||||
data: {
|
||||
categories: aggregation._avg,
|
||||
votes: aggregation._count.id,
|
||||
last_updated: userVote?.last_updated.getTime(),
|
||||
},
|
||||
} satisfies BlitzkitResponse<TankVotes>);
|
||||
} catch (message) {
|
||||
return NextResponse.json({
|
||||
status: 'error',
|
||||
error: 'DATABASE_ERROR',
|
||||
message,
|
||||
} satisfies BlitzkitResponseError);
|
||||
}
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
import {
|
||||
BlitzkitResponse,
|
||||
BlitzkitResponseError,
|
||||
fetchTankDefinitions,
|
||||
isValidBlitzId,
|
||||
StarsInt,
|
||||
TANK_VOTE_CATEGORIES,
|
||||
TankVoteCategory,
|
||||
usersDatabase,
|
||||
} from '@blitzkit/core';
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } },
|
||||
) {
|
||||
const tankId = Number(params.id);
|
||||
const tankDefinitions = await fetchTankDefinitions();
|
||||
|
||||
if (isNaN(tankId) || !tankDefinitions.tanks[tankId]) {
|
||||
return NextResponse.json({
|
||||
status: 'error',
|
||||
error: 'INVALID_TANK_ID',
|
||||
} satisfies BlitzkitResponseError);
|
||||
}
|
||||
|
||||
const blitzIdString = request.nextUrl.searchParams.get('player');
|
||||
const blitzId = Number(blitzIdString);
|
||||
const blitzToken = request.nextUrl.searchParams.get('token');
|
||||
|
||||
if (blitzIdString === null || blitzToken === null) {
|
||||
return NextResponse.json({
|
||||
status: 'error',
|
||||
error: 'UNDEFINED_TOKEN_OR_PLAYER_ID',
|
||||
} satisfies BlitzkitResponseError);
|
||||
}
|
||||
|
||||
if (
|
||||
blitzIdString !== null &&
|
||||
(!(await isValidBlitzId(blitzId, blitzToken ?? undefined)) ||
|
||||
isNaN(blitzId))
|
||||
) {
|
||||
return NextResponse.json({
|
||||
status: 'error',
|
||||
error: 'INVALID_TOKEN_OR_PLAYER_ID',
|
||||
} satisfies BlitzkitResponseError);
|
||||
}
|
||||
|
||||
const votes: Partial<Record<TankVoteCategory, StarsInt>> = {};
|
||||
let recognizedVoteCategories = 0;
|
||||
|
||||
for (const category of TANK_VOTE_CATEGORIES) {
|
||||
const voteRaw = request.nextUrl.searchParams.get(category);
|
||||
|
||||
if (voteRaw === null) continue;
|
||||
|
||||
const vote = Number(voteRaw);
|
||||
|
||||
if (isNaN(vote) || !Number.isInteger(vote) || vote < 1 || vote > 5) {
|
||||
return NextResponse.json({
|
||||
status: 'error',
|
||||
error: 'INVALID_VOTE',
|
||||
} satisfies BlitzkitResponseError);
|
||||
}
|
||||
|
||||
votes[category] = vote as StarsInt;
|
||||
recognizedVoteCategories++;
|
||||
}
|
||||
|
||||
if (recognizedVoteCategories === 0) {
|
||||
return NextResponse.json({
|
||||
status: 'error',
|
||||
error: 'NO_VOTES',
|
||||
} satisfies BlitzkitResponseError);
|
||||
}
|
||||
|
||||
try {
|
||||
await usersDatabase.user.upsert({
|
||||
where: { blitz_id: blitzId },
|
||||
update: {
|
||||
last_used: new Date(),
|
||||
tank_votes: {
|
||||
upsert: {
|
||||
where: { id: tankId },
|
||||
update: { ...votes, last_updated: new Date() },
|
||||
create: { ...votes, id: tankId, last_updated: new Date() },
|
||||
},
|
||||
},
|
||||
},
|
||||
create: {
|
||||
blitz_id: blitzId,
|
||||
last_used: new Date(),
|
||||
tank_votes: {
|
||||
create: { ...votes, id: tankId, last_updated: new Date() },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
status: 'ok',
|
||||
data: undefined,
|
||||
} satisfies BlitzkitResponse);
|
||||
} catch (message) {
|
||||
return NextResponse.json({
|
||||
status: 'error',
|
||||
error: 'DATABASE_ERROR',
|
||||
message,
|
||||
} satisfies BlitzkitResponseError);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"plugins": [{ "name": "next" }]
|
||||
},
|
||||
"include": ["src/**/*"]
|
||||
}
|
||||
Reference in new issue
Block a user