Comparing version 0.4.1 to 0.4.2
@@ -1,1 +0,1 @@ | ||
export const isNotEmpty=e=>{for(let s in e)return!0;return!1};let e=(e,s)=>{if(Array.isArray(s)){e.delete("Set-Cookie");for(let r=0;r<s.length;r++){let t=s[r].indexOf("=");e.append("Set-Cookie",`${s[r].slice(0,t)}=${s[r].slice(t+1)}`)}}return e};export const mapEarlyResponse=(s,r)=>{if(isNotEmpty(r.headers)||200!==r.status||r.redirect)switch(r.redirect&&(r.headers.Location=r.redirect,r.status=302),r.headers["Set-Cookie"]&&(r.headers=e(new Headers(r.headers),r.headers["Set-Cookie"])),typeof s){case"string":return new Response(s,{status:r.status,headers:r.headers});case"object":if(s instanceof Error)return errorToResponse(s,r.headers);if(s instanceof Response){for(let e in r.headers)s.headers.append(e,r.headers[e]);return s}if(s instanceof Blob)return new Response(s,{status:r.status,headers:r.headers});return r.headers["Content-Type"]||(r.headers["Content-Type"]="application/json"),new Response(JSON.stringify(s),{status:r.status,headers:r.headers});case"function":if(s instanceof Blob)return new Response(s,{status:r.status,headers:r.headers});for(let e in r.headers)s.headers.append(e,r.headers[e]);return s;case"number":case"boolean":return new Response(s.toString(),{status:r.status,headers:r.headers})}else switch(typeof s){case"string":return new Response(s);case"object":if(s instanceof Response)return s;if(s instanceof Error)return errorToResponse(s,r.headers);if(s instanceof Blob)return new Response(s);return new Response(JSON.stringify(s),{headers:{"content-type":"application/json"}});case"function":if(s instanceof Blob)return new Response(s);return s;case"number":case"boolean":return new Response(s.toString())}};export const mapResponse=(s,r)=>{if(Object.keys(r.headers).length||200!==r.status||r.redirect)switch(r.redirect&&(r.headers.Location=r.redirect,r.status=302),r.headers?.["Set-Cookie"]&&(r.headers=e(new Headers(r.headers),r.headers["Set-Cookie"])),typeof s){case"string":return new Response(s,{status:r.status,headers:r.headers});case"object":switch(s?.constructor){case Error:return errorToResponse(s,r.headers);case Response:for(let e in r.headers)s.headers.append(e,r.headers[e]);return s;case Blob:return new Response(s,{status:r.status,headers:r.headers});default:return r.headers["Content-Type"]||(r.headers["Content-Type"]="application/json"),new Response(JSON.stringify(s),{status:r.status,headers:r.headers})}case"function":if(s instanceof Blob)return new Response(s,{status:r.status,headers:r.headers});return s();case"number":case"boolean":return new Response(s.toString(),{status:r.status,headers:r.headers});case"undefined":return new Response("",{status:r.status,headers:r.headers});default:return new Response(s,{status:r.status,headers:r.headers})}else switch(typeof s){case"string":default:return new Response(s);case"object":if(s instanceof Response)return s;if(s instanceof Error)return errorToResponse(s,r.headers);if(s instanceof Blob)return new Response(s);return new Response(JSON.stringify(s),{headers:{"content-type":"application/json"}});case"function":if(s instanceof Blob)return new Response(s);return s();case"number":case"boolean":return new Response(s.toString());case"undefined":return new Response("")}};export const errorToResponse=(e,s)=>new Response(JSON.stringify({name:e?.name,message:e?.message,cause:e?.cause}),{status:500,headers:s}); | ||
export const isNotEmpty=e=>{for(let s in e)return!0;return!1};let e=(e,s)=>{if(Array.isArray(s)){e.delete("Set-Cookie");for(let r=0;r<s.length;r++){let t=s[r].indexOf("=");e.append("Set-Cookie",`${s[r].slice(0,t)}=${s[r].slice(t+1)}`)}}return e};export const mapEarlyResponse=(s,r)=>{if(isNotEmpty(r.headers)||200!==r.status||r.redirect)switch(r.redirect&&(r.headers.Location=r.redirect,r.status=302),r.headers["Set-Cookie"]&&(r.headers=e(new Headers(r.headers),r.headers["Set-Cookie"])),typeof s){case"string":return new Response(s,{status:r.status,headers:r.headers});case"object":switch(s?.constructor){case Error:return errorToResponse(s,r.headers);case Response:for(let e in r.headers)s.headers.append(e,r.headers[e]);return s;case Blob:return new Response(s,{status:r.status,headers:r.headers});case Promise:return s.then(e=>{let s=mapEarlyResponse(e,r);if(void 0!==s)return s});default:return r.headers["Content-Type"]||(r.headers["Content-Type"]="application/json"),new Response(JSON.stringify(s),{status:r.status,headers:r.headers})}case"function":if(s instanceof Blob)return new Response(s,{status:r.status,headers:r.headers});for(let e in r.headers)s.headers.append(e,r.headers[e]);return s;case"number":case"boolean":return new Response(s.toString(),{status:r.status,headers:r.headers})}else switch(typeof s){case"string":return new Response(s);case"object":switch(s?.constructor){case Error:return errorToResponse(s,r.headers);case Response:return s;case Blob:return new Response(s);case Promise:return s.then(e=>{let s=mapEarlyResponse(e,r);if(void 0!==s)return s});default:return new Response(JSON.stringify(s),{headers:{"content-type":"application/json"}})}case"function":if(s instanceof Blob)return new Response(s);return s;case"number":case"boolean":return new Response(s.toString())}};export const mapResponse=(s,r)=>{if(Object.keys(r.headers).length||200!==r.status||r.redirect)switch(r.redirect&&(r.headers.Location=r.redirect,r.status=302),r.headers?.["Set-Cookie"]&&(r.headers=e(new Headers(r.headers),r.headers["Set-Cookie"])),typeof s){case"string":return new Response(s,{status:r.status,headers:r.headers});case"object":switch(s?.constructor){case Error:return errorToResponse(s,r.headers);case Response:for(let e in r.headers)s.headers.append(e,r.headers[e]);return s;case Blob:return new Response(s,{status:r.status,headers:r.headers});case Promise:return s.then(e=>mapResponse(e,r));default:return r.headers["Content-Type"]||(r.headers["Content-Type"]="application/json"),new Response(JSON.stringify(s),{status:r.status,headers:r.headers})}case"function":if(s instanceof Blob)return new Response(s,{status:r.status,headers:r.headers});return s();case"number":case"boolean":return new Response(s.toString(),{status:r.status,headers:r.headers});case"undefined":return new Response("",{status:r.status,headers:r.headers});default:return new Response(s,{status:r.status,headers:r.headers})}else switch(typeof s){case"string":return new Response(s);case"object":switch(s?.constructor){case Error:return errorToResponse(s,r.headers);case Response:return s;case Blob:return new Response(s);case Promise:return s.then(e=>mapResponse(e,r));default:return new Response(JSON.stringify(s),{headers:{"content-type":"application/json"}})}case"function":if(s instanceof Blob)return new Response(s);return s();case"number":case"boolean":return new Response(s.toString());case"undefined":return new Response("");default:return new Response(s)}};export const errorToResponse=(e,s)=>new Response(JSON.stringify({name:e?.name,message:e?.message,cause:e?.cause}),{status:500,headers:s}); |
{ | ||
"name": "elysia", | ||
"description": "Fast, and friendly Bun web framework", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "saltyAom", |
@@ -0,1 +1,3 @@ | ||
/* eslint-disable no-case-declarations */ | ||
import type { MaybePromise } from './types' | ||
import type { Context } from './context' | ||
@@ -29,3 +31,6 @@ | ||
// We don't want to assign new variable to be used only once here | ||
export const mapEarlyResponse = (response: unknown, set: Context['set']) => { | ||
export const mapEarlyResponse = ( | ||
response: unknown, | ||
set: Context['set'] | ||
): Response | undefined => { | ||
if (isNotEmpty(set.headers) || set.status !== 200 || set.redirect) { | ||
@@ -52,24 +57,41 @@ if (set.redirect) { | ||
case 'object': | ||
if (response instanceof Error) | ||
return errorToResponse(response, set.headers) | ||
if (response instanceof Response) { | ||
for (const key in set.headers) | ||
response.headers.append(key, set.headers[key]) | ||
switch (response?.constructor) { | ||
case Error: | ||
return errorToResponse(response as Error, set.headers) | ||
return response | ||
} | ||
if (response instanceof Blob) | ||
return new Response(response, { | ||
status: set.status, | ||
headers: set.headers | ||
}) | ||
case Response: | ||
for (const key in set.headers) | ||
(response as Response)!.headers.append( | ||
key, | ||
set.headers[key] | ||
) | ||
if (!set.headers['Content-Type']) | ||
set.headers['Content-Type'] = 'application/json' | ||
return response as Response | ||
return new Response(JSON.stringify(response), { | ||
status: set.status, | ||
headers: set.headers | ||
}) | ||
case Blob: | ||
return new Response(response as Blob, { | ||
status: set.status, | ||
headers: set.headers | ||
}) | ||
case Promise: | ||
// @ts-ignore | ||
return (response as Promise<unknown>).then((x) => { | ||
const r = mapEarlyResponse(x, set) | ||
if (r !== undefined) return r | ||
return | ||
}) | ||
default: | ||
if (!set.headers['Content-Type']) | ||
set.headers['Content-Type'] = 'application/json' | ||
return new Response(JSON.stringify(response), { | ||
status: set.status, | ||
headers: set.headers | ||
}) | ||
} | ||
// ? Maybe response or Blob | ||
@@ -107,13 +129,30 @@ case 'function': | ||
case 'object': | ||
if (response instanceof Response) return response | ||
if (response instanceof Error) | ||
return errorToResponse(response, set.headers) | ||
if (response instanceof Blob) return new Response(response) | ||
switch (response?.constructor) { | ||
case Error: | ||
return errorToResponse(response as Error, set.headers) | ||
return new Response(JSON.stringify(response), { | ||
headers: { | ||
'content-type': 'application/json' | ||
} | ||
}) | ||
case Response: | ||
return response as Response | ||
case Blob: | ||
return new Response(response as Blob) | ||
case Promise: | ||
// @ts-ignore | ||
return (response as Promise<unknown>).then((x) => { | ||
const r = mapEarlyResponse(x, set) | ||
if (r !== undefined) return r | ||
return | ||
}) | ||
default: | ||
return new Response(JSON.stringify(response), { | ||
headers: { | ||
'content-type': 'application/json' | ||
} | ||
}) | ||
} | ||
// ? Maybe response or Blob | ||
@@ -178,2 +217,6 @@ case 'function': | ||
case Promise: | ||
// @ts-ignore | ||
return response.then((x) => mapResponse(x, set)) | ||
default: | ||
@@ -224,13 +267,24 @@ if (!set.headers['Content-Type']) | ||
case 'object': | ||
if (response instanceof Response) return response | ||
if (response instanceof Error) | ||
return errorToResponse(response, set.headers) | ||
if (response instanceof Blob) return new Response(response) | ||
switch (response?.constructor) { | ||
case Error: | ||
return errorToResponse(response as Error, set.headers) | ||
return new Response(JSON.stringify(response), { | ||
headers: { | ||
'content-type': 'application/json' | ||
} | ||
}) | ||
case Response: | ||
return response as Response | ||
case Blob: | ||
return new Response(response as Blob) | ||
case Promise: | ||
// @ts-ignore | ||
return response.then((x) => mapResponse(x, set)) | ||
default: | ||
return new Response(JSON.stringify(response), { | ||
headers: { | ||
'content-type': 'application/json' | ||
} | ||
}) | ||
} | ||
// ? Maybe response or Blob | ||
@@ -237,0 +291,0 @@ case 'function': |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
201053
5193