Skip to content

Commit

Permalink
Merge pull request #54 from xebia-france/feat/fcv
Browse files Browse the repository at this point in the history
✨ fcv
  • Loading branch information
blacroix committed Dec 20, 2022
2 parents 55c4c3e + 0c0506e commit 0b236c6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
8 changes: 8 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,13 @@
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
},
"hosting": {
"rewrites": [
{
"source": "/cv/**",
"functions": "fcv"
}
]
}
}
19 changes: 12 additions & 7 deletions functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
"main": "lib/index.js",
"dependencies": {
"axios": "^1.2.1",
"cors": "^2.8.5",
"firebase-admin": "^10.0.2",
"firebase-functions": "^4.1.0"
"firebase-functions": "^4.1.1"
},
"devDependencies": {
"@types/cors": "^2.8.13",
"@typescript-eslint/eslint-plugin": "^5.12.0",
"@typescript-eslint/parser": "^5.12.0",
"eslint": "^8.9.0",
Expand Down
4 changes: 2 additions & 2 deletions functions/src/cv.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";

export const getCv = async (userId: string) => {
const {data} = await axios.get(`https://cdn.contentful.com/spaces/${process.env.CONTENTFUL_SPACE}/environments/master/entries/?content_type=curriculumVitae&include=2&fields.id=${userId}`, {
export const getCv = async (userId: string, locale: string) => {
const {data} = await axios.get(`https://cdn.contentful.com/spaces/${process.env.CONTENTFUL_SPACE}/environments/master/entries/?content_type=curriculumVitae&include=2&fields.id=${userId}&locale=${locale}`, {
headers: {
Authorization: `Bearer ${process.env.CONTENTFUL_TOKEN}`,
},
Expand Down
24 changes: 15 additions & 9 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import * as functions from "firebase-functions";
import {initializeApp} from "firebase-admin/app";
import * as cors from "cors";
import {getUserId, isUserAuthorized} from "./auth";
import {getCv} from "./cv";

const corsHandler = cors({origin: true});

initializeApp();

// noinspection JSUnusedGlobalSymbols
export const cv = functions
export const fcv = functions
.region("europe-central2")
.https.onRequest(async (request, response) => {
const userId = await getUserId(request.get("Authorization"));
if (!userId || !isUserAuthorized(userId)) {
response.sendStatus(403);
return;
}
const cv = await getCv(userId);
response.status(200).send(cv);
.https.onRequest((request, response) => {
corsHandler(request, response, async () => {
const userId = await getUserId(request.get("Authorization"));
const locale = (request.query.locale || "fr") as string;
if (!userId || !isUserAuthorized(userId)) {
response.sendStatus(403);
return;
}
const cv = await getCv(userId, locale);
response.status(200).send(cv);
});
});

0 comments on commit 0b236c6

Please sign in to comment.