Skip to content

Commit bd6ed4a

Browse files
committed
Add typecheck example
1 parent 6097fb9 commit bd6ed4a

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

tests/typescript/typecheck.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import axios from 'axios'
2+
import { createStore, Store } from 'vuex'
3+
4+
import HalJsonVuexPlugin from '../../src/HalJsonVuexPlugin'
5+
import { State } from '../../src/storeModule'
6+
7+
import type CollectionInterface from '../../src/interfaces/CollectionInterface'
8+
import type ResourceInterface from '../../src/interfaces/ResourceInterface'
9+
10+
/* eslint-disable no-unused-expressions */
11+
12+
export type ResourceReference<T extends ResourceInterface<T>> = () => T;
13+
14+
export type CollectionType<
15+
Item extends ResourceInterface<Item>,
16+
Self extends CollectionInterface<Item, Self> = CollectionInterface<Item>,
17+
> = CollectionInterface<Item, Self>;
18+
19+
type SingleEndpointResource<T extends ResourceInterface<T>> = (params: { id: string }) => T;
20+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
21+
type QueryEndpointResources<T extends ResourceInterface<T>, Param = Record<string, any>> = (
22+
params?: Param,
23+
) => CollectionType<T>;
24+
25+
interface PeriodEntity extends ResourceInterface<PeriodEntity> {
26+
name: string
27+
}
28+
29+
interface CampEntity extends ResourceInterface<CampEntity> {
30+
organizer: string
31+
period: ResourceReference<PeriodEntity>
32+
}
33+
34+
interface RootEndpoint extends ResourceInterface<RootEndpoint> {
35+
camps: SingleEndpointResource<CampEntity> & QueryEndpointResources<CampEntity>
36+
}
37+
38+
axios.defaults.baseURL = 'http://localhost'
39+
const store: Store<State<unknown>> = createStore({})
40+
const api = HalJsonVuexPlugin(store, axios, {})
41+
42+
api.get<RootEndpoint>().camps({ id: '' }).organizer
43+
api.get<RootEndpoint>().camps({ id: '' }).$reload().then((camp) => camp.organizer)
44+
api.get<RootEndpoint>().camps({ id: '' }).period().name
45+
api.get<RootEndpoint>().camps({ id: '' }).period()._meta.load.then((period) => period.name)
46+
api.get<RootEndpoint>().camps().items.map((item) => item.organizer)

0 commit comments

Comments
 (0)