Skip to content

Commit

Permalink
cors
Browse files Browse the repository at this point in the history
  • Loading branch information
vjnvisakh-jtc committed Mar 18, 2024
1 parent be10601 commit b421d40
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 16 deletions.
4 changes: 2 additions & 2 deletions netlify/functions/gemini.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QuoteResponse } from './types';
import { QuoteResponse, QuoteSource } from './types';
const { GoogleGenerativeAI, HarmCategory, HarmBlockThreshold } = require("@google/generative-ai");

const MODEL_NAME = 'gemini-1.0-pro';
Expand Down Expand Up @@ -45,5 +45,5 @@ export const getQuoteFromGemini = async (): Promise<QuoteResponse> => {
});

const response = result.response;
return JSON.parse(response.text()) as QuoteResponse;
return { ...JSON.parse(response.text()), type: QuoteSource.GEMINI } as QuoteResponse;
}
4 changes: 2 additions & 2 deletions netlify/functions/ninja.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QuoteResponse } from './types';
import { QuoteResponse, QuoteSource } from './types';

const NINJA_API_KEY = process.env.REACT_APP_NINJA_API_KEY as string;
const NINJA_URL = process.env.REACT_APP_NINJA_URL as string;
Expand All @@ -12,5 +12,5 @@ export const getQuoteFromNinja = async () => {
);

const data = await response.json();
return data[0] as QuoteResponse;
return { ...data[0], type: QuoteSource.NINJA } as QuoteResponse;
}
14 changes: 14 additions & 0 deletions netlify/functions/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@ import type { Context } from '@netlify/functions';
import { getQuoteFromNinja } from './ninja';
import { getQuoteFromGemini } from './gemini';

const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE'
};

const quote = async (req: Request, context: Context) => {
let data;

if (req.method === 'OPTIONS') {
return {
statusCode: 200,
headers,
body: 'OPTIONS'
};
}

if (Math.random() < 0.5) {
data = await getQuoteFromNinja();
} else {
Expand Down
6 changes: 6 additions & 0 deletions netlify/functions/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export enum QuoteSource {
NINJA = 'NINJA',
GEMINI = 'GEMINI'
}

export type QuoteResponse = {
quote: string;
author: string;
type: QuoteSource;
}
16 changes: 4 additions & 12 deletions src/components/main/main.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import { ImBullhorn, ImCopy } from "react-icons/im";
import { HiVolumeOff } from "react-icons/hi";
import { MonoHeadingXXLarge, MonoParagraphLarge } from "baseui/typography";
import { StyledLink } from "baseui/link";
import { getQuoteFromGemini } from "../../services/gemini";
import { QuoteResponse } from "../../types/common";
import { getQuoteFromNinja } from "../../services/ninja";
import { QuoteResponse, QuoteSource } from "../../types/common";
import { Input } from "baseui/input";
import { addSubscriber, isSubscribedAlready } from "../../services/firebase";
import { getQuoteFromNetlify } from "../../services/netlify";

let firstLoad = true;

Expand Down Expand Up @@ -53,15 +52,8 @@ export const Main = () => {
const fetchNewQuote = async () => {
setIsLoading(true);

let data: QuoteResponse = { quote: '', author: ''};

if (Math.random() < 0.5) {
setPoweredByGemini(false);
data = await getQuoteFromNinja();
} else {
data = await getQuoteFromGemini();
setPoweredByGemini(true);
}
let data: QuoteResponse = await getQuoteFromNetlify();
setPoweredByGemini(data.type === QuoteSource.GEMINI);

setQuote(data.quote);
setAuthor(data.author);
Expand Down
9 changes: 9 additions & 0 deletions src/services/netlify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { QuoteResponse } from "../types/common";

const NETLIFY_QUOTE_URL = process.env.REACT_APP_NETLIFY_QUOTE_URL as string;

export const getQuoteFromNetlify = async () => {
const response = await fetch(NETLIFY_QUOTE_URL);
const data = await response.json();
return data.data as QuoteResponse;
}
6 changes: 6 additions & 0 deletions src/types/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export enum QuoteSource {
NINJA = 'NINJA',
GEMINI = 'GEMINI'
}

export type QuoteResponse = {
quote: string;
author: string;
type: QuoteSource;
}

0 comments on commit b421d40

Please sign in to comment.