UNPKG

@gabliam/web-core

Version:
212 lines (211 loc) 6.16 kB
export interface GabRequest<T = any> { /** * Return the original request */ readonly originalRequest: T; body: object; /** * Request headers */ headers: any; /** * Check if the request is fresh, aka * Last-Modified and/or the ETag * still match. */ readonly fresh: boolean; /** * Parse the "Host" header field hostname * and support X-Forwarded-Host when a * proxy is enabled. */ readonly hostname: string; /** * Request remote address. Supports X-Forwarded-For when app.proxy is true. */ readonly ip: string; /** * When `app.proxy` is `true`, parse * the "X-Forwarded-For" ip address list. * * For example if the value were "client, proxy1, proxy2" * you would receive the array `["client", "proxy1", "proxy2"]` * where "proxy2" is the furthest down-stream. */ readonly ips: string[]; readonly file: any; readonly files: any; /** * Get/Set request method. */ method: string; /** * Get original url */ readonly originalUrl: string; /** * Get/Set request URL. */ url: string; /** * Get/Set request param */ params: any; /** * Get request pathname. * Set pathname, retaining the query-string when present. */ path: string; /** * Return the protocol string "http" or "https" * when requested with TLS. When the proxy setting * is enabled the "X-Forwarded-Proto" header * field will be trusted. If you're running behind * a reverse proxy that supplies https for you this * may be enabled. */ readonly protocol: string; /** * Get parsed query-string. * Set query-string as an object. */ query: any; /** * Get/Set query string. */ querystring: string; /** * Short-hand for: * * this.protocol == 'https' */ readonly secure: boolean; /** * Check if the request is stale, aka * "Last-Modified" and / or the "ETag" for the * resource has changed. */ readonly stale: boolean; /** * Return subdomains as an array. * * Subdomains are the dot-separated parts of the host before the main domain * of the app. By default, the domain of the app is assumed to be the last two * parts of the host. This can be changed by setting `app.subdomainOffset`. * * For example, if the domain is "tobi.ferrets.example.com": * If `app.subdomainOffset` is not set, this.subdomains is * `["ferrets", "tobi"]`. * If `app.subdomainOffset` is 3, this.subdomains is `["tobi"]`. */ readonly subdomains: string[]; /** * Check if the request was an _XMLHttpRequest_. */ readonly xhr: boolean; /** * Check if the given `type(s)` is acceptable, returning * the best match when true, otherwise `undefined`, in which * case you should respond with 406 "Not Acceptable". * * The `type` value may be a single mime type string * such as "application/json", the extension name * such as "json", a comma-delimted list such as "json, html, text/plain", * or an array `["json", "html", "text/plain"]`. When a list * or array is given the _best_ match, if any is returned. * * Examples: * * // Accept: text/html * req.accepts('html'); * // => "html" * * // Accept: text/*, application/json * req.accepts('html'); * // => "html" * req.accepts('text/html'); * // => "text/html" * req.accepts('json, text'); * // => "json" * req.accepts('application/json'); * // => "application/json" * * // Accept: text/*, application/json * req.accepts('image/png'); * req.accepts('png'); * // => undefined * * // Accept: text/*;q=.5, application/json * req.accepts(['html', 'json']); * req.accepts('html, json'); * // => "json" */ accepts(...type: string[]): string[] | string | false; /** * Return accepted charsets or best fit based on `charsets`. * * Given `Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5` * an array sorted by quality is returned: * * ['utf-8', 'utf-7', 'iso-8859-1'] */ acceptsCharsets(...charsets: string[]): string[] | string | boolean; /** * Return accepted encodings or best fit based on `encodings`. * * Given `Accept-Encoding: gzip, deflate` * an array sorted by quality is returned: * * ['gzip', 'deflate'] */ acceptsEncodings(...encodings: string[]): string[] | string | boolean; /** * Return accepted languages or best fit based on `langs`. * * Given `Accept-Language: en;q=0.8, es, pt` * an array sorted by quality is returned: * * ['es', 'pt', 'en'] */ acceptsLanguages(...langs: string[]): string[] | string | boolean; /** * Return request header. * * The `Referrer` header field is special-cased, * both `Referrer` and `Referer` are interchangeable. * * Examples: * * this.get('Content-Type'); * // => "text/plain" * * this.get('content-type'); * // => "text/plain" * * this.get('Something'); * // => undefined */ get(field: string): string | undefined; /** * Check if the incoming request contains the "Content-Type" * header field, and it contains the give mime `type`. * * Examples: * * // With Content-Type: text/html; charset=utf-8 * req.is('html'); * req.is('text/html'); * req.is('text/*'); * // => true * * // When Content-Type is application/json * req.is('json'); * req.is('application/json'); * req.is('application/*'); * // => true * * req.is('html'); * // => false */ is(type: string): string | false; }