Creating custom services in medusa

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Now that we know how to use an existing service in our routes. Let's take it to. The next level and see how we can create our service even.
A service is something that you can use throughout your application.
Let's say we want to add a translation service, for instance.
We'll have to open up our medusa server project and create a new file inside the src/services.
If our service is the translateService, we should name the file translate.js (filename without the Service part).
The most basic service file would look like this.
import { TransactionBaseService } from '@medusajs/medusa';
class TranslateService extends TransactionBaseService {
func() {
return '';
}
}
export default TranslateService;
As you can see, the service extends the TransactionBaseService, which gives us access to all the existing and other services.
Let's create a mock demo function so that we can give it a try. With translations, we would need some mapping table, but let's mock it out for now.
const messages = {
en: {
title: 'Hello world',
},
nl: {
title: 'Hallo wereld',
},
};
class TranslateService extends TransactionBaseService {
translate(msg, locale) {
return messages[locale][msg] ?? msg;
}
}
This translate function will be able to translate specific messages into provided locales. If there is no match, the original message will be returned.
Let's go ahead and see how we can use this custom service. I'll use it in our custom routes as we already had a good intro.
Let's add a new route that will get our title.
router.get('/store/title', (req, res) => {
const translateService = req.scope.resolve('translateService');
const title = translateService.translate('title', 'en');
res.json({ title });
});
The first thing we do is retrieve our translate service, then we define a new variable for the title and request the title in a specific locale. And eventually, return that.
Spool up your server and test out the route.

Now let's change the locale to nl and try it again.
const title = translateService.translate('title', 'nl');

And you can even try to request a message that doesn't have any translations.
const title = translateService.translate('Products', 'en');

And that's it. We now have a fully functional custom service in medusa.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter