Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 5c9946a

Browse files
author
Je
committed
refactor: improve api of API
1 parent 0b1ab39 commit 5c9946a

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

api.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1+
import log from './log.ts'
12
import type { ServerRequest } from './std.ts'
2-
import type { APIRequest, APIResponse } from './types.ts'
3+
import type { APIRequest, APIRequestURL, APIResponse, RouterURL } from './types.ts'
34

45
export class AlephAPIRequest implements APIRequest {
56
#req: ServerRequest
6-
7+
#url: APIRequestURL
78
cookies: ReadonlyMap<string, string>
8-
params: ReadonlyMap<string, string>
9-
query: URLSearchParams
109

11-
constructor(req: ServerRequest, params: Record<string, string>, query: URLSearchParams) {
10+
constructor(req: ServerRequest, url: RouterURL) {
1211
this.#req = req
1312

1413
const paramsMap = new Map<string, string>()
15-
for (const key in params) {
16-
paramsMap.set(key, params[key])
14+
for (const key in url.params) {
15+
paramsMap.set(key, url.params[key])
16+
}
17+
this.#url = {
18+
pathname: url.pathname,
19+
params: paramsMap,
20+
query: url.query,
1721
}
18-
this.params = paramsMap
19-
2022
this.cookies = new Map()
2123
// todo: parse cookies
22-
23-
this.query = query
2424
}
2525

26-
get url(): string {
27-
return this.#req.url
26+
get url(): APIRequestURL {
27+
return this.#url
2828
}
2929

3030
get method(): string {
@@ -90,7 +90,7 @@ export class AlephAPIResponse implements APIResponse {
9090
status: this.#status,
9191
headers: this.#headers,
9292
body
93-
})
93+
}).catch(err => log.warn('ServerRequest.respond:', err.message))
9494
}
9595

9696
json(data: any) {
@@ -99,6 +99,6 @@ export class AlephAPIResponse implements APIResponse {
9999
status: this.#status,
100100
headers: this.#headers,
101101
body: JSON.stringify(data)
102-
})
102+
}).catch(err => log.warn('ServerRequest.respond:', err.message))
103103
}
104104
}

project.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,30 +153,31 @@ export class Project {
153153
}
154154

155155
async callAPI(req: ServerRequest, loc: { pathname: string, search?: string }): Promise<APIHandle | null> {
156-
const [{ pagePath, params, query }] = this.#apiRouting.createRouter(loc)
157-
if (pagePath != '') {
158-
const moduleID = pagePath + '.js'
156+
const [url] = this.#apiRouting.createRouter(loc)
157+
if (url.pagePath != '') {
158+
const moduleID = url.pagePath + '.js'
159159
if (this.#modules.has(moduleID)) {
160160
try {
161161
const { default: handle } = await import('file://' + this.#modules.get(moduleID)!.jsFile)
162-
handle(
163-
new AlephAPIRequest(req, params, query),
162+
await handle(
163+
new AlephAPIRequest(req, url),
164164
new AlephAPIResponse(req)
165165
)
166166
} catch (err) {
167167
req.respond({
168168
status: 500,
169169
headers: new Headers({ 'Content-Type': 'text/plain; charset=utf-8' }),
170170
body: JSON.stringify({ error: { status: 500, message: err.message } })
171-
})
171+
}).catch(err => log.warn('ServerRequest.respond:', err.message))
172+
log.error('callAPI:', err)
172173
}
173174
}
174175
} else {
175176
req.respond({
176177
status: 404,
177178
headers: new Headers({ 'Content-Type': 'application/javascript; charset=utf-8' }),
178179
body: JSON.stringify({ error: { status: 404, message: 'page not found' } })
179-
})
180+
}).catch(err => log.warn('ServerRequest.respond:', err.message))
180181
}
181182
return null
182183
}
@@ -859,21 +860,21 @@ export class Project {
859860
` };`,
860861
` if (ref.current) {`,
861862
` ref.current.querySelectorAll("a").forEach(a => {`,
862-
` const href = a.getAttribute("href")`,
863+
` const href = a.getAttribute("href");`,
863864
` if (href && !/^(https?|mailto|file):/i.test(href)) {`,
864865
` a.addEventListener("click", onClick, false);`,
865866
` appLinks.push(a);`,
866867
` }`,
867868
` });`,
868869
` }`,
869870
` return () => appLinks.forEach(a => a.removeEventListener("click", onClick));`,
870-
` }, [])`,
871+
` }, []);`,
871872
` return React.createElement("div", {className: "markdown-page", ref, dangerouslySetInnerHTML: {__html: ${JSON.stringify(html)}}});`,
872873
`}`,
873874
`MarkdownPage.meta = ${JSON.stringify(props, undefined, this.isDev ? 4 : undefined)};`,
874875
this.isDev && `_s(MarkdownPage, "useRef{ref}\\nuseEffect{}");`,
875876
this.isDev && `$RefreshReg$(MarkdownPage, "MarkdownPage");`,
876-
].filter(Boolean).join(this.isDev ? '\n' : '')
877+
].filter(Boolean).map(l => this.isDev ? String(l).trim() : l).join(this.isDev ? '\n' : '')
877878
mod.jsSourceMap = ''
878879
mod.hash = (new Sha1).update(mod.jsContent).hex()
879880
} else {

types.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,20 @@ export interface Config {
2626
}
2727
}
2828

29+
export interface APIRequestURL {
30+
readonly pathname: string
31+
readonly params: ReadonlyMap<string, string>
32+
readonly query: URLSearchParams
33+
}
34+
2935
export interface APIRequest {
30-
readonly url: string
36+
readonly url: APIRequestURL
3137
readonly method: string
3238
readonly proto: string
3339
readonly protoMinor: number
3440
readonly protoMajor: number
3541
readonly headers: Headers
3642
readonly cookies: ReadonlyMap<string, string>
37-
readonly params: ReadonlyMap<string, string>
38-
readonly query: URLSearchParams
3943
}
4044

4145
export interface APIResponse {

0 commit comments

Comments
 (0)