0.0.18-9 • Published 1 year ago

nextjs-backend-helpers v0.0.18-9

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

nextjs-backend-helpers

A collection of helpers designed to make fullstack NextJS services easier to create. There are helpers to register API style controllers, a database Repository class designed to work with Prisma, and even testing tools.

The Problem

The NextJS Documentation says "you can build your entire API with API Routes.", but writing API routes in NextJS sucks. Here is an example handler for a post request:

export default function handler(req, res) {
  if (req.method === 'POST') {
    // Process a POST request
  } else {
    // Handle any other HTTP method
  }
}

While this is fine for simple routes, it's easy to see how this doesn's scale.

The Solution

nextjs-backend-helpers exports a number of use classes and functions to make not suck.

Controllers

To create a class based controller, simply extend the Controller base class and install it. Controllers support middleware through their before and after methods.

Kitchen Sink Example

export class AppController extends Controller {
  constructor() {
    super()

    this.rescue(PrismaClientInitializationError, (error, request, response) => {
      Logger.error({
        message: 'Looks like we cant reach the database.'
        + 'Is the connection string right?'
        + 'Is the database up?',
      })

      response.status(500).json({
        errors: ['unable to reach database'],
      })
    })

    this.rescue(Error, (error, _request, response) => {
      response.status(500).json({
        errors: [error.constructor.name, error.message],
      })
    })
  }
}

type UserIdQuery = {
  id: string
}

type UserBody = {
  name: string,
  birthday: date
}

type Response = {
  id: string,
  name: string
}

export class UserController extends AppController {
  constructor() {
    super()
    this.before((req: NextApiRequest) => {
      console.log(Cookie.get('secret-cookie-value'))
    }).only('get')

    this.after(() => {
      console.log('im running after the post action has run')
    }).only('post')
  }

  async get(request: NextApiRequest, response: NextApiResponse<Response>) {
    //...
    const {id} = getQuery<UserIdQuery>(request)
    const {name, birthday} = getBody<UserBody>(request)
    const cookieValue = Cookie.get('secret-cookie-value')
    //..

    response.json({
      id,
      name,
      cookieValue
    })
  }
}

We also have test helpers

// don't import the default export, but the named export
import { UserController, RequestBuilder } from './user-controller.ts'
import { get, ReponseType, RequestBuilder } from 'nextjs-backend-helpers'

describe('UserController', () => {
  describe('returns a user', () => {
    let response: ResponseType
    let request: RequestBuilder
    let user: User

    beforeEach(async () => {
      request = RequestBuilder().query({
        id: '123'
      })
      response = await get(UserController, request)
    })

    it("returns the requested user", () => {
      expect(response.json).toEqual({
        data: {
          id: '123',
          name: 'Example user'
        }
      })
    })
  })
})
0.0.18-8

1 year ago

0.0.18-9

1 year ago

0.0.18-3

1 year ago

0.0.18-4

1 year ago

0.0.18-5

1 year ago

0.0.18-6

1 year ago

0.0.18-7

1 year ago

0.0.18

1 year ago

0.0.18-1

1 year ago

0.0.18-2

1 year ago

0.0.13-1

2 years ago

0.0.15-2

2 years ago

0.0.15-1

2 years ago

0.0.15-4

2 years ago

0.0.15-3

2 years ago

0.0.15-6

2 years ago

0.0.15-5

2 years ago

0.0.15-8

2 years ago

0.0.15-7

2 years ago

0.0.15-9

2 years ago

0.0.15

2 years ago

0.0.16

2 years ago

0.0.16-1

2 years ago

0.0.16-2

2 years ago

0.0.14

2 years ago

0.0.16-4

2 years ago

0.0.16-7

2 years ago

0.0.16-6

2 years ago

0.0.16-8

2 years ago

0.0.3-0

2 years ago

0.0.2-0

2 years ago

0.0.2-6

2 years ago

0.0.2-5

2 years ago

0.0.2-4

2 years ago

0.0.2-3

2 years ago

0.0.10-12

2 years ago

0.0.10-10

2 years ago

0.0.10-1

2 years ago

0.0.10-0

2 years ago

0.0.12-1

2 years ago

0.0.10-3

2 years ago

0.0.10-2

2 years ago

0.0.12-3

2 years ago

0.0.10-5

2 years ago

0.0.12-2

2 years ago

0.0.10-4

2 years ago

0.0.11

2 years ago

0.0.10-7

2 years ago

0.0.12-4

2 years ago

0.0.12

2 years ago

0.0.10-6

2 years ago

0.0.13

2 years ago

0.0.12-7

2 years ago

0.0.10-9

2 years ago

0.0.12-6

2 years ago

0.0.10-8

2 years ago

0.0.1

2 years ago

0.0.3

2 years ago

0.0.8

2 years ago

0.0.9-0

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

4.0.3

2 years ago

3.0.0

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.0

2 years ago