@nitrotool/jwt 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,14 +6,22 @@ Lightweight JWT utilities for Nitro/UnJS environments with optional h3 helpers.
6
6
  - Helpers that read `jwtSecret` from your Nitro runtime config
7
7
  - h3 utilities to extract tokens from requests and enforce authentication
8
8
 
9
+ Learn more about JWT and how it works here:
10
+ - [What is a JWT?](https://www.jwt.io/introduction#what-is-json-web-token)
11
+ - [JWT.io](https://jwt.io/)
12
+
9
13
  ## Installation
10
14
 
15
+ ```bash
16
+ pnpm install @nitrotool/jwt
17
+ ```
18
+
11
19
  ```bash
12
20
  npm install @nitrotool/jwt
13
21
  ```
14
22
 
15
23
  Peer dependency:
16
- - `h3` is required only when using the h3 helpers.
24
+ - `h3` is required.
17
25
 
18
26
  ## Importing
19
27
 
@@ -24,7 +32,7 @@ You can import from the main entry or subpath exports:
24
32
  import { encodeJwt, verifyJwt, decodeJwt } from '@nitrotool/jwt';
25
33
 
26
34
  // Subpath (JWT-only)
27
- import { encodeJwtRaw, verifyJwtRaw, decodeJwtRaw } from '@nitrotool/jwt/jwt';
35
+ import { encodeJwtRaw, verifyJwtRaw, decodeJwtRaw } from '@nitrotool/jwt/core';
28
36
 
29
37
  // Subpath (h3 helpers)
30
38
  import { extractApiToken, requireApiToken } from '@nitrotool/jwt/h3';
@@ -75,52 +83,6 @@ Supported token locations:
75
83
  - Authorization header: `Authorization: Bearer <token>`
76
84
  - Query string: `?token=<token>`
77
85
 
78
- ## API Reference
79
-
80
- All helpers are asynchronous.
81
-
82
- ### JWT helpers
83
-
84
- - `encodeJwtRaw<T>(payload, secret, ttl = 60): Promise<string>`
85
- - Signs a token with the provided `secret`.
86
- - `ttl` is in seconds. Default: `60`.
87
- - `exp` is set automatically from `ttl`.
88
-
89
- - `encodeJwt<T>(payload): Promise<string>`
90
- - Same as `encodeJwtRaw`, but uses `useRuntimeConfig().jwtSecret`.
91
-
92
- - `verifyJwtRaw(token, secret): Promise<boolean>`
93
- - Verifies signature and expiry using the provided `secret`.
94
-
95
- - `verifyJwt(token): Promise<boolean>`
96
- - Same as `verifyJwtRaw`, but uses `useRuntimeConfig().jwtSecret`.
97
-
98
- - `decodeJwtRaw<T>(token, secret, { verify = true } = {}): Promise<T & Partial<JwtPayload>>`
99
- - Decodes the token. When `verify` is `true`, verifies signature and expiry.
100
- - Throws `UnauthorizedError('Invalid JWT token.')` if verification fails.
101
- - Throws if `verify` is `true` but `secret` is empty.
102
-
103
- - `decodeJwt<T>(token, { verify = true } = {}): Promise<T & Partial<JwtPayload>>`
104
- - Same as `decodeJwtRaw`, but uses `useRuntimeConfig().jwtSecret`.
105
- - Throws `UnauthorizedError('Invalid JWT token.')` if verification fails.
106
-
107
- Types:
108
- - `ExtendableJwtPayload<T>` lets you define custom claims merged with standard JWT claims.
109
-
110
- ### h3 helpers
111
-
112
- - `extractBearerToken(event): string | undefined`
113
- - Reads `Authorization` header and returns the token without `Bearer `.
114
-
115
- - `extractQueryToken(event): string | undefined`
116
- - Reads `token` from the query string.
117
-
118
- - `extractApiToken(event): string | undefined`
119
- - Returns the first non-empty token found by `extractBearerToken` or `extractQueryToken`.
120
-
121
- - `requireApiToken(event): string`
122
- - Same as `extractApiToken`, but throws `UnauthenticatedError` if missing.
123
-
124
86
  ## Configuration
125
87
 
126
88
  When using non-`Raw` helpers, ensure a secret is available at runtime:
@@ -176,6 +138,53 @@ const ok = await verifyJwtRaw(token, process.env.JWT_SECRET!);
176
138
  - Only set `verify: false` for non-sensitive, debug-like operations.
177
139
  - Rotate secrets periodically and invalidate old tokens if needed.
178
140
 
141
+ ## API Reference
142
+
143
+ All helpers are asynchronous.
144
+
145
+ ### JWT helpers
146
+
147
+ - `encodeJwtRaw<T>(payload, secret, ttl = 60): Promise<string>`
148
+ - Signs a token with the provided `secret`.
149
+ - `ttl` is in seconds. Default: `60`.
150
+ - `exp` is set automatically from `ttl`.
151
+
152
+ - `encodeJwt<T>(payload): Promise<string>`
153
+ - Same as `encodeJwtRaw`, but uses `useRuntimeConfig().jwtSecret`.
154
+
155
+ - `verifyJwtRaw(token, secret): Promise<boolean>`
156
+ - Verifies signature and expiry using the provided `secret`.
157
+
158
+ - `verifyJwt(token): Promise<boolean>`
159
+ - Same as `verifyJwtRaw`, but uses `useRuntimeConfig().jwtSecret`.
160
+
161
+ - `decodeJwtRaw<T>(token, secret, { verify = true } = {}): Promise<T & Partial<JwtPayload>>`
162
+ - Decodes the token. When `verify` is `true`, verifies signature and expiry.
163
+ - Throws `UnauthorizedError('Invalid JWT token.')` if verification fails.
164
+ - Throws if `verify` is `true` but `secret` is empty.
165
+
166
+ - `decodeJwt<T>(token, { verify = true } = {}): Promise<T & Partial<JwtPayload>>`
167
+ - Same as `decodeJwtRaw`, but uses `useRuntimeConfig().jwtSecret`.
168
+ - Throws `UnauthorizedError('Invalid JWT token.')` if verification fails.
169
+
170
+ Types:
171
+ - `ExtendableJwtPayload<T>` lets you define custom claims merged with standard JWT claims.
172
+
173
+ ### h3 helpers
174
+
175
+ - `extractBearerToken(event): string | undefined`
176
+ - Reads `Authorization` header and returns the token without `Bearer `.
177
+
178
+ - `extractQueryToken(event, key = 'token'): string | undefined`
179
+ - Reads `token` from the query string.
180
+
181
+ - `extractApiToken(event): string | undefined`
182
+ - Returns the first non-empty token found by `extractBearerToken` or `extractQueryToken`.
183
+
184
+ - `requireApiToken(event): string`
185
+ - Same as `extractApiToken`, but throws `UnauthenticatedError` if missing.
186
+
187
+
179
188
  ## License
180
189
 
181
190
  MIT
package/dist/h3.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { H3Event } from 'h3';
2
2
 
3
3
  declare const extractBearerToken: (event: H3Event) => string;
4
- declare const extractQueryToken: (event: H3Event) => string | undefined;
4
+ declare const extractQueryToken: (event: H3Event, key?: string) => string | undefined;
5
5
  declare const extractApiToken: (event: H3Event) => string | undefined;
6
6
  declare const requireApiToken: (event: H3Event) => string;
7
7
 
package/dist/h3.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { H3Event } from 'h3';
2
2
 
3
3
  declare const extractBearerToken: (event: H3Event) => string;
4
- declare const extractQueryToken: (event: H3Event) => string | undefined;
4
+ declare const extractQueryToken: (event: H3Event, key?: string) => string | undefined;
5
5
  declare const extractApiToken: (event: H3Event) => string | undefined;
6
6
  declare const requireApiToken: (event: H3Event) => string;
7
7
 
package/dist/h3.mjs CHANGED
@@ -2,7 +2,7 @@ import { getRequestHeader, getQuery } from 'h3';
2
2
  import { UnauthenticatedError } from '@nitrotool/errors';
3
3
 
4
4
  const extractBearerToken = (event) => getRequestHeader(event, "Authorization")?.replace("Bearer ", "") || void 0;
5
- const extractQueryToken = (event) => getQuery(event)?.token || void 0;
5
+ const extractQueryToken = (event, key = "token") => getQuery(event)?.[key] || void 0;
6
6
  const extractApiToken = (event) => extractBearerToken(event) || extractQueryToken(event);
7
7
  const requireApiToken = (event) => {
8
8
  const token = extractApiToken(event);
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@nitrotool/jwt",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "main": "dist/index.mjs",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": "./dist/index.mjs",
8
- "./jwt": "./dist/jwt.mjs",
8
+ "./core": "./dist/jwt.mjs",
9
9
  "./h3": "./dist/h3.mjs"
10
10
  },
11
11
  "types": "./dist/index.d.ts",
12
12
  "dependencies": {
13
13
  "@tsndr/cloudflare-worker-jwt": "^3.2.0",
14
- "@nitrotool/errors": "0.0.7"
14
+ "@nitrotool/errors": "0.0.9"
15
15
  },
16
16
  "peerDependencies": {
17
17
  "h3": "^1.15.3"