Code Monkey home page Code Monkey logo

nest-gitlab's Introduction

Nest Logo

A gitbeaker wrapper for NestJS framework.

NPM Version Travis build Package License

Description

Gitlab API library module for Nest.

Installation

npm install --save nest-gitlab @gitbeaker/node

Compatibility

gitbeaker nest-gitlab
node-gitlab (former gitbeaker) 1.x
19.0.0 ~ 23.5.0 2.x
23.6.0 ~ 3.x

Quick Start

Import GitlabModule and configure it with the same initiating options as the gitbeaker package.

import { GitlabModule } from 'nest-gitlab';

@Module({
  imports: [
    GitlabModule.forRoot({
      // options
    }),
  ],
})
export class AppModule {}

Afterward, the gitlab instance will be ready to be injected across the entire project using the gitlab injection token.

import { Controller, Inject } from '@nestjs/common';
import { GitlabInstance } from 'nest-gitlab';

@Controller('cats')
export class CatsController {
  constructor(@Inject('gitlab') private readonly gitlab: GitlabInstance) { }
}

You could also use the InjectGitlabInstance decorator to inject gitlab instance.

import { Controller, Inject } from '@nestjs/common';
import { GitlabInstance, InjectGitlabInstance } from 'nest-gitlab';

@Controller('cats')
export class CatsController {
  constructor(@InjectGitlabInstance() private readonly gitlab: GitlabInstance) { }

  @Get('/projects')
  public async getProjects() {
    return await this.gitlab.Projects.all();
  }
}

Async Configuration

You might want to asynchronously pass your module options. In such case, use the forRootAsync() method. The option object could be returned by useFactory method:

import { Module } from '@nestjs/common';
import { GitlabModule } from 'nest-gitlab';

@Module({
  imports: [
    GitlabModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (config: ConfigService) => {
        // return options
      },
      inject: [ConfigService],
    }),
  ],
})
export class AppModule {}

Bundle Support

gitbeaker provides bundle feature which is a export for importing and instantiating all related API's of a specific resource at once. In order to use this feature, you could use the InjectBundleRef decorator combined with forFeature method:

import { GitlabModule } from 'nest-gitlab';

@Module({
  imports: [
    GitlabModule.forRoot({
      // options
    }),
  ],
})
export class AppModule {}
import { Module } from '@nestjs/common';
import { BundleType, GitlabModule } from 'nest-gitlab';

@Module({
  imports: [
    GitlabModule.forFeature([BundleType.Projects]),
  ],
  controllers: [CatsController],
  providers: [CatsService],
})
export class CatsModule {}

BundleType could be Projects, Users, or Groups.

import { Controller, Inject } from '@nestjs/common';
import { BundleType, GitlabInstance, InjectBundleRef, ProjectsBundleRef } from 'nest-gitlab';

@Controller('cats')
export class CatsController {
  constructor(@InjectBundleRef(BundleType.Projects) private readonly pbr: ProjectsBundleRef) { }

  @Get('/projects')
  public async getProjects() {
    return await this.pbr.Projects.all();
  }
}

Multiple GitLab Instances

In some cases, your projects may require multiple GitLab instances. This can be achieved by naming the gitlab instances:

import { Module } from '@nestjs/common';
import { GitlabModule } from 'nest-gitlab';

@Module({
  imports: [
    GitlabModule.forRootAsync({
      name: 'ins1',
      imports: [ConfigModule],
      useFactory: async (config: ConfigService) => {
        // return options
      },
      inject: [ConfigService],
    }),
    GitlabModule.forRoot({
      name: 'ins2',
      // options
    }),
  ],
})
export class AppModule {}
import { Module } from '@nestjs/common';
import { BundleType, GitlabModule } from 'nest-gitlab';

@Module({
  imports: [
    GitlabModule.forFeature([BundleType.Projects], 'ins1'),
  ],
  controllers: [CatsController],
  providers: [CatsService],
})
export class CatsModule {}
import { Controller, Inject } from '@nestjs/common';
import { BundleType, GitlabInstance, InjectBundleRef, InjectGitlabInstance, ProjectsBundleRef } from 'nest-gitlab';

@Controller('cats')
export class CatsController {
  constructor(@InjectBundleRef(BundleType.Projects, 'ins1') private readonly pbr1: ProjectsBundleRef,
              @InjectGitlabInstance('ins2') private readonly gitlab2: GitlabInstance) { }
}

License

MIT

nest-gitlab's People

Contributors

hexenq avatar morpheus-87 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.