@gofynd/fdk-client-javascript 1.0.4 → 1.1.0
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/README.md +20 -0
- package/index.d.ts +4 -1
- package/index.js +19 -3
- package/package.json +13 -4
- package/partner.d.ts +4 -0
- package/partner.js +7 -0
- package/sdk/application/ApplicationAPIClient.js +2 -4
- package/sdk/application/Catalog/CatalogApplicationModel.js +4 -2
- package/sdk/application/Content/ContentApplicationModel.js +0 -1
- package/sdk/common/BaseOAuthClient.d.ts +17 -0
- package/sdk/common/BaseOAuthClient.js +67 -0
- package/sdk/common/RequestSigner.js +0 -5
- package/sdk/partner/OAuthClient.d.ts +14 -0
- package/sdk/partner/OAuthClient.js +112 -0
- package/sdk/{APIClient.d.ts → partner/PartnerAPIClient.d.ts} +2 -1
- package/sdk/{APIClient.js → partner/PartnerAPIClient.js} +13 -6
- package/sdk/{Client.d.ts → partner/PartnerClient.d.ts} +2 -2
- package/sdk/{Client.js → partner/PartnerClient.js} +2 -2
- package/sdk/partner/PartnerConfig.d.ts +30 -0
- package/sdk/partner/PartnerConfig.js +39 -0
- package/sdk/partner/index.d.ts +3 -0
- package/sdk/partner/index.js +5 -0
- package/sdk/platform/Billing/BillingPlatformClient.d.ts +24 -0
- package/sdk/platform/Billing/BillingPlatformClient.js +134 -0
- package/sdk/platform/Billing/BillingPlatformModel.d.ts +4 -0
- package/sdk/platform/Billing/BillingPlatformModel.js +44 -0
- package/sdk/platform/Billing/BillingPlatformValidator.d.ts +2 -0
- package/sdk/platform/Billing/BillingPlatformValidator.js +14 -0
- package/sdk/platform/Catalog/CatalogPlatformModel.js +2 -2
- package/sdk/platform/Content/ContentPlatformApplicationClient.d.ts +10 -0
- package/sdk/platform/Content/ContentPlatformApplicationClient.js +61 -0
- package/sdk/platform/Content/ContentPlatformApplicationValidator.d.ts +1 -0
- package/sdk/platform/Content/ContentPlatformApplicationValidator.js +6 -0
- package/sdk/platform/Content/ContentPlatformModel.js +0 -1
- package/sdk/platform/OAuthClient.js +3 -9
- package/sdk/platform/PlatformApplicationClient.d.ts +5 -1
- package/sdk/platform/PlatformApplicationClient.js +4 -0
- package/sdk/platform/PlatformClient.d.ts +39 -4
- package/sdk/platform/PlatformClient.js +42 -3
- package/sdk/platform/Rewards/RewardsPlatformApplicationClient.d.ts +14 -16
- package/sdk/platform/Rewards/RewardsPlatformApplicationClient.js +63 -77
- package/sdk/platform/Rewards/RewardsPlatformApplicationValidator.d.ts +1 -1
- package/sdk/platform/Rewards/RewardsPlatformApplicationValidator.js +7 -8
- package/sdk/Config.d.ts +0 -9
- package/sdk/Config.js +0 -17
- package/sdk/constructUrl.d.ts +0 -5
- package/sdk/constructUrl.js +0 -13
- package/sdk/index.d.ts +0 -3
- package/sdk/index.js +0 -5
package/README.md
CHANGED
|
@@ -13,6 +13,8 @@ Get started with the Javascript Development SDK for Fynd Platform
|
|
|
13
13
|
|
|
14
14
|
### Usage
|
|
15
15
|
|
|
16
|
+
#### Node
|
|
17
|
+
|
|
16
18
|
```
|
|
17
19
|
npm install @gofynd/fdk-client-javascript
|
|
18
20
|
```
|
|
@@ -26,6 +28,22 @@ const {
|
|
|
26
28
|
} = require("fdk-client-javascript");
|
|
27
29
|
```
|
|
28
30
|
|
|
31
|
+
#### Browser
|
|
32
|
+
you can load fdk-client-javascript's application browser bundle from CDN; `ApplicationConfig`, `ApplicationClient` and `ApplicationModels` will be attached to browser's `window` object.
|
|
33
|
+
|
|
34
|
+
```html
|
|
35
|
+
<script src="https://cdn.jsdelivr.net/gh/gofynd/fdk-client-javascript@<version>/dist/application.js"></script>
|
|
36
|
+
```
|
|
37
|
+
Install Specific version
|
|
38
|
+
```html
|
|
39
|
+
<script src="https://cdn.jsdelivr.net/gh/gofynd/fdk-client-javascript@1.0.1/dist/application.js"></script>
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
const { ApplicationConfig, ApplicationClient } = window;
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
|
|
29
47
|
### Logging
|
|
30
48
|
|
|
31
49
|
For logging support user can pass `logLevel` in `ApplicationConfig` or `PlatformConfig` while declaration.
|
|
@@ -168,4 +186,6 @@ import { ApplicationConfig, ApplicationClient } from "fdk-client-javascript";
|
|
|
168
186
|
- [Public Front](documentation/public/README.md)
|
|
169
187
|
- [Application Front](documentation/application/README.md)
|
|
170
188
|
- [Platform Front](documentation/platform/README.md)
|
|
189
|
+
- [Admin Front](documentation/admin/README.md)
|
|
190
|
+
- [Partner Front](documentation/partner/README.md)
|
|
171
191
|
|
package/index.d.ts
CHANGED
|
@@ -7,7 +7,10 @@ import { ApplicationModel } from "./sdk/application";
|
|
|
7
7
|
import { PlatformConfig } from "./sdk/platform";
|
|
8
8
|
import { PlatformClient } from "./sdk/platform";
|
|
9
9
|
import { PlatformModel } from "./sdk/platform";
|
|
10
|
+
import { PartnerConfig } from "./sdk/partner";
|
|
11
|
+
import { PartnerClient } from "./sdk/partner";
|
|
12
|
+
import { PartnerModel } from "./sdk/partner";
|
|
10
13
|
import { fdkAxios } from "./sdk/common/AxiosHelper";
|
|
11
14
|
import Utility = require("./sdk/common/Utility");
|
|
12
15
|
import Constant = require("./sdk/common/Constant");
|
|
13
|
-
export { PublicConfig, PublicClient, PublicModel, ApplicationConfig, ApplicationClient, ApplicationModel, PlatformConfig, PlatformClient, PlatformModel, fdkAxios as FdkAxios, Utility, Constant };
|
|
16
|
+
export { PublicConfig, PublicClient, PublicModel, ApplicationConfig, ApplicationClient, ApplicationModel, PlatformConfig, PlatformClient, PlatformModel, PartnerConfig, PartnerClient, PartnerModel, fdkAxios as FdkAxios, Utility, Constant };
|
package/index.js
CHANGED
|
@@ -1,25 +1,41 @@
|
|
|
1
1
|
|
|
2
|
-
const { PublicConfig, PublicClient, PublicModel } = require('./sdk/public');
|
|
3
|
-
const { ApplicationConfig, ApplicationClient, ApplicationModel } = require('./sdk/application');
|
|
4
|
-
const { PlatformConfig, PlatformClient, PlatformModel } = require('./sdk/platform');
|
|
2
|
+
const { PublicConfig, PublicClient, PublicModel } = require('./sdk/public');
|
|
3
|
+
const { ApplicationConfig, ApplicationClient, ApplicationModel } = require('./sdk/application');
|
|
4
|
+
const { PlatformConfig, PlatformClient, PlatformModel } = require('./sdk/platform');
|
|
5
|
+
|
|
6
|
+
const { PartnerConfig, PartnerClient, PartnerModel } = require('./sdk/partner');
|
|
5
7
|
const {fdkAxios} = require('./sdk/common/AxiosHelper');
|
|
6
8
|
const Utility = require('./sdk/common/Utility');
|
|
7
9
|
const Constant = require('./sdk/common/Constant');
|
|
8
10
|
|
|
9
11
|
module.exports = {
|
|
10
12
|
|
|
13
|
+
|
|
11
14
|
PublicConfig,
|
|
12
15
|
PublicClient,
|
|
13
16
|
PublicModel,
|
|
14
17
|
|
|
18
|
+
|
|
19
|
+
|
|
15
20
|
ApplicationConfig,
|
|
16
21
|
ApplicationClient,
|
|
17
22
|
ApplicationModel,
|
|
18
23
|
|
|
24
|
+
|
|
25
|
+
|
|
19
26
|
PlatformConfig,
|
|
20
27
|
PlatformClient,
|
|
21
28
|
PlatformModel,
|
|
22
29
|
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
PartnerConfig,
|
|
35
|
+
PartnerClient,
|
|
36
|
+
PartnerModel,
|
|
37
|
+
|
|
38
|
+
|
|
23
39
|
FdkAxios: fdkAxios,
|
|
24
40
|
Utility,
|
|
25
41
|
Constant,
|
package/package.json
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gofynd/fdk-client-javascript",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "jest --coverage",
|
|
8
|
+
"test:standalone": "npm run build && npx cypress run",
|
|
9
|
+
"build": "webpack",
|
|
8
10
|
"prettier": "npx prettier -w ./sdk ./tests && npx tsc"
|
|
9
11
|
},
|
|
10
12
|
"author": "Jigar Dafda<jigar.dafda@gmail.com>",
|
|
@@ -13,23 +15,30 @@
|
|
|
13
15
|
"axios": "^0.27.2",
|
|
14
16
|
"camelcase": "^6.3.0",
|
|
15
17
|
"crypto-js": "^4.1.1",
|
|
18
|
+
"isomorphic-base64": "^1.0.2",
|
|
16
19
|
"joi": "^17.7.0",
|
|
17
20
|
"loglevel": "^1.8.1",
|
|
18
|
-
"query-string": "^
|
|
21
|
+
"query-string": "^7.1.3"
|
|
19
22
|
},
|
|
20
23
|
"devDependencies": {
|
|
21
24
|
"axios-cookiejar-support": "^4.0.6",
|
|
22
25
|
"axios-mock-adapter": "^1.21.2",
|
|
26
|
+
"clean-webpack-plugin": "^4.0.0",
|
|
23
27
|
"coveralls": "^3.1.1",
|
|
28
|
+
"cypress": "^9.2.0",
|
|
24
29
|
"dotenv": "^16.0.3",
|
|
25
30
|
"jest": "^29.5.0",
|
|
26
|
-
"tough-cookie": "^4.1.2"
|
|
31
|
+
"tough-cookie": "^4.1.2",
|
|
32
|
+
"webpack": "^5.81.0",
|
|
33
|
+
"webpack-cli": "^5.0.2"
|
|
27
34
|
},
|
|
28
35
|
"files": [
|
|
29
36
|
"sdk/**/*.js",
|
|
30
37
|
"sdk/**/*.d.ts",
|
|
31
38
|
"./*.js",
|
|
32
39
|
"./*.d.ts",
|
|
33
|
-
"
|
|
40
|
+
"!**.config.*",
|
|
41
|
+
"!dist",
|
|
42
|
+
"!cypress"
|
|
34
43
|
]
|
|
35
44
|
}
|
package/partner.d.ts
ADDED
package/partner.js
ADDED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const { fdkAxios } = require("../common/AxiosHelper");
|
|
2
|
+
const { btoa } = require("isomorphic-base64");
|
|
2
3
|
|
|
3
4
|
class APIClient {
|
|
4
5
|
/**
|
|
@@ -10,10 +11,7 @@ class APIClient {
|
|
|
10
11
|
*/
|
|
11
12
|
|
|
12
13
|
static execute(conf, method, url, query, body, xHeaders) {
|
|
13
|
-
const token =
|
|
14
|
-
`${conf.applicationID}:${conf.applicationToken}`,
|
|
15
|
-
"utf8"
|
|
16
|
-
).toString("base64");
|
|
14
|
+
const token = btoa(`${conf.applicationID}:${conf.applicationToken}`);
|
|
17
15
|
|
|
18
16
|
let headers = { Authorization: "Bearer " + token };
|
|
19
17
|
if (conf.cookie) {
|
|
@@ -16,7 +16,7 @@ class CatalogModel {
|
|
|
16
16
|
}
|
|
17
17
|
static ApplicationStoreListing() {
|
|
18
18
|
return Joi.object({
|
|
19
|
-
filters: Joi.array().items(
|
|
19
|
+
filters: Joi.array().items(Joi.any()),
|
|
20
20
|
items: Joi.array().items(CatalogModel.AppStore()),
|
|
21
21
|
page: CatalogModel.Page(),
|
|
22
22
|
});
|
|
@@ -29,6 +29,7 @@ class CatalogModel {
|
|
|
29
29
|
departments: Joi.array().items(CatalogModel.StoreDepartments()),
|
|
30
30
|
manager: CatalogModel.StoreManagerSerializer(),
|
|
31
31
|
name: Joi.string().allow(""),
|
|
32
|
+
store_code: Joi.string().allow(""),
|
|
32
33
|
uid: Joi.number(),
|
|
33
34
|
});
|
|
34
35
|
}
|
|
@@ -940,7 +941,7 @@ class CatalogModel {
|
|
|
940
941
|
}
|
|
941
942
|
static StoreDepartments() {
|
|
942
943
|
return Joi.object({
|
|
943
|
-
logo: Joi.
|
|
944
|
+
logo: Joi.any(),
|
|
944
945
|
name: Joi.string().allow(""),
|
|
945
946
|
priority_order: Joi.number(),
|
|
946
947
|
slug: Joi.string().allow(""),
|
|
@@ -964,6 +965,7 @@ class CatalogModel {
|
|
|
964
965
|
departments: Joi.array().items(CatalogModel.StoreDepartments()),
|
|
965
966
|
manager: CatalogModel.StoreManagerSerializer(),
|
|
966
967
|
name: Joi.string().allow(""),
|
|
968
|
+
store_code: Joi.string().allow(""),
|
|
967
969
|
timing: Joi.array().items(CatalogModel.StoreTiming()),
|
|
968
970
|
uid: Joi.number(),
|
|
969
971
|
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export = BaseOAuthClient;
|
|
2
|
+
declare class BaseOAuthClient {
|
|
3
|
+
constructor(config: any);
|
|
4
|
+
config: any;
|
|
5
|
+
token: any;
|
|
6
|
+
refreshToken: any;
|
|
7
|
+
retryOAuthTokenTimer: NodeJS.Timeout;
|
|
8
|
+
raw_token: any;
|
|
9
|
+
token_expires_in: any;
|
|
10
|
+
token_expires_at: number;
|
|
11
|
+
useAutoRenewTimer: any;
|
|
12
|
+
getAccessToken(): Promise<any>;
|
|
13
|
+
isTokenExpired(ttl?: number): boolean;
|
|
14
|
+
setToken(token: any): void;
|
|
15
|
+
retryOAuthToken(expires_in: any): void;
|
|
16
|
+
renewAccessToken(): Promise<void>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const { Logger } = require("./Logger");
|
|
2
|
+
|
|
3
|
+
class BaseOAuthClient {
|
|
4
|
+
constructor(config) {
|
|
5
|
+
this.config = config;
|
|
6
|
+
this.token = null;
|
|
7
|
+
this.refreshToken = null;
|
|
8
|
+
this.retryOAuthTokenTimer = null;
|
|
9
|
+
this.raw_token = null;
|
|
10
|
+
this.token_expires_in = null;
|
|
11
|
+
this.token_expires_at = 0;
|
|
12
|
+
this.useAutoRenewTimer =
|
|
13
|
+
config.useAutoRenewTimer !== undefined ? config.useAutoRenewTimer : true;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async getAccessToken() {
|
|
17
|
+
if (
|
|
18
|
+
!this.useAutoRenewTimer &&
|
|
19
|
+
this.refreshToken &&
|
|
20
|
+
this.isTokenExpired(120)
|
|
21
|
+
) {
|
|
22
|
+
// Check if token is about to expire in less than 2 mins.
|
|
23
|
+
// Renew if to be expired and auto renew timer is not enabled.
|
|
24
|
+
await this.renewAccessToken();
|
|
25
|
+
}
|
|
26
|
+
return this.token;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// default TTL checked 0 seconds
|
|
30
|
+
isTokenExpired(ttl = 0) {
|
|
31
|
+
const currentTimestamp = new Date().getTime();
|
|
32
|
+
// Check if token is about to expire in less than 2 mins
|
|
33
|
+
if ((this.token_expires_at - currentTimestamp) / 1000 < ttl) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
setToken(token) {
|
|
40
|
+
this.raw_token = token;
|
|
41
|
+
this.token_expires_in = token.expires_in;
|
|
42
|
+
this.token = token.access_token;
|
|
43
|
+
this.token_expires_at = token.expires_at || this.token_expires_at;
|
|
44
|
+
this.refreshToken = token.refresh_token ? token.refresh_token : null;
|
|
45
|
+
if (this.refreshToken && this.useAutoRenewTimer) {
|
|
46
|
+
this.retryOAuthToken(token.expires_in);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
retryOAuthToken(expires_in) {
|
|
51
|
+
Logger({ type: "INFO", message: "Retrying OAuth Token" });
|
|
52
|
+
if (this.retryOAuthTokenTimer) {
|
|
53
|
+
clearTimeout(this.retryOAuthTokenTimer);
|
|
54
|
+
}
|
|
55
|
+
if (expires_in > 60) {
|
|
56
|
+
this.retryOAuthTokenTimer = setTimeout(() => {
|
|
57
|
+
this.renewAccessToken();
|
|
58
|
+
}, (expires_in - 60) * 1000);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async renewAccessToken() {
|
|
63
|
+
// Impementation to renew Access Token
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
module.exports = BaseOAuthClient;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const url = require("url");
|
|
4
3
|
const querystring = require("query-string");
|
|
5
4
|
const sha256 = require("crypto-js/sha256");
|
|
6
5
|
const hmacSHA256 = require("crypto-js/hmac-sha256");
|
|
@@ -39,10 +38,6 @@ const HEADERS_TO_INCLUDE = ["x-fp-.*", "host"];
|
|
|
39
38
|
// request: { path | body, [host], [method], [headers], [service], [region] }
|
|
40
39
|
class RequestSigner {
|
|
41
40
|
constructor(request) {
|
|
42
|
-
if (typeof request === "string") {
|
|
43
|
-
request = url.parse(request);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
41
|
let headers = (request.headers = request.headers || {});
|
|
47
42
|
this.request = request;
|
|
48
43
|
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export = OAuthClient;
|
|
2
|
+
declare class OAuthClient extends BaseOAuthClient {
|
|
3
|
+
startAuthorization(options: any): string;
|
|
4
|
+
verifyCallback(query: any): Promise<void>;
|
|
5
|
+
renewAccessToken(isOfflineToken?: boolean): Promise<any>;
|
|
6
|
+
getNewAccessToken(): Promise<any>;
|
|
7
|
+
getAccesstokenObj({ grant_type, client_id, client_secret, scope }: {
|
|
8
|
+
grant_type: any;
|
|
9
|
+
client_id: any;
|
|
10
|
+
client_secret: any;
|
|
11
|
+
scope: any;
|
|
12
|
+
}): Promise<any>;
|
|
13
|
+
}
|
|
14
|
+
import BaseOAuthClient = require("../common/BaseOAuthClient");
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
const { fdkAxios } = require("../common/AxiosHelper");
|
|
2
|
+
const { FDKTokenIssueError } = require("../common/FDKError");
|
|
3
|
+
const { Logger } = require("../common/Logger");
|
|
4
|
+
const BaseOAuthClient = require("../common/BaseOAuthClient");
|
|
5
|
+
const querystring = require("querystring");
|
|
6
|
+
const { sign } = require("../common/RequestSigner");
|
|
7
|
+
|
|
8
|
+
class OAuthClient extends BaseOAuthClient {
|
|
9
|
+
constructor(config) {
|
|
10
|
+
super(config);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
startAuthorization(options) {
|
|
14
|
+
Logger({ type: "DEBUG", message: "Starting Authorization" });
|
|
15
|
+
let query = {
|
|
16
|
+
client_id: this.config.apiKey,
|
|
17
|
+
scope: options.scope.join(","),
|
|
18
|
+
redirect_uri: options.redirectUri,
|
|
19
|
+
state: options.state,
|
|
20
|
+
access_mode: options.access_mode,
|
|
21
|
+
response_type: "code",
|
|
22
|
+
extension_id: this.config.extensionId,
|
|
23
|
+
};
|
|
24
|
+
const queryString = querystring.stringify(query);
|
|
25
|
+
|
|
26
|
+
let reqPath = `/service/panel/authentication/v1.0/organization/${this.config.organizationId}/oauth/authorize?${queryString}`;
|
|
27
|
+
let signingOptions = {
|
|
28
|
+
method: "GET",
|
|
29
|
+
host: new URL(this.config.domain).host,
|
|
30
|
+
path: reqPath,
|
|
31
|
+
body: null,
|
|
32
|
+
headers: {},
|
|
33
|
+
signQuery: true,
|
|
34
|
+
};
|
|
35
|
+
signingOptions = sign(signingOptions);
|
|
36
|
+
Logger({ type: "DEBUG", message: "Authorization successful" });
|
|
37
|
+
|
|
38
|
+
return `${this.config.domain}${signingOptions.path}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async verifyCallback(query) {
|
|
42
|
+
if (query.error) {
|
|
43
|
+
throw new FDKOAuthCodeError(query.error_description, {
|
|
44
|
+
error: query.error,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
let res = await this.getAccesstokenObj({
|
|
50
|
+
grant_type: "authorization_code",
|
|
51
|
+
code: query.code,
|
|
52
|
+
});
|
|
53
|
+
res.expires_at =
|
|
54
|
+
res.expires_at || new Date().getTime() + res.expires_in * 1000;
|
|
55
|
+
this.setToken(res);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
if (error.isAxiosError) {
|
|
58
|
+
throw new FDKTokenIssueError(error.message);
|
|
59
|
+
}
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async renewAccessToken(isOfflineToken = true) {
|
|
65
|
+
if (isOfflineToken) {
|
|
66
|
+
await this.getNewAccessToken();
|
|
67
|
+
} else {
|
|
68
|
+
res = await this.getAccesstokenObj({
|
|
69
|
+
grant_type: "refresh_token",
|
|
70
|
+
refresh_token: this.refreshToken,
|
|
71
|
+
});
|
|
72
|
+
res.expires_at =
|
|
73
|
+
res.expires_at || new Date().getTime() + res.expires_in * 1000;
|
|
74
|
+
this.setToken(res);
|
|
75
|
+
return res;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async getNewAccessToken() {
|
|
80
|
+
try {
|
|
81
|
+
let res = await this.getAccesstokenObj({
|
|
82
|
+
grant_type: "client_credentials",
|
|
83
|
+
client_id: this.config.apiKey,
|
|
84
|
+
client_secret: this.config.apiSecret,
|
|
85
|
+
scope: this.config.scope,
|
|
86
|
+
});
|
|
87
|
+
res.expires_at =
|
|
88
|
+
res.expires_at || new Date().getTime() + res.expires_in * 1000;
|
|
89
|
+
this.setToken(res);
|
|
90
|
+
return this.token;
|
|
91
|
+
} catch (error) {
|
|
92
|
+
if (error.isAxiosError) {
|
|
93
|
+
throw new FDKTokenIssueError(error.message);
|
|
94
|
+
}
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async getAccesstokenObj({ grant_type, client_id, client_secret, scope }) {
|
|
100
|
+
Logger({ type: "DEBUG", message: "Processing Access token object..." });
|
|
101
|
+
let reqData = {
|
|
102
|
+
grant_type: grant_type,
|
|
103
|
+
client_id,
|
|
104
|
+
client_secret,
|
|
105
|
+
scope,
|
|
106
|
+
};
|
|
107
|
+
let url = `${this.config.domain}/service/panel/authentication/v1.0/organization/${this.config.organizationId}/oauth/token`;
|
|
108
|
+
return fdkAxios.post(url, reqData);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
module.exports = OAuthClient;
|
|
@@ -7,5 +7,6 @@ declare class APIClient {
|
|
|
7
7
|
* @param {object} query
|
|
8
8
|
* @param {object} body
|
|
9
9
|
*/
|
|
10
|
-
static execute(conf: object, method: string, url: string, query: object, body: object
|
|
10
|
+
static execute(conf: object, method: string, url: string, query: object, body: object): Promise<any>;
|
|
11
|
+
get(url: any, config: any): Promise<any>;
|
|
11
12
|
}
|
|
@@ -8,9 +8,8 @@ class APIClient {
|
|
|
8
8
|
* @param {object} query
|
|
9
9
|
* @param {object} body
|
|
10
10
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
let headers = {};
|
|
11
|
+
static async execute(conf, method, url, query, body) {
|
|
12
|
+
const token = await conf.oauthClient.getNewAccessToken();
|
|
14
13
|
|
|
15
14
|
const extraHeaders = conf.extraHeaders.reduce((acc, curr) => {
|
|
16
15
|
acc = { ...acc, ...curr };
|
|
@@ -18,18 +17,26 @@ class APIClient {
|
|
|
18
17
|
}, {});
|
|
19
18
|
|
|
20
19
|
const rawRequest = {
|
|
20
|
+
baseURL: conf.domain,
|
|
21
21
|
method: method,
|
|
22
22
|
url: url,
|
|
23
23
|
params: query,
|
|
24
24
|
data: body,
|
|
25
25
|
headers: {
|
|
26
|
-
|
|
26
|
+
Authorization: "Bearer " + token,
|
|
27
27
|
...extraHeaders,
|
|
28
|
-
...xHeaders,
|
|
29
28
|
},
|
|
30
29
|
};
|
|
30
|
+
|
|
31
31
|
return fdkAxios.request(rawRequest);
|
|
32
32
|
}
|
|
33
|
-
}
|
|
34
33
|
|
|
34
|
+
async get(url, config) {
|
|
35
|
+
let access_token = await this.configuration.getAccessToken();
|
|
36
|
+
config = config || {};
|
|
37
|
+
config.headers = config.headers || {};
|
|
38
|
+
config.headers.Authorization = "Bearer " + access_token;
|
|
39
|
+
return axios.get(url);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
35
42
|
module.exports = APIClient;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const { FDKClientValidationError } = require("../common/FDKError");
|
|
2
2
|
|
|
3
|
-
class
|
|
3
|
+
class PartnerClient {
|
|
4
4
|
constructor(config) {
|
|
5
5
|
this.config = config;
|
|
6
6
|
}
|
|
@@ -14,4 +14,4 @@ class Client {
|
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
module.exports =
|
|
17
|
+
module.exports = PartnerClient;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export = PartnerConfig;
|
|
2
|
+
declare class PartnerConfig {
|
|
3
|
+
/**
|
|
4
|
+
* @param {Object} config
|
|
5
|
+
* @param {string} config.companyId
|
|
6
|
+
* @param {string} config.domain
|
|
7
|
+
* @param {string} config.apiKey
|
|
8
|
+
* @param {string} config.apiSecret
|
|
9
|
+
* @param {boolean} config.useAutoRenewTimer
|
|
10
|
+
*/
|
|
11
|
+
constructor(config: {
|
|
12
|
+
companyId: string;
|
|
13
|
+
domain: string;
|
|
14
|
+
apiKey: string;
|
|
15
|
+
apiSecret: string;
|
|
16
|
+
useAutoRenewTimer: boolean;
|
|
17
|
+
});
|
|
18
|
+
organizationId: any;
|
|
19
|
+
domain: string;
|
|
20
|
+
apiKey: string;
|
|
21
|
+
apiSecret: string;
|
|
22
|
+
scope: any;
|
|
23
|
+
useAutoRenewTimer: boolean;
|
|
24
|
+
oauthClient: OauthClient;
|
|
25
|
+
logLevel: any;
|
|
26
|
+
extraHeaders: any[];
|
|
27
|
+
setLogLevel(level: any): void;
|
|
28
|
+
getAccessToken(): Promise<any>;
|
|
29
|
+
}
|
|
30
|
+
import OauthClient = require("./OAuthClient");
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const OauthClient = require("./OAuthClient");
|
|
2
|
+
const { setLoggerLevel } = require("../common/Logger");
|
|
3
|
+
|
|
4
|
+
class PartnerConfig {
|
|
5
|
+
/**
|
|
6
|
+
* @param {Object} config
|
|
7
|
+
* @param {string} config.companyId
|
|
8
|
+
* @param {string} config.domain
|
|
9
|
+
* @param {string} config.apiKey
|
|
10
|
+
* @param {string} config.apiSecret
|
|
11
|
+
* @param {boolean} config.useAutoRenewTimer
|
|
12
|
+
*/
|
|
13
|
+
constructor(config) {
|
|
14
|
+
this.organizationId = config.organizationId;
|
|
15
|
+
this.domain = config.domain || "https://api.fynd.com";
|
|
16
|
+
this.apiKey = config.apiKey;
|
|
17
|
+
this.apiSecret = config.apiSecret;
|
|
18
|
+
(this.scope = config.scope),
|
|
19
|
+
(this.useAutoRenewTimer =
|
|
20
|
+
config.useAutoRenewTimer !== undefined
|
|
21
|
+
? config.useAutoRenewTimer
|
|
22
|
+
: true);
|
|
23
|
+
this.oauthClient = new OauthClient(this);
|
|
24
|
+
this.logLevel = config.logLevel || "ERROR";
|
|
25
|
+
this.setLogLevel(this.logLevel);
|
|
26
|
+
this.extraHeaders = [];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
setLogLevel(level) {
|
|
30
|
+
setLoggerLevel(level);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async getAccessToken() {
|
|
34
|
+
let token = await this.oauthClient.getAccessToken();
|
|
35
|
+
return token.access_token;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
module.exports = PartnerConfig;
|
|
@@ -46,6 +46,18 @@ declare class Billing {
|
|
|
46
46
|
plan: string;
|
|
47
47
|
couponCode: string;
|
|
48
48
|
}): Promise<CheckValidityResponse>;
|
|
49
|
+
/**
|
|
50
|
+
* @param {Object} arg - Arg object.
|
|
51
|
+
* @param {string} arg.extensionId - Extension _id
|
|
52
|
+
* @param {CreateOneTimeCharge} arg.body
|
|
53
|
+
* @returns {Promise<CreateOneTimeChargeResponse>} - Success response
|
|
54
|
+
* @summary: Create one time subscription charge
|
|
55
|
+
* @description: Register one time subscription charge for a seller of your extension.
|
|
56
|
+
*/
|
|
57
|
+
createOneTimeCharge({ extensionId, body }?: {
|
|
58
|
+
extensionId: string;
|
|
59
|
+
body: CreateOneTimeCharge;
|
|
60
|
+
}): Promise<CreateOneTimeChargeResponse>;
|
|
49
61
|
/**
|
|
50
62
|
* @param {Object} arg - Arg object.
|
|
51
63
|
* @param {string} arg.extensionId - Extension _id
|
|
@@ -58,6 +70,18 @@ declare class Billing {
|
|
|
58
70
|
extensionId: string;
|
|
59
71
|
body: CreateSubscriptionCharge;
|
|
60
72
|
}): Promise<CreateSubscriptionResponse>;
|
|
73
|
+
/**
|
|
74
|
+
* @param {Object} arg - Arg object.
|
|
75
|
+
* @param {string} arg.extensionId - Extension _id
|
|
76
|
+
* @param {string} arg.chargeId - Standalone charge _id
|
|
77
|
+
* @returns {Promise<OneTimeChargeEntity>} - Success response
|
|
78
|
+
* @summary: Get subscription charge details
|
|
79
|
+
* @description: Get created subscription charge details
|
|
80
|
+
*/
|
|
81
|
+
getChargeDetails({ extensionId, chargeId }?: {
|
|
82
|
+
extensionId: string;
|
|
83
|
+
chargeId: string;
|
|
84
|
+
}): Promise<OneTimeChargeEntity>;
|
|
61
85
|
/**
|
|
62
86
|
* @param {Object} arg - Arg object.
|
|
63
87
|
* @returns {Promise<SubscriptionCustomer>} - Success response
|