-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
147 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,138 @@ | ||
import { IncomingHttpResponse } from "@ollierelph/sensible-fetch"; | ||
import { ImmutableHeaders } from "~/src/ImmutableHeaders"; | ||
import { ImmutableURL } from "~/src/ImmutableURL"; | ||
|
||
export class ImmutableResponse { | ||
constructor( | ||
public status: number, | ||
private body: unknown, | ||
public headers: ImmutableHeaders = new ImmutableHeaders(), | ||
) {} | ||
type StatusCode = | ||
| 100 | ||
| 101 | ||
| 102 | ||
| 103 | ||
| 200 | ||
| 201 | ||
| 202 | ||
| 203 | ||
| 204 | ||
| 205 | ||
| 206 | ||
| 207 | ||
| 208 | ||
| 218 | ||
| 226 | ||
| 300 | ||
| 301 | ||
| 302 | ||
| 303 | ||
| 304 | ||
| 306 | ||
| 307 | ||
| 308 | ||
| 400 | ||
| 401 | ||
| 402 | ||
| 403 | ||
| 404 | ||
| 405 | ||
| 406 | ||
| 407 | ||
| 408 | ||
| 409 | ||
| 410 | ||
| 411 | ||
| 412 | ||
| 413 | ||
| 414 | ||
| 415 | ||
| 416 | ||
| 417 | ||
| 418 | ||
| 419 | ||
| 420 | ||
| 421 | ||
| 422 | ||
| 423 | ||
| 424 | ||
| 426 | ||
| 428 | ||
| 429 | ||
| 431 | ||
| 440 | ||
| 444 | ||
| 449 | ||
| 450 | ||
| 451 | ||
| 494 | ||
| 495 | ||
| 496 | ||
| 497 | ||
| 498 | ||
| 499 | ||
| 500 | ||
| 501 | ||
| 502 | ||
| 503 | ||
| 504 | ||
| 505 | ||
| 506 | ||
| 507 | ||
| 508 | ||
| 509 | ||
| 510 | ||
| 511 | ||
| 520 | ||
| 521 | ||
| 522 | ||
| 523 | ||
| 524 | ||
| 525 | ||
| 526 | ||
| 527 | ||
| 530 | ||
| 598; | ||
|
||
export class ImmutableResponse<Status extends StatusCode> | ||
implements Readonly<IncomingHttpResponse<ImmutableURL, Status>> | ||
{ | ||
#response: IncomingHttpResponse; | ||
#status: Status; | ||
protected constructor(status: Status, response: IncomingHttpResponse) { | ||
this.#status = status; | ||
this.#response = response; | ||
} | ||
|
||
fromResponse(response: IncomingHttpResponse) { | ||
return new ImmutableResponse(response.status as StatusCode, response); | ||
} | ||
|
||
get status() { | ||
return this.#status; | ||
} | ||
get statusText() { | ||
return this.#response.statusText; | ||
} | ||
get type() { | ||
return this.#response.type; | ||
} | ||
get url() { | ||
return new ImmutableURL(this.#response.url); | ||
} | ||
get body() { | ||
return this.#response.body; | ||
} | ||
get headers() { | ||
return new ImmutableHeaders(this.#response.headers); | ||
} | ||
|
||
json(): Promise<unknown> { | ||
return Promise.resolve(this.body); | ||
return this.clone().json(); | ||
} | ||
blob(): Promise<Blob> { | ||
return this.clone().blob(); | ||
} | ||
text(): Promise<string> { | ||
return this.clone().text(); | ||
} | ||
|
||
clone() { | ||
return new ImmutableResponse(this.status, this.#response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.