-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhatsu.ts
51 lines (48 loc) · 1.3 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 { createMiddleware } from 'hono/helper.ts'
export interface Options {
/**
* Hatsu Instance URL.
* @example
* ```ts
* new URL('https://hatsu.local')
* ```
*/
instance: URL
}
/**
* Redirects `.well-known/*` to a Hatsu instance.
* @example
* ```ts
* import { Hono } from 'hono'
* const app = new Hono()
* app.use('/.well-known/*', hatsuWellKnown({
* instance: new URL('https://hatsu.local'),
* }))
* ```
*/
export const hatsuWellKnown = ({ instance }: Options) =>
// deno-lint-ignore require-await
createMiddleware(async (c) => {
const { pathname, search } = new URL(c.req.url)
return c.redirect(new URL(pathname + search, instance).href, 308)
})
/**
* Redirects `activity+json` requests to the AS2 object of the Hatsu instance.
* @example
* ```ts
* import { Hono } from 'hono'
* const app = new Hono()
* app.use('/posts/*', hatsuObject({
* instance: new URL('https://hatsu.local')
* }))
* ```
*/
export const hatsuObject = ({ instance }: Options) =>
createMiddleware(async (c, next) => {
const accept = c.req.header('Accept')
const url = c.req.url.includes('?') ? c.req.url.split('?')[0] : c.req.url
if (accept?.includes('application/activity+json')) {
return c.redirect(new URL(`/posts/${url}`, instance).href, 308)
}
await next()
})