Skip to content

Instantly share code, notes, and snippets.

@himanshu-dixit
Created October 9, 2024 14:23
Show Gist options
  • Save himanshu-dixit/b7d9aa37bd1fdd250a2d9a1efc13524b to your computer and use it in GitHub Desktop.
Save himanshu-dixit/b7d9aa37bd1fdd250a2d9a1efc13524b to your computer and use it in GitHub Desktop.
shopify.js
import {Composio} from "./src/index";
(async function setupUserConnectionIfNotExists(entityId: string = "default", appName: string = "shopify") {
try {
console.log(`Setting up user connection for entity ${entityId} with app name ${appName}`);
const toolset = new Composio("vohqlw2wgvc7wtnm81buwq");
const entity = await toolset.getEntity(entityId);
if (!entity) {
throw new Error(`Entity with ID ${entityId} not found`);
}
const connection = await entity.getConnection(appName);
console.log('connection', connection);
if (!connection) {
const {appId} = await toolset.apps.get({appKey: 'shopify'})
const a = await toolset.integrations.create({
"name": "shopify_fit_silver",
"appId": appId,
"authScheme": "OAUTH2",
"forceNewIntegration": false,
"authConfig": {
"client_id": "client_id",
"client_secret": "client_secret"
}
})
const newConnection = await entity.initiateConnection(appName,undefined,undefined,undefined,a.id,{
shop: "sample_subdomain",
});
// Redirect to the Auth URL provided by the new connection
if (newConnection.redirectUrl) {
console.log('redirectUrl', newConnection.redirectUrl);
return {
redirect: {
destination: newConnection.redirectUrl,
permanent: false,
}
};
}
console.log('newConnection', newConnection);
try {
return await newConnection.waitUntilActive(60);
} catch (error) {
throw new Error(`Connection activation timeout: ${error.message}`);
}
}
return connection;
} catch (error) {
console.error('Error setting up user connection:', error);
throw error;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment