Skip to content

Commit d536226

Browse files
committed
feat: RestApiClient example
1 parent 352c1ae commit d536226

File tree

6 files changed

+139
-0
lines changed

6 files changed

+139
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Custom class with defaults and plugins (TypeScript Declaration example)
2+
3+
This example does not implement any code, it's meant as a reference for types only.
4+
5+
Usage example:
6+
7+
```js
8+
import { RestApiClient } from "javascript-plugin-architecture-with-typescript-definitions/examples/rest-api-client-dts";
9+
10+
const client = new RestApiClient({
11+
baseUrl: "https://api.github.com",
12+
userAgent: "my-app/1.0.0",
13+
headers: {
14+
authorization: "token ghp_aB3...",
15+
},
16+
});
17+
18+
const { data } = await client.request("GET /user");
19+
console.log("You are logged in as %s", data.login);
20+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Base } from "../../index.js";
2+
3+
import { requestPlugin } from "./request-plugin.js";
4+
5+
declare type Constructor<T> = new (...args: any[]) => T;
6+
7+
export class RestApiClient extends Base {
8+
request: ReturnType<typeof requestPlugin>["request"];
9+
}

examples/rest-api-client-dts/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Base } from "../../index.js";
2+
3+
export const RestApiClient = Base.withPlugins([requestPlugin]).withDefaults({
4+
userAgent: "rest-api-client/1.0.0",
5+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { expectType } from "tsd";
2+
3+
import { RestApiClient } from "./index.js";
4+
5+
// @ts-expect-error - An argument for 'options' was not provided
6+
new RestApiClient();
7+
8+
expectType<{ userAgent: string }>(RestApiClient.defaults);
9+
10+
// @ts-expect-error - Type '{}' is missing the following properties from type 'Options': myRequiredUserOption
11+
new RestApiClient({});
12+
13+
new RestApiClient({
14+
baseUrl: "https://api.github.com",
15+
userAgent: "my-app/v1.0.0",
16+
});
17+
18+
export async function test() {
19+
const client = new RestApiClient({
20+
baseUrl: "https://api.github.com",
21+
headers: {
22+
authorization: "token 123456789",
23+
},
24+
});
25+
26+
expectType<
27+
Promise<{
28+
status: number;
29+
headers: Record<string, string>;
30+
data?: Record<string, unknown>;
31+
}>
32+
>(client.request(""));
33+
34+
const getUserResponse = await client.request("GET /user");
35+
expectType<{
36+
status: number;
37+
headers: Record<string, string>;
38+
data?: Record<string, unknown>;
39+
}>(getUserResponse);
40+
41+
client.request("GET /repos/{owner}/{repo}", {
42+
owner: "gr2m",
43+
repo: "javascript-plugin-architecture-with-typescript-definitions",
44+
});
45+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Base } from "../../index.js";
2+
3+
declare module "../.." {
4+
namespace Base {
5+
interface Options {
6+
/**
7+
* Base URL for all http requests
8+
*/
9+
baseUrl: string;
10+
11+
/**
12+
* Set a custom user agent. Defaults to "rest-api-client/1.0.0"
13+
*/
14+
userAgent?: string;
15+
16+
/**
17+
* Optional http request headers that will be set on all requsets
18+
*/
19+
headers?: {
20+
authorization?: string;
21+
accept?: string;
22+
[key: string]: string | undefined;
23+
};
24+
}
25+
}
26+
}
27+
28+
interface Response {
29+
status: number;
30+
headers: Record<string, string>;
31+
data?: Record<string, unknown>;
32+
}
33+
34+
interface Parameters {
35+
headers?: Record<string, string>;
36+
[parameter: string]: unknown;
37+
}
38+
39+
interface RequestInterface {
40+
(route: string, parameters?: Parameters): Promise<Response>;
41+
}
42+
43+
export declare function requestPlugin(
44+
base: Base,
45+
options: Base.Options
46+
): {
47+
request: RequestInterface;
48+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* This example does not implement any logic, it's just meant as
3+
* a reference for its types
4+
*
5+
* @param {Base} base
6+
* @param {Base.Options} options
7+
*/
8+
export function requestPlugin(base, options) {
9+
return {
10+
async request(route, parameters) {},
11+
};
12+
}

0 commit comments

Comments
 (0)