thaitype / nammatham

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

PoC Azure/Functions v4 fit with Nammatham APIs (Functional Approach & Type-safe) #82

Closed mildronize closed 8 months ago

mildronize commented 1 year ago

From my research, TypeScript 5.0 Decorator support more type-safe decorator, however, most popular typeScript Dependency Injection currently support below TypeScript 5.0 Decorator which must provide following tsconfig.json

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

With TypeScript 5.0 Decorator can evict both tsconfig.json.

Currently issues that most popular is already stuck with TypeScript Decorator below version 5.0:

Another issues about Inversify currently find the new maintainer

mildronize commented 1 year ago

PoC Branch: poc-func-v4.functional-type-safe based on poc-func-v4

mildronize commented 12 months ago

My PoC and tested with the official v4 azure functions lib

result is in branch: ~poc-func-v4.functional-type-safe~ , next.v2-experiment

/**
 * Nammatham v2 - Proof of concept: Type-safe & Functional Style
 * 
 * Github Issue: https://github.com/thaitype/nammatham/issues/82
 * Implementation: https://github.com/thaitype/nammatham/blob/poc-func-v4.functional-type-safe/examples/with-functional/src/functions/copyBlob1.ts
 */

import { initNammatham } from '@nammatham/core';

const nmt = initNammatham.create();

nmt
  .httpGet('CopyBlob', {
    authLevel: 'anonymous',
  })
  .addInput(
    'blobInput',
    nmt.input.storageBlob({
      connection: 'AzureWebJobsStorage',
      path: 'demo-input/xxx.txt',
    })
  )
  .addOutput(
    'blobOutput',
    nmt.output.storageBlob({
      connection: 'AzureWebJobsStorage',
      path: 'demo-output/xxx-{rand-guid}.txt',
    })
  )
  .handler((request, context) => {
    context.log('function processed work item:', request);
    const blobInputValue = context.inputs.blobInput.get();

    context.outputs.blobOutput.set(blobInputValue);
    return {
      body: `Hello ${blobInputValue}`,
    };
  });
mildronize commented 8 months ago

The PoC implements addInput and addOutput, it can lead more complex to maintain in the future, so, in v2 upcoming release, it will use inputs and outputs helpers directly from Azure functions packages (@azure/functions@4.1.0)

As you can see in the alpha release of v2, you can see the example to use inputs and outputs helpers directly from Azure functions packages, as following code below:

import { input } from '@azure/functions';
import { func } from '../nammatham';

const blobInput = input.storageBlob({
  connection: 'AzureWebJobsStorage',
  path: 'demo-input/xxx.txt',
});

const blobOutput = input.storageBlob({
  connection: 'AzureWebJobsStorage',
  path: 'demo-output/xxx-{rand-guid}.txt',
});

export default func
  .httpGet('CopyBlob', {
    authLevel: 'anonymous',
    extraInputs: [blobInput],
    extraOutputs: [blobOutput],
  })
  .handler((request, ctx) => {
    ctx.context.log('function processed work item:', request);
    const blobInputValue = ctx.context.extraInputs.get(blobOutput);

    ctx.context.extraOutputs.set(blobOutput, blobInputValue);
    return {
      body: `Hello ${blobInputValue}`,
    };
  });

This issue is quite good to PoC how make Nammatham has more fluent APIs, however, this will mark as v2-experiment instead, it can be considered to implement in the future release.

So, this issue can be close now.