Skip to content

Commit 97209d0

Browse files
authored
feat(ext/fetch): Request() constructor
feat(ext/fetch): Request() constructor
2 parents 9b5f933 + e0fd3ae commit 97209d0

File tree

7 files changed

+848
-3
lines changed

7 files changed

+848
-3
lines changed

examples/request.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// https://developer.mozilla.org/en-US/docs/Web/API/Request#examples
2+
const test1 = () => {
3+
const request = new Request("https://www.mozilla.org/favicon.ico");
4+
5+
const url = request.url;
6+
console.log("url", url);
7+
const method = request.method;
8+
console.log("method", method);
9+
const credentials = request.credentials;
10+
console.log("credentials", credentials);
11+
};
12+
test1();
13+
14+
// https://developer.mozilla.org/en-US/docs/Web/API/Request#examples
15+
const test2 = () => {
16+
const request = new Request("https://example.com", {
17+
method: "POST",
18+
body: "{\"foo\": \"bar\"}",
19+
});
20+
21+
const url = request.url;
22+
console.log("url", url);
23+
const method = request.method;
24+
console.log("method", method);
25+
const credentials = request.credentials;
26+
console.log("credentials", credentials);
27+
const bodyUsed = request.bodyUsed;
28+
console.log("bodyUsed", bodyUsed);
29+
};
30+
test2();
31+
32+
// https://developer.mozilla.org/en-US/docs/Web/API/Request/Request#options
33+
// TODO: comment in nova support module
34+
// function test3() {
35+
// const oldRequest = new Request(
36+
// "https://github.com/mdn/content/issues/12959",
37+
// { headers: { From: "webmaster@example.org" } },
38+
// );
39+
// console.log(oldRequest.headers.get("From")); // "webmaster@example.org"
40+
// const newRequest = new Request(oldRequest, {
41+
// headers: { From: "developer@example.org" },
42+
// });
43+
// console.log(newRequest.headers.get("From")); // "developer@example.org"
44+
// }
45+
// test3();

runtime/src/ext/fetch/headers/mod.ts

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,25 @@
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44
// deno-lint-ignore-file no-unused-vars
55

6+
type Header = [string, string];
7+
8+
type HeaderList = Header[];
9+
610
class Headers {
7-
// TODO: Private properties
8-
#guard: "none" | "immutable" = "none";
9-
#headerList: [string, string][] = [];
11+
#guard: "immutable" | "request" | "request-no-cors" | "response" | "none" = "none";
12+
#headerList: HeaderList = [];
1013

1114
// TODO: this is HeaderList type
1215
// https://fetch.spec.whatwg.org/#headers-class
1316
constructor(init = undefined) {
1417
fillHeaders(this, init);
1518
}
1619

20+
clear() {
21+
this.#headerList = [];
22+
this.#guard = "none";
23+
}
24+
1725
// https://fetch.spec.whatwg.org/#dom-headers-get
1826
get(name: string) {
1927
return getHeader(this.#headerList, name);
@@ -27,11 +35,38 @@ class Headers {
2735
get headerList() {
2836
return this.#headerList;
2937
}
38+
3039
get guard() {
3140
return this.#guard;
3241
}
42+
43+
static getHeadersGuard(
44+
o: Headers,
45+
guard: "immutable" | "request" | "request-no-cors" | "response" | "none",
46+
) {
47+
return o.#guard;
48+
}
49+
50+
static setHeadersGuard(
51+
o: Headers,
52+
guard: "immutable" | "request" | "request-no-cors" | "response" | "none",
53+
) {
54+
o.#guard = guard;
55+
}
56+
57+
static getHeadersList(
58+
target: Headers,
59+
) {
60+
return target.#headerList;
61+
}
62+
63+
static setHeadersList(target: Headers, list: HeaderList) {
64+
target.#headerList = list;
65+
}
3366
}
3467

68+
const { setHeadersList, setHeadersGuard, getHeadersList, getHeadersGuard } = Headers;
69+
3570
// deno-lint-ignore no-explicit-any
3671
function fillHeaders(headers: Headers, object: any) {
3772
if (Array.isArray(object)) {
@@ -136,3 +171,13 @@ function getHeader(list: [string, string][], name: string): string | null {
136171
return entries.join("\x2C\x20");
137172
}
138173
}
174+
175+
// TODO: comment in nova support module
176+
// export {
177+
// fillHeaders,
178+
// getHeadersGuard,
179+
// getHeadersList,
180+
// type HeaderList,
181+
// setHeadersGuard,
182+
// setHeadersList,
183+
// };

runtime/src/ext/fetch/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

55
mod headers;
6+
mod request;
67

78
pub use headers::*;
9+
pub use request::*;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
use andromeda_core::Extension;
6+
7+
#[derive(Default)]
8+
pub struct RequestExt;
9+
10+
impl RequestExt {
11+
pub fn new_extension() -> Extension {
12+
Extension {
13+
name: "request",
14+
ops: vec![],
15+
storage: None,
16+
files: vec![include_str!("./mod.ts")],
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)