nestjs-amo
v0.1.0
Published
Wrapper over the @shevernitskiy/amo library for interacting with the amoCRM API from a NestJS application.
Downloads
608
Maintainers
Readme
Introduction
This is a simple wrapper of @shevernitskiy/amo library. With this module you can interact with the amoCRM/Kommo API from a NestJS application.
Installation
# npm
npm install nestjs-amo
# yarn
yarn add nestjs-amo
Furthermore, install @shevernitskiy/amo in your NestJS application, if you haven't already.
# npm
npm install @shevernitskiy/amo
# yarn
yarn add @shevernitskiy/amo
Usage
Add AmoModule
to the imports
section in your AppModule
or other modules to
gain access to AmoService
.
Static import
import { Module } from "@nestjs/common";
import { AmoModule } from "nestjs-amo";
@Module({
imports: [
AmoModule.forRoot({
isGlobal: true,
amoServiceOptions: {
widget_settings: {
client_id: "1111-2222-3333",
client_secret: "myclientsecret",
redirect_uri: "https://myredirect.org",
},
getCredentials: async (amoId) => {
// Implement your logic to retrieve a token from your long-term storage facility
return {
access_token: "some token",
expires_at: 86400,
expires_in: 1693211983,
refresh_token: "saved token",
domain: "https://example.ru",
};
},
onTokenUpdate: async (amoId, token) => {
// Implement your logic for saving authorization keys to long-term storage
console.log("New token obtained", amoId, token);
},
},
}),
],
})
export class AppModule {}
Additionally, AmoModule
provides a forRootAsync
to pass options
asynchronously.
Async configuration
import { Module } from "@nestjs/common";
import { AmoModule } from "nestjs-amo";
@Module({
imports: [
AmoModule.forRootAync({
isGlobal: true,
useFactory: async () => {
return {
widget_settings: {
client_id: "1111-2222-3333",
client_secret: "myclientsecret",
redirect_uri: "https://myredirect.org",
},
getCredentials: async (amoId) => {
// Implement your logic to retrieve a token from your long-term storage facility
return {
access_token: "some token",
expires_at: 86400,
expires_in: 1693211983,
refresh_token: "saved token",
domain: "https://example.ru",
};
},
onTokenUpdate: async (amoId, token) => {
// Implement your logic for saving authorization keys to long-term storage
console.log("New token obtained", amoId, token);
},
};
},
}),
],
})
export class AppModule {}
You can inject dependencies such as ConfigModule
to load options from .env
files.
import { Module } from "@nestjs/common";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { AmoModule } from "nestjs-amo";
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
}),
AmoModule.forRootAync({
isGlobal: true,
useFactory: async (configService: ConfigService) => {
return {
widget_settings: {
client_id: configService.get("AMO_CLIENT_ID"),
client_secret: configService.get("AMO_CLIENT_SECRET"),
redirect_uri: configService.get("AMO_REDIRECT_URI"),
},
getCredentials: async (amoId) => {
// Implement your logic to retrieve a token from your long-term storage facility
return {
access_token: "some token",
expires_at: 86400,
expires_in: 1693211983,
refresh_token: "saved token",
domain: "https://example.ru",
};
},
onTokenUpdate: async (amoId, token) => {
// Implement your logic for saving authorization keys to long-term storage
console.log("New token obtained", amoId, token);
},
};
},
inject: [ConfigService],
}),
],
})
export class AppModule {}
Calling send method
import { AmoService } from "nestjs-amo";
@Injectable()
export class YourService {
constructor(private amoService: AmoService) {}
async method() {
const amoApi = await this.amoService.create(123);
return amoApi.account.getAccount({ with: ["version"] });
}
}
Calling custom endpoint
import { AmoService } from "nestjs-amo";
@Injectable()
export class YourService {
constructor(private amoService: AmoService) {}
async method() {
const amoApi = await this.amoService.create(123);
return amoApi.raw.get({
url: "/ajax/merge/leads/save",
headers: {
"X-Requested-With": "XMLHttpRequest",
},
payload: "some payload",
});
}
}
Contributing
Contributions welcome! See Contributing.
Notes
This project is not endorsed by or affiliated with AmoCRM.
Author
Yaroslav Tolstoy - GitHub
License
Licensed under the MIT License - see the LICENSE file for details.