File tree Expand file tree Collapse file tree 6 files changed +139
-0
lines changed
examples/rest-api-client-dts Expand file tree Collapse file tree 6 files changed +139
-0
lines changed Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ import { Base } from "../../index.js" ;
2
+
3
+ export const RestApiClient = Base . withPlugins ( [ requestPlugin ] ) . withDefaults ( {
4
+ userAgent : "rest-api-client/1.0.0" ,
5
+ } ) ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments