Consuming APIs in Nuxt Using The Repository Pattern
Tags: webdev, nuxt, coding, vue
Published: 2020-05-02 19:35:54

---
title: Consuming APIs in Nuxt Using The Repository Pattern
published: true
date: 2020-05-02 08:30:42 UTC
tags: web-development,nuxtjs,coding,vuejs
canonical_url: https://medium.com/js-dojo/consuming-apis-in-nuxt-using-the-repository-pattern-8a13ea57d520
---

In this article, we will be discussing how to consume APIs the smart way in Nuxtjs using the Repository pattern.

![](https://cdn-images-1.medium.com/max/500/1*Q4G_Q9QoO0NThOH7_bhrgw.png)

**Prerequisites**

1. Basic knowledge of Design patterns especially Repository pattern
2. Basic knowledge of Nuxtjs
3. Basic knowledge of JavaScript

_This list shouldn’t hinder you from following through with this article, as you can always read up on any of them._

When Consuming APIs, give requisite attention to structuring them using best practices and industry standards so that it’s easy to debug, less code written, and most importantly using the DRY principle , etc.

Here, we would follow best practices in Consuming APIs using the Repository pattern and also create a custom plugin in Nuxtjs to do the work.

The Repository Pattern is one of the most popular patterns used when creating an enterprise level application. It restricts one to work directly with data in the application and it creates new layers for database operations, business logic and application UI.

This article is a replica of the formal one for Vuejs developers, so if you are using Vuejs, [do check out the article](https://dev.to/kaperskyguru/consuming-apis-using-repository-design-pattern-in-vue-js-jl5) and also to get the general knowledge of Repository pattern and its benefits check it out here.

- **NuxtJS**

Nuxtjs is a free and open source web application framework based on Vuejs, Node.js, Webpack and Babel.js. An open source framework making web development simple and powerful. I refer to it as **Framework of a Framework.**

Nuxtjs ships with lots of benefits out of the box viz:

1. Server-side Rendering

2. Static Side generation

3. Single page Application

4. Modular

5. Performant

6. Enjoyable

[Read the official documentation](https://nuxtjs.org/guide).

Consuming APIs in Nuxtjs using the Repository Pattern is a little tricky than that of Vue.js. However, we would achieve it with these simple steps.

1. Create the Repository folder.
2. Create a general Repository class that will import all your repositories. e.g. _Repository.js_
3. Create a Nuxtjs Plugin.
4. Import the general repository class into the Nuxtjs Plugin class and inject into Nuxtjs.
5. Add the Custom Plugin into nuxt.config.js file
6. Create the individual repository inside the repositories folder.
7. Import all the individual repositories e.g. _PostRepository.js_ inside the general Repository class (_Repository.js_).
8. Import the individual repository inside your vuex store or components.

Enough of the theories, lets dive into the code and see how to put all this together to arrive at a solution.

**Create a Repository folder**

I will go ahead and create a repositories folder inside the src directory of my application.

cd src && mkdir repositories

**Create a general repository class.**

This will serve as a single export point for all the individual repositories, instead of individually importing all the repositories inside the Nuxtjs Plugin file and updating it each time, we simply add everything here and export.

touch Repository.js

**Create Nuxtjs Plugin**

_This is where the magic happens, Nuxt.js allows you to define JavaScript plugins to be run before instantiating the root Vue.js Application._

cd plugins && touch repositories.js

In Nuxtjs, when adding global methods, configs, components, etc. just like in Vue.js when using Vue plugins ( Vue.use() ). We can also do that by creating a custom Nuxtjs Plugins and call the inject method which Nuxtjs provides freely to us.

**Import the General repository class into the Nuxtjs Plugin class and inject into Nuxtjs**

A Nuxtjs Plugin is automatically packaged with the Context API and the Inject method, we will use the context API to get access to Nuxtjs’ default Axios instance (which is passed as argument to the General Repository class) only in this article but you can read more about [Nuxt Context here.](https://nuxtjs.org/api/context)

The Inject method takes in two parameters viz: name and what you’re injecting into the Vue instance.

{% gist https://gist.github.com/Kaperskyguru/1b5871c26ffb1292dd10ef2951c933aa %}

**Add the Custom Plugin into**  **nuxt.config.js file**

Its time to fire up our new Plugin each time we start Nuxtjs app, we will add the newly created plugin into the Plugins array of our Nuxtjs configuration file.

{% gist https://gist.github.com/Kaperskyguru/1d46af46cc810b60e8354a00842073a9 %}

**Create the individual repository inside the repositories folder.**

The class contains the different API endpoints or operations that will be done within a particular feature of your application, which would be demonstrated with a single feature in the application which would be demonstrated with a single feature of the application… POST.

So I will create a _PostRepository.js_ file inside the _`repositories`_ folder and put in the below codes and even more.

```
cd repositories && touch PromotionRepository.js
```

Add the following code and even more to it.

{% gist https://gist.github.com/Kaperskyguru/bc1c711e7b3c4e0d34793b3b8119a791 %}

**Import Individual Repository into _Repository.js_**

Next, we will import all the individual repositories e.g. _PromotionRepository.js_ into _Repository.js_ and pass on the axios instance gotten from our custom plugin like so:

{% gist https://gist.github.com/Kaperskyguru/072ea52718eebb7be5b007731167f3e3 %}

**Import the individual repository inside your vuex store or components.**

Welldone for getting this far, we’re done with all the configuration setup, let’s move onto the fun part, using our repository pattern to make http calls inside Vuex store.

Vuex is a state management library for Vue.js. [You can read more and how to get started here.](https://vuex.vuejs.org/guide/)

FYI, I always make all my API calls inside Vuex stores because its the “center of truth” for all my data — my choice anyways.

For easy demonstration, we will create a post.js file inside the store folder and include the following codes to interact with our API, retrieve list of posts and store it in the state for easy retrieval between multiple components.

{% gist https://gist.github.com/Kaperskyguru/28f4db5c8337ba27b5a0bd70e8f1aaca %}

**Using Vuex store inside a component.**

You can simply use the Vuex store inside any components of your choice like so;

{% gist https://gist.github.com/Kaperskyguru/8a34ae9de1b3b9e55c366b9291f20cf6 %}

Congratulations for getting to the end of this article, and thank you for reading

There you have it, if you have any further contributions, questions or feedback, please leave a comment.

**Summary**

Using Repository Design pattern can help you write clean and less code in a component therefore reducing code coupling just as we have demonstrated.

**Conclusion**

There you have it, if you have any further contributions, questions or feedback, please drop it. Also don’t forget to **CLAP** if you find the article useful,

I hope you learnt something new with Nuxt, Vuex and Repository Pattern. [The full code is on GitHub, get it now](https://github.com/Kaperskyguru/nuxt-with-repository-pattern).

Thank you for reading my article. Here at my [blog](https://bit.ly/multimegablog) or [medium](https://bit.ly/mymediumprofile) I regularly write about backend development, digital marketing and content management system. To read my future posts simply [join my publication](https://bit.ly/mymediumpublication) or click ‘Follow’ Also feel free to connect with me via [Twitter](http://twitter.com/kaperskyguru), [Facebook](https://facebook.com/multimegaitschool), [Instagram](https://instagram.com/kaperskyguru).

_If you are interested in backend development (or you’re internet enthusiast) both (Mobile | Web | Desktop) videos subscribe to my_ [_Youtube channel_](http://bit.ly/multimegaschool)_, we will be posting a collection of help full tutorials and guides like this one for artisans_.

If you enjoy this post, make sure to let us know and share it with your friends and subscribe to my growing [channel](http://bit.ly/multimegaschool).

Sharing is caring.

PS: Don’t forget to drop 50 **CLAPS and SHARE** to your friends

* * *