Added tuya space to community

This commit is contained in:
hannathkadher
2024-10-29 14:24:01 +04:00
parent 00ed2c5bbf
commit d6e6eb9b1f
8 changed files with 82 additions and 46 deletions

View File

@ -0,0 +1,38 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
@Injectable()
export class TuyaService {
private tuya: TuyaContext;
constructor(private readonly configService: ConfigService) {
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
const tuyaEuUrl = this.configService.get<string>('tuya-config.TUYA_EU_URL');
this.tuya = new TuyaContext({
baseUrl: tuyaEuUrl,
accessKey,
secretKey,
});
}
async createSpace({ name }: { name: string }) {
const path = '/v2.0/cloud/space/creation';
const body = { name };
const response = await this.tuya.request({
method: 'POST',
path,
body,
});
if (response.success) {
return response.result as string;
} else {
throw new HttpException(
'Error creating space in Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}