Skip to content

Commit

Permalink
Scaffolded all API routes, worked on DELETE requests, added nodemon/uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
heythisischris committed Apr 21, 2024
1 parent 8f13660 commit 841da5b
Show file tree
Hide file tree
Showing 18 changed files with 439 additions and 19 deletions.
17 changes: 14 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"author": "Christopher Aitken <[email protected]>",
"license": "AGPL-3.0-only",
"scripts": {
"start:prod": "esbuild --format=esm src/**/*.ts --outdir=dist && node --env-file=.env.production dist/app.js",
"start:staging": "esbuild --format=esm src/**/*.ts --platform=node --outdir=dist && node --env-file=.env.staging dist/app.js",
"start:prod": "esbuild --format=esm src/**/*.ts --outdir=dist && nodemon --env-file=.env.production dist/app.js",
"start:staging": "esbuild --format=esm src/**/*.ts --outdir=dist && nodemon --env-file=.env.staging dist/app.js",
"build:prod": "esbuild src/*.ts --bundle --platform=node --outdir=dist --external:@aws-sdk && cd dist && zip -r lambda.zip * > /dev/null && aws --profile equalify lambda update-function-code --function-name equalify-api --zip-file \"fileb://lambda.zip\" > /dev/null && rm -rf lambda.zip",
"build:staging": "esbuild src/*.ts --bundle --platform=node --outdir=dist --external:@aws-sdk && cd dist && zip -r lambda.zip * > /dev/null && aws --profile equalify lambda update-function-code --function-name equalify-api-staging --zip-file \"fileb://lambda.zip\" > /dev/null && rm -rf lambda.zip"
},
Expand All @@ -21,14 +21,25 @@
"esbuild": "^0.20.1",
"fastify": "^4.26.2",
"graphql": "^16.8.1",
"nodemon": "^3.1.0",
"openai": "^4.36.0",
"pg": "^8.11.5",
"postgraphile": "^4.13.0",
"postgraphile-plugin-connection-filter": "^2.3.0",
"serverless-postgres": "^2.1.0",
"string-strip-html": "^13.4.8"
"string-strip-html": "^13.4.8",
"uuid": "^9.0.1"
},
"resolutions": {
"graphql": "16.x"
},
"nodemonConfig": {
"ext": "ts,js,json",
"watch": [
"src/"
],
"events": {
"restart": "esbuild --format=esm src/**/*.ts --outdir=dist"
}
}
}
24 changes: 22 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Fastify from 'fastify';
import { getReports, getResults, graphql, help } from './routes/index.js';
import { addProperties, addReports, addResults, addScans, deleteProperties, deleteReports, getProperties, getReports, getResults, getScans, getUpdates, graphql, help, updateProperties, updateReports } from './routes/index.js';
import { CognitoJwtVerifier } from 'aws-jwt-verify';
export const fastify = Fastify({ logger: true });
const cognitoJwtVerifier = CognitoJwtVerifier.create({
Expand All @@ -19,9 +19,29 @@ fastify.addHook('preHandler', async (request, reply) => {
}
})

fastify.post('/graphql', {}, async (request, reply) => graphql({ request, reply }));
// GET requests
fastify.get('/get/results', {}, async (request, reply) => getResults({ request, reply }));
fastify.get('/get/properties', {}, async (request, reply) => getProperties({ request, reply }));
fastify.get('/get/updates', {}, async (request, reply) => getUpdates({ request, reply }));
fastify.get('/get/scans', {}, async (request, reply) => getScans({ request, reply }));
fastify.get('/get/reports', {}, async (request, reply) => getReports({ request, reply }));

// POST requests
fastify.post('/add/results', {}, async (request, reply) => addResults({ request, reply }));
fastify.post('/add/scans', {}, async (request, reply) => addScans({ request, reply }));
fastify.post('/add/reports', {}, async (request, reply) => addReports({ request, reply }));
fastify.post('/add/properties', {}, async (request, reply) => addProperties({ request, reply }));

// PUT requests
fastify.put('/update/properties', {}, async (request, reply) => updateProperties({ request, reply }));
fastify.put('/update/reports', {}, async (request, reply) => updateReports({ request, reply }));

// DELETE requests
fastify.delete('/delete/properties', {}, async (request, reply) => deleteProperties({ request, reply }));
fastify.delete('/delete/reports', {}, async (request, reply) => deleteReports({ request, reply }));

// MISC requests
fastify.post('/graphql', {}, async (request, reply) => graphql({ request, reply }));
fastify.post('/help', {}, async (request, reply) => help({ request, reply }));

fastify.listen({ port: 3000 }, (err) => {
Expand Down
8 changes: 8 additions & 0 deletions src/routes/addProperties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { jwtClaims } from '../app.js';
import { pgClient, validateUuid } from '../utils/index.js';

export const addProperties = async ({ request, reply }) => {
await pgClient.connect();
await pgClient.clean();
return;
}
8 changes: 8 additions & 0 deletions src/routes/addReports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { jwtClaims } from '../app.js';
import { pgClient, validateUuid } from '../utils/index.js';

export const addReports = async ({ request, reply }) => {
await pgClient.connect();
await pgClient.clean();
return;
}
8 changes: 8 additions & 0 deletions src/routes/addResults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { jwtClaims } from '../app.js';
import { pgClient, validateUuid } from '../utils/index.js';

export const addResults = async ({ request, reply }) => {
await pgClient.connect();
await pgClient.clean();
return;
}
8 changes: 8 additions & 0 deletions src/routes/addScans.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { jwtClaims } from '../app.js';
import { pgClient, validateUuid } from '../utils/index.js';

export const addScans = async ({ request, reply }) => {
await pgClient.connect();
await pgClient.clean();
return;
}
37 changes: 37 additions & 0 deletions src/routes/deleteProperties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { jwtClaims } from '../app.js';
import { pgClient, validateUuid } from '../utils/index.js';

export const deleteProperties = async ({ request, reply }) => {
if (!request.query.propertyId) {
return {
status: 'error',
message: 'Property ID is required.',
}
}
else if (!validateUuid(request.query.propertyId)) {
return {
status: 'error',
message: 'Property ID is not a valid UUID.',
}
}

await pgClient.connect();
const deletedIds = (await pgClient.query(`
DELETE FROM "properties" WHERE "id"=$1 AND "user_id"=$2 RETURNING "id"
`, [request.query.propertyId, jwtClaims.sub])).rows.map(obj => obj.id);
await pgClient.clean();

if (deletedIds.length === 0) {
return {
status: 'error',
message: 'propertyId not found, no properties deleted',
}
}
else {
return {
status: 'success',
message: 'Property deletion successful',
result: deletedIds,
};
}
}
37 changes: 37 additions & 0 deletions src/routes/deleteReports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { jwtClaims } from '../app.js';
import { pgClient, validateUuid } from '../utils/index.js';

export const deleteReports = async ({ request, reply }) => {
if (!request.query.reportId) {
return {
status: 'error',
message: 'Property ID is required.',
}
}
else if (!validateUuid(request.query.reportId)) {
return {
status: 'error',
message: 'Property ID is not a valid UUID.',
}
}

await pgClient.connect();
const deletedIds = (await pgClient.query(`
DELETE FROM "reports" WHERE "id"=$1 AND "user_id"=$2 RETURNING "id"
`, [request.query.reportId, jwtClaims.sub])).rows.map(obj => obj.id);
await pgClient.clean();

if (deletedIds.length === 0) {
return {
status: 'error',
message: 'reportId not found, no reports deleted.',
}
}
else {
return {
status: 'success',
message: 'Property deletion successful',
result: deletedIds,
};
}
}
8 changes: 8 additions & 0 deletions src/routes/getProperties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { jwtClaims } from '../app.js';
import { pgClient } from '../utils/index.js';

export const getProperties = async ({ request, reply }) => {
await pgClient.connect();
await pgClient.clean();
return;
}
50 changes: 42 additions & 8 deletions src/routes/getResults.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
import { jwtClaims } from '../app.js';
import { graphqlQuery, pgClient } from '../utils/index.js';
import { graphqlQuery } from '../utils/index.js';

export const getResults = async ({ request, reply }) => {
await pgClient.connect();
const { rows } = await pgClient.query(`SELECT * FROM "users"`);
console.log(rows);
await pgClient.clean();
console.log(jwtClaims);
/*
Ability to filter by propertyIds, urlIds, nodeIds, nodeUpdateIds, messageIds, and tagIds
Messages, Tags, Properties, Pages are sorted by properties related to the most nodes w/ nodeEqualified set to false (most to least)
*/
const response = await graphqlQuery({ query: `{allUsers {nodes {name}}}` });
return { response };
/*
// Return should follow the Equalify Schema:
{
urls: [
{
urlId: 'uuid',
url: 'string',
},
],
messages: [
{
message: 'string',
relatedTagIds: ['uuid'],
relatedNodeIds: ['uuid'],
type: 'enum (pass/error/violation)',
},
],
tags: [
{
tagId: 'uuid',
tag: 'string',
},
],
nodes: [
{
nodeId: 'uuid',
html: 'string',
targets: ['string'], // OPTIONAL
relatedUrlId: 'uuid',
equalified: 'boolean',
}
]
}
*/
return {
response
};
}
8 changes: 8 additions & 0 deletions src/routes/getScans.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { jwtClaims } from '../app.js';
import { pgClient } from '../utils/index.js';

export const getScans = async ({ request, reply }) => {
await pgClient.connect();
await pgClient.clean();
return;
}
8 changes: 8 additions & 0 deletions src/routes/getUpdates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { jwtClaims } from '../app.js';
import { pgClient } from '../utils/index.js';

export const getUpdates = async ({ request, reply }) => {
await pgClient.connect();
await pgClient.clean();
return;
}
15 changes: 13 additions & 2 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
export * from './addProperties.js'
export * from './addReports.js'
export * from './addResults.js'
export * from './addScans.js'
export * from './deleteProperties.js'
export * from './deleteReports.js'
export * from './getProperties.js'
export * from './getReports.js'
export * from './getResults.js'
export * from './getScans.js'
export * from './getUpdates.js'
export * from './graphql.js'
export * from './getReports.js'
export * from './help.js'
export * from './help.js'
export * from './updateProperties.js'
export * from './updateReports.js'
8 changes: 8 additions & 0 deletions src/routes/updateProperties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { jwtClaims } from '../app.js';
import { pgClient, validateUuid } from '../utils/index.js';

export const updateProperties = async ({ request, reply }) => {
await pgClient.connect();
await pgClient.clean();
return;
}
8 changes: 8 additions & 0 deletions src/routes/updateReports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { jwtClaims } from '../app.js';
import { pgClient, validateUuid } from '../utils/index.js';

export const updateReports = async ({ request, reply }) => {
await pgClient.connect();
await pgClient.clean();
return;
}
3 changes: 2 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './graphqlQuery.js'
export * from './pgClient.js'
export * from './openai.js'
export * from './openai.js'
export * from './validateUuid.js'
8 changes: 8 additions & 0 deletions src/utils/validateUuid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { validate } from 'uuid';

export const validateUuid = (value) => {
if (!value.includes('-')) {
value = value.substr(0, 8) + '-' + value.substr(8, 4) + '-' + value.substr(12, 4) + '-' + value.substr(16, 4) + '-' + value.substr(20, 12)
}
return validate(value);
}
Loading

0 comments on commit 841da5b

Please sign in to comment.