Sharing code between Connect applications
Learn how to create common modules for sharing code across multiple Connect applications.
A single Connector can implement multiple application types, such as service and event applications. By creating common modules, you can share common types, functions, and values with every application to deduplicate code and improve efficiency.
Requirements
You must use npm or Yarn Classic as your package manager.
As the common module is directly referenced from the Connector application, no changes are required in the connect.yaml
specification file.
Structure your project directory
To ensure your common modules are shareable, structure your project directory as follows:
.├── README.md├── common-connect # The common module you want to share│ ├── package-lock.json│ ├── package.json│ ├── src│ ├── tsconfig.json│ └── tsconfig.tsbuildinfo├── connect.yaml├── event│ ├── jest.config.cjs│ ├── package-lock.json│ ├── package.json│ ├── src│ ├── tests│ └── tsconfig.json├── job│ ├── jest.config.cjs│ ├── package-lock.json│ ├── package.json│ ├── src│ ├── tests│ ├── tsconfig.json│ └── tsconfig.tsbuildinfo└── service├── jest.config.cjs├── package-lock.json├── package.json├── src├── tests├── tsconfig.json└── tsconfig.tsbuildinfo
Create tests for the common module
Make sure to create tests for the functionalities of the common module, such as the following:
{"name": "common-connect","version": "1.0.0","description": "","main": "dist/index.js","types": "dist/index.d.ts","scripts": {"test": "jest","build": "tsc"},"keywords": [],"author": "","license": "ISC","dependencies": {"@commercetools-backend/loggers": "^22.3.0"},"devDependencies": {"jest": "^29.5.0","typescript": "^5.1.3"}}
Install the common module
To install the common module, you need to add it as a dependency and add additional scripts to the application's package.json
file.
Install the common module using npm
To install the common module using npm, follow these steps:
- Run the following command:
cd servicenpm install ../common-connect
- In the
package.json
file, add a build script that includes building local common modules and includes your test scripts, for example:
"scripts": {..."build": "npm --prefix ../common-connect install &&npm --prefix ../common-connect run build && rimraf ./build && tsc","test": "npm run build && jest --config jest.config.cjs"}
The following is an example of an application's package.json
file:
{"name": "service-typescript","description": "","version": "1.0.0","main": "index.js","private": true,"scripts": {"start": "node build/index.js","start:dev": "concurrently -k \"tsc --watch\"\"nodemon -q build/index.js\"","build": "npm --prefix ../common-connect install &&npm --prefix ../common-connect run build && rimraf ./build && tsc","lint": "eslint . --ext .ts","prettier": "prettier --write '**/*.{js,ts}'","test": "npm run build && jest --config jest.config.cjs","connector:post-deploy": "node build/connector/post-deploy.js","connector:pre-undeploy": "node build/connector/pre-undeploy.js"},"author": "","license": "MIT","devDependencies": {"@types/express": "^4.17.14","@types/jest": "^29.0.3","@types/node": "^18.7.18","@types/supertest": "^2.0.12","@types/validator": "^13.7.10","@typescript-eslint/eslint-plugin": "^5.46.0","@typescript-eslint/parser": "^5.46.0","concurrently": "^7.4.0","eslint": "^8.24.0","jest": "^29.1.1","nodemon": "^2.0.20","prettier": "^2.7.1","rimraf": "^3.0.2","ts-jest": "^29.0.2","ts-node": "^10.9.1","typescript": "^4.8.3"},"dependencies": {"@commercetools/platform-sdk": "^4.0.0","@commercetools/sdk-client-v2": "^2.0.0","body-parser": "^1.20.0","common-connect": "file:../common-connect","dotenv": "^16.0.3","express": "4.18.2","node-fetch": "^3.3.0","supertest": "^6.3.3","validator": "^13.7.0"}}
Install the common module using Yarn
To install the common module using Yarn, follow these steps:
- Run the following command:
cd serviceyarn add link:../common-connect
- In the
package.json
file, add a build script that includes building local common modules and inlcudes your test scripts, for example:
"scripts": {..."build": "yarn --cwd ../common-connect install &&yarn --cwd ../common-connect run build && rimraf ./build && tsc","test": "yarn run build && jest --config jest.config.cjs"}
The following is an example of an application's package.json
file:
{"name": "service-typescript","description": "","version": "1.0.0","main": "index.js","private": true,"scripts": {"start": "node build/index.js","start:dev": "concurrently -k \"tsc --watch\"\"nodemon -q build/index.js\"","build": "yarn --cwd ../common-connect install &&yarn --cwd ../common-connect run build && rimraf ./build && tsc","lint": "eslint . --ext .ts","prettier": "prettier --write '**/*.{js,ts}'","test": "npm run build && jest --config jest.config.cjs","connector:post-deploy": "node build/connector/post-deploy.js","connector:pre-undeploy": "node build/connector/pre-undeploy.js"},"author": "","license": "MIT","devDependencies": {"@types/express": "^4.17.14","@types/jest": "^29.0.3","@types/node": "^18.7.18","@types/supertest": "^2.0.12","@types/validator": "^13.7.10","@typescript-eslint/eslint-plugin": "^5.46.0","@typescript-eslint/parser": "^5.46.0","concurrently": "^7.4.0","eslint": "^8.24.0","jest": "^29.1.1","nodemon": "^2.0.20","prettier": "^2.7.1","rimraf": "^3.0.2","ts-jest": "^29.0.2","ts-node": "^10.9.1","typescript": "^4.8.3"},"dependencies": {"@commercetools/platform-sdk": "^4.0.0","@commercetools/sdk-client-v2": "^2.0.0","body-parser": "^1.20.0","common-connect": "link:../common-connect","dotenv": "^16.0.3","express": "4.18.2","node-fetch": "^3.3.0","supertest": "^6.3.3","validator": "^13.7.0"}}