@vercel/functions 1.5.1 → 1.6.0
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/CHANGELOG.md +12 -0
- package/docs/interfaces/index.Geo.md +20 -7
- package/docs/interfaces/index.Request.md +1 -1
- package/docs/interfaces/oidc.AwsCredentialsProviderInit.md +4 -4
- package/docs/modules/index.md +122 -9
- package/headers.d.ts +7 -0
- package/headers.js +5 -1
- package/index.d.ts +1 -0
- package/index.js +5 -0
- package/middleware.d.ts +110 -0
- package/middleware.js +60 -0
- package/package.json +19 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @vercel/functions
|
|
2
2
|
|
|
3
|
+
## 1.6.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Add middleware-related helper functions ([#12938](https://github.com/vercel/vercel/pull/12938))
|
|
8
|
+
|
|
9
|
+
## 1.5.2
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [vercel/functions] add geolocation.postalCode ([#12753](https://github.com/vercel/vercel/pull/12753))
|
|
14
|
+
|
|
3
15
|
## 1.5.1
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -14,6 +14,7 @@ The location information of a given request.
|
|
|
14
14
|
- [flag](index.Geo.md#flag)
|
|
15
15
|
- [latitude](index.Geo.md#latitude)
|
|
16
16
|
- [longitude](index.Geo.md#longitude)
|
|
17
|
+
- [postalCode](index.Geo.md#postalcode)
|
|
17
18
|
- [region](index.Geo.md#region)
|
|
18
19
|
|
|
19
20
|
## Properties
|
|
@@ -26,7 +27,7 @@ The city that the request originated from.
|
|
|
26
27
|
|
|
27
28
|
#### Defined in
|
|
28
29
|
|
|
29
|
-
[packages/functions/src/headers.ts:
|
|
30
|
+
[packages/functions/src/headers.ts:59](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L59)
|
|
30
31
|
|
|
31
32
|
---
|
|
32
33
|
|
|
@@ -38,7 +39,7 @@ The country that the request originated from.
|
|
|
38
39
|
|
|
39
40
|
#### Defined in
|
|
40
41
|
|
|
41
|
-
[packages/functions/src/headers.ts:
|
|
42
|
+
[packages/functions/src/headers.ts:62](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L62)
|
|
42
43
|
|
|
43
44
|
---
|
|
44
45
|
|
|
@@ -51,7 +52,7 @@ See [docs](https://vercel.com/docs/concepts/edge-network/headers#x-vercel-ip-cou
|
|
|
51
52
|
|
|
52
53
|
#### Defined in
|
|
53
54
|
|
|
54
|
-
[packages/functions/src/headers.ts:
|
|
55
|
+
[packages/functions/src/headers.ts:73](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L73)
|
|
55
56
|
|
|
56
57
|
---
|
|
57
58
|
|
|
@@ -63,7 +64,7 @@ The flag emoji for the country the request originated from.
|
|
|
63
64
|
|
|
64
65
|
#### Defined in
|
|
65
66
|
|
|
66
|
-
[packages/functions/src/headers.ts:
|
|
67
|
+
[packages/functions/src/headers.ts:65](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L65)
|
|
67
68
|
|
|
68
69
|
---
|
|
69
70
|
|
|
@@ -75,7 +76,7 @@ The latitude of the client.
|
|
|
75
76
|
|
|
76
77
|
#### Defined in
|
|
77
78
|
|
|
78
|
-
[packages/functions/src/headers.ts:
|
|
79
|
+
[packages/functions/src/headers.ts:76](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L76)
|
|
79
80
|
|
|
80
81
|
---
|
|
81
82
|
|
|
@@ -87,7 +88,19 @@ The longitude of the client.
|
|
|
87
88
|
|
|
88
89
|
#### Defined in
|
|
89
90
|
|
|
90
|
-
[packages/functions/src/headers.ts:
|
|
91
|
+
[packages/functions/src/headers.ts:79](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L79)
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
### postalCode
|
|
96
|
+
|
|
97
|
+
• `Optional` **postalCode**: `string`
|
|
98
|
+
|
|
99
|
+
The postal code of the client
|
|
100
|
+
|
|
101
|
+
#### Defined in
|
|
102
|
+
|
|
103
|
+
[packages/functions/src/headers.ts:82](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L82)
|
|
91
104
|
|
|
92
105
|
---
|
|
93
106
|
|
|
@@ -99,4 +112,4 @@ The [Vercel Edge Network region](https://vercel.com/docs/concepts/edge-network/r
|
|
|
99
112
|
|
|
100
113
|
#### Defined in
|
|
101
114
|
|
|
102
|
-
[packages/functions/src/headers.ts:
|
|
115
|
+
[packages/functions/src/headers.ts:68](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L68)
|
|
@@ -20,4 +20,4 @@ Represents the headers of a request.
|
|
|
20
20
|
|
|
21
21
|
#### Defined in
|
|
22
22
|
|
|
23
|
-
[packages/functions/src/headers.ts:
|
|
23
|
+
[packages/functions/src/headers.ts:52](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L52)
|
|
@@ -40,7 +40,7 @@ Omit.clientConfig
|
|
|
40
40
|
|
|
41
41
|
#### Defined in
|
|
42
42
|
|
|
43
|
-
node*modules/.pnpm/@aws-sdk+credential-provider-web-identity@3.609.0*@aws-sdk+client-sts@3.
|
|
43
|
+
node*modules/.pnpm/@aws-sdk+credential-provider-web-identity@3.609.0*@aws-sdk+client-sts@3.731.1/node_modules/@aws-sdk/credential-provider-web-identity/dist-types/fromWebToken.d.ts:135
|
|
44
44
|
|
|
45
45
|
---
|
|
46
46
|
|
|
@@ -56,7 +56,7 @@ Omit.clientPlugins
|
|
|
56
56
|
|
|
57
57
|
#### Defined in
|
|
58
58
|
|
|
59
|
-
node*modules/.pnpm/@aws-sdk+credential-provider-web-identity@3.609.0*@aws-sdk+client-sts@3.
|
|
59
|
+
node*modules/.pnpm/@aws-sdk+credential-provider-web-identity@3.609.0*@aws-sdk+client-sts@3.731.1/node_modules/@aws-sdk/credential-provider-web-identity/dist-types/fromWebToken.d.ts:139
|
|
60
60
|
|
|
61
61
|
---
|
|
62
62
|
|
|
@@ -202,7 +202,7 @@ Omit.roleAssumerWithWebIdentity
|
|
|
202
202
|
|
|
203
203
|
#### Defined in
|
|
204
204
|
|
|
205
|
-
node*modules/.pnpm/@aws-sdk+credential-provider-web-identity@3.609.0*@aws-sdk+client-sts@3.
|
|
205
|
+
node*modules/.pnpm/@aws-sdk+credential-provider-web-identity@3.609.0*@aws-sdk+client-sts@3.731.1/node_modules/@aws-sdk/credential-provider-web-identity/dist-types/fromWebToken.d.ts:130
|
|
206
206
|
|
|
207
207
|
---
|
|
208
208
|
|
|
@@ -218,4 +218,4 @@ Omit.roleSessionName
|
|
|
218
218
|
|
|
219
219
|
#### Defined in
|
|
220
220
|
|
|
221
|
-
node*modules/.pnpm/@aws-sdk+credential-provider-web-identity@3.609.0*@aws-sdk+client-sts@3.
|
|
221
|
+
node*modules/.pnpm/@aws-sdk+credential-provider-web-identity@3.609.0*@aws-sdk+client-sts@3.731.1/node_modules/@aws-sdk/credential-provider-web-identity/dist-types/fromWebToken.d.ts:123
|
package/docs/modules/index.md
CHANGED
|
@@ -12,6 +12,8 @@
|
|
|
12
12
|
- [geolocation](index.md#geolocation)
|
|
13
13
|
- [getEnv](index.md#getenv)
|
|
14
14
|
- [ipAddress](index.md#ipaddress)
|
|
15
|
+
- [next](index.md#next)
|
|
16
|
+
- [rewrite](index.md#rewrite)
|
|
15
17
|
- [waitUntil](index.md#waituntil)
|
|
16
18
|
|
|
17
19
|
## Functions
|
|
@@ -47,19 +49,20 @@ The location information of the request, in this way:
|
|
|
47
49
|
|
|
48
50
|
```json
|
|
49
51
|
{
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
"city": "New York",
|
|
53
|
+
"country": "US",
|
|
54
|
+
"flag": "🇺🇸",
|
|
55
|
+
"countryRegion": "NY",
|
|
56
|
+
"region": "iad1",
|
|
57
|
+
"latitude": "40.7128",
|
|
58
|
+
"longitude": "-74.0060"
|
|
59
|
+
"postalCode": "10001"
|
|
57
60
|
}
|
|
58
61
|
```
|
|
59
62
|
|
|
60
63
|
#### Defined in
|
|
61
64
|
|
|
62
|
-
[packages/functions/src/headers.ts:
|
|
65
|
+
[packages/functions/src/headers.ts:180](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L180)
|
|
63
66
|
|
|
64
67
|
---
|
|
65
68
|
|
|
@@ -144,7 +147,117 @@ The IP address of the request.
|
|
|
144
147
|
|
|
145
148
|
#### Defined in
|
|
146
149
|
|
|
147
|
-
[packages/functions/src/headers.ts:
|
|
150
|
+
[packages/functions/src/headers.ts:131](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L131)
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
### next
|
|
155
|
+
|
|
156
|
+
▸ **next**(`init?`): [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
|
157
|
+
|
|
158
|
+
Returns a Response that instructs the system to continue processing the request.
|
|
159
|
+
|
|
160
|
+
**`Example`**
|
|
161
|
+
|
|
162
|
+
<caption>No-op middleware</caption>
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
import { next } from '@vercel/edge';
|
|
166
|
+
|
|
167
|
+
export default function middleware(_req: Request) {
|
|
168
|
+
return next();
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
**`Example`**
|
|
173
|
+
|
|
174
|
+
<caption>Add response headers to all requests</caption>
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
import { next } from '@vercel/edge';
|
|
178
|
+
|
|
179
|
+
export default function middleware(_req: Request) {
|
|
180
|
+
return next({
|
|
181
|
+
headers: { 'x-from-middleware': 'true' },
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
#### Parameters
|
|
187
|
+
|
|
188
|
+
| Name | Type | Description |
|
|
189
|
+
| :------ | :------------------ | :---------------------------------- |
|
|
190
|
+
| `init?` | `ExtraResponseInit` | Additional options for the response |
|
|
191
|
+
|
|
192
|
+
#### Returns
|
|
193
|
+
|
|
194
|
+
[`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
|
195
|
+
|
|
196
|
+
#### Defined in
|
|
197
|
+
|
|
198
|
+
[packages/functions/src/middleware.ts:145](https://github.com/vercel/vercel/blob/main/packages/functions/src/middleware.ts#L145)
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
### rewrite
|
|
203
|
+
|
|
204
|
+
▸ **rewrite**(`destination`, `init?`): [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
|
205
|
+
|
|
206
|
+
Returns a response that rewrites the request to a different URL.
|
|
207
|
+
|
|
208
|
+
**`Example`**
|
|
209
|
+
|
|
210
|
+
<caption>Rewrite all feature-flagged requests from `/:path*` to `/experimental/:path*`</caption>
|
|
211
|
+
|
|
212
|
+
```ts
|
|
213
|
+
import { rewrite, next } from '@vercel/edge';
|
|
214
|
+
|
|
215
|
+
export default async function middleware(req: Request) {
|
|
216
|
+
const flagged = await getFlag(req, 'isExperimental');
|
|
217
|
+
if (flagged) {
|
|
218
|
+
const url = new URL(req.url);
|
|
219
|
+
url.pathname = `/experimental{url.pathname}`;
|
|
220
|
+
return rewrite(url);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return next();
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
**`Example`**
|
|
228
|
+
|
|
229
|
+
<caption>JWT authentication for `/api/:path*` requests</caption>
|
|
230
|
+
|
|
231
|
+
```ts
|
|
232
|
+
import { rewrite, next } from '@vercel/edge';
|
|
233
|
+
|
|
234
|
+
export default function middleware(req: Request) {
|
|
235
|
+
const auth = checkJwt(req.headers.get('Authorization'));
|
|
236
|
+
if (!checkJwt) {
|
|
237
|
+
return rewrite(new URL('/api/error-unauthorized', req.url));
|
|
238
|
+
}
|
|
239
|
+
const url = new URL(req.url);
|
|
240
|
+
url.searchParams.set('_userId', auth.userId);
|
|
241
|
+
return rewrite(url);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export const config = { matcher: '/api/users/:path*' };
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
#### Parameters
|
|
248
|
+
|
|
249
|
+
| Name | Type | Description |
|
|
250
|
+
| :------------ | :------------------------------------------------------------------------ | :---------------------------------- |
|
|
251
|
+
| `destination` | `string` \| [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) | new URL to rewrite the request to |
|
|
252
|
+
| `init?` | `ExtraResponseInit` | Additional options for the response |
|
|
253
|
+
|
|
254
|
+
#### Returns
|
|
255
|
+
|
|
256
|
+
[`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
|
257
|
+
|
|
258
|
+
#### Defined in
|
|
259
|
+
|
|
260
|
+
[packages/functions/src/middleware.ts:101](https://github.com/vercel/vercel/blob/main/packages/functions/src/middleware.ts#L101)
|
|
148
261
|
|
|
149
262
|
---
|
|
150
263
|
|
package/headers.d.ts
CHANGED
|
@@ -24,6 +24,10 @@ export declare const LONGITUDE_HEADER_NAME = "x-vercel-ip-longitude";
|
|
|
24
24
|
* See [docs](https://vercel.com/docs/concepts/edge-network/headers#x-vercel-ip-country-region).
|
|
25
25
|
*/
|
|
26
26
|
export declare const REGION_HEADER_NAME = "x-vercel-ip-country-region";
|
|
27
|
+
/**
|
|
28
|
+
* Postal code of the original client IP as calculated by Vercel Proxy.
|
|
29
|
+
*/
|
|
30
|
+
export declare const POSTAL_CODE_HEADER_NAME = "x-vercel-ip-postal-code";
|
|
27
31
|
/**
|
|
28
32
|
* The request ID for each request generated by Vercel Proxy.
|
|
29
33
|
*/
|
|
@@ -67,6 +71,8 @@ export interface Geo {
|
|
|
67
71
|
latitude?: string;
|
|
68
72
|
/** The longitude of the client. */
|
|
69
73
|
longitude?: string;
|
|
74
|
+
/** The postal code of the client */
|
|
75
|
+
postalCode?: string;
|
|
70
76
|
}
|
|
71
77
|
/**
|
|
72
78
|
* Returns the IP address of the request from the headers.
|
|
@@ -101,6 +107,7 @@ export declare function ipAddress(input: Request | Headers): string | undefined;
|
|
|
101
107
|
* "region": "iad1",
|
|
102
108
|
* "latitude": "40.7128",
|
|
103
109
|
* "longitude": "-74.0060"
|
|
110
|
+
* "postalCode": "10001"
|
|
104
111
|
* }
|
|
105
112
|
* ```
|
|
106
113
|
*
|
package/headers.js
CHANGED
|
@@ -24,6 +24,7 @@ __export(headers_exports, {
|
|
|
24
24
|
IP_HEADER_NAME: () => IP_HEADER_NAME,
|
|
25
25
|
LATITUDE_HEADER_NAME: () => LATITUDE_HEADER_NAME,
|
|
26
26
|
LONGITUDE_HEADER_NAME: () => LONGITUDE_HEADER_NAME,
|
|
27
|
+
POSTAL_CODE_HEADER_NAME: () => POSTAL_CODE_HEADER_NAME,
|
|
27
28
|
REGION_HEADER_NAME: () => REGION_HEADER_NAME,
|
|
28
29
|
REQUEST_ID_HEADER_NAME: () => REQUEST_ID_HEADER_NAME,
|
|
29
30
|
geolocation: () => geolocation,
|
|
@@ -36,6 +37,7 @@ const IP_HEADER_NAME = "x-real-ip";
|
|
|
36
37
|
const LATITUDE_HEADER_NAME = "x-vercel-ip-latitude";
|
|
37
38
|
const LONGITUDE_HEADER_NAME = "x-vercel-ip-longitude";
|
|
38
39
|
const REGION_HEADER_NAME = "x-vercel-ip-country-region";
|
|
40
|
+
const POSTAL_CODE_HEADER_NAME = "x-vercel-ip-postal-code";
|
|
39
41
|
const REQUEST_ID_HEADER_NAME = "x-vercel-id";
|
|
40
42
|
const EMOJI_FLAG_UNICODE_STARTING_POSITION = 127397;
|
|
41
43
|
function getHeader(headers, key) {
|
|
@@ -74,7 +76,8 @@ function geolocation(request) {
|
|
|
74
76
|
getHeader(request.headers, REQUEST_ID_HEADER_NAME)
|
|
75
77
|
),
|
|
76
78
|
latitude: getHeader(request.headers, LATITUDE_HEADER_NAME),
|
|
77
|
-
longitude: getHeader(request.headers, LONGITUDE_HEADER_NAME)
|
|
79
|
+
longitude: getHeader(request.headers, LONGITUDE_HEADER_NAME),
|
|
80
|
+
postalCode: getHeader(request.headers, POSTAL_CODE_HEADER_NAME)
|
|
78
81
|
};
|
|
79
82
|
}
|
|
80
83
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -85,6 +88,7 @@ function geolocation(request) {
|
|
|
85
88
|
IP_HEADER_NAME,
|
|
86
89
|
LATITUDE_HEADER_NAME,
|
|
87
90
|
LONGITUDE_HEADER_NAME,
|
|
91
|
+
POSTAL_CODE_HEADER_NAME,
|
|
88
92
|
REGION_HEADER_NAME,
|
|
89
93
|
REQUEST_ID_HEADER_NAME,
|
|
90
94
|
geolocation,
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -21,16 +21,21 @@ __export(src_exports, {
|
|
|
21
21
|
geolocation: () => import_headers.geolocation,
|
|
22
22
|
getEnv: () => import_get_env.getEnv,
|
|
23
23
|
ipAddress: () => import_headers.ipAddress,
|
|
24
|
+
next: () => import_middleware.next,
|
|
25
|
+
rewrite: () => import_middleware.rewrite,
|
|
24
26
|
waitUntil: () => import_wait_until.waitUntil
|
|
25
27
|
});
|
|
26
28
|
module.exports = __toCommonJS(src_exports);
|
|
27
29
|
var import_headers = require("./headers");
|
|
28
30
|
var import_get_env = require("./get-env");
|
|
29
31
|
var import_wait_until = require("./wait-until");
|
|
32
|
+
var import_middleware = require("./middleware");
|
|
30
33
|
// Annotate the CommonJS export names for ESM import in node:
|
|
31
34
|
0 && (module.exports = {
|
|
32
35
|
geolocation,
|
|
33
36
|
getEnv,
|
|
34
37
|
ipAddress,
|
|
38
|
+
next,
|
|
39
|
+
rewrite,
|
|
35
40
|
waitUntil
|
|
36
41
|
});
|
package/middleware.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
export interface ModifiedRequest {
|
|
2
|
+
/**
|
|
3
|
+
* If set, overwrites the incoming headers to the origin request.
|
|
4
|
+
*
|
|
5
|
+
* This is useful when you want to pass data between a Middleware and a
|
|
6
|
+
* Serverless or Edge Function.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* <caption>Add a `x-user-id` header and remove the `Authorization` header</caption>
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { rewrite } from '@vercel/edge';
|
|
13
|
+
* export default async function middleware(request: Request): Promise<Response> {
|
|
14
|
+
* const newHeaders = new Headers(request.headers);
|
|
15
|
+
* newHeaders.set('x-user-id', 'user_123');
|
|
16
|
+
* newHeaders.delete('authorization');
|
|
17
|
+
* return rewrite(request.url, {
|
|
18
|
+
* request: { headers: newHeaders }
|
|
19
|
+
* })
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
headers?: Headers;
|
|
24
|
+
}
|
|
25
|
+
export interface ExtraResponseInit extends Omit<ResponseInit, 'headers'> {
|
|
26
|
+
/**
|
|
27
|
+
* These headers will be sent to the user response
|
|
28
|
+
* along with the response headers from the origin.
|
|
29
|
+
*/
|
|
30
|
+
headers?: HeadersInit;
|
|
31
|
+
/**
|
|
32
|
+
* Fields to rewrite for the upstream request.
|
|
33
|
+
*/
|
|
34
|
+
request?: ModifiedRequest;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Returns a response that rewrites the request to a different URL.
|
|
38
|
+
*
|
|
39
|
+
* @param destination new URL to rewrite the request to
|
|
40
|
+
* @param init Additional options for the response
|
|
41
|
+
*
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* <caption>Rewrite all feature-flagged requests from `/:path*` to `/experimental/:path*`</caption>
|
|
45
|
+
*
|
|
46
|
+
* ```ts
|
|
47
|
+
* import { rewrite, next } from '@vercel/edge';
|
|
48
|
+
*
|
|
49
|
+
* export default async function middleware(req: Request) {
|
|
50
|
+
* const flagged = await getFlag(req, 'isExperimental');
|
|
51
|
+
* if (flagged) {
|
|
52
|
+
* const url = new URL(req.url);
|
|
53
|
+
* url.pathname = `/experimental{url.pathname}`;
|
|
54
|
+
* return rewrite(url);
|
|
55
|
+
* }
|
|
56
|
+
*
|
|
57
|
+
* return next();
|
|
58
|
+
* }
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* <caption>JWT authentication for `/api/:path*` requests</caption>
|
|
63
|
+
*
|
|
64
|
+
* ```ts
|
|
65
|
+
* import { rewrite, next } from '@vercel/edge';
|
|
66
|
+
*
|
|
67
|
+
* export default function middleware(req: Request) {
|
|
68
|
+
* const auth = checkJwt(req.headers.get('Authorization'));
|
|
69
|
+
* if (!checkJwt) {
|
|
70
|
+
* return rewrite(new URL('/api/error-unauthorized', req.url));
|
|
71
|
+
* }
|
|
72
|
+
* const url = new URL(req.url);
|
|
73
|
+
* url.searchParams.set('_userId', auth.userId);
|
|
74
|
+
* return rewrite(url);
|
|
75
|
+
* }
|
|
76
|
+
*
|
|
77
|
+
* export const config = { matcher: '/api/users/:path*' };
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export declare function rewrite(destination: string | URL, init?: ExtraResponseInit): Response;
|
|
81
|
+
/**
|
|
82
|
+
* Returns a Response that instructs the system to continue processing the request.
|
|
83
|
+
*
|
|
84
|
+
* @param init Additional options for the response
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* <caption>No-op middleware</caption>
|
|
88
|
+
*
|
|
89
|
+
* ```ts
|
|
90
|
+
* import { next } from '@vercel/edge';
|
|
91
|
+
*
|
|
92
|
+
* export default function middleware(_req: Request) {
|
|
93
|
+
* return next();
|
|
94
|
+
* }
|
|
95
|
+
* ```
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* <caption>Add response headers to all requests</caption>
|
|
99
|
+
*
|
|
100
|
+
* ```ts
|
|
101
|
+
* import { next } from '@vercel/edge';
|
|
102
|
+
*
|
|
103
|
+
* export default function middleware(_req: Request) {
|
|
104
|
+
* return next({
|
|
105
|
+
* headers: { 'x-from-middleware': 'true' },
|
|
106
|
+
* })
|
|
107
|
+
* }
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export declare function next(init?: ExtraResponseInit): Response;
|
package/middleware.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var middleware_exports = {};
|
|
20
|
+
__export(middleware_exports, {
|
|
21
|
+
next: () => next,
|
|
22
|
+
rewrite: () => rewrite
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(middleware_exports);
|
|
25
|
+
function handleMiddlewareField(init, headers) {
|
|
26
|
+
if (init?.request?.headers) {
|
|
27
|
+
if (!(init.request.headers instanceof Headers)) {
|
|
28
|
+
throw new Error("request.headers must be an instance of Headers");
|
|
29
|
+
}
|
|
30
|
+
const keys = [];
|
|
31
|
+
for (const [key, value] of init.request.headers) {
|
|
32
|
+
headers.set("x-middleware-request-" + key, value);
|
|
33
|
+
keys.push(key);
|
|
34
|
+
}
|
|
35
|
+
headers.set("x-middleware-override-headers", keys.join(","));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function rewrite(destination, init) {
|
|
39
|
+
const headers = new Headers(init?.headers ?? {});
|
|
40
|
+
headers.set("x-middleware-rewrite", String(destination));
|
|
41
|
+
handleMiddlewareField(init, headers);
|
|
42
|
+
return new Response(null, {
|
|
43
|
+
...init,
|
|
44
|
+
headers
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function next(init) {
|
|
48
|
+
const headers = new Headers(init?.headers ?? {});
|
|
49
|
+
headers.set("x-middleware-next", "1");
|
|
50
|
+
handleMiddlewareField(init, headers);
|
|
51
|
+
return new Response(null, {
|
|
52
|
+
...init,
|
|
53
|
+
headers
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
57
|
+
0 && (module.exports = {
|
|
58
|
+
next,
|
|
59
|
+
rewrite
|
|
60
|
+
});
|
package/package.json
CHANGED
|
@@ -7,9 +7,26 @@
|
|
|
7
7
|
"**/*.d.ts",
|
|
8
8
|
"**/*.md"
|
|
9
9
|
],
|
|
10
|
-
"main": "./index.js",
|
|
11
10
|
"types": "./index.d.ts",
|
|
12
|
-
"
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./index.js",
|
|
14
|
+
"require": "./index.js"
|
|
15
|
+
},
|
|
16
|
+
"./headers": {
|
|
17
|
+
"import": "./headers.js",
|
|
18
|
+
"require": "./headers.js"
|
|
19
|
+
},
|
|
20
|
+
"./oidc": {
|
|
21
|
+
"import": "./oidc/index.js",
|
|
22
|
+
"require": "./oidc/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./middleware": {
|
|
25
|
+
"import": "./middleware.js",
|
|
26
|
+
"require": "./middleware.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"version": "1.6.0",
|
|
13
30
|
"repository": {
|
|
14
31
|
"directory": "packages/functions",
|
|
15
32
|
"type": "git",
|