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.
- package/Makefile +39 -14
- package/README.md +262 -262
- package/deno.json +38 -38
- package/package.json +10 -10
- package/src/core/Cache.ts +72 -72
- package/src/core/Element.ts +150 -150
- package/src/core/Klaim.ts +222 -222
- package/src/core/Route.ts +165 -165
- package/src/tools/fetchWithCache.ts +30 -30
- package/tests/01.api.test.ts +60 -60
- package/tests/02.route.test.ts +186 -186
- package/tests/03.hook.test.ts +34 -34
- package/tests/04.klaim.test.ts +96 -96
- package/tests/05.cache.test.ts +46 -46
- package/tests/06.retry.test.ts +54 -54
- package/tests/07.validate.test.ts +61 -61
package/tests/05.cache.test.ts
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
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://lorem-json.com/api/";
|
|
6
|
-
|
|
7
|
-
const routeName = "testRoute";
|
|
8
|
-
const routeUrl = "/json";
|
|
9
|
-
const routeBody = {
|
|
10
|
-
"name": "{{name()}}",
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
await describe("Cache", async () => {
|
|
14
|
-
it("should not cache the API response", async () => {
|
|
15
|
-
Api.create(apiName, apiUrl, () => {
|
|
16
|
-
Route.post(routeName, routeUrl);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
const { name: nameA } = await Klaim[apiName][routeName]({}, routeBody);
|
|
20
|
-
const { name: nameB } = await Klaim[apiName][routeName]({}, routeBody);
|
|
21
|
-
|
|
22
|
-
expect(nameA).not.toEqual(nameB);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it("should keep the API response in cache", async () => {
|
|
26
|
-
Api.create(apiName, apiUrl, () => {
|
|
27
|
-
Route.post(routeName, routeUrl);
|
|
28
|
-
}).withCache();
|
|
29
|
-
|
|
30
|
-
const { name: nameA } = await Klaim[apiName][routeName]({}, routeBody);
|
|
31
|
-
const { name: nameB } = await Klaim[apiName][routeName]({}, routeBody);
|
|
32
|
-
|
|
33
|
-
expect(nameA).toEqual(nameB);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it("should keep the route response in cache", async () => {
|
|
37
|
-
Api.create(apiName, apiUrl, () => {
|
|
38
|
-
Route.post(routeName, routeUrl).withCache();
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
const { name: nameA } = await Klaim[apiName][routeName]({}, routeBody);
|
|
42
|
-
const { name: nameB } = await Klaim[apiName][routeName]({}, routeBody);
|
|
43
|
-
|
|
44
|
-
expect(nameA).toEqual(nameB);
|
|
45
|
-
});
|
|
46
|
-
});
|
|
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://lorem-json.com/api/";
|
|
6
|
+
|
|
7
|
+
const routeName = "testRoute";
|
|
8
|
+
const routeUrl = "/json";
|
|
9
|
+
const routeBody = {
|
|
10
|
+
"name": "{{name()}}",
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
await describe("Cache", async () => {
|
|
14
|
+
it("should not cache the API response", async () => {
|
|
15
|
+
Api.create(apiName, apiUrl, () => {
|
|
16
|
+
Route.post(routeName, routeUrl);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const { name: nameA } = await Klaim[apiName][routeName]({}, routeBody);
|
|
20
|
+
const { name: nameB } = await Klaim[apiName][routeName]({}, routeBody);
|
|
21
|
+
|
|
22
|
+
expect(nameA).not.toEqual(nameB);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should keep the API response in cache", async () => {
|
|
26
|
+
Api.create(apiName, apiUrl, () => {
|
|
27
|
+
Route.post(routeName, routeUrl);
|
|
28
|
+
}).withCache();
|
|
29
|
+
|
|
30
|
+
const { name: nameA } = await Klaim[apiName][routeName]({}, routeBody);
|
|
31
|
+
const { name: nameB } = await Klaim[apiName][routeName]({}, routeBody);
|
|
32
|
+
|
|
33
|
+
expect(nameA).toEqual(nameB);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should keep the route response in cache", async () => {
|
|
37
|
+
Api.create(apiName, apiUrl, () => {
|
|
38
|
+
Route.post(routeName, routeUrl).withCache();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const { name: nameA } = await Klaim[apiName][routeName]({}, routeBody);
|
|
42
|
+
const { name: nameB } = await Klaim[apiName][routeName]({}, routeBody);
|
|
43
|
+
|
|
44
|
+
expect(nameA).toEqual(nameB);
|
|
45
|
+
});
|
|
46
|
+
});
|
package/tests/06.retry.test.ts
CHANGED
|
@@ -1,54 +1,54 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { Api, Klaim, Route } from "../src/index";
|
|
3
|
-
|
|
4
|
-
const apiName = "testApi";
|
|
5
|
-
const apiUrl = "https://myfakeapi.jardin-des-slimes.com";
|
|
6
|
-
|
|
7
|
-
const routeName = "testRoute";
|
|
8
|
-
const routeUrl = "/fake/1";
|
|
9
|
-
|
|
10
|
-
await describe("Retry", async () => {
|
|
11
|
-
it("should just fail", async () => {
|
|
12
|
-
let a = 0;
|
|
13
|
-
Api.create(apiName, apiUrl, () => {
|
|
14
|
-
Route.post(routeName, routeUrl)
|
|
15
|
-
.onCall(() => a++);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
expect(Klaim[apiName][routeName]()).rejects.toThrow();
|
|
19
|
-
expect(a).toEqual(1);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it.only("should retry api", async () => {
|
|
23
|
-
let a = 0;
|
|
24
|
-
Api.create(apiName, apiUrl, () => {
|
|
25
|
-
Route.post(routeName, routeUrl)
|
|
26
|
-
.onCall(() => a++);
|
|
27
|
-
})
|
|
28
|
-
.onCall(() => a--) // Never call route > api
|
|
29
|
-
.withRetry(3);
|
|
30
|
-
|
|
31
|
-
try {
|
|
32
|
-
await Klaim[apiName][routeName]();
|
|
33
|
-
} catch (error) {
|
|
34
|
-
// Dont care
|
|
35
|
-
}
|
|
36
|
-
expect(a).toEqual(4);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it("should retry route", async () => {
|
|
40
|
-
let a = 0;
|
|
41
|
-
Api.create(apiName, apiUrl, () => {
|
|
42
|
-
Route.post(routeName, routeUrl)
|
|
43
|
-
.onCall(() => a++)
|
|
44
|
-
.withRetry(3);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
try {
|
|
48
|
-
await Klaim[apiName][routeName]();
|
|
49
|
-
} catch (error) {
|
|
50
|
-
// Dont care
|
|
51
|
-
}
|
|
52
|
-
expect(a).toEqual(4);
|
|
53
|
-
});
|
|
54
|
-
});
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { Api, Klaim, Route } from "../src/index";
|
|
3
|
+
|
|
4
|
+
const apiName = "testApi";
|
|
5
|
+
const apiUrl = "https://myfakeapi.jardin-des-slimes.com";
|
|
6
|
+
|
|
7
|
+
const routeName = "testRoute";
|
|
8
|
+
const routeUrl = "/fake/1";
|
|
9
|
+
|
|
10
|
+
await describe("Retry", async () => {
|
|
11
|
+
it("should just fail", async () => {
|
|
12
|
+
let a = 0;
|
|
13
|
+
Api.create(apiName, apiUrl, () => {
|
|
14
|
+
Route.post(routeName, routeUrl)
|
|
15
|
+
.onCall(() => a++);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
expect(Klaim[apiName][routeName]()).rejects.toThrow();
|
|
19
|
+
expect(a).toEqual(1);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it.only("should retry api", async () => {
|
|
23
|
+
let a = 0;
|
|
24
|
+
Api.create(apiName, apiUrl, () => {
|
|
25
|
+
Route.post(routeName, routeUrl)
|
|
26
|
+
.onCall(() => a++);
|
|
27
|
+
})
|
|
28
|
+
.onCall(() => a--) // Never call route > api
|
|
29
|
+
.withRetry(3);
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
await Klaim[apiName][routeName]();
|
|
33
|
+
} catch (error) {
|
|
34
|
+
// Dont care
|
|
35
|
+
}
|
|
36
|
+
expect(a).toEqual(4);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should retry route", async () => {
|
|
40
|
+
let a = 0;
|
|
41
|
+
Api.create(apiName, apiUrl, () => {
|
|
42
|
+
Route.post(routeName, routeUrl)
|
|
43
|
+
.onCall(() => a++)
|
|
44
|
+
.withRetry(3);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
await Klaim[apiName][routeName]();
|
|
49
|
+
} catch (error) {
|
|
50
|
+
// Dont care
|
|
51
|
+
}
|
|
52
|
+
expect(a).toEqual(4);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
import {describe, it} from "vitest";
|
|
2
|
-
import {Api, Klaim, Route} from "../src/index";
|
|
3
|
-
import * as yup from "yup";
|
|
4
|
-
|
|
5
|
-
const apiName = "testApi";
|
|
6
|
-
const apiUrl = "https://jsonplaceholder.typicode.com";
|
|
7
|
-
|
|
8
|
-
const routeName = "testRoute";
|
|
9
|
-
const routeUrl = "todos/[id]";
|
|
10
|
-
|
|
11
|
-
const schema = yup.object().shape({
|
|
12
|
-
userId: yup.number().required(),
|
|
13
|
-
id: yup.number().min(1).max(10).required(),
|
|
14
|
-
title: yup.string().required(),
|
|
15
|
-
completed: yup.boolean().required()
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
describe("Validate Yup", async () => {
|
|
19
|
-
it("should not fail", async () => {
|
|
20
|
-
Api.create(apiName, apiUrl, () => {
|
|
21
|
-
Route.get(routeName, routeUrl);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
// Expect the validation to not fail the promise
|
|
25
|
-
expect(await Klaim[apiName][routeName]({id: 1})).toStrictEqual({
|
|
26
|
-
userId: 1,
|
|
27
|
-
id: 1,
|
|
28
|
-
title: "delectus aut autem",
|
|
29
|
-
completed: false
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it("should fail", async () => {
|
|
34
|
-
Api.create(apiName, apiUrl, () => {
|
|
35
|
-
Route.get(routeName, routeUrl).validate(schema);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
// Expect the validation to fail the promise
|
|
39
|
-
await expect(Klaim[apiName][routeName]({id: 15})).rejects.toThrow();
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it("should reformat if possible", async () => {
|
|
43
|
-
Api.create(apiName, apiUrl, () => {
|
|
44
|
-
Route.get(routeName, routeUrl).validate(yup.object().shape({
|
|
45
|
-
userId: yup.number().required(),
|
|
46
|
-
id: yup.string().required(),
|
|
47
|
-
title: yup.string().required(),
|
|
48
|
-
completed: yup.boolean().required()
|
|
49
|
-
})
|
|
50
|
-
);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
// Expect the id to be a string
|
|
54
|
-
expect(await Klaim[apiName][routeName]({id: "1"})).toStrictEqual({
|
|
55
|
-
userId: 1,
|
|
56
|
-
id: "1",
|
|
57
|
-
title: "delectus aut autem",
|
|
58
|
-
completed: false
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
});
|
|
1
|
+
import {describe, it} from "vitest";
|
|
2
|
+
import {Api, Klaim, Route} from "../src/index";
|
|
3
|
+
import * as yup from "yup";
|
|
4
|
+
|
|
5
|
+
const apiName = "testApi";
|
|
6
|
+
const apiUrl = "https://jsonplaceholder.typicode.com";
|
|
7
|
+
|
|
8
|
+
const routeName = "testRoute";
|
|
9
|
+
const routeUrl = "todos/[id]";
|
|
10
|
+
|
|
11
|
+
const schema = yup.object().shape({
|
|
12
|
+
userId: yup.number().required(),
|
|
13
|
+
id: yup.number().min(1).max(10).required(),
|
|
14
|
+
title: yup.string().required(),
|
|
15
|
+
completed: yup.boolean().required()
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe("Validate Yup", async () => {
|
|
19
|
+
it("should not fail", async () => {
|
|
20
|
+
Api.create(apiName, apiUrl, () => {
|
|
21
|
+
Route.get(routeName, routeUrl);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// Expect the validation to not fail the promise
|
|
25
|
+
expect(await Klaim[apiName][routeName]({id: 1})).toStrictEqual({
|
|
26
|
+
userId: 1,
|
|
27
|
+
id: 1,
|
|
28
|
+
title: "delectus aut autem",
|
|
29
|
+
completed: false
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("should fail", async () => {
|
|
34
|
+
Api.create(apiName, apiUrl, () => {
|
|
35
|
+
Route.get(routeName, routeUrl).validate(schema);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Expect the validation to fail the promise
|
|
39
|
+
await expect(Klaim[apiName][routeName]({id: 15})).rejects.toThrow();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("should reformat if possible", async () => {
|
|
43
|
+
Api.create(apiName, apiUrl, () => {
|
|
44
|
+
Route.get(routeName, routeUrl).validate(yup.object().shape({
|
|
45
|
+
userId: yup.number().required(),
|
|
46
|
+
id: yup.string().required(),
|
|
47
|
+
title: yup.string().required(),
|
|
48
|
+
completed: yup.boolean().required()
|
|
49
|
+
})
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Expect the id to be a string
|
|
54
|
+
expect(await Klaim[apiName][routeName]({id: "1"})).toStrictEqual({
|
|
55
|
+
userId: 1,
|
|
56
|
+
id: "1",
|
|
57
|
+
title: "delectus aut autem",
|
|
58
|
+
completed: false
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|