@faapi/faapi 1.0.2 → 1.1.0-canary.e781003

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -119,6 +119,13 @@ interface FaapiContext {
119
119
  * 无法获取时为空字符串。
120
120
  */
121
121
  ip: string;
122
+ /**
123
+ * 客户端 User-Agent(请求头 `user-agent` 原值)
124
+ *
125
+ * 在 createContext 内部从 request.headers 读取(与 ip 不同,无需调用方传入)。
126
+ * 不做解析/规整,仅原样透传;无该请求头时为空字符串。
127
+ */
128
+ ua: string;
122
129
  /** 解析后的所有 cookie 键值对 */
123
130
  cookies: Record<string, string>;
124
131
  /** 配置文件中的自定义业务配置(类型可通过 declare module '@faapi/faapi' 增强 FaapiContextConfig) */
@@ -1105,6 +1112,9 @@ type ValidationErrorCode = 'TYPE_MISMATCH' | 'MISSING_FIELD' | 'INVALID_FORMAT'
1105
1112
  * @param params 动态路由参数
1106
1113
  * @param config 自定义业务配置(来自 faapi.config.ts)
1107
1114
  * @param ip 客户端 IP(由调用方从 IncomingMessage 提取,HTTP/WS 握手均通过 utils/getClientIp)
1115
+ *
1116
+ * ua 不作为参数传入:User-Agent 是标准 HTTP 请求头,createContext 内部直接从
1117
+ * request.headers 读取(与 ip 不同,ip 需要从 IncomingMessage 提取故由调用方传入)。
1108
1118
  */
1109
1119
  declare function createContext(request: Request, params: Record<string, string>, config?: Record<string, unknown>, ip?: string): FaapiContext;
1110
1120
 
package/dist/index.js CHANGED
@@ -683,6 +683,7 @@ var PARAM_TYPE_MAP = {
683
683
  // 别名
684
684
  cookies: "cookies",
685
685
  ip: "ip",
686
+ ua: "ua",
686
687
  files: "files",
687
688
  fields: "fields"
688
689
  };
@@ -1330,6 +1331,7 @@ function createContext(request, params, config = {}, ip = "") {
1330
1331
  method: request.method,
1331
1332
  path: url.pathname,
1332
1333
  ip,
1334
+ ua: request.headers.get("user-agent") ?? "",
1333
1335
  cookies: cookiesObj,
1334
1336
  config,
1335
1337
  meta,
@@ -1519,6 +1521,8 @@ function getBuiltinInjectionValue(type, ctx, body) {
1519
1521
  return ctx.cookies;
1520
1522
  case "ip":
1521
1523
  return ctx.ip;
1524
+ case "ua":
1525
+ return ctx.ua;
1522
1526
  case "body":
1523
1527
  return body;
1524
1528
  // form 与 body 共享解析结果(resolveInput 已按 Content-Type 解析 form-urlencoded)