Skip to content

Commit 990a3fd

Browse files
authored
feat: add cron job endpoints (#18)
* chore(types): added type definition for cron type * lib(util): added request function generator for primitive endpoints * feat: add cron function to class * chore(release): bump package version to 1.3.0 * refractor: add better wording for function comment * chore(tests): add tests for new primitive endpoint util
1 parent 68e2a32 commit 990a3fd

File tree

5 files changed

+82
-3
lines changed

5 files changed

+82
-3
lines changed

__tests__/utils/request-generator.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,36 @@ import "__mocks__/utils/request";
22

33
import {
44
generateDynamicRequestFn,
5+
generatePrimitiveRequestFn,
56
generateSingleRequestFn,
67
} from "utils/request-generator";
78

89
import { it, expect } from "bun:test";
9-
import type { Planet } from "types/api-entities";
10+
import type { Cron, Planet } from "types/api-entities";
11+
12+
it("PrimitiveRequestFn works as expected", async () => {
13+
// @ts-expect-error
14+
const primitiveRequest = generatePrimitiveRequestFn<Cron>("/api/crons");
15+
16+
const response1 = await primitiveRequest("refresh_from_source");
17+
const response2 = await primitiveRequest();
18+
19+
expect(response1.ok).toBe(true);
20+
expect(response2.ok).toBe(true);
21+
22+
expect(response1.url).toBe("/api/crons/refresh_from_source");
23+
expect(response2.url).toBe("/api/crons");
24+
25+
const json1 = await response1.json();
26+
const json2 = await response2.json();
27+
28+
expect(json1).toHaveProperty("data", null);
29+
expect(json1).toHaveProperty("error", null);
30+
31+
expect(json2).toHaveProperty("data", null);
32+
expect(json2).toHaveProperty("error", null);
33+
expect(json2).toHaveProperty("pagination", null);
34+
});
1035

1136
it("SingleRequestFn works as expected", async () => {
1237
const singleRequest = generateSingleRequestFn<Planet>("https://example.com");

index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { request } from "utils/request";
22

33
import {
4-
generateDynamicRequestFn,
54
generateSingleRequestFn,
5+
generateDynamicRequestFn,
6+
generatePrimitiveRequestFn,
67
} from "utils/request-generator";
78

89
import type {
910
War,
1011
Stat,
12+
Cron,
1113
Biome,
1214
Order,
1315
Effect,
@@ -169,6 +171,15 @@ class SDK {
169171
* Endpoint: `/api/effects(/:id)`
170172
*/
171173
effects = generateDynamicRequestFn<Effect>("effects");
174+
175+
/**
176+
* ### Crons
177+
*
178+
* Allows you to track the internal cron jobs. Fine tune your periodic requests to the
179+
* API and avoid hitting request limits by over fetching.
180+
*/
181+
// @ts-ignore
182+
crons = generatePrimitiveRequestFn<Cron>("crons");
172183
}
173184

174185
const HellHub = SDK.getInstance();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"license": "MIT",
44
"private": false,
55
"description": "The official SDK for HellHub API. Filter and collect data with full type safety out of the box.",
6-
"version": "1.2.1",
6+
"version": "1.3.0",
77
"main": "dist/index.mjs",
88
"types": "dist/index.d.ts",
99
"keywords": [

types/api-entities.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ import type { QueryOptions } from "types/query-generator";
44
* Base types
55
*/
66

7+
export interface Cron {
8+
name: string;
9+
pattern: string;
10+
status: "ok" | "stopped";
11+
busy: boolean;
12+
runs: {
13+
next: number | null;
14+
current: number | null;
15+
previous: number | null;
16+
};
17+
}
18+
719
export interface Entity {
820
id: number;
921
createdAt: string;

utils/request-generator.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,37 @@ import type {
77
APIRequestInit,
88
} from "types/api-entities";
99

10+
/**
11+
* Creates a request function for a given endpoint, which can be used to fetch
12+
* from a endpoint that does not support any query parameters.
13+
*/
14+
export function generatePrimitiveRequestFn<T extends Entity>(entity: string) {
15+
async function requestFn(
16+
input?: Omit<APIRequestInit<T[]>, "query">,
17+
options?: undefined,
18+
): Promise<APIResponse<T[]>>;
19+
20+
async function requestFn(
21+
input: number | string,
22+
options?: Omit<APIRequestInit<T[]>, "query">,
23+
): Promise<APIResponse<T>>;
24+
25+
async function requestFn(
26+
input: Omit<APIRequestInit<T[]>, "query"> | undefined | number | string,
27+
options?: APIRequestInit<T[]> | APIRequestInit<T>,
28+
): Promise<APIResponse<T[]> | APIResponse<T>> {
29+
if (typeof input === "object" || input === undefined) {
30+
return request(entity, { ...input }) as Promise<APIResponse<T[]>>;
31+
} else {
32+
return request(`${entity}/${input}`, {
33+
...(options as APIRequestInit<T>),
34+
}) as Promise<APIResponse<T>>;
35+
}
36+
}
37+
38+
return requestFn;
39+
}
40+
1041
/**
1142
* Creates a request function for a given entity, which can be used to fetch
1243
* data from the API.

0 commit comments

Comments
 (0)