Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update return type of functionRunner #44

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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