klaim 1.6.0 → 1.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,186 +1,186 @@
1
- import { describe, expect, it } from "vitest";
2
- import { Api, Klaim, Registry, Route } from "../src/index";
3
-
4
- const apiName = "testApi";
5
- const apiUrl = "https://jsonplaceholder.typicode.com";
6
-
7
- await describe("Route", async () => {
8
- it("should create an route instance with correct properties", () => {
9
- const name = "testRoute";
10
- const routePath = "/posts";
11
-
12
- Api.create(apiName, apiUrl, () => {
13
- Route.get(name, routePath);
14
- });
15
-
16
- expect(Registry.i.getRoute(apiName, name)).toBeDefined();
17
- });
18
-
19
- it("should format the path correctly", () => {
20
- const name = "testRoute";
21
- const routePath = "/posts/";
22
- const waitingPath = "posts";
23
-
24
- Api.create(apiName, apiUrl, () => {
25
- Route.get(name, routePath);
26
- });
27
-
28
- expect(Registry.i.getRoute(apiName, name).url).toBe(waitingPath);
29
- });
30
-
31
- it("should format the name to camelCase", () => {
32
- const name = "test-route";
33
- const waitingName = "testRoute";
34
-
35
- Api.create(apiName, apiUrl, () => {
36
- Route.get(name, "/posts");
37
- });
38
-
39
- expect(Registry.i.getRoute(apiName, waitingName)).toBeDefined();
40
- });
41
-
42
- it("should access the route instance from Klaim", () => {
43
- const name = "testRoute";
44
-
45
- Api.create(apiName, apiUrl, () => {
46
- Route.get(name, "/posts");
47
- });
48
-
49
- expect(Klaim[apiName][name]).toBeDefined();
50
- });
51
-
52
- it("should set the headers correctly", () => {
53
- const name = "testRoute";
54
- const routePath = "/posts";
55
- const headers = {
56
- "Authorization": "Bearer token 123",
57
- };
58
-
59
- Api.create(apiName, apiUrl, () => {
60
- Route.get(name, routePath, headers);
61
- });
62
-
63
- expect(Registry.i.getRoute(apiName, name).headers).toEqual(headers);
64
- });
65
-
66
- describe("Methods", () => {
67
- it("should create a GET route instance with correct properties", () => {
68
- const name = "testRoute";
69
- const routePath = "/posts";
70
-
71
- Api.create(apiName, apiUrl, () => {
72
- Route.get(name, routePath);
73
- });
74
-
75
- expect(Registry.i.getRoute(apiName, name)).toBeDefined();
76
- });
77
-
78
- it("should create a POST route instance with correct properties", () => {
79
- const name = "testRoute";
80
- const routePath = "/posts";
81
-
82
- Api.create(apiName, apiUrl, () => {
83
- Route.post(name, routePath);
84
- });
85
-
86
- expect(Registry.i.getRoute(apiName, name)).toBeDefined();
87
- });
88
-
89
- it("should create a PUT route instance with correct properties", () => {
90
- const name = "testRoute";
91
- const routePath = "/posts";
92
-
93
- Api.create(apiName, apiUrl, () => {
94
- Route.put(name, routePath);
95
- });
96
-
97
- expect(Registry.i.getRoute(apiName, name)).toBeDefined();
98
- });
99
-
100
- it("should create a DELETE route instance with correct properties", () => {
101
- const name = "testRoute";
102
- const routePath = "/posts";
103
-
104
- Api.create(apiName, apiUrl, () => {
105
- Route.delete(name, routePath);
106
- });
107
-
108
- expect(Registry.i.getRoute(apiName, name)).toBeDefined();
109
- });
110
-
111
- it("should create a PATCH route instance with correct properties", () => {
112
- const name = "testRoute";
113
- const routePath = "/posts";
114
-
115
- Api.create(apiName, apiUrl, () => {
116
- Route.patch(name, routePath);
117
- });
118
-
119
- expect(Registry.i.getRoute(apiName, name)).toBeDefined();
120
- });
121
-
122
- it("should create a OPTIONS route instance with correct properties", () => {
123
- const name = "testRoute";
124
- const routePath = "/posts";
125
-
126
- Api.create(apiName, apiUrl, () => {
127
- Route.options(name, routePath);
128
- });
129
-
130
- expect(Registry.i.getRoute(apiName, name)).toBeDefined();
131
- });
132
- });
133
-
134
- describe("Arguments", () => {
135
- it("should set the arguments correctly", () => {
136
- const name = "testRoute";
137
- const routePath = "/post/[id]";
138
- const args = new Set(["id"]);
139
-
140
- Api.create(apiName, apiUrl, () => {
141
- Route.get(name, routePath, {});
142
- });
143
-
144
- expect(Registry.i.getRoute(apiName, name).arguments).toEqual(args);
145
- });
146
-
147
- it("should set the arguments correctly with multiple arguments", () => {
148
- const name = "testRoute";
149
- const routePath = "/post/[id]/[name]";
150
- const args = new Set(["id", "name"]);
151
-
152
- Api.create(apiName, apiUrl, () => {
153
- Route.get(name, routePath, {});
154
- });
155
-
156
- expect(Registry.i.getRoute(apiName, name).arguments).toEqual(args);
157
- });
158
- });
159
-
160
- await describe("Middlewares", async () => {
161
- it("should set the before middleware correctly", () => {
162
- const name = "testRoute";
163
- const routePath = "/posts";
164
- const before = () => {};
165
-
166
- Api.create(apiName, apiUrl, () => {
167
- Route.get(name, routePath, {}).before(before);
168
- });
169
-
170
- expect(Registry.i.getRoute(apiName, name).callbacks.before)
171
- .toEqual(before);
172
- });
173
- it("should set the after middleware correctly", () => {
174
- const name = "testRoute";
175
- const routePath = "/posts";
176
- const after = () => {};
177
-
178
- Api.create(apiName, apiUrl, () => {
179
- Route.get(name, routePath, {}).after(after);
180
- });
181
-
182
- expect(Registry.i.getRoute(apiName, name).callbacks.after)
183
- .toEqual(after);
184
- });
185
- });
186
- });
1
+ import { describe, expect, it } from "vitest";
2
+ import { Api, Klaim, Registry, Route } from "../src/index";
3
+
4
+ const apiName = "testApi";
5
+ const apiUrl = "https://jsonplaceholder.typicode.com";
6
+
7
+ await describe("Route", async () => {
8
+ it("should create an route instance with correct properties", () => {
9
+ const name = "testRoute";
10
+ const routePath = "/posts";
11
+
12
+ Api.create(apiName, apiUrl, () => {
13
+ Route.get(name, routePath);
14
+ });
15
+
16
+ expect(Registry.i.getRoute(apiName, name)).toBeDefined();
17
+ });
18
+
19
+ it("should format the path correctly", () => {
20
+ const name = "testRoute";
21
+ const routePath = "/posts/";
22
+ const waitingPath = "posts";
23
+
24
+ Api.create(apiName, apiUrl, () => {
25
+ Route.get(name, routePath);
26
+ });
27
+
28
+ expect(Registry.i.getRoute(apiName, name).url).toBe(waitingPath);
29
+ });
30
+
31
+ it("should format the name to camelCase", () => {
32
+ const name = "test-route";
33
+ const waitingName = "testRoute";
34
+
35
+ Api.create(apiName, apiUrl, () => {
36
+ Route.get(name, "/posts");
37
+ });
38
+
39
+ expect(Registry.i.getRoute(apiName, waitingName)).toBeDefined();
40
+ });
41
+
42
+ it("should access the route instance from Klaim", () => {
43
+ const name = "testRoute";
44
+
45
+ Api.create(apiName, apiUrl, () => {
46
+ Route.get(name, "/posts");
47
+ });
48
+
49
+ expect(Klaim[apiName][name]).toBeDefined();
50
+ });
51
+
52
+ it("should set the headers correctly", () => {
53
+ const name = "testRoute";
54
+ const routePath = "/posts";
55
+ const headers = {
56
+ "Authorization": "Bearer token 123",
57
+ };
58
+
59
+ Api.create(apiName, apiUrl, () => {
60
+ Route.get(name, routePath, headers);
61
+ });
62
+
63
+ expect(Registry.i.getRoute(apiName, name).headers).toEqual(headers);
64
+ });
65
+
66
+ describe("Methods", () => {
67
+ it("should create a GET route instance with correct properties", () => {
68
+ const name = "testRoute";
69
+ const routePath = "/posts";
70
+
71
+ Api.create(apiName, apiUrl, () => {
72
+ Route.get(name, routePath);
73
+ });
74
+
75
+ expect(Registry.i.getRoute(apiName, name)).toBeDefined();
76
+ });
77
+
78
+ it("should create a POST route instance with correct properties", () => {
79
+ const name = "testRoute";
80
+ const routePath = "/posts";
81
+
82
+ Api.create(apiName, apiUrl, () => {
83
+ Route.post(name, routePath);
84
+ });
85
+
86
+ expect(Registry.i.getRoute(apiName, name)).toBeDefined();
87
+ });
88
+
89
+ it("should create a PUT route instance with correct properties", () => {
90
+ const name = "testRoute";
91
+ const routePath = "/posts";
92
+
93
+ Api.create(apiName, apiUrl, () => {
94
+ Route.put(name, routePath);
95
+ });
96
+
97
+ expect(Registry.i.getRoute(apiName, name)).toBeDefined();
98
+ });
99
+
100
+ it("should create a DELETE route instance with correct properties", () => {
101
+ const name = "testRoute";
102
+ const routePath = "/posts";
103
+
104
+ Api.create(apiName, apiUrl, () => {
105
+ Route.delete(name, routePath);
106
+ });
107
+
108
+ expect(Registry.i.getRoute(apiName, name)).toBeDefined();
109
+ });
110
+
111
+ it("should create a PATCH route instance with correct properties", () => {
112
+ const name = "testRoute";
113
+ const routePath = "/posts";
114
+
115
+ Api.create(apiName, apiUrl, () => {
116
+ Route.patch(name, routePath);
117
+ });
118
+
119
+ expect(Registry.i.getRoute(apiName, name)).toBeDefined();
120
+ });
121
+
122
+ it("should create a OPTIONS route instance with correct properties", () => {
123
+ const name = "testRoute";
124
+ const routePath = "/posts";
125
+
126
+ Api.create(apiName, apiUrl, () => {
127
+ Route.options(name, routePath);
128
+ });
129
+
130
+ expect(Registry.i.getRoute(apiName, name)).toBeDefined();
131
+ });
132
+ });
133
+
134
+ describe("Arguments", () => {
135
+ it("should set the arguments correctly", () => {
136
+ const name = "testRoute";
137
+ const routePath = "/post/[id]";
138
+ const args = new Set(["id"]);
139
+
140
+ Api.create(apiName, apiUrl, () => {
141
+ Route.get(name, routePath, {});
142
+ });
143
+
144
+ expect(Registry.i.getRoute(apiName, name).arguments).toEqual(args);
145
+ });
146
+
147
+ it("should set the arguments correctly with multiple arguments", () => {
148
+ const name = "testRoute";
149
+ const routePath = "/post/[id]/[name]";
150
+ const args = new Set(["id", "name"]);
151
+
152
+ Api.create(apiName, apiUrl, () => {
153
+ Route.get(name, routePath, {});
154
+ });
155
+
156
+ expect(Registry.i.getRoute(apiName, name).arguments).toEqual(args);
157
+ });
158
+ });
159
+
160
+ await describe("Middlewares", async () => {
161
+ it("should set the before middleware correctly", () => {
162
+ const name = "testRoute";
163
+ const routePath = "/posts";
164
+ const before = () => {};
165
+
166
+ Api.create(apiName, apiUrl, () => {
167
+ Route.get(name, routePath, {}).before(before);
168
+ });
169
+
170
+ expect(Registry.i.getRoute(apiName, name).callbacks.before)
171
+ .toEqual(before);
172
+ });
173
+ it("should set the after middleware correctly", () => {
174
+ const name = "testRoute";
175
+ const routePath = "/posts";
176
+ const after = () => {};
177
+
178
+ Api.create(apiName, apiUrl, () => {
179
+ Route.get(name, routePath, {}).after(after);
180
+ });
181
+
182
+ expect(Registry.i.getRoute(apiName, name).callbacks.after)
183
+ .toEqual(after);
184
+ });
185
+ });
186
+ });
@@ -1,34 +1,34 @@
1
- import { describe, expect, it } from "vitest";
2
- import { Hook } from "../src/index";
3
-
4
- await describe("Route", async () => {
5
- it("should subscribe to a hook", () => {
6
- let a = 1;
7
-
8
- Hook.subscribe("testHook", () => {
9
- a++;
10
- });
11
-
12
- Hook.run("testHook");
13
- expect(a).toBe(2);
14
-
15
- Hook.run("testHook");
16
- expect(a).toBe(3);
17
- });
18
-
19
- it("should not run a hook if it is not subscribed", () => {
20
- const a = 1;
21
-
22
- Hook.run("testHook");
23
- expect(a).toBe(1);
24
- });
25
-
26
- it("should not subscribe to a hook if no callback is provided", () => {
27
- const a = 1;
28
- Hook.subscribe("testHook", undefined);
29
-
30
- Hook.run("testHook");
31
-
32
- expect(a).toBe(1);
33
- });
34
- });
1
+ import { describe, expect, it } from "vitest";
2
+ import { Hook } from "../src/index";
3
+
4
+ await describe("Route", async () => {
5
+ it("should subscribe to a hook", () => {
6
+ let a = 1;
7
+
8
+ Hook.subscribe("testHook", () => {
9
+ a++;
10
+ });
11
+
12
+ Hook.run("testHook");
13
+ expect(a).toBe(2);
14
+
15
+ Hook.run("testHook");
16
+ expect(a).toBe(3);
17
+ });
18
+
19
+ it("should not run a hook if it is not subscribed", () => {
20
+ const a = 1;
21
+
22
+ Hook.run("testHook");
23
+ expect(a).toBe(1);
24
+ });
25
+
26
+ it("should not subscribe to a hook if no callback is provided", () => {
27
+ const a = 1;
28
+ Hook.subscribe("testHook", undefined);
29
+
30
+ Hook.run("testHook");
31
+
32
+ expect(a).toBe(1);
33
+ });
34
+ });
@@ -1,96 +1,96 @@
1
- import { describe, expect, it } from "vitest";
2
- import { Api, Klaim, Route } from "../src/index";
3
-
4
- const apiName = "testApi";
5
- const apiUrl = "https://jsonplaceholder.typicode.com";
6
-
7
- const routeName = "testRoute";
8
- const routeUrl = "/todos/1";
9
- const routeUrlWithArg = "/todos/[id]";
10
- const res = {
11
- userId: 1,
12
- id: 1,
13
- title: "delectus aut autem",
14
- completed: false,
15
- };
16
-
17
- const res2 = {
18
- userId: 1,
19
- id: 2,
20
- title: "quis ut nam facilis et officia qui",
21
- completed: false,
22
- };
23
-
24
- await describe("Route", async () => {
25
- it("should call the API", async () => {
26
- Api.create(apiName, apiUrl, () => {
27
- Route.get(routeName, routeUrl);
28
- });
29
-
30
- expect(Klaim[apiName]).toBeDefined();
31
- expect(Klaim[apiName][routeName]).toBeDefined();
32
- expect(Klaim[apiName][routeName]).toBeInstanceOf(Function);
33
- expect(Klaim[apiName][routeName]()).toBeInstanceOf(Promise);
34
- expect(Klaim[apiName][routeName]()).resolves.toEqual(res);
35
- });
36
-
37
- it("should have a body if post but not if get", async () => {
38
- const apiNameA = `${apiName}A`;
39
- const routeNameA = `${routeName}A`;
40
-
41
- const apiNameB = `${apiName}B`;
42
- const routeNameB = `${routeName}B`;
43
-
44
- Api.create(apiNameA, apiUrl, () => {
45
- Route.get(routeNameA, routeUrl);
46
- });
47
-
48
- Api.create(apiNameB, apiUrl, () => {
49
- Route.post(routeNameB, routeUrl);
50
- });
51
-
52
- expect(Klaim[apiNameA][routeNameA]()).resolves.toEqual(res);
53
- expect(Klaim[apiNameB][routeNameB]()).resolves.toEqual({});
54
- });
55
-
56
- it("should can have arguments", async () => {
57
- Api.create(apiName, apiUrl, () => {
58
- Route.get(routeName, routeUrlWithArg);
59
- });
60
-
61
- expect(Klaim[apiName][routeName]({ id: 1 })).resolves.toEqual(res);
62
- expect(Klaim[apiName][routeName]({ id: 2 })).resolves.toEqual(res2);
63
- // if pass no argument, it should fail
64
- expect(Klaim[apiName][routeName]()).rejects.toThrow();
65
- });
66
-
67
- it("should call the before and after hooks", async () => {
68
- let a = 0;
69
- Api.create(apiName, apiUrl, () => {
70
- Route.get(routeName, routeUrl)
71
- .before(() => {
72
- a = 1;
73
- });
74
- });
75
-
76
- expect(a).toEqual(0);
77
- Klaim[apiName][routeName]();
78
- expect(a).toEqual(1);
79
- });
80
-
81
- it("should call the after hook", async () => {
82
- let a = 0;
83
- Api.create(apiName, apiUrl, () => {
84
- Route.get(routeName, routeUrl)
85
- .after(() => {
86
- a = 1;
87
- });
88
- });
89
-
90
- expect(a).toEqual(0);
91
- Klaim[apiName][routeName]();
92
- expect(a).toEqual(0);
93
- await Klaim[apiName][routeName]();
94
- expect(a).toEqual(1);
95
- });
96
- });
1
+ import { describe, expect, it } from "vitest";
2
+ import { Api, Klaim, Route } from "../src/index";
3
+
4
+ const apiName = "testApi";
5
+ const apiUrl = "https://jsonplaceholder.typicode.com";
6
+
7
+ const routeName = "testRoute";
8
+ const routeUrl = "/todos/1";
9
+ const routeUrlWithArg = "/todos/[id]";
10
+ const res = {
11
+ userId: 1,
12
+ id: 1,
13
+ title: "delectus aut autem",
14
+ completed: false,
15
+ };
16
+
17
+ const res2 = {
18
+ userId: 1,
19
+ id: 2,
20
+ title: "quis ut nam facilis et officia qui",
21
+ completed: false,
22
+ };
23
+
24
+ await describe("Route", async () => {
25
+ it("should call the API", async () => {
26
+ Api.create(apiName, apiUrl, () => {
27
+ Route.get(routeName, routeUrl);
28
+ });
29
+
30
+ expect(Klaim[apiName]).toBeDefined();
31
+ expect(Klaim[apiName][routeName]).toBeDefined();
32
+ expect(Klaim[apiName][routeName]).toBeInstanceOf(Function);
33
+ expect(Klaim[apiName][routeName]()).toBeInstanceOf(Promise);
34
+ expect(Klaim[apiName][routeName]()).resolves.toEqual(res);
35
+ });
36
+
37
+ it("should have a body if post but not if get", async () => {
38
+ const apiNameA = `${apiName}A`;
39
+ const routeNameA = `${routeName}A`;
40
+
41
+ const apiNameB = `${apiName}B`;
42
+ const routeNameB = `${routeName}B`;
43
+
44
+ Api.create(apiNameA, apiUrl, () => {
45
+ Route.get(routeNameA, routeUrl);
46
+ });
47
+
48
+ Api.create(apiNameB, apiUrl, () => {
49
+ Route.post(routeNameB, routeUrl);
50
+ });
51
+
52
+ expect(Klaim[apiNameA][routeNameA]()).resolves.toEqual(res);
53
+ expect(Klaim[apiNameB][routeNameB]()).resolves.toEqual({});
54
+ });
55
+
56
+ it("should can have arguments", async () => {
57
+ Api.create(apiName, apiUrl, () => {
58
+ Route.get(routeName, routeUrlWithArg);
59
+ });
60
+
61
+ expect(Klaim[apiName][routeName]({ id: 1 })).resolves.toEqual(res);
62
+ expect(Klaim[apiName][routeName]({ id: 2 })).resolves.toEqual(res2);
63
+ // if pass no argument, it should fail
64
+ expect(Klaim[apiName][routeName]()).rejects.toThrow();
65
+ });
66
+
67
+ it("should call the before and after hooks", async () => {
68
+ let a = 0;
69
+ Api.create(apiName, apiUrl, () => {
70
+ Route.get(routeName, routeUrl)
71
+ .before(() => {
72
+ a = 1;
73
+ });
74
+ });
75
+
76
+ expect(a).toEqual(0);
77
+ Klaim[apiName][routeName]();
78
+ expect(a).toEqual(1);
79
+ });
80
+
81
+ it("should call the after hook", async () => {
82
+ let a = 0;
83
+ Api.create(apiName, apiUrl, () => {
84
+ Route.get(routeName, routeUrl)
85
+ .after(() => {
86
+ a = 1;
87
+ });
88
+ });
89
+
90
+ expect(a).toEqual(0);
91
+ Klaim[apiName][routeName]();
92
+ expect(a).toEqual(0);
93
+ await Klaim[apiName][routeName]();
94
+ expect(a).toEqual(1);
95
+ });
96
+ });