Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of the explicit partial type middy.MiddyfiedHandler #162

Open
github-actions bot opened this issue Nov 1, 2023 · 0 comments
Open

Get rid of the explicit partial type middy.MiddyfiedHandler #162

github-actions bot opened this issue Nov 1, 2023 · 0 comments
Labels

Comments

@github-actions
Copy link

github-actions bot commented Nov 1, 2023

and use the implicit type instead.

https://api.github.com/nanlabs/devops-reference/blob/41f65c1c2a3173b66a37e22ebfb443074574bdf8/examples/serverless-localstack-with-s3-and-dynamodb/src/rest/program.ts#L49

import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import middy from '@middy/core';
import validator from '@middy/validator';
import { transpileSchema } from '@middy/validator/transpile';
import httpHeaderNormalizer from '@middy/http-header-normalizer';
import httpJsonBodyParser from '@middy/http-json-body-parser';
import { getPackageVersion, getProgramsS3BucketName } from '../helpers/env';
import { uploadFileToS3 } from '../lib/s3';

type PostProgramEvent = APIGatewayProxyEvent & {
  body: {
    content: string;
    filePath: string;
  };
};

const eventSchema = {
  type: 'object',
  required: ['body'],
  properties: {
    body: {
      type: 'object',
      required: ['content', 'filePath'],
      properties: {
        content: {
          type: 'string',
        },
        filePath: {
          type: 'string',
        },
      },
    },
  },
};

const responseSchema = {
  type: 'object',
  required: ['body', 'statusCode'],
  properties: {
    body: {
      type: 'string',
    },
    statusCode: {
      type: 'number',
    },
  },
};

// TODO: Get rid of the explicit partial type middy.MiddyfiedHandler
//      and use the implicit type instead.
const handler: middy.MiddyfiedHandler = middy<PostProgramEvent, APIGatewayProxyResult>()
  .use(httpHeaderNormalizer())
  .use(httpJsonBodyParser())
  .use(
    validator({
      eventSchema: transpileSchema(eventSchema),
      responseSchema: transpileSchema(responseSchema),
    }),
  )
  .handler(async (event) => {
    const { content, filePath } = event.body;

    console.log(`Received program ${filePath} with content: ${content}`);

    const programsS3BucketName = getProgramsS3BucketName();
    if (!programsS3BucketName) throw new Error('Programs S3 Bucket Name is not defined.');

    const packageVersion = getPackageVersion();
    if (!packageVersion) throw new Error('Package Version is not defined.');

    console.log(`Uploading to s3://${programsS3BucketName}/${filePath}...`);

    await uploadFileToS3({
      bucketName: programsS3BucketName,
      filePath,
      fileContent: Buffer.from(event.body.content, 'utf8'),
    });

    return {
      statusCode: 200,
      body: JSON.stringify({
        message: 'Program uploaded successfully to S3.',
      }),
    };
  });

export { handler };
@github-actions github-actions bot added the todo label Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants