Comparing version
@@ -1,2 +0,3 @@ | ||
import { type InternalRoute, type AnyElysia } from '../..'; | ||
import type { AnyElysia } from '../..'; | ||
import type { InternalRoute } from '../../types'; | ||
export declare const createBunRouteHandler: (app: AnyElysia, route: InternalRoute) => any; |
@@ -24,6 +24,7 @@ "use strict"; | ||
module.exports = __toCommonJS(compose_exports); | ||
var import__ = require("../.."); | ||
var import_sucrose = require("../../sucrose"); | ||
var import_compose = require("../../compose"); | ||
var import_sucrose = require("../../sucrose"); | ||
var import_utils = require("../../utils"); | ||
var import_error = require("../../error"); | ||
var import_trace = require("../../trace"); | ||
const allocateIf = (value, condition) => condition ? value : ""; | ||
@@ -84,4 +85,4 @@ const createContext = (app, route, inference, isInline = false) => { | ||
handler: route.compile?.() ?? route.composed, | ||
redirect: import__.redirect, | ||
error: import__.error, | ||
redirect: import_utils.redirect, | ||
error: import_error.error, | ||
// @ts-expect-error private property | ||
@@ -92,3 +93,3 @@ hoc: app.extender.higherOrderFunctions.map((x) => x.fn), | ||
randomId: hasTrace ? import_utils.randomId : void 0, | ||
ELYSIA_TRACE: hasTrace ? import__.ELYSIA_TRACE : void 0, | ||
ELYSIA_TRACE: hasTrace ? import_trace.ELYSIA_TRACE : void 0, | ||
ELYSIA_REQUEST_ID: hasTrace ? import_utils.ELYSIA_REQUEST_ID : void 0, | ||
@@ -95,0 +96,0 @@ trace: hasTrace ? app.event.trace?.map((x) => x?.fn ?? x) : void 0 |
@@ -135,3 +135,4 @@ "use strict"; | ||
validator, | ||
encodeSchema = false | ||
encodeSchema = false, | ||
accelerators | ||
}) => ({ | ||
@@ -141,8 +142,10 @@ composeValidation: (type, value = `c.${type}`) => `c.set.status=422;throw new ValidationError('${type}',validator.${type},${value})`, | ||
let code = injectResponse + "\n"; | ||
if (accelerators) code += `let accelerate | ||
`; | ||
code += `if(${name} instanceof ElysiaCustomStatusResponse){c.set.status=${name}.code | ||
${name}=${name}.response}const isResponse=${name} instanceof Response | ||
switch(c.set.status){`; | ||
${name}=${name}.response}switch(c.set.status){`; | ||
for (const [status, value] of Object.entries(validator.response)) { | ||
code += ` | ||
case ${status}:if(!isResponse){`; | ||
case ${status}:if(${name} instanceof Response)break | ||
`; | ||
code += composeCleaner({ | ||
@@ -156,8 +159,11 @@ name, | ||
if (encodeSchema && (value.sucrose.hasTransform || typeof value.Encode === "function")) | ||
code += `try{${name}=validator.response['${status}'].Encode(${name}) | ||
c.set.status=${status}}catch{throw new ValidationError('response',validator.response['${status}'],${name})}}`; | ||
code += `try{${name}=validator.response[${status}].Encode(${name}) | ||
c.set.status=${status}}catch{throw new ValidationError('response',validator.response[${status}],${name})}`; | ||
else | ||
code += `if(validator.response['${status}'].Check(${name})===false){c.set.status=422 | ||
throw new ValidationError('response',validator.response['${status}'],${name})}c.set.status=${status}} | ||
code += `if(validator.response[${status}].Check(${name})===false){c.set.status=422 | ||
throw new ValidationError('response',validator.response[${status}],${name})}c.set.status=${status} | ||
`; | ||
if (accelerators?.[+status] && (value.schema.type === "object" || value.schema.type === "array")) | ||
code += `accelerate=accelerators[${status}] | ||
`; | ||
code += "break\n"; | ||
@@ -295,6 +301,8 @@ } | ||
const encodeSchema = app.config.experimental?.encodeSchema; | ||
const accelerators = jsonAccelerator ? (0, import_schema.createAccelerators)(validator.response) : void 0; | ||
const { composeValidation, composeResponseValidation } = composeValidationFactory({ | ||
normalize, | ||
validator, | ||
encodeSchema | ||
encodeSchema, | ||
accelerators | ||
}); | ||
@@ -451,4 +459,13 @@ if (hasHeaders) fnLiteral += adapter.headers; | ||
const maybeStream = (typeof handler === "function" ? isGenerator(handler) : false) || !!hooks.beforeHandle?.some(isGenerator) || !!hooks.afterHandle?.some(isGenerator) || !!hooks.transform?.some(isGenerator); | ||
const hasSet = inference.cookie || inference.set || hasHeaders || hasTrace || !!validator.response || isHandleFn && hasDefaultHeaders || maybeStream; | ||
const responseKeys = Object.keys(validator.response ?? {}); | ||
const hasMultipleResponses = responseKeys.length > 1; | ||
const hasSingle200 = responseKeys.length === 0 || responseKeys.length === 1 && responseKeys[0] === "200"; | ||
const hasSet = inference.cookie || inference.set || hasHeaders || hasTrace || hasMultipleResponses || !hasSingle200 || isHandleFn && hasDefaultHeaders || maybeStream; | ||
const mapResponseContext = adapter.mapResponseContext ? `,${adapter.mapResponseContext}` : ""; | ||
const mapAccelerate = (response = "r", compact = false) => jsonAccelerator ? (saveResponse ? `${saveResponse}${response} | ||
` : "") + `if(accelerate){ | ||
c.set.headers['content-type']='application/json' | ||
` + (compact ? `return mapCompactResponse(accelerate(${response})${mapResponseContext})} | ||
` : `return mapResponse(accelerate(${response}),c.set${mapResponseContext})} | ||
`) : ""; | ||
if (hasTrace || inference.route) fnLiteral += `c.route=\`${path}\` | ||
@@ -938,2 +955,3 @@ `; | ||
mapResponseReporter.resolve(); | ||
fnLiteral += mapAccelerate("r", !hasSet); | ||
if (hasSet) | ||
@@ -979,19 +997,7 @@ fnLiteral += `return mapResponse(${saveResponse}r,c.set${mapResponseContext}) | ||
} else if (hasSet) { | ||
if (jsonAccelerator) { | ||
fnLiteral += `${saveResponse}r | ||
if(typeof r==='object'&&c.set.status in accelerate){ | ||
c.set.headers['content-type']='application/json' | ||
return mapResponse(accelerate[c.set.status](r),c.set${mapResponseContext})} | ||
`; | ||
} | ||
fnLiteral += mapAccelerate(); | ||
fnLiteral += `return mapResponse(${saveResponse}r,c.set${mapResponseContext}) | ||
`; | ||
} else { | ||
if (jsonAccelerator) { | ||
fnLiteral += `${saveResponse}r | ||
if(typeof r==='object'&&c.set.status in accelerate){ | ||
c.set.headers['content-type']='application/json' | ||
return mapResponse(accelerate[c.set.status](r),c.set${mapResponseContext})} | ||
`; | ||
} | ||
fnLiteral += mapAccelerate("r", true); | ||
fnLiteral += `return mapCompactResponse(${saveResponse}r${mapResponseContext}) | ||
@@ -1023,9 +1029,3 @@ `; | ||
fnLiteral += encodeCookie; | ||
if (jsonAccelerator) { | ||
fnLiteral += `${saveResponse}r | ||
if(typeof r==='object'&&c.set.status in accelerate){ | ||
c.set.headers['content-type']='application/json' | ||
return mapResponse(accelerate[c.set.status](r),c.set${mapResponseContext})} | ||
`; | ||
} | ||
fnLiteral += mapAccelerate("r", !hasSet); | ||
if (hasSet) | ||
@@ -1047,19 +1047,7 @@ fnLiteral += `return mapResponse(${saveResponse}r,c.set${mapResponseContext}) | ||
} else if (hasSet) { | ||
if (jsonAccelerator) { | ||
fnLiteral += `${saveResponse}${handled} | ||
if(typeof ${handled}==='object'&&c.set.status in accelerate){ | ||
c.set.headers['content-type']='application/json' | ||
return mapResponse(accelerate[c.set.status](${handled}),c.set${mapResponseContext})} | ||
`; | ||
} | ||
fnLiteral += mapAccelerate(handled); | ||
fnLiteral += `return mapResponse(${saveResponse}${handled},c.set${mapResponseContext}) | ||
`; | ||
} else { | ||
if (jsonAccelerator) { | ||
fnLiteral += `${saveResponse}${handled} | ||
if(typeof ${handled}==='object'&&c.set.status in accelerate){ | ||
c.set.headers['content-type']='application/json' | ||
return mapResponse(accelerate[c.set.status](${handled}),c.set${mapResponseContext})} | ||
`; | ||
} | ||
fnLiteral += mapAccelerate(handled, true); | ||
fnLiteral += `return mapCompactResponse(${saveResponse}${handled}${mapResponseContext}) | ||
@@ -1160,3 +1148,3 @@ `; | ||
const adapterVariables = adapter.inject ? Object.keys(adapter.inject).join(",") + "," : ""; | ||
let init = `const {handler,handleError,hooks:e, ` + allocateIf(`validator,`, hasValidation) + `mapResponse,mapCompactResponse,mapEarlyResponse,isNotEmpty,utils:{` + allocateIf(`parseQuery,`, hasBody) + allocateIf(`parseQueryFromURL,`, hasQuery) + `},error:{` + allocateIf(`ValidationError,`, hasValidation) + `InternalServerError,` + allocateIf(`ParseError`, hasBody) + `},schema,definitions,ERROR_CODE,` + allocateIf(`parseCookie,`, hasCookie) + allocateIf(`signCookie,`, hasCookie) + allocateIf(`decodeURIComponent,`, hasQuery) + `ElysiaCustomStatusResponse,` + allocateIf(`ELYSIA_TRACE,`, hasTrace) + allocateIf(`ELYSIA_REQUEST_ID,`, hasTrace) + allocateIf("parser,", hooks.parse?.length) + allocateIf(`getServer,`, inference.server) + allocateIf("accelerate,", jsonAccelerator) + adapterVariables + allocateIf("TypeBoxError", hasValidation) + `}=hooks | ||
let init = `const {handler,handleError,hooks:e, ` + allocateIf(`validator,`, hasValidation) + `mapResponse,mapCompactResponse,mapEarlyResponse,isNotEmpty,utils:{` + allocateIf(`parseQuery,`, hasBody) + allocateIf(`parseQueryFromURL,`, hasQuery) + `},error:{` + allocateIf(`ValidationError,`, hasValidation) + `InternalServerError,` + allocateIf(`ParseError`, hasBody) + `},schema,definitions,ERROR_CODE,` + allocateIf(`parseCookie,`, hasCookie) + allocateIf(`signCookie,`, hasCookie) + allocateIf(`decodeURIComponent,`, hasQuery) + `ElysiaCustomStatusResponse,` + allocateIf(`ELYSIA_TRACE,`, hasTrace) + allocateIf(`ELYSIA_REQUEST_ID,`, hasTrace) + allocateIf("parser,", hooks.parse?.length) + allocateIf(`getServer,`, inference.server) + allocateIf("accelerators,", jsonAccelerator) + adapterVariables + allocateIf("TypeBoxError", hasValidation) + `}=hooks | ||
const trace=e.trace | ||
@@ -1207,3 +1195,3 @@ return ${maybeAsync ? "async " : ""}function handle(c){`; | ||
parser: app["~parser"], | ||
accelerate: jsonAccelerator ? (0, import_schema.createAccelerators)(validator.response) : void 0, | ||
accelerators, | ||
...adapter.inject | ||
@@ -1210,0 +1198,0 @@ }); |
{ | ||
"name": "elysia", | ||
"description": "Ergonomic Framework for Human", | ||
"version": "1.3.0-exp.7", | ||
"version": "1.3.0-exp.8", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "saltyAom", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
19173992
032392
-0.12%