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

Commit 10b3da7

Browse files
committed
Update types
1 parent 594bfcb commit 10b3da7

File tree

3 files changed

+27
-41
lines changed

3 files changed

+27
-41
lines changed

server/response.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { APIResponse as IResponse } from '../types.ts'
22
import log from '../shared/log.ts'
3-
import { getContentType } from './mime.ts'
43
import compress from './compress.ts'
54

65
export class APIResponse implements IResponse {
@@ -29,32 +28,9 @@ export class APIResponse implements IResponse {
2928
return this
3029
}
3130

32-
content(data: string | Uint8Array | ArrayBuffer | ReadableStream<Uint8Array>, contentType?: string): this {
33-
this.body = data
34-
if (contentType) {
35-
this.setHeader('Content-Type', contentType)
36-
}
37-
return this
38-
}
39-
4031
json(data: any, space?: string | number): this {
41-
this.content(JSON.stringify(data, undefined, space), 'application/json; charset=utf-8')
42-
return this
43-
}
44-
45-
async file(path: string): Promise<this> {
46-
this.body = await Deno.readFile(path)
47-
this.setHeader('Content-Type', getContentType(path))
48-
return this
49-
}
50-
51-
async proxy(url: string): Promise<this> {
52-
const resp = await fetch(url)
53-
if (resp.body) {
54-
this.body = resp.body
55-
}
56-
this.headers = resp.headers
57-
this.status = resp.status
32+
this.setHeader('Content-Type', 'application/json; charset=utf-8')
33+
this.body = JSON.stringify(data, undefined, space)
5834
return this
5935
}
6036

server/server.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { VERSION } from '../version.ts'
1010
import { APIContext } from '../types.ts'
1111
import { Aleph } from './aleph.ts'
1212
import compress from './compress.ts'
13+
import { getContentType } from './mime.ts'
1314
import { APIResponse } from './response.ts'
1415

1516
/** The Aleph server class. */
@@ -112,7 +113,9 @@ export class Server {
112113

113114
const relPath = util.trimPrefix(pathname, '/_aleph')
114115
if (relPath == '/main.js') {
115-
resp.content(app.createMainJS(false), 'application/javascript; charset=utf-8').writeTo(e)
116+
resp.body = app.createMainJS(false)
117+
resp.setHeader('Content-Type', 'application/javascript; charset=utf-8')
118+
resp.writeTo(e)
116119
return
117120
}
118121

@@ -138,6 +141,8 @@ export class Server {
138141
}
139142

140143
resp.setHeader('ETag', etag)
144+
resp.setHeader('Content-Type', 'application/javascript; charset=utf-8')
145+
141146
if (app.isDev && app.isHMRable(specifier)) {
142147
let code = new TextDecoder().decode(content)
143148
if (module.denoHooks?.length || module.ssrPropsFn || module.ssgPathsFn) {
@@ -161,10 +166,11 @@ export class Server {
161166
'',
162167
'import.meta.hot.accept();'
163168
].join('\n')
164-
resp.content(code, 'application/javascript; charset=utf-8').writeTo(e)
169+
resp.body = code
165170
} else {
166-
resp.content(content, 'application/javascript; charset=utf-8').writeTo(e)
171+
resp.body = content
167172
}
173+
resp.writeTo(e)
168174
return
169175
}
170176
}
@@ -179,11 +185,15 @@ export class Server {
179185
return
180186
}
181187

182-
(await resp.setHeader('Last-Modified', lastModified).file(filePath)).writeTo(e)
188+
resp.body = await Deno.readFile(filePath)
189+
resp.setHeader('Last-Modified', lastModified)
190+
resp.setHeader('Content-Type', getContentType(filePath))
191+
resp.writeTo(e)
183192
return
184193
}
185194

186-
resp.content('file not found').writeTo(e, 404)
195+
resp.body = 'file not found'
196+
resp.writeTo(e, 404)
187197
return
188198
}
189199

@@ -197,7 +207,10 @@ export class Server {
197207
return
198208
}
199209

200-
(await resp.setHeader('Last-Modified', lastModified).file(filePath)).writeTo(e)
210+
resp.body = await Deno.readFile(filePath)
211+
resp.setHeader('Last-Modified', lastModified)
212+
resp.setHeader('Content-Type', getContentType(filePath))
213+
resp.writeTo(e)
201214
return
202215
}
203216

@@ -273,9 +286,12 @@ export class Server {
273286
pathname,
274287
search: Array.from(url.searchParams.keys()).length > 0 ? '?' + url.searchParams.toString() : ''
275288
})
276-
resp.content(html, 'text/html; charset=utf-8').writeTo(e, status)
289+
resp.body = html
290+
resp.setHeader('Content-Type', 'text/html; charset=utf-8')
291+
resp.writeTo(e, status)
277292
} catch (err) {
278293
try {
294+
// todo: custom error page
279295
e.respondWith(new Response(
280296
[
281297
`<!DOCTYPE html>`,

types.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export type LoaderOutput = {
9393
/**
9494
* The built target of esbuild.
9595
*/
96-
export type BuildTarget = 'es2015' | 'es2016' | 'es2017' | 'es2018' | 'es2019' | 'es2020' | 'esnext'
96+
export type BuildTarget = 'es2015' | 'es2016' | 'es2017' | 'es2018' | 'es2019' | 'es2020' | 'es2021' | 'esnext'
9797

9898
/**
9999
* The supported borwser name of esbuild.
@@ -208,7 +208,7 @@ export interface APIContext extends Deno.RequestEvent {
208208
}
209209

210210
/**
211-
* An interface that aligns to the parts of the `Aleph`.
211+
* An interface that aligns to the parts of the `Response`.
212212
*/
213213
export interface APIResponse {
214214
status: number
@@ -228,14 +228,8 @@ export interface APIResponse {
228228
removeHeader(key: string): this
229229
/** `redirect` replies to redirect the client to another URL with optional response `status` defaulting to 302. */
230230
redirect(url: string, status?: number): this
231-
/** `content` replies to the request with a raw content. */
232-
content(data: string | Uint8Array | ArrayBuffer | ReadableStream<Uint8Array>, contentType?: string): this
233231
/** `json` replies to the request with a json content. */
234232
json(data: any, space?: string | number): this
235-
/** `file` reads the file content and sets `content-type` header by the file name. */
236-
file(name: string): Promise<this>
237-
/** `proxy` proxies the http request. */
238-
proxy(url: string): Promise<this>
239233
}
240234

241235
/**

0 commit comments

Comments
 (0)