Skip to content

Commit 7fb2bca

Browse files
committed
style: update indentation from tabs to 2 spaces
- Update eslint config to enforce 2 spaces indentation - Format all TypeScript files to use 2 spaces instead of tabs - Maintain consistent code style across the codebase Signed-off-by: Hanchin Hsieh <me@yuchanns.xyz>
1 parent dcfc0e4 commit 7fb2bca

File tree

7 files changed

+132
-132
lines changed

7 files changed

+132
-132
lines changed

eslint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default tseslint.config(
1010
{
1111
ignores: ['worker-configuration.d.ts', '.wrangler/**', 'node_modules/**', 'build/**'],
1212
rules: {
13-
"indent": ["error", "tab"],
13+
"indent": ["error", 2],
1414
"quotes": ["error", "double"],
1515
"semi": ["error", "never"],
1616
"@typescript-eslint/no-unused-vars": [

src/apis/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import apiWeather from "./weather"
22

33
export const providers: APIProvider[] = [
4-
apiWeather,
5-
// ...add your providers here
4+
apiWeather,
5+
// ...add your providers here
66
]

src/apis/weather.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,37 @@ import { Hono } from "hono/tiny"
33
const route = new Hono()
44

55
const apiWeather: APIProvider = {
6-
name: "getWeather",
7-
path: "weather",
8-
route: route,
9-
description: "Get the weather based on the given location.",
10-
parameters: {
11-
type: "object",
12-
properties: {
13-
location: {
14-
type: "string",
15-
description: "The location to get the weather for.",
16-
}
17-
},
18-
required: ["location"],
19-
},
6+
name: "getWeather",
7+
path: "weather",
8+
route: route,
9+
description: "Get the weather based on the given location.",
10+
parameters: {
11+
type: "object",
12+
properties: {
13+
location: {
14+
type: "string",
15+
description: "The location to get the weather for.",
16+
}
17+
},
18+
required: ["location"],
19+
},
2020
}
2121

2222
route.get("/*", (c) => c.text("Please use POST method to get the weather."))
2323

2424
route.post("/*", async (c) => {
25-
const { location } = (await c.req.json()) as { location: string }
26-
const url = `https://wttr.in/${location}?format=j1`
25+
const { location } = (await c.req.json()) as { location: string }
26+
const url = `https://wttr.in/${location}?format=j1`
2727

28-
const response = await fetch(url, {
29-
headers: {
30-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
31-
}
32-
})
33-
if (response.status !== 200) {
34-
throw new Error(`Failed to get weather for ${location}.`)
35-
}
36-
return response
28+
const response = await fetch(url, {
29+
headers: {
30+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
31+
}
32+
})
33+
if (response.status !== 200) {
34+
throw new Error(`Failed to get weather for ${location}.`)
35+
}
36+
return response
3737
})
3838

3939
export default apiWeather

src/gateway.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
import { Hono } from "hono/tiny"
22
import {
3-
createErrorResponse,
4-
PluginErrorType,
5-
PluginRequestPayload
3+
createErrorResponse,
4+
PluginErrorType,
5+
PluginRequestPayload
66
} from "@lobehub/chat-plugin-sdk"
77

88
export const apiGateway = new Hono<{ Bindings: Bindings }>()
99
// @ts-expect-error No overload matches this call
10-
.post("/", async (c) => {
11-
const data = await c.req.json() as PluginRequestPayload
12-
const u = new URL(c.req.url)
13-
const apiUrl = data.manifest?.api.find((a) => a.name === data.apiName)?.url
14-
if (!apiUrl) {
15-
return createErrorResponse(PluginErrorType.PluginApiNotFound, {
16-
message: `API not found for ${data.apiName}`,
17-
})
18-
}
19-
const au = new URL(apiUrl)
20-
if (au.host !== u.host) {
21-
return createErrorResponse(PluginErrorType.Forbidden, {
22-
message: `API URL ${apiUrl} is not allowed for security reasons`,
23-
})
24-
}
25-
const headers: Record<string, string> = {}
26-
c.req.raw.headers.forEach((v, k) => {
27-
if (k.toLowerCase().startsWith("x-lobe")) {
28-
headers[k] = v
29-
}
30-
})
31-
return await c.env.MYSELF.fetch(apiUrl, {
32-
method: "POST",
33-
headers: {
34-
"Content-Type": "application/json",
35-
...headers,
36-
},
37-
body: data.arguments,
38-
})
39-
})
10+
.post("/", async (c) => {
11+
const data = await c.req.json() as PluginRequestPayload
12+
const u = new URL(c.req.url)
13+
const apiUrl = data.manifest?.api.find((a) => a.name === data.apiName)?.url
14+
if (!apiUrl) {
15+
return createErrorResponse(PluginErrorType.PluginApiNotFound, {
16+
message: `API not found for ${data.apiName}`,
17+
})
18+
}
19+
const au = new URL(apiUrl)
20+
if (au.host !== u.host) {
21+
return createErrorResponse(PluginErrorType.Forbidden, {
22+
message: `API URL ${apiUrl} is not allowed for security reasons`,
23+
})
24+
}
25+
const headers: Record<string, string> = {}
26+
c.req.raw.headers.forEach((v, k) => {
27+
if (k.toLowerCase().startsWith("x-lobe")) {
28+
headers[k] = v
29+
}
30+
})
31+
return await c.env.MYSELF.fetch(apiUrl, {
32+
method: "POST",
33+
headers: {
34+
"Content-Type": "application/json",
35+
...headers,
36+
},
37+
body: data.arguments,
38+
})
39+
})
4040

src/index.ts

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,50 @@ import { apiGateway } from "./gateway"
99
import { loggingMiddleware } from "./middlewares"
1010

1111
const app = new Hono<{ Bindings: Bindings }>().use(
12-
prettyJSON(),
13-
logger(),
14-
cors({
15-
origin: "*",
16-
credentials: true,
17-
allowHeaders: [
18-
"X-CSRF-Token",
19-
"X-Requested-With",
20-
"Accept",
21-
"Accept-Version",
22-
"Content-Length",
23-
"Content-MD5",
24-
"Content-Type",
25-
"Date",
26-
"X-Api-Version",
27-
"x-lobe-trace",
28-
"x-lobe-plugin-settings",
29-
"x-lobe-chat-auth",
30-
],
31-
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
32-
maxAge: 86400,
33-
}),
12+
prettyJSON(),
13+
logger(),
14+
cors({
15+
origin: "*",
16+
credentials: true,
17+
allowHeaders: [
18+
"X-CSRF-Token",
19+
"X-Requested-With",
20+
"Accept",
21+
"Accept-Version",
22+
"Content-Length",
23+
"Content-MD5",
24+
"Content-Type",
25+
"Date",
26+
"X-Api-Version",
27+
"x-lobe-trace",
28+
"x-lobe-plugin-settings",
29+
"x-lobe-chat-auth",
30+
],
31+
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
32+
maxAge: 86400,
33+
}),
3434
)
3535

3636
app
37-
.get("/", (c) => c.text(`Welcome to ${TITLE}! ${DESCRIPTION} All routes are under \`/api\``))
38-
.get("/manifest.json", (c) => {
39-
const url = new URL(c.req.url)
40-
return c.json(buildManifest(url, providers))
41-
})
37+
.get("/", (c) => c.text(`Welcome to ${TITLE}! ${DESCRIPTION} All routes are under \`/api\``))
38+
.get("/manifest.json", (c) => {
39+
const url = new URL(c.req.url)
40+
return c.json(buildManifest(url, providers))
41+
})
4242

4343
const api = app.basePath("/api")
44-
.route("/gateway", apiGateway)
44+
.route("/gateway", apiGateway)
4545

4646
Object.entries(providers).forEach(([_, provider]) => {
47-
provider.route.use(loggingMiddleware)
48-
api.route(provider.path, provider.route)
47+
provider.route.use(loggingMiddleware)
48+
api.route(provider.path, provider.route)
4949
})
5050

5151
app.onError((err, _) => {
52-
return createErrorResponse(
53-
PluginErrorType.InternalServerError,
54-
err.message,
55-
)
52+
return createErrorResponse(
53+
PluginErrorType.InternalServerError,
54+
err.message,
55+
)
5656
})
5757

5858
export default app

src/manifest.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,32 @@ const AVATAR = ""
1010
const TAGS: string[] = []
1111
const SYSTEM_ROLE = ""
1212
const SETTINGS: PluginSchema = {
13-
type: "object",
14-
properties: {},
13+
type: "object",
14+
properties: {},
1515
}
1616

1717
export const buildManifest = (url: URL, providers: APIProvider[]): LobeChatPluginManifest => {
18-
const { protocol, host } = url
19-
return {
20-
"$schema": "../node_modules/@lobehub/chat-plugin-sdk/schema.json",
21-
"version": "1",
22-
"identifier": IDENTIFIER,
23-
"author": AUTHOR,
24-
"homepage": HOMEPAGE,
25-
"gateway": `${protocol}//${host}/api/gateway`,
26-
"meta": {
27-
"avatar": AVATAR,
28-
"tags": TAGS,
29-
"title": TITLE,
30-
"description": DESCRIPTION
31-
},
32-
"systemRole": SYSTEM_ROLE,
33-
"settings": SETTINGS,
34-
"api": Object.entries(providers).map(([_, provider]) => ({
35-
"name": provider.name,
36-
"url": `${protocol}//${host}/api/${provider.path}`,
37-
"description": provider.description,
38-
"parameters": provider.parameters,
39-
})),
40-
}
18+
const { protocol, host } = url
19+
return {
20+
"$schema": "../node_modules/@lobehub/chat-plugin-sdk/schema.json",
21+
"version": "1",
22+
"identifier": IDENTIFIER,
23+
"author": AUTHOR,
24+
"homepage": HOMEPAGE,
25+
"gateway": `${protocol}//${host}/api/gateway`,
26+
"meta": {
27+
"avatar": AVATAR,
28+
"tags": TAGS,
29+
"title": TITLE,
30+
"description": DESCRIPTION
31+
},
32+
"systemRole": SYSTEM_ROLE,
33+
"settings": SETTINGS,
34+
"api": Object.entries(providers).map(([_, provider]) => ({
35+
"name": provider.name,
36+
"url": `${protocol}//${host}/api/${provider.path}`,
37+
"description": provider.description,
38+
"parameters": provider.parameters,
39+
})),
40+
}
4141
}

src/middlewares/logging.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { Context, Next } from "hono"
22

33
export const loggingMiddleware = async (c: Context, next: Next) => {
4-
await next()
4+
await next()
55

6-
// Log request and response
7-
c.executionCtx.waitUntil((async () => {
8-
const requestBody = c.get("reqBuffer") || ""
9-
console.log("Request:", {
10-
body: requestBody,
11-
})
6+
// Log request and response
7+
c.executionCtx.waitUntil((async () => {
8+
const requestBody = c.get("reqBuffer") || ""
9+
console.log("Request:", {
10+
body: requestBody,
11+
})
1212

13-
await c.get("bufferPromise")
14-
console.log("Response:", {
15-
body: c.get("buffer"),
16-
})
17-
})())
13+
await c.get("bufferPromise")
14+
console.log("Response:", {
15+
body: c.get("buffer"),
16+
})
17+
})())
1818
}

0 commit comments

Comments
 (0)