
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@salla.sa/twilight
Advanced tools
A JavaScript SDK for the Salla storefront APIs provides helper methods, or REST API endpoints, that allow communication between the frontend and backend.
Explore our blogs »
Report Bug ·
Request Feature .
</Salla Developers> .
Official Documentation
Twilight comes with a JavaScript SDK for the Salla storefront APIs. This is to provide the developers with helper methods, or REST API endpoints, that allow communication between the frontend and backend. As a result, developers may use these methods to make merchants' stores dynamic after using the API features and data. In this documentation, we will be referring to it as Twilight JS SDK for short.
SDK stands for "software development kit," and it refers to a library for interacting with a specific REST API using JavaScript.
The main parts of the Twilight JS SDK include REST API endpoints that ease the actions related to the APIs request, such as:
Following are some of the possible uses of the Twilight JS SDK:
Twilight JS SDK can be used without the need to be downloaded or installed; instead, it needs to be included as a short piece of regular JavaScript in the HTML that will asynchronously load the SDK into the Twilight theme.
In case of using the Twilight JS SDK inside the Twilight themes, the developer doesn't need to include the Twilight JS SDK in the theme project's bundle or inside the html, Twilight theme engine will inject the latest version of the Twilight JS SDK in the page.
Basically, the developer does not need to call the method salla.init()
for twilight themes, because it will be called automatically upon the installation of the Twilight theme engine. This is done thanks to the body:end hook {% hook 'body:end' %}
.
The most common approach for setting up the Twilight JS SDK is load it via <script>
befor closing the body
tag of the HTML document.
<script src="https://cdn.jsdelivr.net/npm/@salla.sa/twilight@latest/dist/@salla.sa/twilight.min.js"></script>
The following snippet of code will give the basic version of the SDK where the configurations are set to their minimum requirements, for example, store URL. The method salla.init()
is used to setup and initialize the SDK.
<script>
salla.init({
debug: true, // disbale it in prod
language_code: 'ar', // en
store: {
id: 1305146709, // The store id can found via store pages.
url: "https://the-best-store-ever.sa"
}
});
</script>
The developer may also install the Twilight JS SDK using the following commands:
npm install @salla.sa/twilight --save
yarn add @salla.sa/twilight
Initially, the developer must import the Salla JS Events library as follows:
import '@salla.sa/twilight';
Similar to the HTML/CDN section, the following code snippet will give the basic version of the SDK where the configurations are set to their minimum requirements, wherein the method salla.init()
is used to setup and initialize the SDK.
<script>
salla.init({
debug: true, // disbale it in prod
language_code: 'ar', // en
store: {
id: 1305146709, // The store id can found via store pages.
url: "https://the-best-store-ever.sa"
}
});
</script>
As a result of the SDK initialization, the developer will be able to use any of the SDK's main APIs. For example, the method addItem
adds an item into the cart, the developer may call the method addItem
as follows:
salla.cart.addItem({
id: 1234,
quantity: 1,
notes: "please i need to get the red color"
}).then((response) => {
/* add your code here */
});
In addition to the above example, the method addItem
may adds an item with multiple options to the carts as follows:
salla.cart.addItem({
id: 1234,
quantity: 1,
options: {
117414452: 11232214, // option value id (select choice)
117416632: "http://option-value-as-url-of-image.com",
117411132: "option value as string"
},
notes: "please i need to get the red color"
}).then((response) => {
/* add your code here */
});
Furthermore, a large number of store events will be available for use. Thus, the developer's theme may be configured to respond automatically to a variety of activities, such as:
The user requested an Access Code to perform a login, however, the code was not sent. For this scenario, using the method salla.event.auth.onCodeNotSent()
can be used.
A new item has been added to the cart via the salla.cart.event.onItemAdded()
method.
The user added a new item to the Wishlist using the method salla.event.wishlist.onAdded()
.
Full example for that would be the event onItemAdded
which is triggered when adding an item to the cart is done without having any errors coming back from the backend:
salla.cart.event.onItemAdded((response) => {
console.log(response)
});
On the over hand, the event onItemAddedFailed
is triggered when adding an item to the cart is not completed and an error has occurred. For example, the id of the product to be added to the cart was not found.
salla.cart.event.onItemAddedFailed((errorMessage) => {
console.log(errorMessage)
});
Aside from calling the APIs, the developer has the ability to configure the Twilight engine to meet the needs of his theme. This list of the available configurations can be found in this article. For this purpose, the method salla.config.get()
is used to retrieve a configuration value, while salla.config.set()
is used to set a configuration value.
The developer may set the debug mode to be activated. That is to state, the theme will be run in a debugger. This means that the debugger keeps track of everything that happens while the theme is running.
salla.config.set('debug', true)
Similarly, the developer can use the method salla.config.get()
to get any value from the configuration file. To retrieve a simple value such as the user Id, the following syntax can be followed:
salla.config.get('user.id’)
Furthermore, if the required value is nested inside an inner value, such as a currency code, the following syntax can be followed:
salla.config.get('currencies.SAR.code’)
The team is always here to help you. Happen to face an issue? Want to report a bug? You can submit one here on GitHub using the Issue Tracker. If you still have any questions, please contact us via the Telegram Bot or join the Global Developer Community on Telegram.
Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
git checkout -b feature/AmazingFeature
)git commit -m 'Add some AmazingFeature'
)git push origin feature/AmazingFeature
)The MIT License (MIT). Please see License File for more information.
FAQs
Salla Theme Toolkit, Webcomponents, Events, Requests, Utils
We found that @salla.sa/twilight demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.