-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
55 lines (45 loc) · 1018 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import Axios from "axios"
interface Posts {
id: string;
userId: string,
title: string;
body: string;
}
interface Comment {
id: string;
postId: string,
name: string;
email: string;
body: string;
}
function Service(baseUrl: string) {
return function <T extends { new(...args: any[]): {} }>(constructor: T) {
return class extends constructor {
baseUrl = baseUrl
}
}
}
function Get(path: string) {
return function (target: any, name: string) {
const fetch = (baseUrl: string) => Axios.get(`${baseUrl}${path}`)
Object.defineProperty(target, name, {
get() {
return fetch(this.baseUrl)
}
})
}
}
@Service('https://jsonplaceholder.typicode.com')
class PlaceholderService {
@Get('/posts')
posts!: Promise<Posts[]>;
@Get('/comments')
comments!: Promise<Comment[]>;
}
const svc = new PlaceholderService();
(async () => {
const posts = await svc.posts
console.log(posts)
const comments = await svc.comments
console.log(comments)
})()