@tunframework/tun
Version:
tun framework for node with typescript
209 lines (208 loc) • 7.76 kB
TypeScript
/// <reference types="node" />
import type { IncomingMessage } from 'http';
import { URL } from 'url';
import { RAW_REQUEST } from './constants/http/symbol.js';
export interface TunRequestOptions {
proxy?: boolean;
subdomainOffset?: number;
}
/**
* Wrapped file for formidable
*/
export interface File {
size: number;
path: string;
name: string;
type: string;
lastModifiedDate?: Date;
hash?: string;
toJSON(): Object;
}
export declare class TunRequest {
#private;
[RAW_REQUEST]: IncomingMessage;
/**
* formdata fields
* @see [bodyparser](https://github.com/tunframework/tun-bodyparser)
*/
fields: Record<string, string>;
_fields: Record<string, string | Array<string>>;
/**
* formdata files
* @see [bodyparser](https://github.com/tunframework/tun-bodyparser)
*/
files: Record<string, File>;
_files: Record<string, File | File[]>;
/**
* request body
* @see [bodyparser](https://github.com/tunframework/tun-bodyparser)
*/
body: any;
/**
* @example "/product/abc" matches "/product/:id", ctx.req.slugs.id === "abc"
* @see [rest-router](https://github.com/tunframework/tun-rest-router)
*/
slugs: Record<string, any>;
constructor(req: IncomingMessage, options?: TunRequestOptions);
get header(): import("http").IncomingHttpHeaders;
set header(val: import("http").IncomingHttpHeaders);
get headers(): import("http").IncomingHttpHeaders;
set headers(val: import("http").IncomingHttpHeaders);
/**
*
* http methods
*
* refers: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
*
* Get/Set request method, useful for implementing middleware such as methodOverride().
*
*/
get method(): "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH";
set method(val: "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH");
/**
* Return request Content-Length as a number when preqent, or undefined.
*/
get length(): number | undefined;
get url(): string;
set url(val: string);
get originalUrl(): string | undefined;
/**
* Get origin of URL, include protocol and host.
* @example http://example.com
*/
get origin(): string | undefined;
/**
* Get full request URL, include protocol, host and url.
* @example http://example.com/foo/bar?q=1
*/
get href(): string;
get path(): string;
set path(val: string);
get querystring(): string;
set querystring(val: string);
get search(): string;
set search(val: string);
/**
* Get host (hostname:port) when present.
* Supports X-Forwarded-Host when app.proxy is true,
* otherwise Host is used.
*/
get host(): string | undefined;
/**
* Get hostname when present.
* Supports X-Forwarded-Host when app.proxy is true,
* otherwise Host is used.
*
* //// ignore: If host is IPv6, delegates parsing to WHATWG URL API, Note This may impact performance.
*/
get hostname(): string;
/**
* Get WHATWG parsed URL object.
*
* refers: https://nodebeginner.org/blog/post/nodejs-tutorial-whatwg-url-parser/
*
* url模块现在(node 8.0+)提供了一个额外的实现,它实现了标准化的WHATWG URL API,使得Node.js的url-parsing代码与Web浏览器解析URL的方式相同
*
* 原有的 'url.parse()' Nodejs 被认为
*/
get URL(): URL;
get type(): string;
/**
* Get request charset when present, or undefined:
* @example "utf-8"
*/
get charset(): string | undefined;
get query(): import("url").URLSearchParams;
set query(val: import("url").URLSearchParams);
get protocol(): string | string[];
get secure(): boolean;
/**
* Request remote address. Supports X-Forwarded-For when app.proxy is true.
*/
get ip(): string | undefined;
/**
* When X-Forwarded-For is present and app.proxy is enabled an array of these ips is returned,
* ordered from upstream to downstream.
* When disabled an empty array is returned.
*
* refers: https://support.stackpath.com/hc/en-us/articles/360021658292-Getting-Real-Client-IPs-with-X-Forwarded-For
*/
get ips(): (string | undefined)[];
/**
* 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,
* ctx.subdomains is ["ferrets", "tobi"].
* If app.subdomainOffset is 3, ctx.subdomains is ["tobi"].
*/
get subdomains(): string[];
/**
* Check if the incoming request contains the "Content-Type" header field,
* and it contains any of the give mime types.
* If there is no request body, null is returned.
* If there is no content type, or the match fails false is returned.
* Otherwise, it returns the matching content-type.
*/
is(...types: string[]): string | false;
/**
* Check if the given type(s) is acceptable,
* returning the best match when true, otherwise false.
* The type value may be one or more mime type string such as "application/json",
* the extension name such as "json",
* or an array ["json", "html", "text/plain"].
*
* refers: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Accept
*/
accepts(...types: string[]): string | false;
/**
* Check if encodings are acceptable,
* returning the best match when true, otherwise false.
* Note that you should include identity as one of the encodings!
*
* // Accept-Encoding: gzip
* ctx.acceptsEncodings('gzip', 'deflate', 'identity');
* // "gzip"
*
* ctx.acceptsEncodings(['gzip', 'deflate', 'identity']);
* // "gzip"
*
* Note that the identity encoding (which means no encoding)
* could be unacceptable if the client explicitly sends identity;q=0.
* Although this is an edge case,
* you should still handle the case where this method returns false.
*/
acceptsEncodings(...encodings: Array<string | string[]>): string | boolean | string[];
/**
* Check if charsets are acceptable,
* returning the best match when true, otherwise false.
*/
acceptsCharsets(...charsets: string[]): string | boolean;
/**
* Check if langs are acceptable,
* returning the best match when true,
* otherwise false.
*
* When no arguments are given all accepted languages are returned as an array
*
* refers: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Accept-Language
* @returns return wheather (given) languages acceptable, or all accepted languages (when no arguments are given)
*/
acceptsLanguages(...langs: Array<string | string[]>): string | boolean | string[];
/**
* Check if the request is idempotent.
*
* refers: https://www.cnblogs.com/weidagang2046/archive/2011/06/04/idempotence.html
*/
idempotent(): boolean;
get socket(): import("net").Socket;
/**
* Return request header.
*
* field lower-kebad-case-field-name
*/
get(field: string): string | string[] | undefined;
}