DEV Community

Discussion on: First Look at Lambda Powertools TypeScript

Collapse
 
drupsys profile image
DR • Edited

Well... this solution was designed by someone who has never heard a word "Patterns", just spray syntactic sugar everywhere indiscriminately to solve all your problem.

Could have just used a decorator pattern, would have been backwards compatible with JavaScript, work with any kind of function, need no classes and actually be more compact than the code above...

Collapse
 
drupsys profile image
DR • Edited

sorry, I'm a bit annoyed in case you can't tell, but seriously we couldn't just do something like this?

import { decorate, Tracer } from '@aws-lambda-powertools/tracer';
import type { Context } from 'aws-lambda';
import type { Payment } from '../models/payment';

const {
    captureLambdaHandler,
    superDuperSpecialStuff,
} = Tracer({ serviceName: 'paymentCollections' }); // This can't be a class anymore, because javascript...

const handler = async (
    input: { Payload: { Payment: Payment } },
    _context: Context,
): Promise<{ Status: number; Payment: Payment }> => {
    const min = 0;
    const max = 1;
    const Status = Math.floor(Math.random() * (max - min + 1)) + min;

    return { Status, Payment: input.Payload.Payment };
}

export default decorate(
    handler,
    captureLambdaHandler,
    superDuperSpecialStuff,
    // ...
);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
elthrasher profile image
Matt Morgan

What you want is supported using middy. You can use class decorators, middy-style function "decorators", or inline imperative statements. Check the docs and you'll see each example has a "Middy Middleware" example.