Skip to content

Commit 09d35d3

Browse files
authored
Merge pull request #60 from bknd-io/release/0.7
Release 0.7
2 parents 3d1d52c + f7e6928 commit 09d35d3

File tree

100 files changed

+3448
-634
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+3448
-634
lines changed

app/__test__/api/Api.spec.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { describe, expect, it } from "bun:test";
2+
import { sign } from "hono/jwt";
3+
import { Api } from "../../src/Api";
4+
5+
describe("Api", async () => {
6+
it("should construct without options", () => {
7+
const api = new Api();
8+
expect(api.baseUrl).toBe("http://localhost");
9+
expect(api.isAuthVerified()).toBe(false);
10+
});
11+
12+
it("should ignore force verify if no claims given", () => {
13+
const api = new Api({ verified: true });
14+
expect(api.baseUrl).toBe("http://localhost");
15+
expect(api.isAuthVerified()).toBe(false);
16+
});
17+
18+
it("should construct from request (token)", async () => {
19+
const token = await sign({ foo: "bar" }, "1234");
20+
const request = new Request("http://example.com/test", {
21+
headers: {
22+
Authorization: `Bearer ${token}`
23+
}
24+
});
25+
const api = new Api({ request });
26+
expect(api.isAuthVerified()).toBe(false);
27+
28+
const params = api.getParams();
29+
expect(params.token).toBe(token);
30+
expect(params.token_transport).toBe("header");
31+
expect(params.host).toBe("http://example.com");
32+
});
33+
34+
it("should construct from request (cookie)", async () => {
35+
const token = await sign({ foo: "bar" }, "1234");
36+
const request = new Request("http://example.com/test", {
37+
headers: {
38+
Cookie: `auth=${token}`
39+
}
40+
});
41+
const api = new Api({ request });
42+
expect(api.isAuthVerified()).toBe(false);
43+
44+
const params = api.getParams();
45+
console.log(params);
46+
expect(params.token).toBe(token);
47+
expect(params.token_transport).toBe("cookie");
48+
expect(params.host).toBe("http://example.com");
49+
});
50+
51+
it("should construct from token", async () => {
52+
const token = await sign({ foo: "bar" }, "1234");
53+
const api = new Api({ token });
54+
expect(api.isAuthVerified()).toBe(false);
55+
56+
const params = api.getParams();
57+
expect(params.token).toBe(token);
58+
expect(params.token_transport).toBe("header");
59+
expect(params.host).toBe("http://localhost");
60+
});
61+
62+
it("should prefer host when request is given", async () => {
63+
const request = new Request("http://example.com/test");
64+
const api = new Api({ request, host: "http://another.com" });
65+
66+
const params = api.getParams();
67+
expect(params.token).toBeUndefined();
68+
expect(params.token_transport).toBe("header");
69+
expect(params.host).toBe("http://another.com");
70+
});
71+
});

app/__test__/api/ModuleApi.spec.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ describe("ModuleApi", () => {
2727

2828
it("fetches endpoint", async () => {
2929
const app = new Hono().get("/endpoint", (c) => c.json({ foo: "bar" }));
30-
const api = new Api({ host });
31-
32-
// @ts-expect-error it's protected
33-
api.fetcher = app.request as typeof fetch;
30+
const api = new Api({ host }, app.request as typeof fetch);
3431

3532
const res = await api.get("/endpoint");
3633
expect(res.res.ok).toEqual(true);
@@ -41,10 +38,7 @@ describe("ModuleApi", () => {
4138

4239
it("has accessible request", async () => {
4340
const app = new Hono().get("/endpoint", (c) => c.json({ foo: "bar" }));
44-
const api = new Api({ host });
45-
46-
// @ts-expect-error it's protected
47-
api.fetcher = app.request as typeof fetch;
41+
const api = new Api({ host }, app.request as typeof fetch);
4842

4943
const promise = api.get("/endpoint");
5044
expect(promise.request).toBeDefined();

app/__test__/core/utils.spec.ts

Lines changed: 98 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { describe, expect, test } from "bun:test";
22
import { Perf } from "../../src/core/utils";
3-
import * as reqres from "../../src/core/utils/reqres";
4-
import * as strings from "../../src/core/utils/strings";
3+
import * as utils from "../../src/core/utils";
54

65
async function wait(ms: number) {
76
return new Promise((resolve) => {
@@ -13,7 +12,7 @@ describe("Core Utils", async () => {
1312
describe("[core] strings", async () => {
1413
test("objectToKeyValueArray", async () => {
1514
const obj = { a: 1, b: 2, c: 3 };
16-
const result = strings.objectToKeyValueArray(obj);
15+
const result = utils.objectToKeyValueArray(obj);
1716
expect(result).toEqual([
1817
{ key: "a", value: 1 },
1918
{ key: "b", value: 2 },
@@ -22,24 +21,24 @@ describe("Core Utils", async () => {
2221
});
2322

2423
test("snakeToPascalWithSpaces", async () => {
25-
const result = strings.snakeToPascalWithSpaces("snake_to_pascal");
24+
const result = utils.snakeToPascalWithSpaces("snake_to_pascal");
2625
expect(result).toBe("Snake To Pascal");
2726
});
2827

2928
test("randomString", async () => {
30-
const result = strings.randomString(10);
29+
const result = utils.randomString(10);
3130
expect(result).toHaveLength(10);
3231
});
3332

3433
test("pascalToKebab", async () => {
35-
const result = strings.pascalToKebab("PascalCase");
34+
const result = utils.pascalToKebab("PascalCase");
3635
expect(result).toBe("pascal-case");
3736
});
3837

3938
test("replaceSimplePlaceholders", async () => {
4039
const str = "Hello, {$name}!";
4140
const vars = { name: "John" };
42-
const result = strings.replaceSimplePlaceholders(str, vars);
41+
const result = utils.replaceSimplePlaceholders(str, vars);
4342
expect(result).toBe("Hello, John!");
4443
});
4544
});
@@ -49,7 +48,7 @@ describe("Core Utils", async () => {
4948
const headers = new Headers();
5049
headers.append("Content-Type", "application/json");
5150
headers.append("Authorization", "Bearer 123");
52-
const obj = reqres.headersToObject(headers);
51+
const obj = utils.headersToObject(headers);
5352
expect(obj).toEqual({
5453
"content-type": "application/json",
5554
authorization: "Bearer 123"
@@ -59,21 +58,21 @@ describe("Core Utils", async () => {
5958
test("replaceUrlParam", () => {
6059
const url = "/api/:id/:name";
6160
const params = { id: "123", name: "test" };
62-
const result = reqres.replaceUrlParam(url, params);
61+
const result = utils.replaceUrlParam(url, params);
6362
expect(result).toBe("/api/123/test");
6463
});
6564

6665
test("encode", () => {
6766
const obj = { id: "123", name: "test" };
68-
const result = reqres.encodeSearch(obj);
67+
const result = utils.encodeSearch(obj);
6968
expect(result).toBe("id=123&name=test");
7069

7170
const obj2 = { id: "123", name: ["test1", "test2"] };
72-
const result2 = reqres.encodeSearch(obj2);
71+
const result2 = utils.encodeSearch(obj2);
7372
expect(result2).toBe("id=123&name=test1&name=test2");
7473

7574
const obj3 = { id: "123", name: { test: "test" } };
76-
const result3 = reqres.encodeSearch(obj3, { encode: true });
75+
const result3 = utils.encodeSearch(obj3, { encode: true });
7776
expect(result3).toBe("id=123&name=%7B%22test%22%3A%22test%22%7D");
7877
});
7978
});
@@ -108,4 +107,91 @@ describe("Core Utils", async () => {
108107
expect(count).toBe(2);
109108
});
110109
});
110+
111+
describe("objects", () => {
112+
test("omitKeys", () => {
113+
const objects = [
114+
[{ a: 1, b: 2, c: 3 }, ["a"], { b: 2, c: 3 }],
115+
[{ a: 1, b: 2, c: 3 }, ["b"], { a: 1, c: 3 }],
116+
[{ a: 1, b: 2, c: 3 }, ["c"], { a: 1, b: 2 }],
117+
[{ a: 1, b: 2, c: 3 }, ["a", "b"], { c: 3 }],
118+
[{ a: 1, b: 2, c: 3 }, ["a", "b", "c"], {}]
119+
] as [object, string[], object][];
120+
121+
for (const [obj, keys, expected] of objects) {
122+
const result = utils.omitKeys(obj, keys as any);
123+
expect(result).toEqual(expected);
124+
}
125+
});
126+
127+
test("isEqual", () => {
128+
const objects = [
129+
[1, 1, true],
130+
[1, "1", false],
131+
[1, 2, false],
132+
["1", "1", true],
133+
["1", "2", false],
134+
[true, true, true],
135+
[true, false, false],
136+
[false, false, true],
137+
[1, NaN, false],
138+
[NaN, NaN, true],
139+
[null, null, true],
140+
[null, undefined, false],
141+
[undefined, undefined, true],
142+
[new Map([["a", 1]]), new Map([["a", 1]]), true],
143+
[new Map([["a", 1]]), new Map([["a", 2]]), false],
144+
[new Map([["a", 1]]), new Map([["b", 1]]), false],
145+
[
146+
new Map([["a", 1]]),
147+
new Map([
148+
["a", 1],
149+
["b", 2]
150+
]),
151+
false
152+
],
153+
[{ a: 1 }, { a: 1 }, true],
154+
[{ a: 1 }, { a: 2 }, false],
155+
[{ a: 1 }, { b: 1 }, false],
156+
[{ a: "1" }, { a: "1" }, true],
157+
[{ a: "1" }, { a: "2" }, false],
158+
[{ a: "1" }, { b: "1" }, false],
159+
[{ a: 1 }, { a: 1, b: 2 }, false],
160+
[{ a: [1, 2, 3] }, { a: [1, 2, 3] }, true],
161+
[{ a: [1, 2, 3] }, { a: [1, 2, 4] }, false],
162+
[{ a: [1, 2, 3] }, { a: [1, 2, 3, 4] }, false],
163+
[{ a: { b: 1 } }, { a: { b: 1 } }, true],
164+
[{ a: { b: 1 } }, { a: { b: 2 } }, false],
165+
[{ a: { b: 1 } }, { a: { c: 1 } }, false],
166+
[{ a: { b: 1 } }, { a: { b: 1, c: 2 } }, false],
167+
[[1, 2, 3], [1, 2, 3], true],
168+
[[1, 2, 3], [1, 2, 4], false],
169+
[[1, 2, 3], [1, 2, 3, 4], false],
170+
[[{ a: 1 }], [{ a: 1 }], true],
171+
[[{ a: 1 }], [{ a: 2 }], false],
172+
[[{ a: 1 }], [{ b: 1 }], false]
173+
] as [any, any, boolean][];
174+
175+
for (const [a, b, expected] of objects) {
176+
const result = utils.isEqual(a, b);
177+
expect(result).toEqual(expected);
178+
}
179+
});
180+
181+
test("getPath", () => {
182+
const tests = [
183+
[{ a: 1, b: 2, c: 3 }, "a", 1],
184+
[{ a: 1, b: 2, c: 3 }, "b", 2],
185+
[{ a: { b: 1 } }, "a.b", 1],
186+
[{ a: { b: 1 } }, "a.b.c", null, null],
187+
[{ a: { b: 1 } }, "a.b.c", 1, 1],
188+
[[[1]], "0.0", 1]
189+
] as [object, string, any, any][];
190+
191+
for (const [obj, path, expected, defaultValue] of tests) {
192+
const result = utils.getPath(obj, path, defaultValue);
193+
expect(result).toEqual(expected);
194+
}
195+
});
196+
});
111197
});

app/__test__/data/data-query-impl.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,23 @@ describe("data-query-impl", () => {
123123
}
124124
}
125125
);
126+
127+
// over http
128+
{
129+
const output = { with: { images: {} } };
130+
decode({ with: "images" }, output);
131+
decode({ with: '["images"]' }, output);
132+
decode({ with: ["images"] }, output);
133+
decode({ with: { images: {} } }, output);
134+
}
135+
136+
{
137+
const output = { with: { images: {}, comments: {} } };
138+
decode({ with: "images,comments" }, output);
139+
decode({ with: ["images", "comments"] }, output);
140+
decode({ with: '["images", "comments"]' }, output);
141+
decode({ with: { images: {}, comments: {} } }, output);
142+
}
126143
});
127144
});
128145

0 commit comments

Comments
 (0)