@ripwords/myinvois-client 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bun.lock +0 -11
- package/dist/cjs/index.cjs +26 -505
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/index.js +26 -498
- package/package.json +10 -13
- package/src/utils/MyInvoisClient.ts +26 -24
- package/test/MyInvoiClientRealData.test.ts +19 -0
- package/test/MyInvoisClient.test.ts +52 -32
- package/vitest.config.ts +3 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { MyInvoisClient } from '../src/utils/MyInvoisClient'
|
|
3
|
+
|
|
4
|
+
describe('MyInvoisClientRealData', () => {
|
|
5
|
+
it('should verify TIN with real data', async () => {
|
|
6
|
+
const client = new MyInvoisClient(
|
|
7
|
+
process.env.CLIENT_ID!,
|
|
8
|
+
process.env.CLIENT_SECRET!,
|
|
9
|
+
'sandbox',
|
|
10
|
+
)
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
await client.refreshToken()
|
|
13
|
+
const result = await client.verifyTin(
|
|
14
|
+
process.env.TIN_VALUE!,
|
|
15
|
+
process.env.NRIC_VALUE!,
|
|
16
|
+
)
|
|
17
|
+
expect(result).toBe(true)
|
|
18
|
+
})
|
|
19
|
+
})
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
2
2
|
import { MyInvoisClient } from '../src/utils/MyInvoisClient'
|
|
3
|
-
import { ofetch } from 'ofetch/node'
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
// Mock global fetch
|
|
5
|
+
const mockFetch = vi.fn()
|
|
6
|
+
vi.stubGlobal('fetch', mockFetch)
|
|
8
7
|
|
|
9
8
|
vi.useFakeTimers()
|
|
10
9
|
|
|
@@ -24,7 +23,7 @@ describe('MyInvoisClient', () => {
|
|
|
24
23
|
'sandbox',
|
|
25
24
|
)
|
|
26
25
|
expect((sandboxClient as any).baseUrl).toBe(
|
|
27
|
-
'https://preprod-
|
|
26
|
+
'https://preprod-api.myinvois.hasil.gov.my',
|
|
28
27
|
)
|
|
29
28
|
})
|
|
30
29
|
|
|
@@ -34,7 +33,9 @@ describe('MyInvoisClient', () => {
|
|
|
34
33
|
process.env.CLIENT_SECRET!,
|
|
35
34
|
'production',
|
|
36
35
|
)
|
|
37
|
-
expect((prodClient as any).baseUrl).toBe(
|
|
36
|
+
expect((prodClient as any).baseUrl).toBe(
|
|
37
|
+
'https://api.myinvois.hasil.gov.my',
|
|
38
|
+
)
|
|
38
39
|
})
|
|
39
40
|
})
|
|
40
41
|
|
|
@@ -45,12 +46,15 @@ describe('MyInvoisClient', () => {
|
|
|
45
46
|
expires_in: 3600,
|
|
46
47
|
}
|
|
47
48
|
|
|
48
|
-
|
|
49
|
+
mockFetch.mockResolvedValueOnce({
|
|
50
|
+
ok: true,
|
|
51
|
+
json: () => Promise.resolve(mockToken),
|
|
52
|
+
} as Response)
|
|
49
53
|
|
|
50
54
|
await client.verifyTin('123', '456')
|
|
51
55
|
|
|
52
|
-
expect(
|
|
53
|
-
'https://preprod-
|
|
56
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
57
|
+
'https://preprod-api.myinvois.hasil.gov.my/connect/token',
|
|
54
58
|
expect.any(Object),
|
|
55
59
|
)
|
|
56
60
|
})
|
|
@@ -61,40 +65,45 @@ describe('MyInvoisClient', () => {
|
|
|
61
65
|
expires_in: 3600,
|
|
62
66
|
}
|
|
63
67
|
vi.advanceTimersByTime(8000)
|
|
64
|
-
|
|
65
|
-
.mockResolvedValueOnce(
|
|
66
|
-
|
|
68
|
+
mockFetch
|
|
69
|
+
.mockResolvedValueOnce({
|
|
70
|
+
ok: true,
|
|
71
|
+
json: () => Promise.resolve(mockToken),
|
|
72
|
+
} as Response)
|
|
73
|
+
.mockResolvedValueOnce({
|
|
74
|
+
ok: true,
|
|
75
|
+
json: () => Promise.resolve(undefined),
|
|
76
|
+
} as Response)
|
|
67
77
|
|
|
68
78
|
await client.verifyTin('123', '456')
|
|
69
79
|
|
|
70
80
|
// Check first call (token request)
|
|
71
|
-
expect(
|
|
81
|
+
expect(mockFetch).toHaveBeenNthCalledWith(
|
|
72
82
|
1,
|
|
73
|
-
'https://preprod-
|
|
83
|
+
'https://preprod-api.myinvois.hasil.gov.my/connect/token',
|
|
74
84
|
{
|
|
75
85
|
method: 'POST',
|
|
76
86
|
headers: {
|
|
77
87
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
78
88
|
},
|
|
79
|
-
body: {
|
|
89
|
+
body: new URLSearchParams({
|
|
80
90
|
grant_type: 'client_credentials',
|
|
81
91
|
client_id: 'test-id',
|
|
82
92
|
client_secret: 'test-secret',
|
|
83
93
|
scope: 'InvoicingAPI',
|
|
84
|
-
},
|
|
94
|
+
}),
|
|
85
95
|
},
|
|
86
96
|
)
|
|
87
97
|
|
|
88
98
|
// Check second call (verifyTin request)
|
|
89
|
-
expect(
|
|
99
|
+
expect(mockFetch).toHaveBeenNthCalledWith(
|
|
90
100
|
2,
|
|
91
|
-
`https://preprod-
|
|
101
|
+
`https://preprod-api.myinvois.hasil.gov.my/api/v1.0/taxpayer/validate/123?idType=NRIC&idValue=456`,
|
|
92
102
|
{
|
|
93
103
|
method: 'GET',
|
|
94
104
|
headers: {
|
|
95
105
|
Authorization: `Bearer ${mockToken.access_token}`,
|
|
96
106
|
},
|
|
97
|
-
responseType: 'json',
|
|
98
107
|
},
|
|
99
108
|
)
|
|
100
109
|
})
|
|
@@ -105,7 +114,10 @@ describe('MyInvoisClient', () => {
|
|
|
105
114
|
expires_in: 3600,
|
|
106
115
|
}
|
|
107
116
|
|
|
108
|
-
|
|
117
|
+
mockFetch.mockResolvedValueOnce({
|
|
118
|
+
ok: true,
|
|
119
|
+
json: () => Promise.resolve(mockToken),
|
|
120
|
+
} as Response)
|
|
109
121
|
|
|
110
122
|
// First call to get token
|
|
111
123
|
await client.verifyTin(process.env.TIN_VALUE!, process.env.NRIC_VALUE!)
|
|
@@ -116,8 +128,8 @@ describe('MyInvoisClient', () => {
|
|
|
116
128
|
await client.verifyTin(process.env.TIN_VALUE!, process.env.NRIC_VALUE!)
|
|
117
129
|
|
|
118
130
|
// Token endpoint should only be called once
|
|
119
|
-
expect(
|
|
120
|
-
'https://preprod-
|
|
131
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
132
|
+
'https://preprod-api.myinvois.hasil.gov.my/connect/token',
|
|
121
133
|
expect.any(Object),
|
|
122
134
|
)
|
|
123
135
|
})
|
|
@@ -130,15 +142,20 @@ describe('MyInvoisClient', () => {
|
|
|
130
142
|
expires_in: 3600,
|
|
131
143
|
}
|
|
132
144
|
|
|
133
|
-
|
|
134
|
-
.mockResolvedValueOnce(
|
|
135
|
-
|
|
145
|
+
mockFetch
|
|
146
|
+
.mockResolvedValueOnce({
|
|
147
|
+
ok: true,
|
|
148
|
+
json: () => Promise.resolve(mockToken),
|
|
149
|
+
} as Response)
|
|
150
|
+
.mockResolvedValueOnce({
|
|
151
|
+
ok: true,
|
|
152
|
+
json: () => Promise.resolve(undefined),
|
|
153
|
+
} as Response)
|
|
136
154
|
|
|
137
|
-
|
|
138
|
-
expect(result).toBe(true)
|
|
155
|
+
await client.verifyTin('123', '456')
|
|
139
156
|
|
|
140
|
-
expect(
|
|
141
|
-
`https://preprod-
|
|
157
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
158
|
+
`https://preprod-api.myinvois.hasil.gov.my/api/v1.0/taxpayer/validate/123?idType=NRIC&idValue=456`,
|
|
142
159
|
expect.objectContaining({
|
|
143
160
|
method: 'GET',
|
|
144
161
|
headers: {
|
|
@@ -154,9 +171,12 @@ describe('MyInvoisClient', () => {
|
|
|
154
171
|
expires_in: 3600,
|
|
155
172
|
}
|
|
156
173
|
|
|
157
|
-
|
|
158
|
-
.mockResolvedValueOnce(
|
|
159
|
-
|
|
174
|
+
mockFetch
|
|
175
|
+
.mockResolvedValueOnce({
|
|
176
|
+
ok: true,
|
|
177
|
+
json: () => Promise.resolve(mockToken),
|
|
178
|
+
} as Response)
|
|
179
|
+
.mockRejectedValueOnce(new Error('Invalid TIN'))
|
|
160
180
|
|
|
161
181
|
const result = await client.verifyTin(
|
|
162
182
|
process.env.TIN_VALUE!,
|