-
-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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 | undefined export 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.
fmt
to fix the code format before commit.CHANGELOG.md
.