Skip to content

Commit

Permalink
Fixing eslint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mzkrasner committed Apr 11, 2024
1 parent 04514cc commit 594c53a
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 464 deletions.
6 changes: 2 additions & 4 deletions demo/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@
"key-did-resolver": "^2.1.3",
"nodemon": "^3.1.0",
"ts-node": "^10.9.1",
"tslint": "^6.1.3",
"typescript": "^4.8.4",
"@composexp/points": "workspace:^",
"@composexp/ceramic-utils": "workspace:^",
"@composexp/did-utils": "workspace:^",
"uint8arrays": "^5.0.1"
"uint8arrays": "^5.0.3"
},
"dependencies": {
"@ceramicnetwork/http-client": "^5.5.0",
"@ceramicnetwork/http-client": "^5.6.0",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
Expand Down
60 changes: 52 additions & 8 deletions demo/server/src/controllers/multiController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,39 @@ type ContextAggregationContent = {
context: string
}

const getContextAggregation = async (req: Request, res: Response, next: NextFunction) => {
export interface GetContextAggregationRequest extends Request {
body: {
recipient: string
context: string
}
}

export interface UpdateTotalAggregationRequest extends Request {
body: {
recipient: string
amount: number
}
}

export interface UpdateContextAggregationRequest extends Request {
body: {
recipient: string
context: string
amount: number
}
}

export interface GetTotalRequest extends Request {
body: {
recipient: string
}
}

const getContextAggregation = async (
req: GetContextAggregationRequest,
res: Response,
next: NextFunction,
): Promise<void> => {
try {
const { ceramic, aggregationModelID } = await getContext()
const { recipient, context } = req.body
Expand All @@ -29,11 +61,15 @@ const getContextAggregation = async (req: Request, res: Response, next: NextFunc
return next()
} catch (error) {
console.error(error)
return error
next(error)
}
}

const getTotalAggregation = async (req: Request, res: Response, next: NextFunction) => {
const getTotalAggregation = async (
req: GetTotalRequest,
res: Response,
next: NextFunction,
): Promise<void> => {
try {
const { ceramic } = await getContext()
const { recipient } = req.body
Expand All @@ -52,11 +88,15 @@ const getTotalAggregation = async (req: Request, res: Response, next: NextFuncti
return next()
} catch (error) {
console.error(error)
return error
next(error)
}
}

const updateContextAggregation = async (req: Request, res: Response, next: NextFunction) => {
const updateContextAggregation = async (
req: UpdateContextAggregationRequest,
res: Response,
next: NextFunction,
): Promise<void> => {
try {
const { ceramic, aggregationModelID } = await getContext()
const { amount, recipient, context } = req.body
Expand Down Expand Up @@ -106,11 +146,15 @@ const updateContextAggregation = async (req: Request, res: Response, next: NextF
return next()
} catch (error) {
console.error(error)
return error
next(error)
}
}

const updateTotalAggregation = async (req: Request, res: Response, next: NextFunction) => {
const updateTotalAggregation = async (
req: UpdateTotalAggregationRequest,
res: Response,
next: NextFunction,
): Promise<void> => {
try {
const { ceramic } = await getContext()
const { amount, recipient } = req.body
Expand Down Expand Up @@ -147,7 +191,7 @@ const updateTotalAggregation = async (req: Request, res: Response, next: NextFun
return next()
} catch (error) {
console.error(error)
return error
next(error)
}
}

Expand Down
81 changes: 42 additions & 39 deletions demo/server/src/controllers/singleController.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,70 @@
import { getContext } from "../utils/context.js";
import { Request, Response, NextFunction } from "express";
import {
SinglePointReader,
SinglePointWriter,
} from '@composexp/points';
import { getContext } from '../utils/context.js'
import { Request, Response, NextFunction } from 'express'
import { SinglePointReader, SinglePointWriter } from '@composexp/points'

export interface CreateSinglePointRequest extends Request {
body: {
recipient: string
}
}

const createSinglePoint = async (
req: Request,
req: CreateSinglePointRequest,
res: Response,
next: NextFunction
next: NextFunction,
) => {
try {
const { ceramic } = await getContext();
const writer = new SinglePointWriter({ ceramic });
await writer.addPointTo(req.body.recipient);
res.locals.ceramic = ceramic;
return next();
const { ceramic } = await getContext()
const writer = new SinglePointWriter({ ceramic })
await writer.addPointTo(req.body.recipient)
res.locals.ceramic = ceramic
return next()
} catch (error) {
console.error(error);
return error;
console.error(error)
return error
}
};
}

const removeSinglePoint = async (
req: Request,
req: CreateSinglePointRequest,
_res: Response,
next: NextFunction
next: NextFunction,
) => {
try {
const { ceramic } = await getContext();
const { ceramic } = await getContext()
const reader = new SinglePointReader({
ceramic,
issuer: ceramic.did!.id,
});
const documents = await reader.queryPointDocumentsFor(req.body.recipient);
const id = documents.documents[documents.documents.length - 1].id;
const writer = new SinglePointWriter({ ceramic });
await writer.removePoint(id.toString());
return next();
})
const documents = await reader.queryPointDocumentsFor(req.body.recipient)
const id = documents.documents[documents.documents.length - 1].id
const writer = new SinglePointWriter({ ceramic })
await writer.removePoint(id.toString())
return next()
} catch (error) {
console.error(error);
return error;
console.error(error)
return error
}
};
}

const getSinglePoints = async (
req: Request,
req: CreateSinglePointRequest,
res: Response,
next: NextFunction
next: NextFunction,
) => {
try {
const ceramic = res.locals.ceramic ?? (await getContext()).ceramic;
const { ceramic } = await getContext()
const reader = new SinglePointReader({
ceramic,
issuer: ceramic.did!.id,
});
const totalPoints = await reader.countPointsFor(req.body.recipient);
res.locals.totalPoints = totalPoints;
return next();
})
const totalPoints = await reader.countPointsFor(req.body.recipient)
res.locals.totalPoints = totalPoints
return next()
} catch (error) {
console.error(error);
return error;
console.error(error)
return error
}
};
}

export const singlePointController = { createSinglePoint, getSinglePoints, removeSinglePoint };
export const singlePointController = { createSinglePoint, getSinglePoints, removeSinglePoint }
49 changes: 22 additions & 27 deletions demo/server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
import express from "express";
import cors from "cors";
import singleRouter from "./routes/single.js";
import multiRouter from "./routes/multi.js";

const app = express();
const port = process.env.PORT || 8080;
import express, { json, Request, Response, NextFunction } from 'express'
import cors from 'cors'
import singleRouter from './routes/single.js'
import multiRouter from './routes/multi.js'

const app = express()
const port = process.env.PORT || 8080

const corsOptions = {
origin: ["http://localhost:8080", "https://developers.ceramic.network"],
optionsSuccessStatus: 200 // For legacy browser support
}
origin: ['http://localhost:8080', 'https://developers.ceramic.network'],
optionsSuccessStatus: 200, // For legacy browser support
}

app.use(express.json());
app.use(cors(corsOptions));
app.use(json())
app.use(cors(corsOptions))
// app.use(bodyParser.json());

const allowCrossDomain = (
_req: any,
res: { header: (arg0: string, arg1: string) => void },
next: () => void
) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
res.header("Access-Control-Allow-Headers", "Content-Type");
next();
};
const allowCrossDomain = (_req: Request, res: Response, next: NextFunction) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
res.header('Access-Control-Allow-Headers', 'Content-Type')
next()
}

app.use(allowCrossDomain);
app.use(allowCrossDomain)

app.use("/single", singleRouter);
app.use("/multi", multiRouter);
app.use('/single', singleRouter)
app.use('/multi', multiRouter)

app.listen(port, () => {
// tslint:disable-next-line:no-console
console.log(`server started at http://localhost:${port}`);
});
console.log(`server started at http://localhost:${port}`)
})
30 changes: 21 additions & 9 deletions demo/server/src/routes/multi.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
import express from 'express'
import { RequestHandler, Request, Response, Router } from 'express'
import { multiplePointsController } from '../controllers/multiController.js'

const router: express.Router = express.Router()
const router: Router = Router()

type R = Response & {
locals: {
contextTotal: number
total: number
contextDocument: unknown
document: unknown
}
}

router.post(
'/aggregate',
multiplePointsController.updateContextAggregation,
multiplePointsController.updateTotalAggregation,
(_req, res) => {
return res.json({ contextTotal: res.locals.contextTotal, total: res.locals.total })
multiplePointsController.updateContextAggregation as RequestHandler,
multiplePointsController.updateTotalAggregation as RequestHandler,
(_req: Request, res: R) => {
return res.json({
contextTotal: res.locals.contextTotal,
total: res.locals.total,
})
},
)

router.get(
'/getAggregations',
multiplePointsController.getContextAggregation,
multiplePointsController.getTotalAggregation,
(_req, res) => {
multiplePointsController.getContextAggregation as RequestHandler,
multiplePointsController.getTotalAggregation as RequestHandler,
(_req: Request, res: R) => {
return res.json({
contextTotal: res.locals.contextTotal,
total: res.locals.total,
Expand Down
52 changes: 31 additions & 21 deletions demo/server/src/routes/single.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
import express from "express";
import { singlePointController } from "../controllers/singleController.js";
import { RequestHandler, Response, Router, Request } from 'express'
import { singlePointController } from '../controllers/singleController.js'

const router: express.Router = express.Router();
const router: Router = Router()

router.get("/", singlePointController.getSinglePoints, (_req, res) => {
return res.json({ totalPoints: res.locals.totalPoints });
});
type R = Response & {
locals: {
totalPoints: number
}
}

router.get(
'/',
singlePointController.getSinglePoints as RequestHandler,
(_req: Request, res: R): Response => {
return res.json({ totalPoints: res.locals.totalPoints })
},
)

router.post(
"/create",
singlePointController.createSinglePoint,
singlePointController.getSinglePoints,
(_req, res) => {
return res.json({ totalPoints: res.locals.totalPoints });
}
);
'/create',
singlePointController.createSinglePoint as RequestHandler,
singlePointController.getSinglePoints as RequestHandler,
(_req: Request, res: R): Response => {
return res.json({ totalPoints: res.locals.totalPoints })
},
)

router.delete(
"/remove",
singlePointController.removeSinglePoint,
singlePointController.getSinglePoints,
(_req, res) => {
return res.json({ totalPoints: res.locals.totalPoints });
}
);
'/remove',
singlePointController.removeSinglePoint as RequestHandler,
singlePointController.getSinglePoints as RequestHandler,
(_req: Request, res: R): Response => {
return res.json({ totalPoints: res.locals.totalPoints })
},
)

export default router;
export default router
Loading

0 comments on commit 594c53a

Please sign in to comment.