Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
@daldalso/next-typed-route
Advanced tools
Type-safe API call and routing library for Next.js
yarn add @daldalso/next-typed-route
NextTypedRoutePlugin
like below:
/** @type {import('next').NextConfig} */
const nextConfig = {
webpack: (config, context) => {
if(context.isServer){
config.plugins ??= [];
config.plugins.push(new NextTypedRoutePlugin());
}
return config;
}
};
yarn dev
will run the plugin and make some type definitions.Mark your page components with NextTypedPage
for type-safe routing.
import { NextTypedPage } from "@daldalso/next-typed-route";
const MyPage:NextTypedPage<"/my-page/[foo]"> = ({ params }) => {
return <div>Hello, {params.foo}!</div>; // Type-safe access to `params.foo`
};
export default Page;
Mark your API routes with NextTypedRoute
for type-safe routing.
import type { NextTypedRoute } from "@daldalso/next-typed-route";
import { NextResponse } from "next/server";
export const GET:NextTypedRoute = () => new NextResponse();
You can use type-safe useSearchParams
with a NextTypedPage
.
import { NextTypedPage } from "@daldalso/next-typed-route";
import { useSearchParams } from "next/navigation";
const Page:NextTypedPage<"/", "foo"|"bar?"|"baz[]"> = () => {
const searchParams = useSearchParams<typeof Page>();
return <>
{/* Fine */}
{searchParams.get("foo").toString()}
{/* This raises a type error. */}
{searchParams.get("baar")}
{/* This raises a type error; you should use `getAll` for an array. */}
{searchParams.get("baz")}
</>;
};
export default Page;
You can use type-safe page
function that returns a path string starting with /
.
import { NextTypedPage, page } from "@daldalso/next-typed-route";
import { useRouter } from "next/navigation";
const Page:NextTypedPage<"/", "foo"|"bar?"> = ({ params }) => {
const router = useRouter();
return <button onClick={() => router.push(page("/shop"))}>
Shop
</button>;
};
export default Page;
You can call your endpoints with type-safe callAPI
function.
The types of its parameters and return value are defined by the endpoint.
import callAPI, { NextTypedPage } from "@daldalso/next-typed-route";
const Page:NextTypedPage<"/"> = ({ params }) => {
return <button onClick={() => callAPI("/api/foo")}>
Submit
</button>;
};
export default Page;
FAQs
Type-safe API call and routing library for Next.js
We found that @daldalso/next-typed-route demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.