Community of Trust Javascript/Typescript Integration Library
This library provides a comprehensive JavaScript/Typescript interface for integrating with the Community of Trust (COT) platform, facilitating seamless interactions with its services.
Requirements
- node.js for development
- npm for package management
Installation
To install the library, run the following command in your project directory:
npm i @trustedshops-public/cot-integration-library
Usage
Here is a basic example of how to use the library:
On the backend side:
import { Client } from '@trustedshops-public/cot-integration-library';
// Initialize the client with the required parameters once
const client = new Client(
<TSID>, // Trusted Shops ID - e.g. 'X1234567890123456789012345678901'
<CLIENT_ID>, // Client ID - e.g. 'cot-switch-X1234567890123456789012345678901'
<CLIENT_SECRET>, // Client Secret - e.g. '1234567890123456789012345678901234567890123456789012345678901234'
<AUTH_STORAGE_INSTANCE>, // It can be any storage option implementing AuthStorageInterface - e.g. new DatabaseAuthStorage()
<ENV> // Environment (optional) - dev, test, or prod, defaults to prod
);
// Set the cookie handler to handle cookies everytime before a request is made and handled
client.setCookieHandler(<COOKIE_HANDLER_INSTANCE>);
// Pass the actual url which the authentication has been started from to let authentication server verify the redirect uri e.g. 'https://www.example.com/shop'
client.setRedirectUri(<REDIRECT_URI>);
// Invoke handleCallback function to handle code coming from the authentication server
await client.handleCallback(authCode);
// Get anonymous consumer data for the current user
const consumerData = await client.getAnonymousConsumerData();
On the frontend side, place the following code in your HTML file where you want the widget to appear:
<trstd-switch tsId="X1234567890123456789012345678901"></trstd-switch>
<script type="module" src="https://widgets.trustedshops.com/switch/switch.js"></script>
For more detailed examples, please refer to the examples/
directory.
Development
To get started with development, clone the repository and install the dependencies:
npm i
Setup the environment variables:
cp .env.example .env.local
and fill in the required values.
Hint: You can use this environment variable for local testing of examples. Copy .env.example
to .env
and fill in the required values in the root directory of the example, e.g., shopify-integration
.
Generate local ssl certificates:
npm run generate-ssl-cert
To run local development server, use the following command:
npm run dev
To run the tests, use the following command:
npm test
To build the library, use the following command:
npm run build
Contributing
Contributions are welcome! Please refer to the CONTRIBUTING.md file for guidelines.
License
This library is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
This project relies on several open-source libraries that have made development easier and more efficient. We would like to acknowledge the following libraries:
- jose - A comprehensive and robust JavaScript library for JSON Web Tokens (JWT) and related standards.
- node-cache - A simple and efficient in-memory caching module for Node.js.
- winston - A versatile and powerful logging library for Node.js.
We are grateful to the developers and maintainers of these projects for their hard work and dedication.
Versioning
This project adheres to Semantic Versioning. For the versions available, see the tags on this repository.