Skip to content

Commit

Permalink
Merge pull request #44 from dhensby/pulls/return-type
Browse files Browse the repository at this point in the history
Update return type of functionRunner
  • Loading branch information
dhensby authored Jun 20, 2023
2 parents d735987 + 5bd31ae commit 9bf55f3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 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 extends AzureFunction = AzureFunction>(azFunction: T, bindingDefinitions: BindingDefinition[] | string = [], bindingData: Record<string, Binding> = {}, augmentContext?: AugmentContextCallback): Promise<Awaited<ReturnType<T>> extends void ? Context : ReturnType<T>> {
return new Promise((resolve, reject) => {
const resolver = (err: null | Error, result?: any) => {
if (err) {
Expand Down
7 changes: 4 additions & 3 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,20 +33,20 @@ 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');
});
it('returns the context when value sent to done callback', async () => {
const functionStub = stub().callsFake((ctx) => {
const functionStub = stub<[Context], Promise<void>>().callsFake(async (ctx) => {
ctx.done(null, { myOutput: 'My message' });
});
const result = await functionRunner(functionStub, [{ type: 'queue', direction: 'out', name: 'myOutput' }]);
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 9bf55f3

Please sign in to comment.