Skip to content

Commit 4843e27

Browse files
committed
feat(connect): allow passing headers to set on request and add test coverage
1 parent 8c0c0d9 commit 4843e27

File tree

3 files changed

+148
-12
lines changed

3 files changed

+148
-12
lines changed
Lines changed: 135 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,140 @@
11
import { assert, assertEquals } from "../dev_deps.ts";
22

33
import { generateToken } from "../deps.deno.ts";
4+
import { hyper, HYPER_LEGACY_GET_HEADER } from "../utils/hyper-request.ts";
5+
import { HyperRequest } from "../types.ts";
46

5-
Deno.test("generateToken", async () => {
6-
try {
7-
const res = await generateToken("SUB", "SECRET");
8-
assert(true);
9-
assertEquals(typeof res, "string");
10-
// deno-lint-ignore no-explicit-any
11-
} catch (error: any) {
12-
assert(false, error.message);
13-
}
7+
Deno.test("hyper-request", async (t) => {
8+
await t.step("generateToken", async (t) => {
9+
await t.step("should generate a token successfully", async () => {
10+
try {
11+
const res = await generateToken("SUB", "SECRET");
12+
assert(true);
13+
assertEquals(typeof res, "string");
14+
// deno-lint-ignore no-explicit-any
15+
} catch (error: any) {
16+
assert(false, error.message);
17+
}
18+
});
19+
});
20+
21+
await t.step("hyper", async (t) => {
22+
const req: HyperRequest = {
23+
service: "data",
24+
method: "GET",
25+
};
26+
27+
await t.step("url", async (t) => {
28+
await t.step("it should append params", async () => {
29+
const resource = await hyper(
30+
new URL("cloud://mock.hyper.io/foobar"),
31+
"default",
32+
)({
33+
...req,
34+
params: {
35+
foo: "bar",
36+
},
37+
});
38+
assertEquals(
39+
resource.url,
40+
"https://mock.hyper.io/foobar/data/default?foo=bar",
41+
);
42+
});
43+
44+
await t.step("isCloud", async (t) => {
45+
const cloudCs = "cloud://mock.hyper.io/foobar";
46+
47+
await t.step("should build the resource url correctly", async () => {
48+
const resource = await hyper(new URL(cloudCs), "default")(req);
49+
assertEquals(
50+
resource.url,
51+
"https://mock.hyper.io/foobar/data/default",
52+
);
53+
});
54+
55+
await t.step("should build the action url correctly", async () => {
56+
const action = await hyper(
57+
new URL(cloudCs),
58+
"default",
59+
)({ ...req, action: "_bulk" });
60+
assertEquals(
61+
action.url,
62+
"https://mock.hyper.io/foobar/data/default/_bulk",
63+
);
64+
});
65+
});
66+
67+
await t.step("not isCloud", async (t) => {
68+
const notCloud = "http://localhost:6363/foobar";
69+
70+
await t.step("should build the resource url correctly", async () => {
71+
const resource = await hyper(new URL(notCloud), "default")(req);
72+
assertEquals(resource.url, "http://localhost:6363/data/foobar");
73+
});
74+
75+
await t.step("should build the action url correctly", async () => {
76+
const action = await hyper(
77+
new URL(notCloud),
78+
"default",
79+
)({ ...req, action: "_bulk" });
80+
assertEquals(action.url, "http://localhost:6363/data/foobar/_bulk");
81+
});
82+
});
83+
});
84+
85+
await t.step("options", async (t) => {
86+
await t.step("headers", async (t) => {
87+
await t.step("should set the Authorization header", async () => {
88+
const resource = await hyper(
89+
new URL("http://foo:bar@localhost:6363/foobar"),
90+
"default",
91+
)(req);
92+
assert(resource.options?.headers.has("Authorization"));
93+
});
94+
95+
await t.step("should set the Content-Type header", async () => {
96+
const resource = await hyper(
97+
new URL("http://foo:bar@localhost:6363/foobar"),
98+
"default",
99+
)(req);
100+
assert(resource.options?.headers.has("Content-Type"));
101+
});
102+
103+
await t.step("should set any provided headers", async () => {
104+
const resource = await hyper(
105+
new URL("http://foo:bar@localhost:6363/foobar"),
106+
"default",
107+
)({
108+
...req,
109+
headers: new Headers({ [HYPER_LEGACY_GET_HEADER]: "true" }),
110+
});
111+
assert(resource.options?.headers.has(HYPER_LEGACY_GET_HEADER));
112+
});
113+
});
114+
115+
await t.step("should add the body", async () => {
116+
const resource = await hyper(
117+
new URL("http://localhost:6363/foobar"),
118+
"default",
119+
)({ ...req, body: { foo: "bar" } });
120+
assertEquals(resource.options?.body, JSON.stringify({ foo: "bar" }));
121+
});
122+
123+
await t.step("should add the method", async () => {
124+
const resource = await hyper(
125+
new URL("http://localhost:6363/foobar"),
126+
"default",
127+
)(req);
128+
assertEquals(resource.options?.method, "GET");
129+
130+
const resource2 = await hyper(
131+
new URL("http://localhost:6363/foobar"),
132+
"default",
133+
// deno-lint-ignore ban-ts-comment
134+
// @ts-ignore
135+
)({ ...req, method: undefined });
136+
assertEquals(resource2.options?.method, "GET");
137+
});
138+
});
139+
});
14140
});

packages/connect/deno/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ export interface HyperRequest {
169169
service: "data" | "cache" | "storage" | "search" | "queue" | "info";
170170
method: Method;
171171
resource?: string;
172+
headers?: Headers;
172173
body?: unknown;
173174
// deno-lint-ignore no-explicit-any
174175
params?: undefined | Record<string, any>;

packages/connect/deno/utils/hyper-request.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,24 @@ interface HyperRequestParams {
1414
options?: RequestOptions;
1515
}
1616

17+
export const HYPER_LEGACY_GET_HEADER = "X-HYPER-LEGACY-GET";
18+
1719
export const hyper = (conn: URL, domain: string) =>
18-
async (
19-
{ service, method, resource, body, params, action }: HyperRequest,
20-
): Promise<HyperRequestParams> => {
20+
async ({
21+
service,
22+
method,
23+
headers,
24+
resource,
25+
body,
26+
params,
27+
action,
28+
}: HyperRequest): Promise<HyperRequestParams> => {
2129
const isCloud = /^cloud/.test(conn.protocol);
2230
const protocol = isCloud ? "https:" : conn.protocol;
2331

2432
let options = {
2533
headers: new Headers({
34+
...(headers ? Object.fromEntries(headers.entries()) : {}),
2635
"Content-Type": "application/json",
2736
}),
2837
method: method ? method : Method.GET,

0 commit comments

Comments
 (0)