Skip to content

Commit 0ab3a30

Browse files
authored
Add user agent header
Add user agent header
2 parents b27af5f + faea903 commit 0ab3a30

13 files changed

Lines changed: 97 additions & 9 deletions

File tree

apps/api/src/main.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
import { NestFactory } from '@nestjs/core';
22
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
3+
import axios from 'axios';
4+
import {
5+
CREATOR_AI_USER_AGENT,
6+
installCreatorAiAxiosDefaults,
7+
installCreatorAiFetchDefaults,
8+
} from '@repo/validation';
39
import { AppModule } from './app.module';
410

11+
installCreatorAiFetchDefaults();
12+
installCreatorAiAxiosDefaults(axios);
13+
514
async function bootstrap() {
615
const app = await NestFactory.create(AppModule, { rawBody: true });
716

17+
app.use((_req, res, next) => {
18+
res.setHeader('Server', CREATOR_AI_USER_AGENT);
19+
next();
20+
});
21+
822
const isWorker = process.env.NODE_ENV === 'worker';
923

1024
if (isWorker) {

apps/api/src/youtube/youtube.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { getUserId } from '../common/get-user-id';
1010
@Controller('youtube')
1111
@UseGuards(SupabaseAuthGuard)
1212
export class YoutubeController {
13-
constructor(private readonly youtubeService: YoutubeService) {}
13+
constructor(private readonly youtubeService: YoutubeService) { }
1414

1515
@Get('video-metadata')
1616
@ApiOperation({ summary: 'Resolve YouTube video metadata from URL' })

apps/api/src/youtube/youtube.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export class YoutubeService {
169169
if (!usageData.allowed) {
170170
throw new BadRequestException(usageData.message);
171171
}
172-
172+
173173
const { data: channel, error } = await this.supabase
174174
.from('youtube_channels')
175175
.select('channel_id, provider_token, refresh_token')

apps/web/app/api/auth/callback/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { type EmailOtpType } from '@supabase/supabase-js';
22
import { type NextRequest } from 'next/server';
33
import { redirect } from 'next/navigation';
44
import { getSupabaseServer } from '@/lib/supabase/server';
5+
import { creatorAiFetch } from '@/lib/creator-ai-http';
56
import { Resend } from 'resend';
67
import { BACKEND_URL } from '@/lib/constants';
78

@@ -104,7 +105,7 @@ async function processReferral(
104105
userEmail: string,
105106
) {
106107
try {
107-
const response = await fetch(`${BACKEND_URL}/api/v1/referral/track`, {
108+
const response = await creatorAiFetch(`${BACKEND_URL}/api/v1/referral/track`, {
108109
method: 'POST',
109110
headers: { 'Content-Type': 'application/json' },
110111
body: JSON.stringify({ referralCode, userEmail }),

apps/web/instrumentation.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export async function register() {
2+
if (process.env.NEXT_RUNTIME !== 'nodejs') return;
3+
4+
const axios = (await import('axios')).default;
5+
const { installCreatorAiAxiosDefaults } = await import('@repo/validation');
6+
installCreatorAiAxiosDefaults(axios);
7+
}

apps/web/lib/api-client.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import axios, {
1010
AxiosProgressEvent
1111
} from 'axios';
1212
import { createClient } from "@/lib/supabase/client";
13+
import { CREATOR_AI_USER_AGENT } from "@repo/validation";
1314

1415

1516
export interface ApiErrorResponse {
@@ -88,6 +89,8 @@ const axiosInstance = axios.create({
8889
baseURL: getBackendUrl(),
8990
headers: {
9091
'Content-Type': 'application/json',
92+
'User-Agent': CREATOR_AI_USER_AGENT,
93+
'X-Creator-AI-Agent': CREATOR_AI_USER_AGENT,
9194
},
9295
});
9396

apps/web/lib/creator-ai-http.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {
2+
CREATOR_AI_USER_AGENT,
3+
mergeCreatorAiHeaders,
4+
} from '@repo/validation';
5+
6+
export { CREATOR_AI_USER_AGENT, mergeCreatorAiHeaders };
7+
8+
export function creatorAiFetch(
9+
input: RequestInfo | URL,
10+
init?: RequestInit,
11+
): Promise<Response> {
12+
return fetch(input, {
13+
...init,
14+
headers: mergeCreatorAiHeaders(init?.headers),
15+
});
16+
}

apps/web/middleware.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createServerClient } from "@supabase/ssr"
2+
import { CREATOR_AI_USER_AGENT, type UserRole } from "@repo/validation"
23
import { NextResponse } from "next/server"
34
import type { NextRequest } from "next/server"
45

@@ -10,14 +11,19 @@ const BYPASS_PATHS = [
1011
"/api/youtube/callback",
1112
]
1213

13-
async function getUserRole(supabase: ReturnType<typeof createServerClient>, userId: string) {
14+
async function getUserRole(
15+
supabase: ReturnType<typeof createServerClient>,
16+
userId: string,
17+
): Promise<UserRole | undefined> {
1418
try {
15-
const { data } = await supabase
19+
const { data, error } = await supabase
1620
.from("profiles")
1721
.select("role")
1822
.eq("user_id", userId)
19-
.single()
20-
return data?.role as string | undefined
23+
.single<{ role: UserRole }>()
24+
25+
if (error || !data) return undefined
26+
return data.role
2127
} catch {
2228
return undefined
2329
}
@@ -27,10 +33,13 @@ export async function middleware(request: NextRequest) {
2733
const { pathname } = request.nextUrl
2834

2935
if (BYPASS_PATHS.includes(pathname)) {
30-
return NextResponse.next()
36+
const response = NextResponse.next()
37+
response.headers.set("Server", CREATOR_AI_USER_AGENT)
38+
return response
3139
}
3240

3341
const response = NextResponse.next()
42+
response.headers.set("Server", CREATOR_AI_USER_AGENT)
3443

3544
const supabase = createServerClient(
3645
process.env.NEXT_PUBLIC_SUPABASE_URL!,

apps/web/next.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const nextConfig = {
3333
{
3434
source: "/(.*)",
3535
headers: [
36+
{ key: "Server", value: "CreatorAI/1.0 (+https://tryscriptai.com)" },
3637
{ key: "X-Content-Type-Options", value: "nosniff" },
3738
{ key: "X-Frame-Options", value: "SAMEORIGIN" },
3839
{ key: "Referrer-Policy", value: "strict-origin-when-cross-origin" },

packages/train-ai-worker/src/main.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import { NestFactory } from '@nestjs/core';
22
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
3+
import axios from 'axios';
4+
import {
5+
installCreatorAiAxiosDefaults,
6+
installCreatorAiFetchDefaults,
7+
} from '@repo/validation';
38
import { WorkerModule } from './worker.module';
49

10+
installCreatorAiFetchDefaults();
11+
installCreatorAiAxiosDefaults(axios);
12+
513
async function bootstrap() {
614
const app = await NestFactory.create(WorkerModule);
715

0 commit comments

Comments
 (0)