Skip to content

Commit

Permalink
response is backed
Browse files Browse the repository at this point in the history
  • Loading branch information
BBB committed Nov 5, 2023
1 parent 3c64420 commit f226a33
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/http-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"license": "ISC",
"dependencies": {
"@foxify/events": "^2.1.0",
"@ollierelph/sensible-fetch": "workspace:*",
"@ollierelph/result4t": "^0.3.0"
},
"devDependencies": {
Expand Down
6 changes: 4 additions & 2 deletions packages/http-client/src/ImmutableHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { Headers, SpecIterableIterator, SpecIterator } from "undici";
export class ImmutableHeaders implements Readonly<Headers> {
readonly #headers: Headers;

constructor(nextHeaders?: Headers | Record<string, string>) {
constructor(nextHeaders?: Headers | Record<string, string> | null) {
this.#headers =
nextHeaders instanceof Headers
? nextHeaders
: new Headers(nextHeaders) ?? new Headers();
: nextHeaders
? new Headers(nextHeaders)
: new Headers();
}

append(name: string, value: string) {
Expand Down
139 changes: 132 additions & 7 deletions packages/http-client/src/ImmutableResponse.ts
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);
}
}
11 changes: 7 additions & 4 deletions packages/sensible-fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,12 @@ export interface HttpFormData {
readonly [Symbol.iterator]: () => SpecIterator<[string, string]>;
}

export interface IncomingHttpResponse {
readonly url: string;
readonly status: number;
/**
* An approximation of Response
*/
export interface IncomingHttpResponse<URL = string, Status = number> {
readonly url: URL;
readonly status: Status;
readonly statusText: string;
readonly type:
| "default"
Expand All @@ -153,7 +156,7 @@ export interface IncomingHttpResponse {
| "opaqueredirect";
readonly body: ReadableStream | null;
readonly headers: Headers | null;
clone(): IncomingHttpResponse;
clone(): IncomingHttpResponse<URL, Status>;
json(): Promise<unknown>;
text(): Promise<string>;
blob(): Promise<Blob>;
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f226a33

Please sign in to comment.