Using npm packages in AdonisJS

One of the most asked questions in the AdonisJS community has to be about how to use X package in AdonisJS. Those asking this question tend to forget that AdonisJS is a Node.js framework and as a result, we can use an npm package the same way we’d use …


This content originally appeared on DEV Community and was authored by Chimezie Enyinnaya

One of the most asked questions in the AdonisJS community has to be about how to use X package in AdonisJS. Those asking this question tend to forget that AdonisJS is a Node.js framework and as a result, we can use an npm package the same way we'd use it in any Node.js project.

In this tutorial, I'll show two ways of using an npm package in AdonisJS. I'll use the Paddle Node.js SDK to demonstrate this but the same principles apply to other npm packages.

Using the package directly

The simplest way to use an npm package in AdonisJS is to install the package and use it directly wherever we want to use it in our codebase.

We need to first install the package:

npm install @paddle/paddle-node-sdk

Let's say we are working with the concept of customer and we have a CustomersController. First, we import the package and then use it in the controller:

// app/controllers/customers_controller.ts

import { Paddle } from '@paddle/paddle-node-sdk'
import config from '@adonisjs/core/services/config'

export default class CustomersController {
  async handle({ auth }: HttpContext) {
    const paddle = new Paddle(config.get('services.paddle.apiKey'))

    await paddle.customers.get(auth.user!.paddleId)
  }
}

Using a service class

The problem with the above approach is that we end up littering our codebase with the same package import (and initialisation) over and over. For some packages that could be okay. For example, using a package like lodash. But with the package and concept we are working with, that might not be the case.

So to take the above implementation one step further, we could create a PaddleService. We could create the service manually but there's a command for that:

node ace make:service paddle

This will create an empty paddle_service.ts file inside app/services. Add the following code inside the file:

// app/services/paddle_service.ts

import config from '@adonisjs/core/services/config'
import { Customer, Paddle } from '@paddle/paddle-node-sdk'
import app from '@adonisjs/core/services/app'

export default class PaddleService {
  private paddle: Paddle

  constructor() {
    this.paddle = new Paddle(config.get('services.paddle.apiKey'))
 }

  async getCustomer(customerId: string): Promise<Customer> {
    return this.paddle.customers.get(customerId)
 }
}

Like before, we import the package and initiate it. For the usage, we create a method that we can use in controllers or other places. Now, we can keep all Paddle-specified implementations contained in one place.

We can make use of the service class in two ways. First, we import and initialise the class manually:

// app/controllers/customers_controller.ts

import { inject } from '@adonisjs/core'
import PaddleService from '#services/paddle_service'

export default class CustomersController {
  async handle({ auth }: HttpContext) {
    const paddle = new PaddleService()

    await paddle.customers.get(auth.user!.paddleId)
 }
}

The other way is to leverage AdonisJS Dependency Injection (DI) to initialise the class with the necessary dependencies. We can do that using the @inject decorator:

// app/controllers/customers_controller.ts

import { inject } from '@adonisjs/core'
import PaddleService from '#services/paddle_service'

export default class CustomersController {
  @inject()
  async handle({ auth }: HttpContext, paddle: PaddleService) {
    await paddle.customers.get(auth.user!.paddleId)
  }
}

Here, we are using method injection but we could also use constructor injection.

Conclusion

So that's how to use npm packages in AdonisJS. Remember, AdonisJS is just a Node.js framework.


This content originally appeared on DEV Community and was authored by Chimezie Enyinnaya


Print Share Comment Cite Upload Translate Updates
APA

Chimezie Enyinnaya | Sciencx (2024-08-11T07:07:19+00:00) Using npm packages in AdonisJS. Retrieved from https://www.scien.cx/2024/08/11/using-npm-packages-in-adonisjs/

MLA
" » Using npm packages in AdonisJS." Chimezie Enyinnaya | Sciencx - Sunday August 11, 2024, https://www.scien.cx/2024/08/11/using-npm-packages-in-adonisjs/
HARVARD
Chimezie Enyinnaya | Sciencx Sunday August 11, 2024 » Using npm packages in AdonisJS., viewed ,<https://www.scien.cx/2024/08/11/using-npm-packages-in-adonisjs/>
VANCOUVER
Chimezie Enyinnaya | Sciencx - » Using npm packages in AdonisJS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/11/using-npm-packages-in-adonisjs/
CHICAGO
" » Using npm packages in AdonisJS." Chimezie Enyinnaya | Sciencx - Accessed . https://www.scien.cx/2024/08/11/using-npm-packages-in-adonisjs/
IEEE
" » Using npm packages in AdonisJS." Chimezie Enyinnaya | Sciencx [Online]. Available: https://www.scien.cx/2024/08/11/using-npm-packages-in-adonisjs/. [Accessed: ]
rf:citation
» Using npm packages in AdonisJS | Chimezie Enyinnaya | Sciencx | https://www.scien.cx/2024/08/11/using-npm-packages-in-adonisjs/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.