Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

feat: expose a req/res context for API implementation #40

Open
hexcowboy opened this issue Nov 10, 2022 · 1 comment
Open

feat: expose a req/res context for API implementation #40

hexcowboy opened this issue Nov 10, 2022 · 1 comment

Comments

@hexcowboy
Copy link

Currently I'm unable to authenticate user requests since doing so with NextAuth requires the Request and Response objects to check auth.

I'd like to propose an API similar to this, exposing a ctx object:

// pages/api/message.swr.tsx
import { query } from "@next-fetch/swr";
import z from "zod";

import { authOptions } from 'pages/api/auth/[...nextauth]';
import { unstable_getServerSession } from "next-auth/next";

export const useMessage = query(
  async function ({ ctx }) {
    const session = await unstable_getServerSession(ctx.req, ctx.res, authOptions)
    return { hello: `world, ${session.user.name}` };
  }
);

Additionally, the documentation is misleading:

export const useMessage = query(
  // use zod or other input validation libraries
  z.object({
    name: z.string(),
  }),
  async function ({ name }) {
    // this.request is a `Request` instance            <------------ this.request is not available here
    return { hello: "world, " + name };
  }
);
@hexcowboy hexcowboy changed the title Request: expose a req/res context for API implementation feat: expose a req/res context for API implementation Nov 10, 2022
@prichodko
Copy link

Agreed with exposing the response object.

Btw, make sure that you are passing a normal function and not an arrow function. Arrow function this behaves differently. Normal function works for me.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants