-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1185 from contentstack/feat/CS-42859
feat: New marketplace sdk adapter added in utility [CS-42859]
- Loading branch information
Showing
5 changed files
with
165 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
packages/contentstack-utilities/src/contentstack-marketplace-sdk.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { Agent } from 'node:https'; | ||
import { client, ContentstackConfig, ContentstackClient, ContentstackToken } from '@contentstack/marketplace-sdk'; | ||
|
||
import authHandler from './auth-handler'; | ||
import configStore from './config-handler'; | ||
|
||
type ConfigType = Pick<ContentstackConfig, 'host' | 'endpoint' | 'retryDelay' | 'retryLimit'> & { | ||
management_token?: string; | ||
skipTokenValidity?: string; | ||
}; | ||
|
||
class MarketplaceSDKInitiator { | ||
private analyticsInfo: string; | ||
|
||
/** | ||
* The function returns a default configuration object for Contentstack API requests in TypeScript. | ||
* @returns a default configuration object of type `ContentstackConfig`. | ||
*/ | ||
get defaultOptions(): ContentstackConfig { | ||
return { | ||
headers: {}, | ||
retryLimit: 3, | ||
timeout: 60000, | ||
maxRequests: 10, | ||
// host: 'api.contentstack.io', | ||
maxContentLength: 100000000, | ||
maxBodyLength: 1000000000, | ||
httpsAgent: new Agent({ | ||
timeout: 60000, // active socket keepalive for 60 seconds | ||
maxSockets: 100, | ||
keepAlive: true, | ||
maxFreeSockets: 10, | ||
}), | ||
retryDelay: Math.floor(Math.random() * (8000 - 3000 + 1) + 3000), | ||
retryCondition: (error: any): boolean => { | ||
if (error?.response?.status) { | ||
switch (error.response.status) { | ||
case 401: | ||
case 429: | ||
case 408: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
}, | ||
retryDelayOptions: { | ||
base: 1000, | ||
customBackoff: () => 1, | ||
}, | ||
}; | ||
} | ||
|
||
init(context) { | ||
this.analyticsInfo = context?.analyticsInfo; | ||
} | ||
|
||
/** | ||
* The function `refreshTokenHandler` returns a promise that resolves with a `ContentstackToken` | ||
* object based on the `authorizationType` parameter. | ||
* @param {string} authorizationType - The `authorizationType` parameter is a string that specifies | ||
* the type of authorization being used. It can have one of the following values: | ||
* @returns The refreshTokenHandler function returns a function that returns a Promise of type | ||
* ContentstackToken. | ||
*/ | ||
refreshTokenHandler(authorizationType: string) { | ||
return (): Promise<ContentstackToken> => { | ||
return new Promise((resolve, reject) => { | ||
if (authorizationType === 'BASIC') { | ||
// NOTE Handle basic auth 401 here | ||
reject(new Error('Session timed out, please login to proceed')); | ||
} else if (authorizationType === 'OAUTH') { | ||
authHandler | ||
.compareOAuthExpiry(true) | ||
.then(() => resolve({ authorization: `Bearer ${configStore.get('oauthAccessToken')}` })) | ||
.catch(reject); | ||
} else { | ||
reject(new Error('You do not have permissions to perform this action, please login to proceed')); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
/** | ||
* The function creates a Contentstack SDK client with the provided configuration. | ||
* @param config - The `config` parameter is an object that contains the following properties: | ||
* @returns a Promise that resolves to a ContentstackClient object. | ||
*/ | ||
async createAppSDKClient(config?: ConfigType): Promise<ContentstackClient> { | ||
const authorizationType = configStore.get('authorisationType'); | ||
|
||
const option = this.defaultOptions; | ||
option.refreshToken = this.refreshTokenHandler(authorizationType); | ||
|
||
if (config.host) { | ||
option.host = config.host; | ||
} | ||
|
||
if (config.endpoint) { | ||
option.endpoint = config.endpoint; | ||
} | ||
|
||
if (config.retryLimit) { | ||
option.retryLimit = config.retryLimit; | ||
} | ||
|
||
if (config.retryDelay) { | ||
option.retryDelay = config.retryDelay; | ||
} | ||
|
||
if (this.analyticsInfo) { | ||
option.headers['X-CS-CLI'] = this.analyticsInfo; | ||
} | ||
|
||
if (!config.management_token) { | ||
option.authtoken = ''; | ||
option.authorization = ''; | ||
|
||
if (authorizationType === 'BASIC') { | ||
option.authtoken = configStore.get('authtoken'); | ||
} else if (authorizationType === 'OAUTH') { | ||
if (!config.skipTokenValidity) { | ||
await authHandler.compareOAuthExpiry(); | ||
option.authorization = `Bearer ${configStore.get('oauthAccessToken')}`; | ||
} | ||
} | ||
} | ||
|
||
return client(option); | ||
} | ||
} | ||
|
||
export const marketplaceSDKInitiator = new MarketplaceSDKInitiator(); | ||
const marketplaceSDKClient: typeof marketplaceSDKInitiator.createAppSDKClient = | ||
marketplaceSDKInitiator.createAppSDKClient.bind(marketplaceSDKInitiator); | ||
export { MarketplaceSDKInitiator, ContentstackConfig, ContentstackClient }; | ||
|
||
export default marketplaceSDKClient; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters