Skip to content

Commit

Permalink
feat: export Context and TypedContext type utility (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
mildronize authored Feb 24, 2023
2 parents 2f0e187 + 3bf1930 commit efb07af
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class UserFunction extends BaseFunction {
- Simple Framework, support all functionality with Azure Functions features
- Provide type utiltiy wrapping around `function.json`
- Better project orgnization
- Binding & Trigger Built-in type support
- Bindings & Triggers Built-in type support
- Http Trigger
- Timer Trigger
- Cosmos DB Trigger
Expand Down
2 changes: 1 addition & 1 deletion packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class UserFunction extends BaseFunction {
- Simple Framework, support all functionality with Azure Functions features
- Provide type utiltiy wrapping around `function.json`
- Better project orgnization
- Binding & Trigger Built-in type support
- Bindings & Triggers Built-in type support
- Http Trigger
- Timer Trigger
- Cosmos DB Trigger
Expand Down
47 changes: 44 additions & 3 deletions packages/core/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Ref: https://github.com/inversify/inversify-express-utils/blob/master/src/interfaces.ts

import { Context, HttpRequest, HttpResponse } from '@azure/functions';
import { Context as AzureFuncContext, HttpRequest, HttpResponse } from '@azure/functions';
import { GetBindings, FunctionBinding, BaseFunctionBinding, HttpType, HttpTriggerType } from './bindings';
import { NotAny } from '@type-challenges/utils';

Expand All @@ -18,9 +18,50 @@ export type IsBindingWith<
: GetReturnType<ReturnType, FallbackReturnType> | undefined
: GetReturnType<ReturnType, FallbackReturnType> | undefined;

type OriginalContextRes = NonNullable<Context['res']>;
type OriginalContextRes = NonNullable<AzureFuncContext['res']>;

export interface TypedContext<T extends readonly FunctionBinding<unknown>[]> extends Omit<Context, 'req' | 'bindingDefinitions'> {
/**
* Azure Function Context for `TypeContext<any>` (Ignore Type)
*
* The context object can be used for writing logs, reading data from bindings, setting outputs and using
* the context.done callback when your exported function is synchronous. A context object is passed
* to your function from the Azure Functions runtime on function invocation.
*
* @example
```
import { Context } from 'nammatham';
function processContext(context: Context){
const { req } = context.bindings;
// ^-- This will be `any`
}
```
*/
export type Context = TypedContext<any>;

/**
* Wrapping Azure Function Context with Type
*
* The context object can be used for writing logs, reading data from bindings, setting outputs and using
* the context.done callback when your exported function is synchronous. A context object is passed
* to your function from the Azure Functions runtime on function invocation.
*
* @example
```
import { TypedContext, binding } from 'nammatham';
const bindings = [
binding.httpTrigger({ name: 'req' as const }),
binding.http({ name: 'res' as const }),
] as const;
function processContext(context: TypedContext<typeof bindings>){
const { req } = context.bindings;
// ^-- This will be `HttpRequest`
}
```
*/
export interface TypedContext<T extends readonly FunctionBinding<unknown>[]>
extends Omit<AzureFuncContext, 'req' | 'bindingDefinitions'> {
/**
* Add prop from the base interface, based on FunctionBinding
*/
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ export { StatusCodes as HttpStatus } from 'http-status-codes';
import * as _bindings from './bindings';
export { _bindings as binding };

// import * as bindings from "./bindings";
// export * from "./external";
export type { Context , TypedContext } from './interfaces';

0 comments on commit efb07af

Please sign in to comment.