Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api #734

Merged
merged 13 commits into from
Dec 19, 2023
Prev Previous commit
Next Next commit
Add more comments
LunaUrsa committed Dec 18, 2023
commit bd6bad58671176f5a7f3b1b96fcd2844ea102162
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v21.1.0
v21.4.0
106 changes: 70 additions & 36 deletions src/api/app.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/* eslint-disable max-len */

// Add return types
// Fix tests

import express from 'express';
import bodyParser from 'body-parser';
import morgan from 'morgan';
@@ -23,7 +20,7 @@

// Middleware to log before rate limiting
app.use((req, res, next) => {
console.log(`Incoming request for ${req.method} ${req.url}`);

Check warning on line 23 in src/api/app.ts

GitHub Actions / Lint

Unexpected console statement
next();
});

@@ -32,7 +29,7 @@
windowMs: 1 * 60 * 1000, // 1 minute
max: 60,
handler: (req, res /* next */) => {
console.log(`Hey fucko, rate limit exceeded for ${req.method} ${req.url}`);

Check warning on line 32 in src/api/app.ts

GitHub Actions / Lint

Unexpected console statement
res.status(429).send('Hey fucko, too many requests, please try again later.');
},
});
@@ -79,40 +76,77 @@
endpoints: {
'/tripsit': {
description: 'TripSit\'s original API, preserved for legacy purposes.',
endpoints: [
'/getInteraction',
'/getDrug',
'/getAllDrugNames',
'/getAllDrugNamesByCategory',
'/getAllDrugs',
'/getAllCategories',
'/getAllDrugAliases',
],
},
'/v1': {
description: 'Same as /tripsit, just renamed to v1 for consistency.',
endpoints: [
'/getInteraction',
'/getDrug',
'/getAllDrugNames',
'/getAllDrugNamesByCategory',
'/getAllDrugs',
'/getAllCategories',
'/getAllDrugAliases',
],
},
'/v2': {
description: 'TripSit\'s new API, under active development.',
warning: 'This does not work, don\'t use it',
endpoints: [
'/drugs',
'/interactions',
'/combinations',
'/categories',
'/aliases',
'/search',
],
endpoints: {
'/getAllDrugNames': {
output: 'string[]',
},
'/getAllDrugNamesByCategory': {
output: 'string[]',
},
'/getAllDrugs': {
output: '{ [drugName: string]: Drug }, See github.com/tripsit/drugs for type info',
},
'/getAllCategories': {
output: 'string[]',
},
'/getAllDrugAliases': {
output: 'string[]',
},
'/getDrug': {
input: {
drugName: 'string',
},
example: '/getDrug/DXM',
output: {
success: 'Drug Object, see github./com/tripsit/drugs for type info',
error: {
err: 'boolean',
msg: 'string',
options: 'string[]',
},
},
},
'/getInteraction': {
input: {
drugA: 'string',
drugB: 'string',
},
example: '/getInteraction/DXM/MDMA',
output: {
success: {
result: 'string',
interactionCategoryA: 'string',
interactionCategoryB: 'string',
definition: 'string?',
thumbnail: 'string?',
color: 'string?',
note: 'string?',
source: 'string?',
},
error: {
err: 'boolean',
msg: 'string',
options: 'string[]',
},
},
},
},
},
// '/v1': {
// description: 'Same as /tripsit, just renamed to v1 for consistency.',
// },
// '/v2': {
// description: 'TripSit\'s new API, under active development.',
// warning: 'This does not work, don\'t use it',
// endpoints: [
// '/drugs',
// '/interactions',
// '/combinations',
// '/categories',
// '/aliases',
// '/search',
// ],
// },
},
});
});
25 changes: 19 additions & 6 deletions src/api/v1/drugs/drugs.queries.ts
Original file line number Diff line number Diff line change
@@ -64,18 +64,27 @@ export default {
};
},

async getDrug(drugName:string):Promise<Drug> {
return (drugData as DrugData)[drugName.toLowerCase()] as Drug;
async getDrug(drugName:string):Promise<Drug | {
err: boolean;
msg: string;
options?: string[];
}> {
const drug = (drugData as DrugData)[drugName.toLowerCase()] as Drug;

if (drug) {
return drug;
}
return {
err: true,
msg: 'Drug with that name not found, please try again.',
options: await this.getAllDrugNames(),
};
},

async getInteraction(
drugAInput:string,
drugBInput:string,
):Promise<{
err: boolean;
msg: string;
options?: string[];
} | {
result: string;
interactionCategoryA: string;
interactionCategoryB: string;
@@ -84,6 +93,10 @@ export default {
color: string;
note?: string;
// source?: string;
} | {
err: boolean;
msg: string;
options?: string[];
}> {
log.debug(F, `getInteraction | drugA: ${drugAInput}, drugB: ${drugBInput}`);
return combo(drugAInput, drugBInput);
43 changes: 33 additions & 10 deletions src/api/v1/drugs/drugs.routes.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import RateLimit from 'express-rate-limit';

import queries from './drugs.queries';

const F = f(__filename);
const F = f(__filename); // eslint-disable-line

const router = express.Router();

@@ -20,12 +20,28 @@ router.use(limiter);
// getInteraction - readme
router.get('/getInteraction', (req, res) => {
res.json({
endpoint: 'getInteraction',
properties: [
'/drugAName',
'/drugBName',
],
input: {
drugA: 'string',
drugB: 'string',
},
example: '/getInteraction/DXM/MDMA',
output: {
success: {
result: 'string',
interactionCategoryA: 'string',
interactionCategoryB: 'string',
definition: 'string?',
thumbnail: 'string?',
color: 'string?',
note: 'string?',
source: 'string?',
},
error: {
err: 'boolean',
msg: 'string',
options: 'string[]',
},
},
});
});

@@ -54,11 +70,18 @@ router.get('/getInteraction/:drugAName/:drugBName', async (req, res, next) => {
// getDrug - readme
router.get('/getDrug', (req, res) => {
res.json({
endpoint: 'getDrug',
properties: [
'/name',
],
input: {
drugName: 'string',
},
example: '/getDrug/DXM',
output: {
success: 'Drug Object, see github./com/tripsit/drugs for type info',
error: {
err: 'boolean',
msg: 'string',
options: 'string[]',
},
},
});
});

65 changes: 56 additions & 9 deletions src/api/v1/index.ts
Original file line number Diff line number Diff line change
@@ -7,15 +7,62 @@ const router = express.Router();
router.get('/', (req, res) => {
res.json({
welcome: 'Welcome to TripSit\'s original API, preserved for legacy purposes.',
publicEndpoints: [
'/getInteraction',
'/getDrug',
'/getAllDrugNames',
'/getAllDrugNamesByCategory',
'/getAllDrugs',
'/getAllCategories',
'/getAllDrugAliases',
],
publicEndpoints: {
'/getAllDrugNames': {
output: 'string[]',
},
'/getAllDrugNamesByCategory': {
output: 'string[]',
},
'/getAllDrugs': {
output: '{ [drugName: string]: Drug }, See github.com/tripsit/drugs for type info',
},
'/getAllCategories': {
output: 'string[]',
},
'/getAllDrugAliases': {
output: 'string[]',
},

'/getDrug': {
input: {
drugName: 'string',
},
example: '/getDrug/DXM',
output: {
success: 'Drug Object, see github./com/tripsit/drugs for type info',
error: {
err: 'boolean',
msg: 'string',
options: 'string[]',
},
},
},
'/getInteraction': {
input: {
drugA: 'string',
drugB: 'string',
},
example: '/getInteraction/DXM/MDMA',
output: {
success: {
result: 'string',
interactionCategoryA: 'string',
interactionCategoryB: 'string',
definition: 'string?',
thumbnail: 'string?',
color: 'string?',
note: 'string?',
source: 'string?',
},
error: {
err: 'boolean',
msg: 'string',
options: 'string[]',
},
},
},
},
});
});

Loading