thaitype / nammatham

Azure Functions Framework
https://nammatham.thaitype.dev/
MIT License
67 stars 2 forks source link

PoC Azure/Functions v4 fit with Nammatham APIs (Inversify Extension) #79

Open mildronize opened 1 year ago

mildronize commented 1 year ago

Propose decorator and usage examples

import {
  InvocationContext, 
  HttpResponse,
  Response,
  Request,
  authLevel,
  blobOutput,
  blobTrigger,
  context,
  functionName,
  httpGet,
  httpTrigger,
  httpPost,
  req,
  res,
} from '@nammatham/core';
import { controller } from '@nammatham/inversify';
import { inject, injectable } from 'inversify';

@controller()
export class MyController {
  @functionName('CopyBlob')
  public copyBlob(
    @blobTrigger({ path: 'samples-workitems/{name}' }) blobInput: Buffer,
    @blobOutput({ path: 'export/{name}' }) blobOutput: unknown,
    @context() context: InvocationContext
  ) {
    context.info(blobInput.toString());
    context.info(context.functionName);
    blobOutput = blobInput;
  }

  @functionName('http')
  public httpTrigger(
    @httpTrigger({ authLevel: 'anonymous', methods: ['GET'], route: 'my-data' }) req: Request,
    @res() res: Response,
    @context() context: InvocationContext
  ): HttpResponse {
    context.info(req.toString());
    return res.body('Hello');
  }

  // Shorthand of httpTrigger
  // Full Example of Shorthand
  // Auto generate function name = class name + method name
  // Default authLevel is anonymous, this should be option
  @authLevel('function')
  @httpGet('my-data')
  public simpleGet(@req() req: Request, @res() res: Response, @context() context: InvocationContext) {
    context.info(req.toString());
    return res.body({
      body: 'Hello',
    });
  }

  // Typically Use of Shorthand httpTrigger,
  @httpPost('my-data')
  public async simplePost(@req() req: Request, @res() res: Response): HttpResponse {
    const name = req.query.get('name') || (await req.text()) || 'world';
    return res.body(`Hello, ${name}!`);
  }
}

Steps

mildronize commented 1 year ago

Working branch: