diff --git a/src/app.ts b/src/app.ts index 20e2f02..5a8ffae 100644 --- a/src/app.ts +++ b/src/app.ts @@ -87,6 +87,10 @@ export function post(name: string, optionsOrHandler: HttpMethodFunctionOptions | http(name, convertToHttpOptions(optionsOrHandler, 'POST')); } +export function query(name: string, optionsOrHandler: HttpMethodFunctionOptions | HttpHandler): void { + http(name, convertToHttpOptions(optionsOrHandler, 'QUERY')); +} + export function patch(name: string, optionsOrHandler: HttpMethodFunctionOptions | HttpHandler): void { http(name, convertToHttpOptions(optionsOrHandler, 'PATCH')); } diff --git a/test/app.http.test.ts b/test/app.http.test.ts new file mode 100644 index 0000000..98420c2 --- /dev/null +++ b/test/app.http.test.ts @@ -0,0 +1,63 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT License. + +import 'mocha'; +import { FunctionMetadata } from '@azure/functions-core'; +import { expect } from 'chai'; +import * as sinon from 'sinon'; +import * as app from '../src/app'; +import * as tryGetCoreApiLazyModule from '../src/utils/tryGetCoreApiLazy'; +import { HttpHandler, HttpMethodFunctionOptions } from '../types'; + +describe('app.query', () => { + const handler: HttpHandler = () => ({ body: 'results' }); + const registerFunction = sinon.stub(); + + beforeEach(() => { + registerFunction.reset(); + sinon.stub(tryGetCoreApiLazyModule, 'tryGetCoreApiLazy').returns({ + registerFunction, + setProgrammingModel: sinon.stub(), + } as unknown as ReturnType); + }); + + afterEach(() => { + sinon.restore(); + }); + + for (const useOptions of [false, true]) { + it(`registers QUERY with ${useOptions ? 'options' : 'a handler'}`, () => { + const options: HttpMethodFunctionOptions = { + handler, + route: 'search', + authLevel: 'function', + }; + app.query('queryFn', useOptions ? options : handler); + + sinon.assert.calledOnce(registerFunction); + const [metadata, callback] = registerFunction.firstCall.args as [FunctionMetadata, HttpHandler]; + expect(metadata.name).to.equal('queryFn'); + const binding = Object.values(metadata.bindings).find((b) => b.type === 'httpTrigger'); + expect(binding).to.deep.include({ + methods: ['QUERY'], + authLevel: useOptions ? 'function' : 'anonymous', + }); + if (useOptions) { + expect(binding).to.include({ route: 'search' }); + } else { + expect(binding).not.to.have.property('route'); + } + expect(metadata.bindings.$return?.type).to.equal('http'); + expect(callback).to.equal(handler); + }); + } + + it('preserves QUERY alongside other methods in app.http', () => { + app.http('mixedFn', { methods: ['GET', 'QUERY'], handler }); + + sinon.assert.calledOnce(registerFunction); + const metadata = registerFunction.firstCall.args[0] as FunctionMetadata; + const binding = Object.values(metadata.bindings).find((b) => b.type === 'httpTrigger'); + expect(binding).to.deep.include({ methods: ['GET', 'QUERY'] }); + }); +}); diff --git a/test/http/HttpRequest.test.ts b/test/http/HttpRequest.test.ts index b7463b2..beca697 100644 --- a/test/http/HttpRequest.test.ts +++ b/test/http/HttpRequest.test.ts @@ -12,6 +12,22 @@ import * as workerLogModule from '../../src/utils/workerSystemLog'; chai.use(chaiAsPromised); describe('HttpRequest', () => { + it('preserves a QUERY request method and JSON body when cloned', async () => { + const body = { filter: 'active' }; + const req = new HttpRequest({ + method: 'QUERY', + url: 'http://localhost:7071/api/search', + headers: { 'content-type': 'application/json' }, + body: { string: JSON.stringify(body) }, + }); + const cloned = req.clone(); + + expect(req.method).to.equal('QUERY'); + expect(cloned.method).to.equal('QUERY'); + expect(await req.json()).to.deep.equal(body); + expect(await cloned.json()).to.deep.equal(body); + }); + it('clone', async () => { const req = new HttpRequest({ method: 'POST', diff --git a/test/types/index.test.ts b/test/types/index.test.ts index 8f9f38b..b7e5ee3 100644 --- a/test/types/index.test.ts +++ b/test/types/index.test.ts @@ -73,6 +73,24 @@ const invalidServiceBusTopicTriggerOptions: ServiceBusTopicTriggerOptions = { declare const app: typeof import('../../types').app; declare const trigger: typeof import('../../types').trigger; +function registerHttpQuery(): void { + app.query('queryHandler', async (request, context) => { + context.log(request.method); + return { jsonBody: await request.json() }; + }); + app.query('queryOptions', { + route: 'search', + authLevel: 'function', + handler: async (request) => ({ body: await request.text() }), + }); + app.http('queryMethods', { + methods: ['GET', 'QUERY'], + handler: () => ({ body: 'results' }), + }); + trigger.http({ methods: ['QUERY'] }); +} +void registerHttpQuery; + function registerServiceBusTopic(): void { app.serviceBusTopic('serviceBusTopic', serviceBusTopicFunctionOptions); } diff --git a/types/app.d.ts b/types/app.d.ts index 0dfcdb2..a9fcb7c 100644 --- a/types/app.d.ts +++ b/types/app.d.ts @@ -75,6 +75,20 @@ export function post(name: string, handler: HttpHandler): void; */ export function post(name: string, options: HttpMethodFunctionOptions): void; +/** + * Registers an http function in your app that will be triggered by making a 'QUERY' request to the function url + * @param name The name of the function. This will be the route unless a route is explicitly configured in the `HttpTriggerOptions` + * @param handler The handler for this function + */ +export function query(name: string, handler: HttpHandler): void; + +/** + * Registers an http function in your app that will be triggered by making a 'QUERY' request to the function url + * @param name The name of the function. This will be the route unless a route is explicitly configured in the `HttpTriggerOptions` + * @param options Configuration options describing the inputs, outputs, and handler for this function + */ +export function query(name: string, options: HttpMethodFunctionOptions): void; + /** * Registers an http function in your app that will be triggered by making a 'PATCH' request to the function url * @param name The name of the function. This will be the route unless a route is explicitly configured in the `HttpTriggerOptions` diff --git a/types/http.d.ts b/types/http.d.ts index 8e8ed73..7641526 100644 --- a/types/http.d.ts +++ b/types/http.d.ts @@ -219,7 +219,17 @@ export interface HttpRequestUser { /** * Possible values for an HTTP request method. */ -export type HttpMethod = 'GET' | 'POST' | 'DELETE' | 'HEAD' | 'PATCH' | 'PUT' | 'OPTIONS' | 'TRACE' | 'CONNECT'; +export type HttpMethod = + | 'GET' + | 'POST' + | 'DELETE' + | 'HEAD' + | 'PATCH' + | 'PUT' + | 'OPTIONS' + | 'TRACE' + | 'CONNECT' + | 'QUERY'; /** * Possible values for an HTTP Request user type