@vercel/edge
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -1,14 +0,81 @@ | ||
declare type ExtraResponseInit = Omit<ResponseInit, 'headers'> & { | ||
interface ExtraResponseInit extends Omit<ResponseInit, 'headers'> { | ||
/** | ||
* These headers will be sent to the user response | ||
* along with the response headers from the origin | ||
* along with the response headers from the origin. | ||
*/ | ||
headers?: HeadersInit; | ||
}; | ||
} | ||
/** | ||
* Rewrite the request into a different URL. | ||
* Returns a response that rewrites the request to a different URL. | ||
* | ||
* @param destination new URL to rewrite the request to | ||
* @param init Additional options for the response | ||
* | ||
* | ||
* @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*' }; | ||
* ``` | ||
*/ | ||
declare function rewrite(destination: string | URL, init?: ExtraResponseInit): Response; | ||
/** | ||
* This tells the Middleware to continue with the request. | ||
* Returns a Response that instructs the system to continue processing the request. | ||
* | ||
* @param init Additional options for the response | ||
* | ||
* @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' }, | ||
* }) | ||
* } | ||
* ``` | ||
*/ | ||
@@ -18,23 +85,23 @@ declare function next(init?: ExtraResponseInit): Response; | ||
/** | ||
* City of the original client IP calculated by Vercel Proxy. | ||
* City of the original client IP as calculated by Vercel Proxy. | ||
*/ | ||
declare const CITY_HEADER_NAME = "x-vercel-ip-city"; | ||
/** | ||
* Country of the original client IP calculated by Vercel Proxy. | ||
* Country of the original client IP as calculated by Vercel Proxy. | ||
*/ | ||
declare const COUNTRY_HEADER_NAME = "x-vercel-ip-country"; | ||
/** | ||
* Ip from Vercel Proxy. Do not confuse it with the client Ip. | ||
* Client IP as calcualted by Vercel Proxy. | ||
*/ | ||
declare const IP_HEADER_NAME = "x-real-ip"; | ||
/** | ||
* Latitude of the original client IP calculated by Vercel Proxy. | ||
* Latitude of the original client IP as calculated by Vercel Proxy. | ||
*/ | ||
declare const LATITUDE_HEADER_NAME = "x-vercel-ip-latitude"; | ||
/** | ||
* Longitude of the original client IP calculated by Vercel Proxy. | ||
* Longitude of the original client IP as calculated by Vercel Proxy. | ||
*/ | ||
declare const LONGITUDE_HEADER_NAME = "x-vercel-ip-longitude"; | ||
/** | ||
* Region of the original client IP calculated by Vercel Proxy. | ||
* Region of the original client IP as calculated by Vercel Proxy. | ||
*/ | ||
@@ -52,14 +119,14 @@ declare const REGION_HEADER_NAME = "x-vercel-ip-country-region"; | ||
/** | ||
* The location information of a given request | ||
* The location information of a given request. | ||
*/ | ||
interface Geo { | ||
/** The city that the request originated from */ | ||
/** The city that the request originated from. */ | ||
city?: string; | ||
/** The country that the request originated from */ | ||
/** The country that the request originated from. */ | ||
country?: string; | ||
/** The Vercel Edge Network region that received the request */ | ||
/** The [Vercel Edge Network region](https://vercel.com/docs/concepts/edge-network/regions) that received the request. */ | ||
region?: string; | ||
/** The latitude of the client */ | ||
/** The latitude of the client. */ | ||
latitude?: string; | ||
/** The longitude of the client */ | ||
/** The longitude of the client. */ | ||
longitude?: string; | ||
@@ -71,6 +138,7 @@ } | ||
* @see {@link IP_HEADER_NAME} | ||
* @param request The incoming request object which provides the IP | ||
*/ | ||
declare function ipAddress(request: Request): string | undefined; | ||
/** | ||
* Returns the location information from for the incoming request | ||
* Returns the location information for the incoming request. | ||
* | ||
@@ -82,2 +150,3 @@ * @see {@link CITY_HEADER_NAME} | ||
* @see {@link LONGITUDE_HEADER_NAME} | ||
* @param request The incoming request object which provides the geolocation data | ||
*/ | ||
@@ -84,0 +153,0 @@ declare function geolocation(request: Request): Geo; |
{ | ||
"name": "@vercel/edge", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"license": "MIT", | ||
@@ -11,3 +11,4 @@ "main": "dist/index.js", | ||
"test": "jest --env node --verbose --runInBand --bail", | ||
"test-unit": "yarn test" | ||
"test-unit": "yarn test", | ||
"build:docs": "typedoc && prettier --write docs/**/*.md docs/*.md" | ||
}, | ||
@@ -19,2 +20,5 @@ "devDependencies": { | ||
"tsup": "6.1.2", | ||
"typedoc": "0.23.10", | ||
"typedoc-plugin-markdown": "3.13.4", | ||
"typedoc-plugin-mdn-links": "2.0.0", | ||
"typescript": "4.7.4" | ||
@@ -32,3 +36,3 @@ }, | ||
}, | ||
"gitHead": "32afd67d29d46f67027091ab9695d8ff330355b5" | ||
"gitHead": "2906d83eaeddc72f9d874981dbe6698e812f3ca3" | ||
} |
# `@vercel/edge` | ||
A set of utilities to help you deploy your app on the Edge using Vercel. | ||
A set of utilities to help you deploy any framework on the Edge using Vercel. | ||
Please [follow the documentation](./docs) for examples and usage. |
/** | ||
* City of the original client IP calculated by Vercel Proxy. | ||
* City of the original client IP as calculated by Vercel Proxy. | ||
*/ | ||
export const CITY_HEADER_NAME = 'x-vercel-ip-city'; | ||
/** | ||
* Country of the original client IP calculated by Vercel Proxy. | ||
* Country of the original client IP as calculated by Vercel Proxy. | ||
*/ | ||
export const COUNTRY_HEADER_NAME = 'x-vercel-ip-country'; | ||
/** | ||
* Ip from Vercel Proxy. Do not confuse it with the client Ip. | ||
* Client IP as calcualted by Vercel Proxy. | ||
*/ | ||
export const IP_HEADER_NAME = 'x-real-ip'; | ||
/** | ||
* Latitude of the original client IP calculated by Vercel Proxy. | ||
* Latitude of the original client IP as calculated by Vercel Proxy. | ||
*/ | ||
export const LATITUDE_HEADER_NAME = 'x-vercel-ip-latitude'; | ||
/** | ||
* Longitude of the original client IP calculated by Vercel Proxy. | ||
* Longitude of the original client IP as calculated by Vercel Proxy. | ||
*/ | ||
export const LONGITUDE_HEADER_NAME = 'x-vercel-ip-longitude'; | ||
/** | ||
* Region of the original client IP calculated by Vercel Proxy. | ||
* Region of the original client IP as calculated by Vercel Proxy. | ||
*/ | ||
@@ -37,14 +37,18 @@ export const REGION_HEADER_NAME = 'x-vercel-ip-country-region'; | ||
/** | ||
* The location information of a given request | ||
* The location information of a given request. | ||
*/ | ||
export interface Geo { | ||
/** The city that the request originated from */ | ||
/** The city that the request originated from. */ | ||
city?: string; | ||
/** The country that the request originated from */ | ||
/** The country that the request originated from. */ | ||
country?: string; | ||
/** The Vercel Edge Network region that received the request */ | ||
/** The [Vercel Edge Network region](https://vercel.com/docs/concepts/edge-network/regions) that received the request. */ | ||
region?: string; | ||
/** The latitude of the client */ | ||
/** The latitude of the client. */ | ||
latitude?: string; | ||
/** The longitude of the client */ | ||
/** The longitude of the client. */ | ||
longitude?: string; | ||
@@ -61,2 +65,3 @@ } | ||
* @see {@link IP_HEADER_NAME} | ||
* @param request The incoming request object which provides the IP | ||
*/ | ||
@@ -68,3 +73,3 @@ export function ipAddress(request: Request): string | undefined { | ||
/** | ||
* Returns the location information from for the incoming request | ||
* Returns the location information for the incoming request. | ||
* | ||
@@ -76,2 +81,3 @@ * @see {@link CITY_HEADER_NAME} | ||
* @see {@link LONGITUDE_HEADER_NAME} | ||
* @param request The incoming request object which provides the geolocation data | ||
*/ | ||
@@ -78,0 +84,0 @@ export function geolocation(request: Request): Geo { |
@@ -1,11 +0,52 @@ | ||
export type ExtraResponseInit = Omit<ResponseInit, 'headers'> & { | ||
export interface ExtraResponseInit extends Omit<ResponseInit, 'headers'> { | ||
/** | ||
* These headers will be sent to the user response | ||
* along with the response headers from the origin | ||
* along with the response headers from the origin. | ||
*/ | ||
headers?: HeadersInit; | ||
}; | ||
} | ||
/** | ||
* Rewrite the request into a different URL. | ||
* Returns a response that rewrites the request to a different URL. | ||
* | ||
* @param destination new URL to rewrite the request to | ||
* @param init Additional options for the response | ||
* | ||
* | ||
* @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*' }; | ||
* ``` | ||
*/ | ||
@@ -25,3 +66,29 @@ export function rewrite( | ||
/** | ||
* This tells the Middleware to continue with the request. | ||
* Returns a Response that instructs the system to continue processing the request. | ||
* | ||
* @param init Additional options for the response | ||
* | ||
* @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' }, | ||
* }) | ||
* } | ||
* ``` | ||
*/ | ||
@@ -28,0 +95,0 @@ export function next(init?: ExtraResponseInit): Response { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40315
18
615
5
8