get3BoxProfile handler

This commit is contained in:
Pacien Boisson
2020-07-13 11:40:26 +02:00
committed by Hammad Jutt
parent c2cfd22571
commit 8c4df7e547
5 changed files with 96 additions and 10 deletions

View File

@@ -1,5 +1,24 @@
export const CONFIG = {
port: process.env.PORT || 4000,
graphqlURL: process.env.GRAPHQL_URL || 'http://localhost:8080/v1/graphql',
adminKey: process.env.HASURA_GRAPHQL_ADMIN_SECRET || 'metagame_secret',
interface IConfig {
port: number,
graphqlURL: string,
adminKey: string,
ipfsEndpoint: string,
}
function parseEnv<T>(v: any, type: string, defaultValue: T): T {
if(!v) return defaultValue;
if(type === 'number') {
return (Number(v) as any) as T;
}
return v as T;
}
const config: IConfig = {
port: parseEnv<number>(process.env.PORT, 'number', 4000),
graphqlURL: parseEnv<string>(process.env.GRAPHQL_URL, 'string', 'http://localhost:8080/v1/graphql'),
adminKey: parseEnv<string>(process.env.HASURA_GRAPHQL_ADMIN_SECRET, 'string', "metagame_secret"),
ipfsEndpoint: parseEnv<string>(process.env.IPFS_ENDPOINT, 'string', 'https://ipfs.infura.io'),
};
export default config;

View File

@@ -0,0 +1,54 @@
import { Request, Response } from 'express';
import Box from '3box';
import config from '../../../config';
const handler = async (req: Request, res: Response) => {
const { address } = req.body.input;
const boxProfile = await Box.getProfile(address);
if(Object.keys(boxProfile).length === 0) {
return res.json({});
}
const parsedProfile: BoxProfile = {
name: boxProfile.name,
description: boxProfile.description,
location: boxProfile.location,
job: boxProfile.job,
emoji: boxProfile.emoji,
imageUrl: getProfilePicture(boxProfile),
};
return res.json(parsedProfile);
};
function getProfilePicture(boxProfile: any) {
const imageHash = boxProfile && boxProfile.image && boxProfile.image[0] && boxProfile.image[0].contentUrl && boxProfile.image[0].contentUrl['/'];
if(imageHash) {
return `${config.ipfsEndpoint}/ipfs/${imageHash}`;
} else {
return 'https://i.imgur.com/RXJO8FD.png';
}
}
export default handler;
/**
type Query {
get3BoxProfile(address: String): 3BoxProfile
}
type 3BoxProfile {
name: String
description: String
location: String
job: String
emoji: String
}
**/

View File

@@ -1,11 +1,13 @@
import express from 'express';
import { asyncHandlerWrapper } from '../../lib/apiHelpers';
import getBoxProfile from './getBoxProfile/handler';
import { updateBoxProfileHandler } from './updateBoxProfile/handler';
export const actionRoutes = express.Router();
actionRoutes.post(
'/updateBoxProfile',
asyncHandlerWrapper(updateBoxProfileHandler),
);
actionRoutes.post('/getBoxProfile', asyncHandlerWrapper(getBoxProfile));
actionRoutes.post('/updateBoxProfile', asyncHandlerWrapper(updateBoxProfileHandler));

View File

@@ -1,4 +1,15 @@
type Maybe<T> = T | null;
type Maybe<T> = T | null
type BoxProfile = {
name: string|null,
description: string|null,
location: string|null,
job: string|null,
emoji: string|null,
imageUrl: string|null,
}
type UpdateBoxProfileResponse = {
success: boolean;

View File

@@ -20,7 +20,7 @@ export function asyncHandlerWrapper(middleware: any) {
};
}
export function errorMiddleware(error: Error, _: Request, res: Response) {
export function errorMiddleware(error: Error, _: Request, res: Response, __: NextFunction) {
console.error(error);
res.status(500).send('Unexpected error');
}