0.0.4 • Published 6 months ago

next-compose-middlewares v0.0.4

Weekly downloads
-
License
-
Repository
github
Last release
6 months ago

next-compose-middlewares

using koa style middlewares inside nextjs

NPM version NPM downloads

npm install next-compose-middlewares

Usage

nextjs middleware

src/middleware.ts

// @ts-ignore
export { middleware } from 'next-compose-middlewares/middleware';

extends type

declare module 'next-compose-middlewares' {
  interface NextContext {
    user: string;
  }
}

page

src/app/page.tsx

import React from 'react';
import { createPage } from 'next-compose-middlewares';

export default createPage([
  async (context, next) => {
    context.user = 'test';
    await next();
  },
  ({ user, response }) => {
    response.jsx = (
      <>
        <p>{user}</p>
      </>
    );
  },
]);

route

src/app/get/route.ts

import { createRoute } from 'next-compose-middlewares';

export const GET = createRoute([
  async (context, next) => {
    context.user = 'test';
    await next();
  },
  ({ user, response }) => {
    response.json = { user };
  },
]);

API Report File for "next-compose-middlewares"

Do not edit this file. It is a report generated by API Extractor.

import { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import type { default as React_2 } from 'react';
import { ResponseCookies } from 'next/dist/compiled/@edge-runtime/cookies';

// @public (undocumented)
export function compose(middleware: Function[]): (context: any, next?: Function) => Promise<any>;

// @public (undocumented)
export function createFinishMiddleware(): MiddlewareFunction;

// @public (undocumented)
export function createPage(fns: MiddlewareFunction[], props?: createPageProps): {
    (): Promise<any>;
    name: string;
};

// @public (undocumented)
export interface createPageProps {
    // (undocumented)
    name?: string;
    // (undocumented)
    request?: () => PageRequest;
}

// @public (undocumented)
export function createRoute(fns: MiddlewareFunction[], props?: createRouteProps): {
    (r: NextRequest): Promise<any>;
    name: string;
};

// @public (undocumented)
export interface createRouteProps {
    // (undocumented)
    name?: string;
    // (undocumented)
    request?: (r: NextRequest) => RouteRequest;
}

// @public (undocumented)
export const finishMiddleware: MiddlewareFunction;

// @public (undocumented)
export function getNextContextFromPage(props?: PageRequest): NextContext;

// @public (undocumented)
export function getNextContextFromRoute(request: NextRequest, props?: RouteRequest): NextContext;

// @public (undocumented)
export function middleware(request: NextRequest): Promise<NextResponse<unknown>>;

// @public (undocumented)
export type MiddlewareFunction = (context: NextContext, next?: NextFunction) => Promise<any> | void;

// @public (undocumented)
export interface NextContext {
    // (undocumented)
    cookies: ResponseCookies;
    // (undocumented)
    request: {
        url: string;
        text: () => Promise<string>;
        json: () => Promise<any>;
        method: string;
        pathname: string;
        searchParams: any;
        headers: NextRequest['headers'];
    };
    // (undocumented)
    response: {
        redirect?: string;
        text?: () => string;
        jsx?: React_2.ReactNode;
        json?: any;
        status: number;
        message?: string;
        headers?: any;
    };
    // (undocumented)
    type: 'page' | 'route';
}

// @public (undocumented)
export type NextFunction = () => Promise<any> | void;

// @public (undocumented)
export interface PageRequest {
    // (undocumented)
    json?: () => Promise<any>;
    // (undocumented)
    method?: string;
    // (undocumented)
    params?: any;
    // (undocumented)
    text?: () => Promise<string>;
}

// @public (undocumented)
export interface RouteRequest {
    // (undocumented)
    params?: any;
}

// (No @packageDocumentation comment for this package)

0.0.4

6 months ago

0.0.3

6 months ago

0.0.2

6 months ago

0.0.1

6 months ago