Skip to content

Commit 802e37a

Browse files
committed
Add typecheck example
1 parent 6097fb9 commit 802e37a

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

tests/typescript/typecheck.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
import MockAdapter from "axios-mock-adapter";
10+
11+
export type ResourceReference<T extends ResourceInterface<T>> = () => T;
12+
13+
export type CollectionType<
14+
Item extends ResourceInterface<Item>,
15+
Self extends CollectionInterface<Item, Self> = CollectionInterface<Item>,
16+
> = CollectionInterface<Item, Self>;
17+
18+
type SingleEndpointResource<T extends ResourceInterface<T>> = (params: { id: string }) => T;
19+
type QueryEndpointResources<T extends ResourceInterface<T>, Param = Record<string, any>> = (
20+
params?: Param,
21+
) => CollectionType<T>;
22+
23+
interface PeriodEntity extends ResourceInterface<PeriodEntity> {
24+
name: string
25+
}
26+
27+
interface CampEntity extends ResourceInterface<CampEntity> {
28+
organizer: string
29+
period: ResourceReference<PeriodEntity>
30+
}
31+
32+
interface RootEndpoint extends ResourceInterface<RootEndpoint> {
33+
camps: SingleEndpointResource<CampEntity> & QueryEndpointResources<CampEntity>
34+
}
35+
36+
axios.defaults.baseURL = 'http://localhost'
37+
const axiosMock = new MockAdapter(axios)
38+
const store: Store<State<unknown>> = createStore({})
39+
const api = HalJsonVuexPlugin(store, axios, {})
40+
41+
api.get<RootEndpoint>().camps({ id: '' }).organizer
42+
api.get<RootEndpoint>().camps({ id: '' }).$reload().then((camp) => camp.organizer)
43+
api.get<RootEndpoint>().camps({ id: '' }).period().name
44+
api.get<RootEndpoint>().camps({ id: '' }).period()._meta.load.then((period) => period.name)
45+
api.get<RootEndpoint>().camps().items.map((item) => item.organizer)

0 commit comments

Comments
 (0)