API Interfaces
This page provides an overview of the API interfaces available in Sparrow Home REST endpoints.
Auto-generated services
Sparrow Home uses NestJS Swagger module to auto-generate API documentation and client services. The API documentation is available at:
http://localhost:3000/api
once the server is running.
This approach allows you to easily integrate Sparrow Home with your existing applications. The generated services can be used to interact with the backend API without needing to manually write HTTP requests.
To include classes and methods you need to add Swagger decorators to the class and method definitions. For example:
import { Body, Controller, Get, Put, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { AuthGuard } from '@sparrow-server/auth';
import { AlarmService } from '../services/alarm.service';
import { GetAlarmModeResponse } from './model/get-alarm-mode.response';
import { SetAlarmModeRequest } from './model/set-alarm-mode.request';
@ApiTags('Alarm')
@UseGuards(AuthGuard)
@ApiBearerAuth()
@Controller('alarm')
export class AlarmController {
public constructor(private readonly _alarmService: AlarmService) {}
@ApiOperation({ operationId: 'setAlarmMode' })
@Put('set-mode')
public async setAlarmMode(@Body() request: SetAlarmModeRequest): Promise<void> {
await this._alarmService.setAlarmMode(request.isActive);
}
@ApiOperation({ operationId: 'getAlarmMode' })
@ApiResponse({ type: GetAlarmModeResponse })
@Get('mode')
public async getAlarmMode(): Promise<GetAlarmModeResponse> {
return this._alarmService.getAlarmMode();
}
}
This is crucial to ensure that the generated services are correctly configured and ready to use. If your IDE does not provide auto-completion or type checking for the generated services, its probably because the decorators are not properly applied.