-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProxy.ts
33 lines (27 loc) · 875 Bytes
/
Proxy.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
interface IPaymentAPI {
getPaymenyDetail(id: number): IPaymentDetail | undefined;
}
interface IPaymentDetail {
id: number;
sum: number;
}
class PaymentAPI implements IPaymentAPI {
private data = [{id: 1, sum: 1000 }]
getPaymenyDetail(id: number): IPaymentDetail | undefined {
return this.data.find(d => d.id === id)
}
}
class PaymentAccessProxy implements IPaymentAPI {
constructor(private api: PaymentAPI, private userId: number) {}
getPaymenyDetail(id: number): IPaymentDetail | undefined{
if (this.userId === 1) {
return this.api.getPaymenyDetail(id)
}
console.log('Попытка получить данные платежа')
return undefined
}
}
const proxy = new PaymentAccessProxy(new PaymentAPI(), 1)
console.log(proxy.getPaymenyDetail(1))
const proxy2 = new PaymentAccessProxy(new PaymentAPI(), 2)
console.log(proxy.getPaymenyDetail(2))