Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions packages/nextjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<div align="center">
<h1 align="center">@unkey/nextjs</h1>
<h5>`@unkey/nextjs` the official SDK for Next.js. Just use it in your route handlers a direct and type-safe method to verify API keys.</h5>
</div>

<div align="center">
<a href="https://www.unkey.com/docs/libraries/ts/nextjs">Documentation</a>
</div>
<br/>

## Installation

```bash
npm install @unkey/nextjs
```

## Quickstart

Protecting API routes is as simple as wrapping them with the ```withUnkey``` handler:

```ts
import { NextRequestWithUnkeyContext, withUnkey } from '@unkey/nextjs';

export const POST = withUnkey(async (req) => {

// Process the request here
// You have access to the verification response using `req.unkey`
console.log(req.unkey);

return new Response('Your API key is valid!');
});
```

That’s it! Since this is just handling key verification, there’s no need to specify a root key as an environment variable.

If you want to customize how withUnkey processes incoming requests, you can do so as follows:

## ```getKey```
By default, withUnkey will look for a bearer token located in the ```authorization``` header. If you want to customize this, you can do so by passing a getter in the configuration object:

```ts
export const GET = withUnkey(async (req) => {
// ...
}, {
getKey: (req) => new URL(req.url).searchParams.get("key"),
});
```

## ```onError```

You can specify custom error handling. By default errors will be logged to the console, and ```withUnkey``` will return a NextResponse with status 500.

```ts
export const GET = withUnkey(async (req) => {
// ...
}, {
onError: async (req, res) => {
await analytics.trackEvent(`Error ${res.code}: ${res.message}`)
return new NextResponse("Unkey error", { status: 500 })
}
});
```

## ```handleInvalidKey```

Specify what to do if Unkey reports that your key is invalid.

```ts
export const GET = withUnkey(async (req) => {
// ...
}, {
handleInvalidKey: (req, res) => {
return new Response("Unauthorized", { status: 401 })
}
});
```
Comment on lines +36 to +76
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Issues Found: Inconsistent Indentation in README.md

The README.md file exhibits inconsistent indentation, using both tabs and spaces within the code examples and documentation. Specifically:

  • Uses Tabs:

    • getKey: (req) => new URL(req.url).searchParams.get("key"),
    • onError: async (req, res) => {
    • handleInvalidKey: (req, res) => {
  • Uses Spaces:

    • Various lines in the documentation sections and other parts of the file.

Recommendation:

  • Standardize Indentation: Adopt a single indentation style, preferably spaces, across all code examples and documentation to enhance readability and maintain consistency.
🔗 Analysis chain

LGTM: Customization options are well-explained with clear examples.

The customization section covers important options like getKey, onError, and handleInvalidKey, providing users with the flexibility to adapt the SDK to their needs.

  1. Consider using consistent indentation throughout the code examples. Some lines use tabs (e.g., lines 45, 57, 72) while others use spaces.
  2. In the onError example, consider adding a comment explaining what res.code and res.message represent.

To check for consistent indentation across the file, run:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for consistent indentation in the README.md file

# Test: Look for lines with tabs
rg '\t' packages/nextjs/README.md

# Test: Look for lines with spaces at the beginning
rg '^ +' packages/nextjs/README.md

Length of output: 874

🧰 Tools
🪛 LanguageTool

[style] ~39-~39: You have already used this phrasing in nearby sentences. Consider replacing it to add variety to your writing.
Context: ... the authorization header. If you want to customize this, you can do so by passin...

(REP_WANT_TO_VB)


[uncategorized] ~51-~51: Did you mean: “By default,”?
Context: ... You can specify custom error handling. By default errors will be logged to the console, a...

(BY_DEFAULT_COMMA)

🪛 Markdownlint

45-45: Column: 1
Hard tabs

(MD010, no-hard-tabs)


57-57: Column: 1
Hard tabs

(MD010, no-hard-tabs)


72-72: Column: 1
Hard tabs

(MD010, no-hard-tabs)



## Disable telemetry

By default, Unkey collects anonymous telemetry data to help us understand how our SDKs are used.

If you wish to disable this, you can do so by passing a boolean flag to the constructor:

```ts
export const GET = withUnkey(async (req) => {
// ...
}, { disableTelemetry: true });
```

### Documentation

[Read the full documentation](https://www.unkey.com/docs/libraries/ts/nextjs)
2 changes: 1 addition & 1 deletion packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"url": "https://github.com/unkeyed/unkey/issues"
},
"homepage": "https://github.com/unkeyed/unkey#readme",
"files": ["./dist/**"],
"files": ["./dist/**", "README.md"],
"author": "Andreas Thomas <andreas@chronark.com>",
"scripts": {
"build": "tsup"
Expand Down