@gofynd/fdk-client-javascript 0.1.35
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/.github/workflows/on_create_release.yml +23 -0
- package/.github/workflows/on_merge_main.yml +38 -0
- package/.github/workflows/on_pull_request.yml +35 -0
- package/.prettierrc +5 -0
- package/LICENSE +21 -0
- package/README.md +118 -0
- package/application.d.ts +3 -0
- package/application.js +6 -0
- package/common.d.ts +4 -0
- package/common.js +9 -0
- package/documentation/application/CART.md +7290 -0
- package/documentation/application/CATALOG.md +9570 -0
- package/documentation/application/COMMON.md +448 -0
- package/documentation/application/COMMUNICATION.md +463 -0
- package/documentation/application/CONFIGURATION.md +2902 -0
- package/documentation/application/CONTENT.md +3350 -0
- package/documentation/application/FILESTORAGE.md +491 -0
- package/documentation/application/LEAD.md +1891 -0
- package/documentation/application/LOGISTIC.md +439 -0
- package/documentation/application/ORDER.md +3428 -0
- package/documentation/application/PAYMENT.md +5035 -0
- package/documentation/application/POSCART.md +7889 -0
- package/documentation/application/README.md +21 -0
- package/documentation/application/REWARDS.md +714 -0
- package/documentation/application/SHARE.md +729 -0
- package/documentation/application/THEME.md +5796 -0
- package/documentation/application/USER.md +4326 -0
- package/documentation/platform/ANALYTICS.md +1150 -0
- package/documentation/platform/AUDITTRAIL.md +572 -0
- package/documentation/platform/BILLING.md +2396 -0
- package/documentation/platform/CART.md +5539 -0
- package/documentation/platform/CATALOG.md +21624 -0
- package/documentation/platform/COMMON.md +448 -0
- package/documentation/platform/COMMUNICATION.md +5714 -0
- package/documentation/platform/COMPANYPROFILE.md +2141 -0
- package/documentation/platform/CONFIGURATION.md +7335 -0
- package/documentation/platform/CONTENT.md +8989 -0
- package/documentation/platform/DISCOUNT.md +773 -0
- package/documentation/platform/FILESTORAGE.md +994 -0
- package/documentation/platform/INVENTORY.md +1775 -0
- package/documentation/platform/LEAD.md +4736 -0
- package/documentation/platform/ORDER.md +3764 -0
- package/documentation/platform/PARTNER.md +216 -0
- package/documentation/platform/PAYMENT.md +3444 -0
- package/documentation/platform/README.md +27 -0
- package/documentation/platform/REWARDS.md +871 -0
- package/documentation/platform/SHARE.md +602 -0
- package/documentation/platform/THEME.md +35725 -0
- package/documentation/platform/USER.md +2305 -0
- package/documentation/platform/WEBHOOK.md +587 -0
- package/documentation/public/CONFIGURATION.md +448 -0
- package/documentation/public/INVENTORY.md +686 -0
- package/documentation/public/WEBHOOK.md +348 -0
- package/index.d.ts +10 -0
- package/index.js +18 -0
- package/jest.config.d.ts +4 -0
- package/jest.config.js +6 -0
- package/package.json +35 -0
- package/platform.d.ts +3 -0
- package/platform.js +6 -0
- package/public.d.ts +3 -0
- package/public.js +6 -0
- package/sdk/application/ApplicationAPIClient.d.ts +11 -0
- package/sdk/application/ApplicationAPIClient.js +44 -0
- package/sdk/application/ApplicationClient.d.ts +40 -0
- package/sdk/application/ApplicationClient.js +84 -0
- package/sdk/application/ApplicationConfig.d.ts +19 -0
- package/sdk/application/ApplicationConfig.js +55 -0
- package/sdk/application/ApplicationModels.d.ts +826 -0
- package/sdk/application/ApplicationModels.js +10474 -0
- package/sdk/application/client/CartApplicationClient.d.ts +432 -0
- package/sdk/application/client/CartApplicationClient.js +1066 -0
- package/sdk/application/client/CatalogApplicationClient.d.ts +694 -0
- package/sdk/application/client/CatalogApplicationClient.js +1629 -0
- package/sdk/application/client/CommonApplicationClient.d.ts +38 -0
- package/sdk/application/client/CommonApplicationClient.js +105 -0
- package/sdk/application/client/CommunicationApplicationClient.d.ts +39 -0
- package/sdk/application/client/CommunicationApplicationClient.js +131 -0
- package/sdk/application/client/ConfigurationApplicationClient.d.ts +210 -0
- package/sdk/application/client/ConfigurationApplicationClient.js +649 -0
- package/sdk/application/client/ContentApplicationClient.d.ts +256 -0
- package/sdk/application/client/ContentApplicationClient.js +785 -0
- package/sdk/application/client/FileStorageApplicationClient.d.ts +88 -0
- package/sdk/application/client/FileStorageApplicationClient.js +214 -0
- package/sdk/application/client/LeadApplicationClient.d.ts +90 -0
- package/sdk/application/client/LeadApplicationClient.js +264 -0
- package/sdk/application/client/LogisticApplicationClient.d.ts +42 -0
- package/sdk/application/client/LogisticApplicationClient.js +128 -0
- package/sdk/application/client/OrderApplicationClient.d.ts +190 -0
- package/sdk/application/client/OrderApplicationClient.js +479 -0
- package/sdk/application/client/PaymentApplicationClient.d.ts +454 -0
- package/sdk/application/client/PaymentApplicationClient.js +1378 -0
- package/sdk/application/client/PosCartApplicationClient.d.ts +449 -0
- package/sdk/application/client/PosCartApplicationClient.js +1123 -0
- package/sdk/application/client/RewardsApplicationClient.d.ts +93 -0
- package/sdk/application/client/RewardsApplicationClient.js +293 -0
- package/sdk/application/client/ShareApplicationClient.d.ts +87 -0
- package/sdk/application/client/ShareApplicationClient.js +268 -0
- package/sdk/application/client/ThemeApplicationClient.d.ts +52 -0
- package/sdk/application/client/ThemeApplicationClient.js +161 -0
- package/sdk/application/client/UserApplicationClient.d.ts +446 -0
- package/sdk/application/client/UserApplicationClient.js +1275 -0
- package/sdk/application/constructUrl.d.ts +5 -0
- package/sdk/application/constructUrl.js +13 -0
- package/sdk/application/index.d.ts +2 -0
- package/sdk/application/index.js +4 -0
- package/sdk/application/models/CartValidator.d.ts +29 -0
- package/sdk/application/models/CartValidator.js +223 -0
- package/sdk/application/models/CatalogValidator.d.ts +33 -0
- package/sdk/application/models/CatalogValidator.js +233 -0
- package/sdk/application/models/CommonValidator.d.ts +5 -0
- package/sdk/application/models/CommonValidator.js +18 -0
- package/sdk/application/models/CommunicationValidator.d.ts +6 -0
- package/sdk/application/models/CommunicationValidator.js +20 -0
- package/sdk/application/models/ConfigurationValidator.d.ts +19 -0
- package/sdk/application/models/ConfigurationValidator.js +88 -0
- package/sdk/application/models/ContentValidator.d.ts +22 -0
- package/sdk/application/models/ContentValidator.js +106 -0
- package/sdk/application/models/FileStorageValidator.d.ts +6 -0
- package/sdk/application/models/FileStorageValidator.js +24 -0
- package/sdk/application/models/LeadValidator.d.ts +10 -0
- package/sdk/application/models/LeadValidator.js +48 -0
- package/sdk/application/models/LogisticValidator.d.ts +6 -0
- package/sdk/application/models/LogisticValidator.js +22 -0
- package/sdk/application/models/OrderValidator.d.ts +15 -0
- package/sdk/application/models/OrderValidator.js +87 -0
- package/sdk/application/models/PaymentValidator.d.ts +40 -0
- package/sdk/application/models/PaymentValidator.js +235 -0
- package/sdk/application/models/PosCartValidator.d.ts +30 -0
- package/sdk/application/models/PosCartValidator.js +232 -0
- package/sdk/application/models/RewardsValidator.d.ts +10 -0
- package/sdk/application/models/RewardsValidator.js +43 -0
- package/sdk/application/models/ShareValidator.d.ts +10 -0
- package/sdk/application/models/ShareValidator.js +44 -0
- package/sdk/application/models/ThemeValidator.d.ts +7 -0
- package/sdk/application/models/ThemeValidator.js +27 -0
- package/sdk/application/models/UserValidator.d.ts +38 -0
- package/sdk/application/models/UserValidator.js +233 -0
- package/sdk/common/AxiosHelper.d.ts +1 -0
- package/sdk/common/AxiosHelper.js +141 -0
- package/sdk/common/Constant.d.ts +294 -0
- package/sdk/common/Constant.js +332 -0
- package/sdk/common/FDKError.d.ts +26 -0
- package/sdk/common/FDKError.js +51 -0
- package/sdk/common/Logger.d.ts +2 -0
- package/sdk/common/Logger.js +85 -0
- package/sdk/common/Paginator.d.ts +16 -0
- package/sdk/common/Paginator.js +26 -0
- package/sdk/common/RequestSigner.d.ts +1 -0
- package/sdk/common/RequestSigner.js +306 -0
- package/sdk/common/Utility.d.ts +9 -0
- package/sdk/common/Utility.js +86 -0
- package/sdk/common/utils.d.ts +14 -0
- package/sdk/common/utils.js +165 -0
- package/sdk/platform/OAuthClient.d.ts +26 -0
- package/sdk/platform/OAuthClient.js +221 -0
- package/sdk/platform/PlatformAPIClient.d.ts +12 -0
- package/sdk/platform/PlatformAPIClient.js +43 -0
- package/sdk/platform/PlatformApplicationClient.d.ts +24398 -0
- package/sdk/platform/PlatformApplicationClient.js +23185 -0
- package/sdk/platform/PlatformApplicationModels.d.ts +333 -0
- package/sdk/platform/PlatformApplicationModels.js +21262 -0
- package/sdk/platform/PlatformClient.d.ts +9535 -0
- package/sdk/platform/PlatformClient.js +12336 -0
- package/sdk/platform/PlatformConfig.d.ts +29 -0
- package/sdk/platform/PlatformConfig.js +36 -0
- package/sdk/platform/PlatformModels.d.ts +1396 -0
- package/sdk/platform/PlatformModels.js +19222 -0
- package/sdk/platform/client/AnalyticsPlatformClient.d.ts +84 -0
- package/sdk/platform/client/AnalyticsPlatformClient.js +215 -0
- package/sdk/platform/client/AuditTrailPlatformClient.d.ts +38 -0
- package/sdk/platform/client/AuditTrailPlatformClient.js +133 -0
- package/sdk/platform/client/BillingPlatformClient.d.ts +109 -0
- package/sdk/platform/client/BillingPlatformClient.js +381 -0
- package/sdk/platform/client/CatalogPlatformClient.d.ts +806 -0
- package/sdk/platform/client/CatalogPlatformClient.js +2462 -0
- package/sdk/platform/client/CommonPlatformClient.d.ts +30 -0
- package/sdk/platform/client/CommonPlatformClient.js +84 -0
- package/sdk/platform/client/CommunicationPlatformClient.d.ts +26 -0
- package/sdk/platform/client/CommunicationPlatformClient.js +72 -0
- package/sdk/platform/client/CompanyProfilePlatformClient.d.ts +182 -0
- package/sdk/platform/client/CompanyProfilePlatformClient.js +540 -0
- package/sdk/platform/client/ConfigurationPlatformClient.d.ts +277 -0
- package/sdk/platform/client/ConfigurationPlatformClient.js +806 -0
- package/sdk/platform/client/DiscountPlatformClient.d.ts +118 -0
- package/sdk/platform/client/DiscountPlatformClient.js +364 -0
- package/sdk/platform/client/FileStoragePlatformClient.d.ts +111 -0
- package/sdk/platform/client/FileStoragePlatformClient.js +268 -0
- package/sdk/platform/client/InventoryPlatformClient.d.ts +125 -0
- package/sdk/platform/client/InventoryPlatformClient.js +409 -0
- package/sdk/platform/client/LeadPlatformClient.d.ts +150 -0
- package/sdk/platform/client/LeadPlatformClient.js +439 -0
- package/sdk/platform/client/OrderPlatformClient.d.ts +190 -0
- package/sdk/platform/client/OrderPlatformClient.js +521 -0
- package/sdk/platform/client/PaymentPlatformClient.d.ts +98 -0
- package/sdk/platform/client/PaymentPlatformClient.js +331 -0
- package/sdk/platform/client/WebhookPlatformClient.d.ts +64 -0
- package/sdk/platform/client/WebhookPlatformClient.js +207 -0
- package/sdk/platform/index.d.ts +2 -0
- package/sdk/platform/index.js +4 -0
- package/sdk/platform/models/AnalyticsValidator.d.ts +7 -0
- package/sdk/platform/models/AnalyticsValidator.js +36 -0
- package/sdk/platform/models/AuditTrailValidator.d.ts +7 -0
- package/sdk/platform/models/AuditTrailValidator.js +26 -0
- package/sdk/platform/models/BillingValidator.d.ts +15 -0
- package/sdk/platform/models/BillingValidator.js +72 -0
- package/sdk/platform/models/CartValidator.d.ts +0 -0
- package/sdk/platform/models/CartValidator.js +0 -0
- package/sdk/platform/models/CatalogValidator.d.ts +74 -0
- package/sdk/platform/models/CatalogValidator.js +484 -0
- package/sdk/platform/models/CommonValidator.d.ts +5 -0
- package/sdk/platform/models/CommonValidator.js +18 -0
- package/sdk/platform/models/CommunicationValidator.d.ts +4 -0
- package/sdk/platform/models/CommunicationValidator.js +11 -0
- package/sdk/platform/models/CompanyProfileValidator.d.ts +17 -0
- package/sdk/platform/models/CompanyProfileValidator.js +93 -0
- package/sdk/platform/models/ConfigurationValidator.d.ts +21 -0
- package/sdk/platform/models/ConfigurationValidator.js +134 -0
- package/sdk/platform/models/ContentValidator.d.ts +0 -0
- package/sdk/platform/models/ContentValidator.js +0 -0
- package/sdk/platform/models/DiscountValidator.d.ts +13 -0
- package/sdk/platform/models/DiscountValidator.js +75 -0
- package/sdk/platform/models/FileStorageValidator.d.ts +9 -0
- package/sdk/platform/models/FileStorageValidator.js +44 -0
- package/sdk/platform/models/InventoryValidator.d.ts +15 -0
- package/sdk/platform/models/InventoryValidator.js +81 -0
- package/sdk/platform/models/LeadValidator.d.ts +14 -0
- package/sdk/platform/models/LeadValidator.js +78 -0
- package/sdk/platform/models/OrderValidator.d.ts +14 -0
- package/sdk/platform/models/OrderValidator.js +110 -0
- package/sdk/platform/models/PartnerValidator.d.ts +0 -0
- package/sdk/platform/models/PartnerValidator.js +0 -0
- package/sdk/platform/models/PaymentValidator.d.ts +13 -0
- package/sdk/platform/models/PaymentValidator.js +65 -0
- package/sdk/platform/models/RewardsValidator.d.ts +0 -0
- package/sdk/platform/models/RewardsValidator.js +0 -0
- package/sdk/platform/models/ShareValidator.d.ts +0 -0
- package/sdk/platform/models/ShareValidator.js +0 -0
- package/sdk/platform/models/ThemeValidator.d.ts +0 -0
- package/sdk/platform/models/ThemeValidator.js +0 -0
- package/sdk/platform/models/UserValidator.d.ts +0 -0
- package/sdk/platform/models/UserValidator.js +0 -0
- package/sdk/platform/models/WebhookValidator.d.ts +9 -0
- package/sdk/platform/models/WebhookValidator.js +42 -0
- package/sdk/public/PublicAPIClient.d.ts +11 -0
- package/sdk/public/PublicAPIClient.js +38 -0
- package/sdk/public/PublicClient.d.ts +12 -0
- package/sdk/public/PublicClient.js +24 -0
- package/sdk/public/PublicConfig.d.ts +10 -0
- package/sdk/public/PublicConfig.js +12 -0
- package/sdk/public/PublicModels.d.ts +50 -0
- package/sdk/public/PublicModels.js +712 -0
- package/sdk/public/client/ConfigurationPublicClient.d.ts +38 -0
- package/sdk/public/client/ConfigurationPublicClient.js +102 -0
- package/sdk/public/client/InventoryPublicClient.d.ts +81 -0
- package/sdk/public/client/InventoryPublicClient.js +239 -0
- package/sdk/public/client/WebhookPublicClient.d.ts +28 -0
- package/sdk/public/client/WebhookPublicClient.js +92 -0
- package/sdk/public/constructUrl.d.ts +5 -0
- package/sdk/public/constructUrl.js +13 -0
- package/sdk/public/index.d.ts +2 -0
- package/sdk/public/index.js +4 -0
- package/sdk/public/models/ConfigurationValidator.d.ts +5 -0
- package/sdk/public/models/ConfigurationValidator.js +18 -0
- package/sdk/public/models/InventoryValidator.d.ts +9 -0
- package/sdk/public/models/InventoryValidator.js +43 -0
- package/sdk/public/models/WebhookValidator.d.ts +5 -0
- package/sdk/public/models/WebhookValidator.js +14 -0
- package/tests/application/catalog.spec.js +47 -0
- package/tests/common/action-url.spec.js +35 -0
- package/tests/common/schema/action-url.json +163 -0
- package/tests/common/schema/url-action.json +667 -0
- package/tests/common/url-action.spec.js +48 -0
- package/tests/helpers/cookie.helper.js +31 -0
- package/tests/helpers/oauth.helper.js +43 -0
- package/tests/platform/catalog.spec.js +49 -0
- package/tests/public/location.spec.js +39 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
const querystring = require("query-string");
|
|
2
|
+
const { fdkAxios } = require("../common/AxiosHelper");
|
|
3
|
+
const { sign } = require("../common/RequestSigner");
|
|
4
|
+
const { FDKTokenIssueError, FDKOAuthCodeError } = require("../common/FDKError");
|
|
5
|
+
const { Logger } = require("../common/Logger");
|
|
6
|
+
|
|
7
|
+
const refreshTokenRequestCache = {};
|
|
8
|
+
class OAuthClient {
|
|
9
|
+
constructor(config) {
|
|
10
|
+
this.config = config;
|
|
11
|
+
this.token = null;
|
|
12
|
+
this.refreshToken = null;
|
|
13
|
+
this.retryOAuthTokenTimer = null;
|
|
14
|
+
this.raw_token = null;
|
|
15
|
+
this.token_expires_in = null;
|
|
16
|
+
this.token_expires_at = 0;
|
|
17
|
+
this.useAutoRenewTimer =
|
|
18
|
+
config.useAutoRenewTimer !== undefined ? config.useAutoRenewTimer : true;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async getAccessToken() {
|
|
22
|
+
if (
|
|
23
|
+
!this.useAutoRenewTimer &&
|
|
24
|
+
this.refreshToken &&
|
|
25
|
+
this.isTokenExpired(120)
|
|
26
|
+
) {
|
|
27
|
+
// Check if token is about to expire in less than 2 mins.
|
|
28
|
+
// Renew if to be expired and auto renew timer is not enabled.
|
|
29
|
+
await this.renewAccessToken();
|
|
30
|
+
}
|
|
31
|
+
return this.token;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// default TTL checked 0 seconds
|
|
35
|
+
isTokenExpired(ttl = 0) {
|
|
36
|
+
const currentTimestamp = new Date().getTime();
|
|
37
|
+
// Check if token is about to expire in less than 2 mins
|
|
38
|
+
if ((this.token_expires_at - currentTimestamp) / 1000 < ttl) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
setToken(token) {
|
|
45
|
+
this.raw_token = token;
|
|
46
|
+
this.token_expires_in = token.expires_in;
|
|
47
|
+
this.token = token.access_token;
|
|
48
|
+
this.refreshToken = token.refresh_token ? token.refresh_token : null;
|
|
49
|
+
if (this.refreshToken && this.useAutoRenewTimer) {
|
|
50
|
+
this.retryOAuthToken(token.expires_in);
|
|
51
|
+
}
|
|
52
|
+
Logger({ type: "INFO", message: "Token set." });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
retryOAuthToken(expires_in) {
|
|
56
|
+
Logger({ type: "INFO", message: "Retrying OAuth Token..." });
|
|
57
|
+
if (this.retryOAuthTokenTimer) {
|
|
58
|
+
clearTimeout(this.retryOAuthTokenTimer);
|
|
59
|
+
}
|
|
60
|
+
if (expires_in > 60) {
|
|
61
|
+
this.retryOAuthTokenTimer = setTimeout(() => {
|
|
62
|
+
this.renewAccessToken();
|
|
63
|
+
}, (expires_in - 60) * 1000);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
startAuthorization(options) {
|
|
68
|
+
Logger({ type: "INFO", message: "Starting Authorization..." });
|
|
69
|
+
let query = {
|
|
70
|
+
client_id: this.config.apiKey,
|
|
71
|
+
scope: options.scope.join(","),
|
|
72
|
+
redirect_uri: options.redirectUri,
|
|
73
|
+
state: options.state,
|
|
74
|
+
access_mode: options.access_mode,
|
|
75
|
+
response_type: "code",
|
|
76
|
+
};
|
|
77
|
+
const queryString = querystring.stringify(query);
|
|
78
|
+
|
|
79
|
+
let reqPath = `/service/panel/authentication/v1.0/company/${this.config.companyId}/oauth/authorize?${queryString}`;
|
|
80
|
+
let signingOptions = {
|
|
81
|
+
method: "GET",
|
|
82
|
+
host: new URL(this.config.domain).host,
|
|
83
|
+
path: reqPath,
|
|
84
|
+
body: null,
|
|
85
|
+
headers: {},
|
|
86
|
+
signQuery: true,
|
|
87
|
+
};
|
|
88
|
+
signingOptions = sign(signingOptions);
|
|
89
|
+
Logger({ type: "INFO", message: "Authorization successful.!" });
|
|
90
|
+
|
|
91
|
+
return `${this.config.domain}${signingOptions.path}`;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async verifyCallback(query) {
|
|
95
|
+
if (query.error) {
|
|
96
|
+
throw new FDKOAuthCodeError(query.error_description, {
|
|
97
|
+
error: query.error,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
try {
|
|
102
|
+
let res = await this.getAccesstokenObj({
|
|
103
|
+
grant_type: "authorization_code",
|
|
104
|
+
code: query.code,
|
|
105
|
+
});
|
|
106
|
+
this.setToken(res);
|
|
107
|
+
this.token_expires_at =
|
|
108
|
+
new Date().getTime() + this.token_expires_in * 1000;
|
|
109
|
+
} catch (error) {
|
|
110
|
+
if (error.isAxiosError) {
|
|
111
|
+
throw new FDKTokenIssueError(error.message);
|
|
112
|
+
}
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async renewAccessToken(isOfflineToken = false) {
|
|
118
|
+
try {
|
|
119
|
+
Logger({ type: "INFO", message: "Renewing Access token..." });
|
|
120
|
+
let res;
|
|
121
|
+
if (isOfflineToken) {
|
|
122
|
+
let requestCacheKey = `${this.config.apiKey}:${this.config.companyId}`;
|
|
123
|
+
if (!refreshTokenRequestCache[requestCacheKey]) {
|
|
124
|
+
refreshTokenRequestCache[requestCacheKey] = this.getAccesstokenObj({
|
|
125
|
+
grant_type: "refresh_token",
|
|
126
|
+
refresh_token: this.refreshToken,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
res = await refreshTokenRequestCache[requestCacheKey].finally(() => {
|
|
130
|
+
delete refreshTokenRequestCache[requestCacheKey];
|
|
131
|
+
});
|
|
132
|
+
} else {
|
|
133
|
+
res = await this.getAccesstokenObj({
|
|
134
|
+
grant_type: "refresh_token",
|
|
135
|
+
refresh_token: this.refreshToken,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
this.setToken(res);
|
|
139
|
+
this.token_expires_at =
|
|
140
|
+
new Date().getTime() + this.token_expires_in * 1000;
|
|
141
|
+
Logger({ type: "INFO", message: "Done." });
|
|
142
|
+
return res;
|
|
143
|
+
} catch (error) {
|
|
144
|
+
if (error.isAxiosError) {
|
|
145
|
+
throw new FDKTokenIssueError(error.message);
|
|
146
|
+
}
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async getAccesstokenObj({ grant_type, refresh_token, code }) {
|
|
152
|
+
Logger({ type: "INFO", message: "Processing Access token object..." });
|
|
153
|
+
let reqData = {
|
|
154
|
+
grant_type: grant_type,
|
|
155
|
+
};
|
|
156
|
+
if (grant_type === "refresh_token") {
|
|
157
|
+
reqData = { ...reqData, refresh_token };
|
|
158
|
+
} else if (grant_type === "authorization_code") {
|
|
159
|
+
reqData = { ...reqData, code };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const token = Buffer.from(
|
|
163
|
+
`${this.config.apiKey}:${this.config.apiSecret}`,
|
|
164
|
+
"utf8"
|
|
165
|
+
).toString("base64");
|
|
166
|
+
let url = `${this.config.domain}/service/panel/authentication/v1.0/company/${this.config.companyId}/oauth/token`;
|
|
167
|
+
const rawRequest = {
|
|
168
|
+
method: "post",
|
|
169
|
+
url: url,
|
|
170
|
+
data: querystring.stringify(reqData),
|
|
171
|
+
headers: {
|
|
172
|
+
Authorization: `Basic ${token}`,
|
|
173
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
Logger({ type: "INFO", message: "Done." });
|
|
177
|
+
return fdkAxios.request(rawRequest);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async getOfflineAccessToken(scopes, code) {
|
|
181
|
+
try {
|
|
182
|
+
let res = await this.getOfflineAccessTokenObj(scopes, code);
|
|
183
|
+
this.setToken(res);
|
|
184
|
+
this.token_expires_at =
|
|
185
|
+
new Date().getTime() + this.token_expires_in * 1000;
|
|
186
|
+
return res;
|
|
187
|
+
} catch (error) {
|
|
188
|
+
if (error.isAxiosError) {
|
|
189
|
+
throw new FDKTokenIssueError(error.message);
|
|
190
|
+
}
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async getOfflineAccessTokenObj(scopes, code) {
|
|
196
|
+
let url = `${this.config.domain}/service/panel/authentication/v1.0/company/${this.config.companyId}/oauth/offline-token`;
|
|
197
|
+
let data = {
|
|
198
|
+
client_id: this.config.apiKey,
|
|
199
|
+
client_secret: this.config.apiSecret,
|
|
200
|
+
grant_type: "authorization_code",
|
|
201
|
+
scope: scopes,
|
|
202
|
+
code: code,
|
|
203
|
+
};
|
|
204
|
+
const token = Buffer.from(
|
|
205
|
+
`${this.config.apiKey}:${this.config.apiSecret}`,
|
|
206
|
+
"utf8"
|
|
207
|
+
).toString("base64");
|
|
208
|
+
const rawRequest = {
|
|
209
|
+
method: "post",
|
|
210
|
+
url: url,
|
|
211
|
+
data: data,
|
|
212
|
+
headers: {
|
|
213
|
+
Authorization: `Basic ${token}`,
|
|
214
|
+
"Content-Type": "application/json",
|
|
215
|
+
},
|
|
216
|
+
};
|
|
217
|
+
return fdkAxios.request(rawRequest);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
module.exports = OAuthClient;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export = APIClient;
|
|
2
|
+
declare class APIClient {
|
|
3
|
+
/**
|
|
4
|
+
* @param {object} conf
|
|
5
|
+
* @param {string} method
|
|
6
|
+
* @param {string} url
|
|
7
|
+
* @param {object} query
|
|
8
|
+
* @param {object} body
|
|
9
|
+
*/
|
|
10
|
+
static execute(conf: object, method: string, url: string, query: object, body: object, xHeaders: any): Promise<any>;
|
|
11
|
+
get(url: any, config: any): Promise<any>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const { fdkAxios } = require("../common/AxiosHelper");
|
|
2
|
+
|
|
3
|
+
class APIClient {
|
|
4
|
+
/**
|
|
5
|
+
* @param {object} conf
|
|
6
|
+
* @param {string} method
|
|
7
|
+
* @param {string} url
|
|
8
|
+
* @param {object} query
|
|
9
|
+
* @param {object} body
|
|
10
|
+
*/
|
|
11
|
+
static async execute(conf, method, url, query, body, xHeaders) {
|
|
12
|
+
const token = await conf.oauthClient.getAccessToken();
|
|
13
|
+
|
|
14
|
+
const extraHeaders = conf.extraHeaders.reduce((acc, curr) => {
|
|
15
|
+
acc = { ...acc, ...curr };
|
|
16
|
+
return acc;
|
|
17
|
+
}, {});
|
|
18
|
+
|
|
19
|
+
const rawRequest = {
|
|
20
|
+
baseURL: conf.domain,
|
|
21
|
+
method: method,
|
|
22
|
+
url: url,
|
|
23
|
+
params: query,
|
|
24
|
+
data: body,
|
|
25
|
+
headers: {
|
|
26
|
+
Authorization: "Bearer " + token,
|
|
27
|
+
...extraHeaders,
|
|
28
|
+
...xHeaders,
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
return fdkAxios.request(rawRequest);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async get(url, config) {
|
|
36
|
+
let access_token = await this.configuration.getAccessToken();
|
|
37
|
+
config = config || {};
|
|
38
|
+
config.headers = config.headers || {};
|
|
39
|
+
config.headers.Authorization = "Bearer " + access_token;
|
|
40
|
+
return axios.get(url);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
module.exports = APIClient;
|