Skip to content

Commit

Permalink
Feature/Rest API (sillsdev#39)
Browse files Browse the repository at this point in the history
* added REST endpoint

* rest api POST route

* removed redundancy in post method

* formatting for lint

* removed additional redundancy
  • Loading branch information
tbagwill committed Jul 19, 2023
1 parent 515cc88 commit 2f68f98
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/routes/submit/+server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { db } from '$lib/fbconfig.js';
import { addDoc, collection } from 'firebase/firestore';
import { allKeys, initKeys } from '$lib/stores/keys.js';
import { initPackages } from '$lib/stores/packages.js';

export async function POST({ request }) {
await initKeys();

let valid = false;

const bearer = request.headers.get('authorization');

allKeys.subscribe((keys) => {
keys.forEach((key) => {
if (bearer.includes(key.key)) {
valid = true;
}
});
});

if (valid) {
const pack = await request.json();

const col = collection(db, 'packages');

await addDoc(col, {
accepted: '',
...pack
});

await initPackages();

return new Response(201);
} else {
return new Response(401);
}
}

0 comments on commit 2f68f98

Please sign in to comment.