-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhatsu.ts
51 lines (47 loc) · 1.34 KB
/
hatsu.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import type { Middleware } from 'lume/core/server.ts'
import type { Options as PluginOptions } from '../plugins/hatsu.ts'
export interface Options extends PluginOptions {
/**
* Lume Site Location.
* @defaultValue
* ```ts
* new URL(req.url).host
* ```
* @example
* ```ts
* const site = lume({ location: new URL('https://example.com') })
* const server = new Server()
* server.use(hatsuMiddleware({
* instance: new URL('https://hatsu.local'),
* location: site.options.location,
* }))
* ```
*/
location?: URL
}
export const hatsuMiddleware =
(options: Options): Middleware => async (req, next) => {
const accept = req.headers.get('accept')
const { origin, pathname, search } = new URL(req.url)
// redirect .well-known
if (pathname.startsWith('/.well-known/')) {
return Response.redirect(
new URL(pathname + search, options.instance),
)
} else if (
// redirect application/activity+json request
accept?.includes('application/activity+json') &&
(!options.match ||
options.match.some((matcher) => pathname.match(matcher)))
) {
return Response.redirect(
new URL(
`/posts/${origin + pathname}`,
options.instance,
),
)
} else {
return await next(req)
}
}
export default hatsuMiddleware