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

Commit 8a607f3

Browse files
author
Je
committed
refactor: improve end method of APIResponse
1 parent e236384 commit 8a607f3

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

api.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,6 @@ export class AlephAPIResponse implements APIResponse {
8989
return this
9090
}
9191

92-
async end(status: number) {
93-
return this.#req.respond({
94-
status,
95-
headers: this.#headers,
96-
}).catch(err => log.warn('ServerRequest.respond:', err.message))
97-
}
98-
9992
async json(data: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number) {
10093
return this.send(JSON.stringify(data, replacer, space), 'application/json', true)
10194
}
@@ -105,7 +98,6 @@ export class AlephAPIResponse implements APIResponse {
10598
log.warn('ServerRequest.respond: repeat send calls')
10699
return
107100
}
108-
109101
let body: Uint8Array
110102
if (typeof data === 'string') {
111103
body = new TextEncoder().encode(data)
@@ -132,4 +124,17 @@ export class AlephAPIResponse implements APIResponse {
132124
body
133125
}).catch(err => log.warn('ServerRequest.respond:', err.message))
134126
}
127+
128+
async end(status: number) {
129+
if (this.#sent) {
130+
log.warn('ServerRequest.respond: repeat send calls')
131+
return
132+
}
133+
this.#headers.set('Date', (new Date).toUTCString())
134+
this.#sent = true
135+
return this.#req.respond({
136+
status,
137+
headers: this.#headers,
138+
}).catch(err => log.warn('ServerRequest.respond:', err.message))
139+
}
135140
}

0 commit comments

Comments
 (0)