Files
vercel/packages/edge/docs/README.md
Ethan Arrowood 9c768b98b7 [tests] Migrate from yarn to pnpm (#9198)
<picture data-single-emoji=":pnpm:" title=":pnpm:"><img class="emoji" src="https://single-emoji.vercel.app/api/emoji/eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..4mJzrO94AnSn0Pue.4apgaKtTUdQ-wxNyahjdJj28u8bbXreLoTA8AGqYjLta3MrsFvbo9DsQFth4CoIkBgXFhQ5_BVcKNfYbwLg4bKzyIvItKe4OFS8AzG7Kkicz2kUUZk0.nXyK_PvHzZFGA-MQB6XHfA" alt=":pnpm:" width="20" height="auto" align="absmiddle"></picture> 

yarn has become increasingly more difficult to use as the v1 we rely on no longer receives updates. pnpm is faster and is actively maintained. 

This PR migrates us to pnpm.
2023-01-11 23:35:13 +00:00

8.2 KiB

@vercel/edge

Table of contents

Interfaces

Variables

Functions

Variables

CITY_HEADER_NAME

Const CITY_HEADER_NAME: "x-vercel-ip-city"

City of the original client IP as calculated by Vercel Proxy.

Defined in

packages/edge/src/edge-headers.ts:4


COUNTRY_HEADER_NAME

Const COUNTRY_HEADER_NAME: "x-vercel-ip-country"

Country of the original client IP as calculated by Vercel Proxy.

Defined in

packages/edge/src/edge-headers.ts:8


IP_HEADER_NAME

Const IP_HEADER_NAME: "x-real-ip"

Client IP as calcualted by Vercel Proxy.

Defined in

packages/edge/src/edge-headers.ts:12


LATITUDE_HEADER_NAME

Const LATITUDE_HEADER_NAME: "x-vercel-ip-latitude"

Latitude of the original client IP as calculated by Vercel Proxy.

Defined in

packages/edge/src/edge-headers.ts:16


LONGITUDE_HEADER_NAME

Const LONGITUDE_HEADER_NAME: "x-vercel-ip-longitude"

Longitude of the original client IP as calculated by Vercel Proxy.

Defined in

packages/edge/src/edge-headers.ts:20


REGION_HEADER_NAME

Const REGION_HEADER_NAME: "x-vercel-ip-country-region"

Country region of the original client IP calculated by Vercel Proxy.

See docs.

Defined in

packages/edge/src/edge-headers.ts:26


REQUEST_ID_HEADER_NAME

Const REQUEST_ID_HEADER_NAME: "x-vercel-id"

The request ID for each request generated by Vercel Proxy.

Defined in

packages/edge/src/edge-headers.ts:30

Functions

geolocation

geolocation(request): Geo

Returns the location information for the incoming request.

See

Parameters

Name Type Description
request Request The incoming request object which provides the geolocation data

Returns

Geo

Defined in

packages/edge/src/edge-headers.ts:106


ipAddress

ipAddress(request): string | undefined

Returns the IP address of the request from the headers.

See

IP_HEADER_NAME

Parameters

Name Type Description
request Request The incoming request object which provides the IP

Returns

string | undefined

Defined in

packages/edge/src/edge-headers.ts:77


json

json(data, init?): Response

Builds a response object from a serializable JavaScript object:

  • sets the 'Content-Type' response header to 'application/json'
  • sets the response body from provided data

See

https://fetch.spec.whatwg.org/#dom-response-json

Example

Building a JSON response
import { json } from '@vercel/edge';

const response = json(
  { notification: { success: true, content: 'worked' } },
  { headers: { 'x-custom': '1' } }
);

Parameters

Name Type Description
data any serialized data
init? ResponseInit optional custom response status, statusText and headers

Returns

Response

Defined in

packages/edge/src/response.ts:19


next

next(init?): Response

Returns a Response that instructs the system to continue processing the request.

Example

No-op middleware
import { next } from '@vercel/edge';

export default function middleware(_req: Request) {
  return next();
}

Example

Add response headers to all requests
import { next } from '@vercel/edge';

export default function middleware(_req: Request) {
  return next({
    headers: { 'x-from-middleware': 'true' },
  });
}

Parameters

Name Type Description
init? ExtraResponseInit Additional options for the response

Returns

Response

Defined in

packages/edge/src/middleware-helpers.ts:145


rewrite

rewrite(destination, init?): Response

Returns a response that rewrites the request to a different URL.

Example

Rewrite all feature-flagged requests from `/:path*` to `/experimental/:path*`
import { rewrite, next } from '@vercel/edge';

export default async function middleware(req: Request) {
  const flagged = await getFlag(req, 'isExperimental');
  if (flagged) {
    const url = new URL(req.url);
    url.pathname = `/experimental{url.pathname}`;
    return rewrite(url);
  }

  return next();
}

Example

JWT authentication for `/api/:path*` requests
import { rewrite, next } from '@vercel/edge';

export default function middleware(req: Request) {
  const auth = checkJwt(req.headers.get('Authorization'));
  if (!checkJwt) {
    return rewrite(new URL('/api/error-unauthorized', req.url));
  }
  const url = new URL(req.url);
  url.searchParams.set('_userId', auth.userId);
  return rewrite(url);
}

export const config = { matcher: '/api/users/:path*' };

Parameters

Name Type Description
destination string | URL new URL to rewrite the request to
init? ExtraResponseInit Additional options for the response

Returns

Response

Defined in

packages/edge/src/middleware-helpers.ts:101