Skip to content

Commit

Permalink
create specific route for standalone actions
Browse files Browse the repository at this point in the history
  • Loading branch information
vhcsilva committed Jun 21, 2024
1 parent d1d3e39 commit 4794930
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/actions/update-user-profile-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@ import { isIpfsEnvs } from "src/utils/ipfs-envs-verify";
import logger from "src/utils/logger-handler";
import ipfsService from "src/services/ipfs-service";
import { truncateAddress } from "src/utils/string";
import { HttpBadRequestError } from "src/types/errors";

export const name = "update-user-profile-image";
export const schedule = "*/10 * * * *";
export const description = "Generate User profile image for OG";
export const author = "vhcsilva";

export async function action(query?: Record<string, string>) {
export async function action(query?: Record<string, string | boolean>) {
if (!isIpfsEnvs) {
logger.warn(`${name} Missing id, secret or baseURL, for IPFService`);
return;
}

if (query?.fromRoute && !query?.id) {
logger.warn(`${name} Missing query params`, query);
throw new HttpBadRequestError("Missing query params");
}

const where: WhereOptions = {};

if (query?.id)
Expand Down
6 changes: 5 additions & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import {Router} from "express";

import {seoRoutes} from "./seo.routes";
import readRouter from "./read.router";
import standaloneRoutes from './standalone.router';
import {getChainsRegistryAndNetworks} from "../utils/block-process";

const router = Router();

router.use("/seo", seoRoutes);

router.use(`/read/`, readRouter);
router.use("/read/", readRouter);

router.use("/standalone/", standaloneRoutes);

router.use("/", async (req, res) => {
const info = await getChainsRegistryAndNetworks();
Expand Down
4 changes: 1 addition & 3 deletions src/routes/read.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import {Op} from "sequelize";
import {MIDNIGHT_ACTIONS, MINUTE_ACTIONS, NETWORK_EVENTS, REGISTRY_EVENTS} from "../modules/chain-events";
import {findOnABI} from "../utils/find-on-abi";
import {ApiBlockSniffer} from "src/services/api-block-sniffer";
import { internalApiKey } from "src/middlewares/internal-api-key";

const router = Router();

router.use(internalApiKey);
router.use(eventQuery);

router.get(`/:chainId/:address/:event`, async (req, res) => {
Expand Down Expand Up @@ -60,7 +58,7 @@ router.get(`/:chainId/:address/:event`, async (req, res) => {
return res.status(400).json({message: `unknown network or registry ${address}`});

if (_standaloneKeys.includes(event))
(MIDNIGHT_ACTIONS[event] || MINUTE_ACTIONS[event])({ chainId, address, ...req?.query })
(MIDNIGHT_ACTIONS[event] || MINUTE_ACTIONS[event])({ chainId, address })
.then(data => {
res.status(200).json([data]).end();
});
Expand Down
33 changes: 33 additions & 0 deletions src/routes/standalone.router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Router } from "express";

import { internalApiKey } from "src/middlewares/internal-api-key";
import { MIDNIGHT_ACTIONS, MINUTE_ACTIONS } from "src/modules/chain-events";
import { BaseAPIError } from "src/types/errors";

const router = Router();

router.use(internalApiKey);

router.get("/:action", async (req, res, next) => {
const { action } = req.params;
const handler = MIDNIGHT_ACTIONS[action] || MINUTE_ACTIONS[action];

if (!handler)
return res.status(404).json({ message: "Invalid action" });

try {
const result = await handler({
fromRoute: true,
...req.query,
});

return res.status(200).json(result).end();
} catch (error) {
if (error instanceof BaseAPIError)
return res.status(error.status).json({ message: error.message }).end();

next(error);
}
});

export default router;
17 changes: 17 additions & 0 deletions src/types/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export class BaseAPIError extends Error {
status: number;

constructor(message: string, status: number) {
super(message);

Error.captureStackTrace(this, this.constructor);

this.status = status;
}
}

export class HttpBadRequestError extends BaseAPIError {
constructor(message: string = "Bad Request") {
super(message, 400);
}
}

0 comments on commit 4794930

Please sign in to comment.