Skip to content

Commit

Permalink
Fix github stars endpoint (twentyhq#5301)
Browse files Browse the repository at this point in the history
- Encapsulated GitHub star response in an object
- Fixed rounding of Github stars to align with Github convention
- Fixed CORS issue so that endpoint can be called from twenty.com and
app.twenty.com

Co-authored-by: Ady Beraud <[email protected]>
  • Loading branch information
ady-beraud and ady-test authored May 7, 2024
1 parent a2017ea commit b438fc2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/twenty-website/src/app/api/github-stars/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export async function GET() {
desc(githubStarsModel.timestamp),
);

const formattedGithubNumberOfStars = formatNumberOfStars(
const githubNumberOfStars = formatNumberOfStars(
githubStars[0].numberOfStars,
);

return Response.json(formattedGithubNumberOfStars);
return Response.json({ githubNumberOfStars });
} catch (error: any) {
return new Response(`Github stars error: ${error?.message}`, {
status: 500,
Expand Down
33 changes: 33 additions & 0 deletions packages/twenty-website/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { NextResponse } from 'next/server';

const allowedOrigins = [
'http://localhost:3000',
'https://app.twenty.com',
'https://twenty.com',
];

export function middleware(req: any) {
const res = NextResponse.next();

const origin = req.headers.get('origin');

if (allowedOrigins.includes(origin)) {
res.headers.append('Access-Control-Allow-Origin', origin);
}

res.headers.append('Access-Control-Allow-Credentials', 'true');
res.headers.append(
'Access-Control-Allow-Methods',
'GET,DELETE,PATCH,POST,PUT',
);
res.headers.append(
'Access-Control-Allow-Headers',
'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version',
);

return res;
}

export const config = {
matcher: '/api/:path*',
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const formatNumberOfStars = (numberOfStars: number) => {
return Math.floor(numberOfStars / 100) / 10 + 'k';
return Math.ceil(numberOfStars / 100) / 10 + 'k';
};

0 comments on commit b438fc2

Please sign in to comment.