feat: new middleware redirect_as2#632
Conversation
|
@oscarotero For this middleware, what do you think about renaming it to export type Options = (url: URL) => Promise<URL> | URL | undefinedexport default (options: Options): Middleware => async (req, next) => {
if (req.headers.get('accept')?.includes("application/activity+json")) {
const dest = await options(req.url)
if (dest) return Response.redirect(dest);
}
return await next(req);
};// manual
server.use(redirectAS2(url => new URL(url.href, 'https://hatsu.local/posts/')))// hatsu preset
export const hatsu = (instance: string) => (url: URL) => new URL(`${url.origin}${url.pathname}`, `https://${instance}/posts/`)
import Server from 'lume/core/server.ts'
import redirectAS2, { hatsu } from 'lume/middlewares/redirect_as2.ts'
const server = new Server()
server.use(redirectAS2(hatsu('hatsu.local')))This would also be compatible with Bridgy Fed (not sure of the effect since I'm not currently using it): // bridgy-fed preset
export const bridgyFed = () => (url: URL) => new URL(`${url.origin}${url.pathname}`, 'https://fed.brid.gy/r/')
import Server from 'lume/core/server.ts'
import redirectAS2, { bridgyFed } from 'lume/middlewares/redirect_as2.ts'
const server = new Server()
server.use(redirectAS2(bridgyFed())) |
|
Yep, I like the idea! For consistency with other middlewares, I'd use an options object as the first argument, instead a function. server.use(redirectAS2({
dest: new URL("https://hatsu.local/posts/"),
}));If the logic for calculate the final destination is different for hatsu and bridgy, maybe it can be configured in the server.use(redirectAS2({
dest: new URL("https://hatsu.local/posts/"),
mode: "hatsu"
}));(or automatically detect it from the domain name). |
Using functions allows users who need it to write custom logic, for example: server.use(redirectAS2(url => {
const { origin, pathname } = new URL(URL)
if (pathname === '/') return new URL('https:/hatsu.local/users/example.com')
else if (pathname.includes('/posts/') return new URL(`${origin}${pathname}`, 'https://hatsu.local/posts/')
})) |
Yes, I know. My only concern is about the value provided by this middleware. Internally, it only has an |
If the URL is provided directly, options like |
|
Okay, let's start with the function and we can see later if an url option makes sense. |
I have updated PR. |
|
Great. |
Description
This is an updated version of the same middleware in Aoba.
Check List
CODE OF CONDUCT
CONTRIBUTING
send multiple pull request.
fmtto fix the code format before commit.CHANGELOG.md.