Setting request schema in serverless.ts

Hi all!

Just getting started with serverless and having a blast. I’m struggling to figure out how to represent the following yaml in serverless.ts and would be very grateful for any help!

events:
  - http:
      method: post
      path: items
      request:
        schema:
          application/json: ${file(models/create-todo-model.json)}

I can’t seem to find much in the way of documentation for the typescript file but have been able to figure most things out by looking at the type defenitions… I’m stumped with this though!

1 Like

I wasn’t even aware this could be specified in TypeScript!
If you can share what you have already I’d be happy to try to make it work as I’m now interested in this approach myself

Found an example here https://www.serverless.com/framework/docs/providers/aws/guide/intro/
I’ll have a play around

Have you tried

events: [
    {
      http: {
        method: 'post',
        path: 'items',
        request: {
          schema: {
            'application/json': readFileSync('someFile').toJSON()
          }
        }
      },
    }

or

events: [
    {
      http: {
        method: 'post',
        path: 'items',
        request: {
          schema: {
            'application/json': JSON.parse(readFileSync('someFile').toString())
          }
        }
      },
    }

Or just:

events: [
{
  http: {
    method: 'post',
    path: 'items',
    request: {
      schema: {
        'application/json': readFileSync('someFile').toString()
      }
    }
  },
}

Obviously, you’d need the import in the file too

import { readFileSync } from 'fs';