This module facilitates the usage of Octokit in NestJS.
Octokit is "The all-batteries-included GitHub SDK for Browsers, Node.js, and Deno".
Using nestjs-octokit
you can register the Octokit module and configure it the way NestJS suggests, then inject it as a standard NestJS injectable.
On Yarn:
yarn add nestjs-octokit octokit
On NPM:
npm install nestjs-octokit octokit
First register the module:
import { OctokitModule } from 'nestjs-octokit';
@Module({
imports: [
OctokitModule.forRoot({
isGlobal: true,
octokitOptions: {
auth: 'my-github-token',
},
}),
// ...
],
})
export class AppModule {}
Or if want to inject any dependency:
import { OctokitModule } from 'nestjs-octokit';
@Module({
imports: [
OctokitModule.forRootAsync({
isGlobal: true,
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
octokitOptions: {
auth: configService.get<string>('GITHUB_AUTH_TOKEN'),
},
}),
}),
// ...
],
})
export class AppModule {}
Then you can inject the service:
import { OctokitService } from 'nestjs-octokit';
@Controller()
export class SomeController {
constructor(private readonly octokitService: OctokitService) {}
@Get('/')
someEndpoint() {
const response = await this.octokitService.rest.search.repos({
q: 'nest-js',
});
return response.data.items;
}
}
If your access tokens are short-lived, you can configure a request
scoped
Octokit provider with an auth
callback, to renew the Octokits
and auth tokens:
import { OctokitModule } from 'nestjs-octokit';
import { Scope } from '@nestjs/common';
@Module({
imports: [
OctokitModule.forRootAsync({
isGlobal: true,
// Set request scope
octokitScope: Scope.REQUEST,
imports: [TokenModule],
inject: [TokenService],
useFactory: async (tokenService: TokenService) => ({
octokitOptions: {
// `auth` is a callback now. Return short-lived tokens
auth: () => tokenService.produceToken(),
},
}),
}),
// ...
],
})
export class AppModule {}
To use plugins:
import { OctokitModule } from 'nestjs-octokit';
import { throttling } from '@octokit/plugin-throttling';
@Module({
imports: [
OctokitModule.forRoot({
isGlobal: true,
plugins: [throttling], // Pass them here
octokitOptions: {
// Plugin options:
throttle: {
onRateLimit: (retryAfter, options, octokit) => {
octokit.log.warn(
`Request quota exhausted for request ${options.method} ${options.url}`
);
},
onAbuseLimit: (retryAfter, options, octokit) => {
octokit.log.warn(
`Abuse detected for request ${options.method} ${options.url}`
);
},
},
},
}),
],
})
export class AppModule {}