Back to Vercel

@vercel/edge

packages/edge/docs/README.md

16.1.27.6 KB
Original Source

@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/functions/headers.d.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/functions/headers.d.ts:8


EMOJI_FLAG_UNICODE_STARTING_POSITION

Const EMOJI_FLAG_UNICODE_STARTING_POSITION: 127397

Unicode characters for emoji flags start at this number, and run up to 127469.

Defined in

packages/functions/headers.d.ts:38


IP_HEADER_NAME

Const IP_HEADER_NAME: "x-real-ip"

Client IP as calculated by Vercel Proxy.

Defined in

packages/functions/headers.d.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/functions/headers.d.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/functions/headers.d.ts:20


POSTAL_CODE_HEADER_NAME

Const POSTAL_CODE_HEADER_NAME: "x-vercel-ip-postal-code"

Postal code of the original client IP as calculated by Vercel Proxy.

Defined in

packages/functions/headers.d.ts:30


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/functions/headers.d.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/functions/headers.d.ts:34

Functions

geolocation

geolocation(request): Geo

Returns the location information for the incoming request.

Example

js
import { geolocation } from '@vercel/functions';

export function GET(request) {
  const details = geolocation(request);
  return Response.json(details);
}

Parameters

NameTypeDescription
requestRequestThe incoming request object which provides the geolocation data

Returns

Geo

The location information of the request, in this way:

json
{
 "city": "New York",
 "country": "US",
 "flag": "🇺🇸",
 "countryRegion": "NY",
 "region": "iad1",
 "latitude": "40.7128",
 "longitude": "-74.0060"
 "postalCode": "10001"
}

Defined in

packages/functions/headers.d.ts:125


ipAddress

ipAddress(input): string | undefined

Returns the IP address of the request from the headers.

Example

js
import { ipAddress } from '@vercel/functions';

export function GET(request) {
  const ip = ipAddress(request);
  return new Response(`Your IP is ${ip}`);
}

Parameters

NameTypeDescription
inputRequest | HeadersThe incoming request object or headers.

Returns

string | undefined

The IP address of the request.

Defined in

packages/functions/headers.d.ts:95


next

next(init?): Response

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

Example

<caption>No-op middleware</caption>
ts
import { next } from '@vercel/edge';

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

Example

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

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

Parameters

NameTypeDescription
init?ExtraResponseInitAdditional options for the response

Returns

Response

Defined in

packages/functions/middleware.d.ts:110


rewrite

rewrite(destination, init?): Response

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

Example

<caption>Rewrite all feature-flagged requests from `/:path*` to `/experimental/:path*`</caption>
ts
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

<caption>JWT authentication for `/api/:path*` requests</caption>
ts
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

NameTypeDescription
destinationstring | URLnew URL to rewrite the request to
init?ExtraResponseInitAdditional options for the response

Returns

Response

Defined in

packages/functions/middleware.d.ts:80