Nest.js service structure for API integration

22 Views Asked by At

I'm working on an integration with the DocuSign API in Nest.js project.

My service must meet 3 requirements:

  • Implementing the Code Grant oAuth 2.0 identification process and saving the credentials to Mongo DB record.
  • Performing API operations based on the saved credentials, for example createEnvelope.
  • Implementing Web Hooks listening and updating relevant DB records.

Service Structure

  • How best to plan the service I'm building including performing unit tests from the point of view of the Nest.js architecture? For example - is it worth writing one service that will be responsible for implementing the oAuth process and another to perform the API operations?

Unit Tests

  • What are the guidelines regarding writing the unit tests?

  • Is coverage of the logic responsible for the oAuth correct to be done in the form of a unit test?

1

There are 1 best solutions below

2
Marek Kapusta-Ognicki On

My advice is - build it as if there wasn't any NestJS at play. Think of it as of a standalone SDK. As agnostic as possible.

It'll get supereasy to implement it into NestJS (or any other) architecture later on.


Breaking code into logical parts is always an implementation decision. Sometimes it's pointless to break code into multiple classes/files, sometimes - quite the opposite. That's your decision :)


Generally speaking, testing integration with API is not quite the matter of unit tests, but it all depends on the naming as I've seen hundreds of own definitions where unit tests end, and where features tests start.

But - again. It all depends on the API. If you'd end up writing 100 unit tests for 100 different methods, all of them having axios.get or axios.post, it's pointless. It can be done with a single feature test mocking axios instance on the fly.


Hope that helps anyhow :)