Backend Customization

Backend is based on NodeJS and Express. The entry point for the backend is project/index.ts. To add new functionality just write it in this file, or write it in some other file and import it in project/index.ts.

As Burdy is hook-based you can access and extend burdy server functionality by just adding actions and filters and adding such as new endpoints, email transport setup, and more.

API Endpoint customization

To access Express server and add new endpoints use api/init hook.

// project/index.ts

Hooks.addAction('api/init', async (app) => {
  app.get('/test', asyncMiddleware(async (req, res) => {
    res.send({
      text: 'Hello World'
    })
  }));
});

You can access this endpoint by triggering @GET request <burdy-cms>/api/test

We have also exposed root Express object and you can use it by adding server/init action.

API Endpoint with database access

In this example, we will create a simple endpoint /api/docs that is retrieving all Posts of type pages from the database that have parent with slug docs.

// project/index.ts

Hooks.addAction('api/init', async (app) => {
  app.get('/docs', asyncMiddleware(async (req, res) => {
    const postRepository = getRepository(Post);
    const posts = await postRepository.createQueryBuilder('post')
      .leftJoinAndSelect('post.parent', 'parent')
      .where('post.type = :type', {type: 'page'})
      .andWhere('parent.slug = :slug', {slug: 'docs'})
      .getMany();
    res.send(posts);
  }));
});

For more details please visit TypeORM documentation.

Mail transport

Burdy is using Nodemailer as a built-in email provider. Transport is not set by default so you will need to set your default transport to use sendMail functionality.

To set SMTP transport use filter mail/options:

Hooks.addFilter('mail/options', async (_options) => {
  return {
    host: "smtp.example.com",
    port: 587,
    secure: false, // upgrade later with STARTTLS
    auth: {
      user: "username",
      pass: "password",
    }
  } as any;
});

Or if you wish to set other transport such as AWS SES, you can set it with:

const ses = new aws.SES({
  region: process.env.AWS_SES_REGION
});
Hooks.addFilter('mail/options', async (_options) => {
  return {
    SES: {ses, aws},
  } as any;
});

To send email just use sendMail from mail.driver

import {sendMail} from 'burdy/src/server/drivers/mail.driver';

sendMail({
  from: 'team@burdy.io',
  to: 'user@email.com',
  subject: 'Subject',
  text: 'Text',
  html: 'HTML of Email - Content'
});

For this list of possible transports please visit nodemailer documentation
Copyright © Burdy Technologies. All rights reserved.