@vercel/functions 1.4.2 β†’ 1.5.1

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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @vercel/functions
2
2
 
3
+ ## 1.5.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [@vercel/functions] update headers doc ([#12649](https://github.com/vercel/vercel/pull/12649))
8
+
9
+ ## 1.5.0
10
+
11
+ ### Minor Changes
12
+
13
+ - ipAddress: accept headers as input ([#12429](https://github.com/vercel/vercel/pull/12429))
14
+
3
15
  ## 1.4.2
4
16
 
5
17
  ### Patch Changes
package/README.md CHANGED
@@ -1,60 +1,5 @@
1
- <p align="center">
2
- <a href="https://vercel.com">
3
- <img src="https://assets.vercel.com/image/upload/v1588805858/repositories/vercel/logo.png" height="96">
4
- <h3 align="center">Vercel</h3>
5
- </a>
6
- <p align="center">Develop. Preview. Ship.</p>
7
- </p>
1
+ # `@vercel/functions`
8
2
 
9
- [Join the Vercel Community](https://vercel.community/)
3
+ Runtime functions to be used with your Vercel Functions.
10
4
 
11
- ## OIDC Support Functions
12
-
13
- ### AWS S3 Example
14
-
15
- ```ts
16
- import * as s3 from '@aws-sdk/client-s3';
17
- import { awsCredentialsProvider } from '@vercel/functions/oidc';
18
-
19
- const s3Client = new s3.S3Client({
20
- credentials: awsCredentialsProvider({
21
- roleArn: process.env.AWS_ROLE_ARN,
22
- }),
23
- });
24
-
25
- export const GET = () => {
26
- const result = await s3Client.send(
27
- new s3.ListObjectsV2Command({
28
- Bucket: process.env.BUCKET_NAME,
29
- })
30
- );
31
- return Response.json({ objects: result.Contents });
32
- };
33
- ```
34
-
35
- ### Azure CosmosDB Example
36
-
37
- ```ts
38
- import { ClientAssertionCredential } from '@azure/identity';
39
- import { CosmosClient } from '@azure/cosmos';
40
- import { getVercelOidcToken } from '@vercel/functions/oidc';
41
-
42
- const credentialsProvider = new ClientAssertionCredential(
43
- process.env.AZURE_TENANT_ID,
44
- process.env.AZURE_CLIENT_ID,
45
- getVercelOidcToken
46
- );
47
-
48
- const cosmosClient = new CosmosClient({
49
- endpoint: process.env.COSMOS_DB_ENDPOINT,
50
- aadCredentials: credentialsProvider,
51
- });
52
-
53
- export const GET = () => {
54
- const container = cosmosClient
55
- .database(process.env.COSMOS_DB_NAME)
56
- .container(process.env.COSMOS_DB_CONTAINER);
57
- const items = await container.items.query('SELECT * FROM f').fetchAll();
58
- return Response.json({ items: items.resources });
59
- };
60
- ```
5
+ Please [follow the documentation](https://vercel.com/docs/functions/vercel-functions-package) for examples and usage.
@@ -26,7 +26,7 @@ The city that the request originated from.
26
26
 
27
27
  #### Defined in
28
28
 
29
- [packages/functions/src/headers.ts:50](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L50)
29
+ [packages/functions/src/headers.ts:55](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L55)
30
30
 
31
31
  ---
32
32
 
@@ -38,7 +38,7 @@ The country that the request originated from.
38
38
 
39
39
  #### Defined in
40
40
 
41
- [packages/functions/src/headers.ts:53](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L53)
41
+ [packages/functions/src/headers.ts:58](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L58)
42
42
 
43
43
  ---
44
44
 
@@ -51,7 +51,7 @@ See [docs](https://vercel.com/docs/concepts/edge-network/headers#x-vercel-ip-cou
51
51
 
52
52
  #### Defined in
53
53
 
54
- [packages/functions/src/headers.ts:64](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L64)
54
+ [packages/functions/src/headers.ts:69](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L69)
55
55
 
56
56
  ---
57
57
 
@@ -63,7 +63,7 @@ The flag emoji for the country the request originated from.
63
63
 
64
64
  #### Defined in
65
65
 
66
- [packages/functions/src/headers.ts:56](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L56)
66
+ [packages/functions/src/headers.ts:61](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L61)
67
67
 
68
68
  ---
69
69
 
@@ -75,7 +75,7 @@ The latitude of the client.
75
75
 
76
76
  #### Defined in
77
77
 
78
- [packages/functions/src/headers.ts:67](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L67)
78
+ [packages/functions/src/headers.ts:72](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L72)
79
79
 
80
80
  ---
81
81
 
@@ -87,7 +87,7 @@ The longitude of the client.
87
87
 
88
88
  #### Defined in
89
89
 
90
- [packages/functions/src/headers.ts:70](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L70)
90
+ [packages/functions/src/headers.ts:75](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L75)
91
91
 
92
92
  ---
93
93
 
@@ -99,4 +99,4 @@ The [Vercel Edge Network region](https://vercel.com/docs/concepts/edge-network/r
99
99
 
100
100
  #### Defined in
101
101
 
102
- [packages/functions/src/headers.ts:59](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L59)
102
+ [packages/functions/src/headers.ts:64](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L64)
@@ -2,8 +2,7 @@
2
2
 
3
3
  [index](../modules/index.md).Request
4
4
 
5
- We define a new type so this function can be reused with
6
- the global `Request`, `node-fetch` and other types.
5
+ We define a new type so this function can be reused with the global `Request`, `node-fetch` and other types.
7
6
 
8
7
  ## Table of contents
9
8
 
@@ -15,14 +14,10 @@ the global `Request`, `node-fetch` and other types.
15
14
 
16
15
  ### headers
17
16
 
18
- β€’ **headers**: `Object`
17
+ β€’ **headers**: `Headers`
19
18
 
20
- #### Type declaration
21
-
22
- | Name | Type |
23
- | :---- | :--------------------------------------- |
24
- | `get` | (`name`: `string`) => `null` \| `string` |
19
+ Represents the headers of a request.
25
20
 
26
21
  #### Defined in
27
22
 
28
- [packages/functions/src/headers.ts:40](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L40)
23
+ [packages/functions/src/headers.ts:48](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L48)
@@ -51,7 +51,7 @@ The location information of the request, in this way:
51
51
  "country": "US",
52
52
  "flag": "πŸ‡ΊπŸ‡Έ",
53
53
  "countryRegion": "NY",
54
- "region": "dev1",
54
+ "region": "iad1",
55
55
  "latitude": "40.7128",
56
56
  "longitude": "-74.0060"
57
57
  }
@@ -59,7 +59,7 @@ The location information of the request, in this way:
59
59
 
60
60
  #### Defined in
61
61
 
62
- [packages/functions/src/headers.ts:166](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L166)
62
+ [packages/functions/src/headers.ts:172](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L172)
63
63
 
64
64
  ---
65
65
 
@@ -115,7 +115,7 @@ https://vercel.com/docs/projects/environment-variables/system-environment-variab
115
115
 
116
116
  ### ipAddress
117
117
 
118
- β–Έ **ipAddress**(`request`): `string` \| `undefined`
118
+ β–Έ **ipAddress**(`input`): `string` \| `undefined`
119
119
 
120
120
  Returns the IP address of the request from the headers.
121
121
 
@@ -125,16 +125,16 @@ Returns the IP address of the request from the headers.
125
125
  import { ipAddress } from '@vercel/functions';
126
126
 
127
127
  export function GET(request) {
128
- const ip = ipAddress(request)
129
- return new Response('Your ip is' ${ip});
128
+ const ip = ipAddress(request);
129
+ return new Response(`Your IP is ${ip}`);
130
130
  }
131
131
  ```
132
132
 
133
133
  #### Parameters
134
134
 
135
- | Name | Type | Description |
136
- | :-------- | :------------------------------------------ | :------------------------------------------------ |
137
- | `request` | [`Request`](../interfaces/index.Request.md) | The incoming request object which provides the IP |
135
+ | Name | Type | Description |
136
+ | :------ | :------------------------------------------------------- | :-------------------------------------- |
137
+ | `input` | `Headers` \| [`Request`](../interfaces/index.Request.md) | The incoming request object or headers. |
138
138
 
139
139
  #### Returns
140
140
 
@@ -144,7 +144,7 @@ The IP address of the request.
144
144
 
145
145
  #### Defined in
146
146
 
147
- [packages/functions/src/headers.ts:119](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L119)
147
+ [packages/functions/src/headers.ts:124](https://github.com/vercel/vercel/blob/main/packages/functions/src/headers.ts#L124)
148
148
 
149
149
  ---
150
150
 
package/headers.d.ts CHANGED
@@ -33,13 +33,19 @@ export declare const REQUEST_ID_HEADER_NAME = "x-vercel-id";
33
33
  */
34
34
  export declare const EMOJI_FLAG_UNICODE_STARTING_POSITION = 127397;
35
35
  /**
36
- * We define a new type so this function can be reused with
37
- * the global `Request`, `node-fetch` and other types.
36
+ * Represents the headers of a request.
37
+ */
38
+ export interface Headers {
39
+ get(name: string): string | null;
40
+ }
41
+ /**
42
+ * We define a new type so this function can be reused with the global `Request`, `node-fetch` and other types.
38
43
  */
39
44
  export interface Request {
40
- headers: {
41
- get(name: string): string | null;
42
- };
45
+ /**
46
+ * Represents the headers of a request.
47
+ */
48
+ headers: Headers;
43
49
  }
44
50
  /**
45
51
  * The location information of a given request.
@@ -65,7 +71,7 @@ export interface Geo {
65
71
  /**
66
72
  * Returns the IP address of the request from the headers.
67
73
  *
68
- * @param request The incoming request object which provides the IP
74
+ * @param {(Request|Headers)} input The incoming request object or headers.
69
75
  * @returns The IP address of the request.
70
76
  *
71
77
  * @example
@@ -74,13 +80,13 @@ export interface Geo {
74
80
  * import { ipAddress } from '@vercel/functions';
75
81
  *
76
82
  * export function GET(request) {
77
- * const ip = ipAddress(request)
78
- * return new Response('Your ip is' ${ip});
83
+ * const ip = ipAddress(request);
84
+ * return new Response(`Your IP is ${ip}`);
79
85
  * }
80
86
  * ```
81
87
  *
82
88
  */
83
- export declare function ipAddress(request: Request): string | undefined;
89
+ export declare function ipAddress(input: Request | Headers): string | undefined;
84
90
  /**
85
91
  * Returns the location information for the incoming request.
86
92
  * @param request The incoming request object which provides the geolocation data
@@ -92,7 +98,7 @@ export declare function ipAddress(request: Request): string | undefined;
92
98
  * "country": "US",
93
99
  * "flag": "πŸ‡ΊπŸ‡Έ",
94
100
  * "countryRegion": "NY",
95
- * "region": "dev1",
101
+ * "region": "iad1",
96
102
  * "latitude": "40.7128",
97
103
  * "longitude": "-74.0060"
98
104
  * }
package/headers.js CHANGED
@@ -38,11 +38,11 @@ const LONGITUDE_HEADER_NAME = "x-vercel-ip-longitude";
38
38
  const REGION_HEADER_NAME = "x-vercel-ip-country-region";
39
39
  const REQUEST_ID_HEADER_NAME = "x-vercel-id";
40
40
  const EMOJI_FLAG_UNICODE_STARTING_POSITION = 127397;
41
- function getHeader(request, key) {
42
- return request.headers.get(key) ?? void 0;
41
+ function getHeader(headers, key) {
42
+ return headers.get(key) ?? void 0;
43
43
  }
44
44
  function getHeaderWithDecode(request, key) {
45
- const header = getHeader(request, key);
45
+ const header = getHeader(request.headers, key);
46
46
  return header ? decodeURIComponent(header) : void 0;
47
47
  }
48
48
  function getFlag(countryCode) {
@@ -53,8 +53,9 @@ function getFlag(countryCode) {
53
53
  ...countryCode.split("").map((char) => EMOJI_FLAG_UNICODE_STARTING_POSITION + char.charCodeAt(0))
54
54
  );
55
55
  }
56
- function ipAddress(request) {
57
- return getHeader(request, IP_HEADER_NAME);
56
+ function ipAddress(input) {
57
+ const headers = "headers" in input ? input.headers : input;
58
+ return getHeader(headers, IP_HEADER_NAME);
58
59
  }
59
60
  function getRegionFromRequestId(requestId) {
60
61
  if (!requestId) {
@@ -66,12 +67,14 @@ function geolocation(request) {
66
67
  return {
67
68
  // city name may be encoded to support multi-byte characters
68
69
  city: getHeaderWithDecode(request, CITY_HEADER_NAME),
69
- country: getHeader(request, COUNTRY_HEADER_NAME),
70
- flag: getFlag(getHeader(request, COUNTRY_HEADER_NAME)),
71
- countryRegion: getHeader(request, REGION_HEADER_NAME),
72
- region: getRegionFromRequestId(getHeader(request, REQUEST_ID_HEADER_NAME)),
73
- latitude: getHeader(request, LATITUDE_HEADER_NAME),
74
- longitude: getHeader(request, LONGITUDE_HEADER_NAME)
70
+ country: getHeader(request.headers, COUNTRY_HEADER_NAME),
71
+ flag: getFlag(getHeader(request.headers, COUNTRY_HEADER_NAME)),
72
+ countryRegion: getHeader(request.headers, REGION_HEADER_NAME),
73
+ region: getRegionFromRequestId(
74
+ getHeader(request.headers, REQUEST_ID_HEADER_NAME)
75
+ ),
76
+ latitude: getHeader(request.headers, LATITUDE_HEADER_NAME),
77
+ longitude: getHeader(request.headers, LONGITUDE_HEADER_NAME)
75
78
  };
76
79
  }
77
80
  // Annotate the CommonJS export names for ESM import in node:
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  ],
10
10
  "main": "./index.js",
11
11
  "types": "./index.d.ts",
12
- "version": "1.4.2",
12
+ "version": "1.5.1",
13
13
  "repository": {
14
14
  "directory": "packages/functions",
15
15
  "type": "git",