@longzai-intelligence-transport/http-adapter-bun 0.2.1 → 0.2.3
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 +0 -2
- package/dist/index.js +1 -1
- package/package.json +3 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { ApiResponse, InferRouteBody, InferRouteParams, InferRouteQuery, InferRouteResponse, RouteDefinition, StreamResponseRoute } from "@longzai-intelligence-transport/http-core";
|
|
2
|
-
//#region src/index.d.ts
|
|
3
2
|
/**
|
|
4
3
|
* 验证错误详情
|
|
5
4
|
*
|
|
@@ -389,5 +388,4 @@ declare function createRouteMap<TRoutes extends Record<string, RouteDefinition>,
|
|
|
389
388
|
* @returns fetch 处理函数
|
|
390
389
|
*/
|
|
391
390
|
declare function createFetchHandler<TContext = unknown>(routeMap: Record<string, Record<string, BunRouteHandler<TContext>>>, options?: FetchHandlerOptions): FetchHandlerFn;
|
|
392
|
-
//#endregion
|
|
393
391
|
export { type BunRequestContext, type BunRouteHandler, type BunRouteHandlerOptions, type FetchHandlerFn, type FetchHandlerOptions, type InferStreamRouteResponse, type PathToRegexResult, type RouteHandlerFn, type RouteHandlerResult, type RouteMapEntry, type RoutesMap, type ValidatedRouteContext, type ValidationErrorData, type ValidationErrorDetail, createFetchHandler, createRouteHandler, createRouteMap, extractPathParams, extractQueryParams, parseRequestBody, pathToRegex };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createValidationErrorResponseFromError as e,validateRequest as t}from"@longzai-intelligence-transport/http-core";function n(e){let t=[],n=/:([^/]+)/g,r=n.exec(e);for(;r!==null;)r[1]&&t.push(r[1]),r=n.exec(e);return t}function r(e){let t=n(e),r=e.replace(/:([^/]+)/g,`(?<$1>[^/]+)`);return{regex:RegExp(`^${r}$`),paramNames:t}}function i(e,t){let{regex:n}=r(t),i=new URL(e).pathname,a=n.exec(i);return!a||!a.groups?{}:a.groups}function a(e){let t=new URL(e.url),n={};return t.searchParams.forEach((e,t)=>{let r=n[t];r===void 0?n[t]=e:typeof r==`string`?n[t]=[r,e]:r.push(e)}),n}async function o(e){let t=e.headers.get(`content-type`);if(t){if(t.includes(`application/json`))try{return await e.json()}catch{return}if(t.includes(`multipart/form-data`)
|
|
1
|
+
import{createValidationErrorResponseFromError as e,validateRequest as t}from"@longzai-intelligence-transport/http-core";function n(e){let t=[],n=/:([^/]+)/g,r=n.exec(e);for(;r!==null;)r[1]&&t.push(r[1]),r=n.exec(e);return t}function r(e){let t=n(e),r=e.replace(/:([^/]+)/g,`(?<$1>[^/]+)`);return{regex:RegExp(`^${r}$`),paramNames:t}}function i(e,t){let{regex:n}=r(t),i=new URL(e).pathname,a=n.exec(i);return!a||!a.groups?{}:a.groups}function a(e){let t=new URL(e.url),n={};return t.searchParams.forEach((e,t)=>{let r=n[t];r===void 0?n[t]=e:typeof r==`string`?n[t]=[r,e]:r.push(e)}),n}async function o(e){let t=e.headers.get(`content-type`);if(t){if(t.includes(`application/json`))try{return await e.json()}catch{return}if(t.includes(`multipart/form-data`)||t.includes(`application/x-www-form-urlencoded`)){let t=await e.formData(),n={};return t.forEach((e,t)=>{n[t]=e}),n}if(t.includes(`text/`))return await e.text()}}function s(e){let t=e instanceof Error?e.message:`服务器内部错误`;return new Response(JSON.stringify({success:!1,error:{code:`INTERNAL_ERROR`,message:t},message:t,timestamp:Date.now()}),{status:500,headers:{"Content-Type":`application/json`}})}async function c(e,t,n,r,i,a){let o={request:t,params:n,query:r,body:i};a?.createContext&&(o.context=await a.createContext(o));let s=await e(o);return s instanceof ReadableStream?new Response(s,{status:200,headers:{"Content-Type":`application/octet-stream`}}):new Response(JSON.stringify(s),{status:200,headers:{"Content-Type":`application/json`}})}function l(n,r,l){return async u=>{try{let d=i(u.url,n.path),f=a(u),p=await o(u),m=t(n,{params:d,query:f,body:p});if(!m.success){if(!m.error)return s(/* @__PURE__ */ Error(`验证失败但未提供错误详情`));if(l?.onValidationError)return await l.onValidationError(m.error,u);let t=e(m.error);return new Response(JSON.stringify(t),{status:400,headers:{"Content-Type":`application/json`}})}if(l?.beforeHandle){let e={request:u,params:m.params??{},query:m.query??{},body:m.body},t=await l.beforeHandle(e,n);if(t instanceof Response)return t}return await c(r,u,m.params??{},m.query??{},m.body,l)}catch(e){return l?.onError?l.onError(e,u):s(e)}}}function u(e,t,n){let r={};for(let[i,a]of Object.entries(e)){let e=t[i];if(e){let t=l(a,e,n),i=a.path;r[i]||(r[i]={}),r[i][a.method]=t}}return r}function d(e,t){return async n=>{let r=e[new URL(n.url).pathname];if(!r)return t?.onNotFound?t.onNotFound(n):new Response(JSON.stringify({success:!1,error:{code:`NOT_FOUND`,message:`资源不存在`},message:`资源不存在`,timestamp:Date.now()}),{status:404,headers:{"Content-Type":`application/json`}});let i=r[n.method];return i?i(n):t?.onMethodNotAllowed?t.onMethodNotAllowed(n):new Response(JSON.stringify({success:!1,error:{code:`METHOD_NOT_ALLOWED`,message:`方法不允许`},message:`方法不允许`,timestamp:Date.now()}),{status:405,headers:{"Content-Type":`application/json`}})}}export{d as createFetchHandler,l as createRouteHandler,u as createRouteMap,i as extractPathParams,a as extractQueryParams,o as parseRequestBody,r as pathToRegex};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@longzai-intelligence-transport/http-adapter-bun",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"adapter",
|
|
6
6
|
"bun",
|
|
@@ -26,10 +26,7 @@
|
|
|
26
26
|
"build": "lzi-builder",
|
|
27
27
|
"build:prod": "NODE_ENV=production bun run build",
|
|
28
28
|
"prepublishOnly": "bun run build:prod",
|
|
29
|
-
"typecheck": "
|
|
30
|
-
"typecheck:app": "lzi-tsgo typecheck tsconfig/app.json",
|
|
31
|
-
"typecheck:node": "lzi-tsgo typecheck tsconfig/node.json",
|
|
32
|
-
"typecheck:test": "lzi-tsgo typecheck tsconfig/test.json",
|
|
29
|
+
"typecheck": "lzi-typecheck",
|
|
33
30
|
"lint": "oxlint && oxfmt --check",
|
|
34
31
|
"lint:fix": "oxlint --fix && oxfmt",
|
|
35
32
|
"test": "bun test",
|
|
@@ -38,7 +35,7 @@
|
|
|
38
35
|
"clean": "lzi-dev-cli clean"
|
|
39
36
|
},
|
|
40
37
|
"dependencies": {
|
|
41
|
-
"@longzai-intelligence-transport/http-core": "0.2.
|
|
38
|
+
"@longzai-intelligence-transport/http-core": "0.2.3"
|
|
42
39
|
},
|
|
43
40
|
"devDependencies": {
|
|
44
41
|
"zod": "^4.4.3"
|