Skip to content

Commit

Permalink
functionRunner return type made generic
Browse files Browse the repository at this point in the history
  • Loading branch information
dhensby committed Jun 16, 2023
1 parent d735987 commit 2319d79
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/function-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { extractBindings } from './utils';

export type AugmentContextCallback = (context: Context) => void;

export async function functionRunner(azFunction: AzureFunction, bindingDefinitions: BindingDefinition[] | string = [], bindingData: Record<string, Binding> = {}, augmentContext?: AugmentContextCallback): Promise<any> {
export async function functionRunner<T = ReturnType<AzureFunction>>(azFunction: AzureFunction, bindingDefinitions: BindingDefinition[] | string = [], bindingData: Record<string, Binding> = {}, augmentContext?: AugmentContextCallback): Promise<T> {
return new Promise((resolve, reject) => {
const resolver = (err: null | Error, result?: any) => {
if (err) {
Expand Down
5 changes: 3 additions & 2 deletions test/function-runner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { functionRunner, QueueBinding } from '../lib';
import { match, stub } from 'sinon';
import { expect } from 'chai';
import { resolve } from 'path';
import { Context } from '@azure/functions';

const contextMatcher = match({
invocationId: match.string,
Expand Down Expand Up @@ -32,7 +33,7 @@ describe('function-runner', () => {
expect(augmentor).to.have.callCount(1);
});
it('returns the function result if out name is $return', async () => {
const functionStub = stub().resolves('response value');
const functionStub = stub<[Context], Promise<string>>().resolves('response value');
const result = await functionRunner(functionStub, [{ type: 'queue', direction: 'out', name: '$return' }]);
expect(functionStub).to.have.callCount(1);
expect(result).to.equal('response value');
Expand All @@ -45,7 +46,7 @@ describe('function-runner', () => {
expect(contextMatcher.test(result)).to.equal(true);
});
it('returns the context if nothing returned', async () => {
const functionStub = stub().callsFake((ctx) => ctx.done());
const functionStub = stub<[Context], void>().callsFake((ctx) => ctx.done());
const result = await functionRunner(functionStub);
expect(contextMatcher.test(result)).to.equal(true);
});
Expand Down

0 comments on commit 2319d79

Please sign in to comment.