swc/crates/swc_bundler/tests/.cache/deno/5abe6602338b58d3b1513f0c9284a709047f993e.ts
2021-11-09 20:42:49 +09:00

53 lines
1.2 KiB
TypeScript

// Loaded from https://deno.land/x/tinyhttp@0.1.18/extensions/res/redirect.ts
import { NextFunction } from 'https://esm.sh/@tinyhttp/router'
import { formatResponse } from './format.ts'
import { setLocationHeader } from './headers.ts'
import { Req, Res } from '../../deps.ts'
import { status as getStatus } from 'https://deno.land/x/status@0.1.0/status.ts'
import { escapeHtml } from '../../deps.ts'
export const redirect = <
Request extends Req = Req,
Response extends Res = Res,
Next extends NextFunction = NextFunction
>(
req: Request,
res: Response,
next: Next
) => (url: string, status = 302) => {
let address = url
let body = ''
address = setLocationHeader(req, res)(address).headers?.get('Location') as string
formatResponse(
req,
res,
next
)({
text: () => {
body = getStatus(status) + '. Redirecting to ' + address
},
html: () => {
const u = escapeHtml(address)
body = `<p>${getStatus(status)}. Redirecting to <a href="${u}">${u}</a></p>`
}
})
res.headers.set('Content-Length', body.length.toString())
if (req.method === 'HEAD') req.respond({ status })
else
req.respond({
body,
status,
...res
})
return res
}