
Research
/Security News
Malicious npm Packages Target WhatsApp Developers with Remote Kill Switch
Two npm packages masquerading as WhatsApp developer libraries include a kill switch that deletes all files if the phone number isn’t whitelisted.
remix-api-router
Advanced tools
Library for implementing APIs in Remix using a chaining approach, similar to Express middleware
Library for creating APIs in Remix using a chaining approach, similar to Express middleware.
Note: originally I was planning to implement a chain-of-responsibility pattern with the same contract as Express, but I wasn't sure how much adoption it will have. If you are interesting in it, let's talk!.
npm i remix-api-router
or
yarn add remix-api-router
For example, create a products.tsx file inside a Remix app, under the app/routes/api folder, and paste the following code:
import { apiRouter } from "remix-api-router";
import { checkAuth } from "auth";
import { json } from "@remix-run/node";
import type { ActionFunction, LoaderFunction, DataFunctionArgs } from "@remix-run/node";
/**
* /api/products
*/
// Define all routes for this endpoint and their handlers
const router = apiRouter();
router
.get(checkAuth, async (args: DataFunctionArgs) => {
await fetch("https://google.com");
return json({ message: "GET" }, 200);
})
.post(checkAuth, (args: DataFunctionArgs) => json({ message: "POST" }, 200))
.put((args: DataFunctionArgs) => json({ message: "PUT" }, 200))
.patch((args: DataFunctionArgs) => json({ message: "PATCH" }, 200))
.delete((args: DataFunctionArgs) => {
throw new Error("unexpected error");
})
.error((err) => {
return json({ error: "Custom message: Server unavailable!" }, 500);
});
export const loader: LoaderFunction = router.loader();
export const action: ActionFunction = router.actions();
Note see a fully working example here.
500
(you can provide your own).405
(you can provide your own).Response
object, just what Remix expects.void
or Promise<void>
will tell the chain to continue processing the request with the other handlers.router.error
handler.405 Method not allowed
. You can configure this with your own logic by configuring the router.noMatch
handler.loader
and action
.You can initialize the router using either a class instantiation or a factory instantiation:
Use the default export for a class instantiation:
import ApiRouter from "remix-api-router";
import { json } from "@remix-run/node";
const router = new ApiRouter();
router.get(() => json({ hello: "world" }, 200)).post(() => json({ hello: "world" }, 201));
Use the named export "apiRouter" for the factory instantiation:
import { apiRouter } from "remix-api-router";
import { json } from "@remix-run/node";
const router = new ApiRouter();
router.get(() => json({ hello: "world" }, 200)).post(() => json({ hello: "world" }, 201));
Last, hook the router with the Remix loader
and action
by simply exporting the following at the end of the file:
import { apiRouter } from "remix-api-router";
import { json } from "@remix-run/node";
const router = apiRouter();
router
.get(() => json({ hello: "world" }, 200))
.post(() => json({ hello: "world" }, 201))
...
export const loader: LoaderFunction = router.loader();
export const action: ActionFunction = router.actions();
It supports all HTTP Methods by adding a handler with the same contract provided by Remix. For example:
To handle GET /api/categories/$id
requests, use the following code:
import { apiRouter } from "remix-api-router";
import { json } from "@remix-run/node";
import type { ActionFunction, LoaderFunction, DataFunctionArgs } from "@remix-run/node";
/**
* /api/categories/$id
*/
const router = apiRouter();
router.get(({ request, context, params }: DataFunctionArgs) => {
return json({ message: `Requested category with ID ${params.id}` }, 200);
});
export const loader: LoaderFunction = router.loader();
export const action: ActionFunction = router.actions();
To handle GET /api/categories
and POST /api/categories
requests, use:
import { apiRouter } from "remix-api-router";
import { json } from "@remix-run/node";
import type { ActionFunction, LoaderFunction, DataFunctionArgs } from "@remix-run/node";
/**
* /api/categories
*/
const router = apiRouter();
router
.get(async ({ request, context, params }: DataFunctionArgs) => {
// See https://remix.run/docs/en/v1/guides/data-loading#url-search-params
const url = new URL(request.url);
const term = url.searchParams.get("term");
return json(await fakeProductSearch(term));
})
.post(async ({ request, context, params }: DataFunctionArgs) => {
const body = await request.json();
return json({ message: `Sent body ${JSON.stringify(body)}` });
});
async function fakeProductSearch(term: string | null) {
return [
{ id: 1, name: "Category A" },
{ id: 2, name: "Category B" },
];
}
export const loader: LoaderFunction = router.loader();
export const action: ActionFunction = router.actions();
FAQs
Library for implementing APIs in Remix using a chaining approach, similar to Express middleware
The npm package remix-api-router receives a total of 20 weekly downloads. As such, remix-api-router popularity was classified as not popular.
We found that remix-api-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Two npm packages masquerading as WhatsApp developer libraries include a kill switch that deletes all files if the phone number isn’t whitelisted.
Research
/Security News
Socket uncovered 11 malicious Go packages using obfuscated loaders to fetch and execute second-stage payloads via C2 domains.
Security News
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.