thaitype / nammatham

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

Make Return Output function only pick the direction out #46

Closed mildronize closed 1 year ago

mildronize commented 1 year ago

Previously, we need to use Partial type to wrap the GetBindings type, for multiple return type

// 0.7.0
import { BaseFunction, Binding, functionName, GetBindings } from 'nammatham';

const bindings = [
  Binding.httpTrigger({ name: 'req' as const}),
  Binding.http({ name: 'res' as const }),
];

@functionName('WithTypeUtility', ...bindings)
export class WithTypeUtilityFunction extends BaseFunction<typeof bindings> {

  public override execute(): Partial<GetBindings<typeof bindings>> {
    const { name } = this.req.query;
    return {
      res: this.res.send()
    }
  }
}

multiple outputs Return binding:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-node?tabs=v2-v3-v4-export%2Cv2-v3-v4-done%2Cv2%2Cv2-log-custom-telemetry%2Cv2-accessing-request-and-response%2Cwindows-setting-the-node-version#outputs

as shown in the doc:

module.exports = async function(context) {
    let retMsg = 'Hello, world!';
    return {
        httpResponse: {
            body: retMsg
        },
        queueOutput: retMsg
    };
};

Expected Behavior

We want the GetOutputBindings to remove all input direction

type GetOutputBindings<T  extends readonly FunctionBinding<unknown>[]> = Pick<GetBindings<T>, 'res'>;

So, we can you like this:

import { BaseFunction, Binding, functionName, GetOutputBindings } from 'nammatham';

const bindings = [
  Binding.httpTrigger({ name: 'req' as const}),
  Binding.http({ name: 'res' as const }),
];

@functionName('WithTypeUtility', ...bindings)
export class WithTypeUtilityFunction extends BaseFunction<typeof bindings> {

  public override execute(): GetOutputBindings<typeof bindings> {
    const { name } = this.req.query;
    return {
      res: this.res.send()
    }
  }
}

Todo

mildronize commented 1 year ago

I've done PoC on main.issues-46

mildronize commented 1 year ago

type utils

binding.inferReturn<typeof bindings> // get return bindings
binding.inferOutput<typeof bindings> // get all output bindings
binding.inferInput<typeof bindings> // get all input bindings
binding.infer<typeof bindings> // get all bindings
mildronize commented 1 year ago

Already fixed in #61