@ripwords/myinvois-client 0.0.9 → 0.0.10
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/dist/index.cjs
CHANGED
|
@@ -6,13 +6,16 @@ const getBaseUrl = (environment) => {
|
|
|
6
6
|
};
|
|
7
7
|
|
|
8
8
|
//#endregion
|
|
9
|
-
//#region src/api/platform/
|
|
10
|
-
const
|
|
11
|
-
const { clientId, clientSecret, baseUrl, debug } = client;
|
|
9
|
+
//#region src/api/platform/platformLogin.ts
|
|
10
|
+
const platformLogin = async (client) => {
|
|
11
|
+
const { clientId, clientSecret, baseUrl, onBehalfOf, debug } = client;
|
|
12
12
|
try {
|
|
13
13
|
const response = await fetch(`${baseUrl}/connect/token`, {
|
|
14
14
|
method: "POST",
|
|
15
|
-
headers: {
|
|
15
|
+
headers: {
|
|
16
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
17
|
+
...onBehalfOf ? { onbehalfof: onBehalfOf } : {}
|
|
18
|
+
},
|
|
16
19
|
body: new URLSearchParams({
|
|
17
20
|
grant_type: "client_credentials",
|
|
18
21
|
client_id: clientId,
|
|
@@ -37,20 +40,23 @@ var MyInvoisClient = class {
|
|
|
37
40
|
baseUrl;
|
|
38
41
|
clientId;
|
|
39
42
|
clientSecret;
|
|
43
|
+
onBehalfOf;
|
|
40
44
|
debug;
|
|
41
45
|
token = "";
|
|
42
46
|
tokenExpiration;
|
|
43
|
-
constructor(clientId, clientSecret, environment, debug
|
|
47
|
+
constructor(clientId, clientSecret, environment, onBehalfOf, debug) {
|
|
44
48
|
this.clientId = clientId;
|
|
45
49
|
this.clientSecret = clientSecret;
|
|
46
50
|
this.baseUrl = getBaseUrl(environment);
|
|
47
|
-
this.debug = debug;
|
|
51
|
+
this.debug = debug ?? process.env.MYINVOIS_DEBUG === "true" ? true : false;
|
|
52
|
+
this.onBehalfOf = onBehalfOf;
|
|
48
53
|
}
|
|
49
54
|
async refreshToken() {
|
|
50
|
-
const tokenResponse = await
|
|
55
|
+
const tokenResponse = await platformLogin({
|
|
51
56
|
clientId: this.clientId,
|
|
52
57
|
clientSecret: this.clientSecret,
|
|
53
58
|
baseUrl: this.baseUrl,
|
|
59
|
+
onBehalfOf: this.onBehalfOf,
|
|
54
60
|
debug: this.debug
|
|
55
61
|
});
|
|
56
62
|
this.token = tokenResponse.token;
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["environment: 'sandbox' | 'production'","client: ClientCredentials","tokenResponse: TokenResponse","clientId: string","clientSecret: string","environment: 'sandbox' | 'production'","debug
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["environment: 'sandbox' | 'production'","client: ClientCredentials","tokenResponse: TokenResponse","clientId: string","clientSecret: string","environment: 'sandbox' | 'production'","onBehalfOf?: string","debug?: boolean","path: string","options: Parameters<typeof fetch>[1]","tin: string","nric: string"],"sources":["../src/utils/getBaseUrl.ts","../src/api/platform/platformLogin.ts","../src/utils/MyInvoisClient.ts"],"sourcesContent":["export const getBaseUrl = (environment: 'sandbox' | 'production') => {\n return environment === 'sandbox'\n ? 'https://preprod-api.myinvois.hasil.gov.my'\n : 'https://api.myinvois.hasil.gov.my'\n}\n","import type { ClientCredentials, TokenResponse } from 'src/types'\n\nexport const platformLogin = async (client: ClientCredentials) => {\n const { clientId, clientSecret, baseUrl, onBehalfOf, debug } = client\n try {\n const response = await fetch(`${baseUrl}/connect/token`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/x-www-form-urlencoded',\n ...(onBehalfOf ? { onbehalfof: onBehalfOf } : {}),\n },\n body: new URLSearchParams({\n grant_type: 'client_credentials',\n client_id: clientId,\n client_secret: clientSecret,\n scope: 'InvoicingAPI',\n }),\n })\n\n const tokenResponse: TokenResponse = await response.json()\n return {\n token: tokenResponse.access_token,\n tokenExpiration: new Date(Date.now() + tokenResponse.expires_in * 1000),\n }\n } catch (error) {\n if (debug) console.error(error)\n throw error\n }\n}\n","import { getBaseUrl } from './getBaseUrl'\nimport { platformLogin } from '../api/platform/platformLogin'\n\nexport class MyInvoisClient {\n private readonly baseUrl: string\n private readonly clientId: string\n private readonly clientSecret: string\n private readonly onBehalfOf?: string\n private readonly debug: boolean\n private token = ''\n private tokenExpiration: Date | undefined\n\n constructor(\n clientId: string,\n clientSecret: string,\n environment: 'sandbox' | 'production',\n onBehalfOf?: string,\n debug?: boolean,\n ) {\n this.clientId = clientId\n this.clientSecret = clientSecret\n this.baseUrl = getBaseUrl(environment)\n this.debug = (debug ?? process.env.MYINVOIS_DEBUG === 'true') ? true : false\n this.onBehalfOf = onBehalfOf\n }\n\n private async refreshToken() {\n const tokenResponse = await platformLogin({\n clientId: this.clientId,\n clientSecret: this.clientSecret,\n baseUrl: this.baseUrl,\n onBehalfOf: this.onBehalfOf,\n debug: this.debug,\n })\n\n this.token = tokenResponse.token\n this.tokenExpiration = tokenResponse.tokenExpiration\n }\n\n private async getToken() {\n if (!this.tokenExpiration || this.tokenExpiration < new Date()) {\n if (this.debug) {\n console.log('Refreshing token')\n }\n await this.refreshToken()\n }\n\n return this.token\n }\n\n private async fetch(path: string, options: Parameters<typeof fetch>[1] = {}) {\n const token = await this.getToken()\n\n return fetch(`${this.baseUrl}${path}`, {\n ...options,\n headers: { ...options.headers, Authorization: `Bearer ${token}` },\n })\n }\n\n /**\n * Validates a TIN against a NRIC\n *\n * @param tin\n * @param nric\n * @returns true if the TIN is valid, false otherwise\n */\n async verifyTin(tin: string, nric: string): Promise<boolean> {\n try {\n const response = await this.fetch(\n `/api/v1.0/taxpayer/validate/${tin}?idType=NRIC&idValue=${nric}`,\n {\n method: 'GET',\n },\n )\n\n if (response.status === 200) {\n return true\n }\n\n return false\n } catch (error) {\n if (this.debug) {\n console.error(error)\n }\n return false\n }\n }\n}\n"],"mappings":";;;MAAa,aAAa,CAACA,gBAA0C;AACnE,QAAO,gBAAgB,YACnB,8CACA;AACL;;;;MCFY,gBAAgB,OAAOC,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AuC,CAAE,GAAE;EAC3E,MAAM,QAAQ,MAAM,KAAK,UAAU;AAEnC,SAAO,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,GAAG;GACrC,GAAG;GACH,SAAS;IAAE,GAAG,QAAQ;IAAS,gBAAgB,SAAS,MAAM;GAAG;EAClE,EAAC;CACH;;;;;;;;CASD,MAAM,UAAUC,KAAaC,MAAgC;AAC3D,MAAI;GACF,MAAM,WAAW,MAAM,KAAK,OACzB,8BAA8B,IAAI,uBAAuB,KAAK,GAC/D,EACE,QAAQ,MACT,EACF;AAED,OAAI,SAAS,WAAW,IACtB,QAAO;AAGT,UAAO;EACR,SAAQ,OAAO;AACd,OAAI,KAAK,MACP,SAAQ,MAAM,MAAM;AAEtB,UAAO;EACR;CACF;AACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -5634,6 +5634,7 @@ interface ClientCredentials {
|
|
|
5634
5634
|
clientId: string
|
|
5635
5635
|
clientSecret: string
|
|
5636
5636
|
baseUrl: string
|
|
5637
|
+
onBehalfOf?: string
|
|
5637
5638
|
debug?: boolean
|
|
5638
5639
|
}
|
|
5639
5640
|
|
|
@@ -5643,10 +5644,11 @@ declare class MyInvoisClient {
|
|
|
5643
5644
|
private readonly baseUrl;
|
|
5644
5645
|
private readonly clientId;
|
|
5645
5646
|
private readonly clientSecret;
|
|
5647
|
+
private readonly onBehalfOf?;
|
|
5646
5648
|
private readonly debug;
|
|
5647
5649
|
private token;
|
|
5648
5650
|
private tokenExpiration;
|
|
5649
|
-
constructor(clientId: string, clientSecret: string, environment: "sandbox" | "production", debug?: boolean);
|
|
5651
|
+
constructor(clientId: string, clientSecret: string, environment: "sandbox" | "production", onBehalfOf?: string, debug?: boolean);
|
|
5650
5652
|
private refreshToken;
|
|
5651
5653
|
private getToken;
|
|
5652
5654
|
private fetch;
|
package/dist/index.js
CHANGED
|
@@ -5,13 +5,16 @@ const getBaseUrl = (environment) => {
|
|
|
5
5
|
};
|
|
6
6
|
|
|
7
7
|
//#endregion
|
|
8
|
-
//#region src/api/platform/
|
|
9
|
-
const
|
|
10
|
-
const { clientId, clientSecret, baseUrl, debug } = client;
|
|
8
|
+
//#region src/api/platform/platformLogin.ts
|
|
9
|
+
const platformLogin = async (client) => {
|
|
10
|
+
const { clientId, clientSecret, baseUrl, onBehalfOf, debug } = client;
|
|
11
11
|
try {
|
|
12
12
|
const response = await fetch(`${baseUrl}/connect/token`, {
|
|
13
13
|
method: "POST",
|
|
14
|
-
headers: {
|
|
14
|
+
headers: {
|
|
15
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
16
|
+
...onBehalfOf ? { onbehalfof: onBehalfOf } : {}
|
|
17
|
+
},
|
|
15
18
|
body: new URLSearchParams({
|
|
16
19
|
grant_type: "client_credentials",
|
|
17
20
|
client_id: clientId,
|
|
@@ -36,20 +39,23 @@ var MyInvoisClient = class {
|
|
|
36
39
|
baseUrl;
|
|
37
40
|
clientId;
|
|
38
41
|
clientSecret;
|
|
42
|
+
onBehalfOf;
|
|
39
43
|
debug;
|
|
40
44
|
token = "";
|
|
41
45
|
tokenExpiration;
|
|
42
|
-
constructor(clientId, clientSecret, environment, debug
|
|
46
|
+
constructor(clientId, clientSecret, environment, onBehalfOf, debug) {
|
|
43
47
|
this.clientId = clientId;
|
|
44
48
|
this.clientSecret = clientSecret;
|
|
45
49
|
this.baseUrl = getBaseUrl(environment);
|
|
46
|
-
this.debug = debug;
|
|
50
|
+
this.debug = debug ?? process.env.MYINVOIS_DEBUG === "true" ? true : false;
|
|
51
|
+
this.onBehalfOf = onBehalfOf;
|
|
47
52
|
}
|
|
48
53
|
async refreshToken() {
|
|
49
|
-
const tokenResponse = await
|
|
54
|
+
const tokenResponse = await platformLogin({
|
|
50
55
|
clientId: this.clientId,
|
|
51
56
|
clientSecret: this.clientSecret,
|
|
52
57
|
baseUrl: this.baseUrl,
|
|
58
|
+
onBehalfOf: this.onBehalfOf,
|
|
53
59
|
debug: this.debug
|
|
54
60
|
});
|
|
55
61
|
this.token = tokenResponse.token;
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import type { ClientCredentials, TokenResponse } from 'src/types'
|
|
2
2
|
|
|
3
|
-
export const
|
|
4
|
-
const { clientId, clientSecret, baseUrl, debug } = client
|
|
3
|
+
export const platformLogin = async (client: ClientCredentials) => {
|
|
4
|
+
const { clientId, clientSecret, baseUrl, onBehalfOf, debug } = client
|
|
5
5
|
try {
|
|
6
6
|
const response = await fetch(`${baseUrl}/connect/token`, {
|
|
7
7
|
method: 'POST',
|
|
8
8
|
headers: {
|
|
9
9
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
10
|
+
...(onBehalfOf ? { onbehalfof: onBehalfOf } : {}),
|
|
10
11
|
},
|
|
11
12
|
body: new URLSearchParams({
|
|
12
13
|
grant_type: 'client_credentials',
|
package/src/types/index.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { getBaseUrl } from './getBaseUrl'
|
|
2
|
-
import {
|
|
2
|
+
import { platformLogin } from '../api/platform/platformLogin'
|
|
3
3
|
|
|
4
4
|
export class MyInvoisClient {
|
|
5
5
|
private readonly baseUrl: string
|
|
6
6
|
private readonly clientId: string
|
|
7
7
|
private readonly clientSecret: string
|
|
8
|
+
private readonly onBehalfOf?: string
|
|
8
9
|
private readonly debug: boolean
|
|
9
10
|
private token = ''
|
|
10
11
|
private tokenExpiration: Date | undefined
|
|
@@ -13,19 +14,22 @@ export class MyInvoisClient {
|
|
|
13
14
|
clientId: string,
|
|
14
15
|
clientSecret: string,
|
|
15
16
|
environment: 'sandbox' | 'production',
|
|
16
|
-
|
|
17
|
+
onBehalfOf?: string,
|
|
18
|
+
debug?: boolean,
|
|
17
19
|
) {
|
|
18
20
|
this.clientId = clientId
|
|
19
21
|
this.clientSecret = clientSecret
|
|
20
22
|
this.baseUrl = getBaseUrl(environment)
|
|
21
|
-
this.debug = debug
|
|
23
|
+
this.debug = (debug ?? process.env.MYINVOIS_DEBUG === 'true') ? true : false
|
|
24
|
+
this.onBehalfOf = onBehalfOf
|
|
22
25
|
}
|
|
23
26
|
|
|
24
27
|
private async refreshToken() {
|
|
25
|
-
const tokenResponse = await
|
|
28
|
+
const tokenResponse = await platformLogin({
|
|
26
29
|
clientId: this.clientId,
|
|
27
30
|
clientSecret: this.clientSecret,
|
|
28
31
|
baseUrl: this.baseUrl,
|
|
32
|
+
onBehalfOf: this.onBehalfOf,
|
|
29
33
|
debug: this.debug,
|
|
30
34
|
})
|
|
31
35
|
|