Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

next-define

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-define

Fully typed `define` functions for Next.js

  • 0.1.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source




next-define




Fully typed `define` functions for Next.js

Package Version Package Monthly Downloads Docs



🚀 Install

Install it locally in your project

# npm
npm install next-define

# yarn
yarn add next-define

# pnpm
pnpm install next-define

🦄 Usage

To get started using next-define, you can import the definePage function from the package and use it to define your page.

// pages/index.tsx
import { definePage } from "next-define";

export const { Component } = definePage({
  Component: () => <>Hello World</>,
});

export default Component;
// pages/index.tsx
import { definePage } from "next-define";

export const { Component, getStaticProps } = definePage({
  getStaticProps: () => ({ props: { name: "John" } }),
  Component: ({ name }) => <>Hello {name}</>,
});

export default Component;
// pages/index.tsx
import { definePage } from "next-define";

export const { Component, getServerSideProps } = definePage({
  getServerSideProps: () => ({ props: { name: "John" } }),
  Component: ({ name }) => <>Hello {name}</>,
});

export default Component;

Or you can import defineApi to define a new API route

// pages/api/hello.ts
import { defineApi } from "next-define";

export default defineApi((req, res) =>
  res.status(200).json({
    name: "John",
  })
);
// pages/api/hello.ts
import { defineApi } from "next-define";

export const { config, handler } = defineApi(
  (req, res) =>
    res.status(200).json({
      name: "John",
    }),
  {
    runtime: "nodejs",
  }
);

export default handler;

We even offer support for the new app directory beta by importing from the /app export

// app/page.tsx
import { definePage } from "next-define/app";

const { Component } = definePage({
  Component: ({ params, searchParams }) => <>Hello World</>,
});

export default Component;
// app/layout.tsx
import { defineLayout } from "next-define/app";

const { Component } = defineLayout(({ children, params }) => <>{children}</>);

export default Component;
// app/error.tsx
import { defineError } from "next-define/app";

export default defineError(({ error, reset }) => <></>);
// app/hello/router.ts
import { defineRoute } from "next-define/app";

export const { GET, runtime } = defineRoute({
  runtime: "edge",
  GET: (req, { params }) =>
    new Response(
      JSON.stringify({
        message: "Hello World",
      })
    ),
});

Keywords

FAQs

Package last updated on 08 Apr 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc