UNPKG

@ffsm/napi

Version:

Napi - A framework using with Next.js for building APIs.

149 lines (148 loc) 3.76 kB
import { NapiContent } from "./enums"; import { Cookie } from "@ffsm/cookie"; import { parse } from "./helpers/parse"; export class NapiRouter { constructor(req, _route, _params) { this.req = req; this._route = _route; this._params = _params; this._headers = {}; this._schema = undefined; this._hostname = undefined; this._port = undefined; this._userAgent = undefined; this._href = undefined; this._pathname = undefined; this._username = undefined; this._password = undefined; this._origin = undefined; this._protocol = undefined; this._search = ""; this._query = {}; this.extract(); } extract() { this.req.headers.forEach((value, key) => { this._headers[key] = value; }); this._cookie = Cookie.from(this.req); this._method = this.req.method.toUpperCase(); this._url = this.req.url; this._mode = this.req.mode; this._href = this.req.nextUrl.href; this._schema = this._headers["x-forwarded-proto"]; this._userAgent = this._headers["user-agent"]; this._pathname = this.req.nextUrl.pathname; this._hostname = this.req.nextUrl.hostname; this._port = this.req.nextUrl.port ? Number(this.req.nextUrl.port) : undefined; this._host = this.req.nextUrl.host; this._origin = this.req.nextUrl.origin; this._protocol = this.req.nextUrl.protocol; this._search = this.req.nextUrl.search; this._query = parse(this._search); } getHeader(name) { return this._headers[name]; } getCookie(name) { return this._cookie.get(name); } json() { return this.req.json(); } text() { return this.req.text(); } formData() { return this.req.formData(); } arrayBuffer() { return this.req.arrayBuffer(); } get method() { return this._method; } get headers() { return this._headers; } get cookies() { return this._cookie.getAll(); } get url() { return this._url; } get mode() { return this._mode; } get host() { return this._host; } get schema() { return this._schema; } get hostname() { return this._hostname; } get port() { return this._port; } get userAgent() { return this._userAgent; } get href() { return this._href; } get pathname() { return this._pathname; } get path() { return this._pathname; } get username() { return this._username; } get password() { return this._password; } get origin() { return this._origin; } get protocol() { return this._protocol; } get search() { return this._search; } get query() { return this._query; } get params() { return this._params; } get route() { return this._route; } get auth() { return { username: this._username, password: this._password, }; } get body() { const contentType = this._headers["content-type"]; switch (contentType) { case NapiContent.TEXT: case NapiContent.ENCODED: return this.req.text(); case NapiContent.MULTIPART: return this.req.formData(); case NapiContent.BINARY: return this.req.arrayBuffer(); default: return this.req.json(); } } get cookie() { return this._cookie; } }