@chelseaapps/zoom documentation

File

Description

Sample interface for ZoomService

Customize this as needed to describe the ZoomService

import { Injectable, Inject, Logger, HttpService } from '@nestjs/common';
import jwt from 'jsonwebtoken';
import { ZOOM_OPTIONS } from './constants';
import { ZoomOptions } from './interfaces';
import { IZoomCommand } from './interfaces/command.interface';

/**
 * Sample interface for ZoomService
 *
 * Customize this as needed to describe the ZoomService
 *
 */
interface IZoomService {}

@Injectable()
export class ZoomService implements IZoomService {
    private readonly logger: Logger;
    constructor(@Inject(ZOOM_OPTIONS) private _ZoomOptions: ZoomOptions, private httpService: HttpService) {
        this.logger = new Logger('ZoomService');
        this.logger.log(`Options: ${JSON.stringify(this._ZoomOptions)}`);
    }

    /**
     * Send a command to the Zoom API
     * @param command Command instance to send
     * @returns API Data
     */
    async send<T>(command: IZoomCommand) {
        const token = await this.generateToken();

        return this.httpService
            .request<T>({
                url: this.getURL(command),
                headers: {
                    authorization: `Bearer ${token}`,
                },
                method: command.method,
                data: command.body,
            })
            .toPromise();
    }

    /**
     * Generate a new API token used to access the Zoom API
     * @param expiry Expiration time in `ms` format - default `5s`
     * @returns JWT token
     */
    private async generateToken(expiry = '5s') {
        return jwt.sign({}, this._ZoomOptions.auth.secret, {
            expiresIn: expiry,
            issuer: this._ZoomOptions.auth.key,
        });
    }

    /**
     * Generate the URL for a command request
     * @param command Command instancce
     * @returns URI encoded string
     */
    private getURL(command: IZoomCommand) {
        return encodeURI(`https://api.zoom.us/v2/${command.path}${this.getQueryParams(command)}`);
    }

    /**
     * Generate a query parameter string
     * @param command Command instance
     * @returns Parameters string
     */
    private getQueryParams(command: IZoomCommand) {
        if (!command.queryParameters || command.queryParameters.length === 0) return '';

        return '&' + command.queryParameters?.map((param) => `${param.name}=${param.value}`).join('&');
    }
}

Was this helpful?