Skip to content

Commit 942b107

Browse files
authored
Merge pull request #126 from tjholm/options-requests
Add support for OPTIONS requests.
2 parents 2d32d22 + aaa7768 commit 942b107

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/resources/api.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('Api', () => {
4141
});
4242

4343
it("should provide Faas with ApiWorkerOptions", () => {
44-
const expectedOpts = new ApiWorkerOptions("main", "/newroute/", ['GET', 'POST', 'PATCH', 'PUT', 'DELETE']);
44+
const expectedOpts = new ApiWorkerOptions("main", "/newroute/", ['GET', 'POST', 'PATCH', 'PUT', 'DELETE', 'OPTIONS']);
4545
expect(faas.Faas).toBeCalledWith(expectedOpts)
4646
});
4747

@@ -52,7 +52,7 @@ describe('Api', () => {
5252
});
5353

5454
// individual method handlers
55-
["get", "post", "delete", "patch", "put"].forEach(method => {
55+
["get", "post", "delete", "patch", "put", "options"].forEach(method => {
5656
describe(`when creating a new ${method} handler`, () => {
5757
beforeAll(async () => {
5858
await api("main")[method]("/test/", mockFn)

src/resources/api.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,21 @@ class Route {
115115
return this.method(['DELETE'], ...middleware);
116116
}
117117

118+
/**
119+
* Register a handler function for OPTIONS requests to this route
120+
*/
121+
async options(...middleware: HttpMiddleware[]): Promise<void> {
122+
return this.method(['OPTIONS'], ...middleware);
123+
}
124+
118125
/**
119126
* Register a handler function for GET, POST, PATCH, PUT and DELETE requests to this route.
120127
*
121128
* Most useful when routing isn't important or you're doing you own internal routing.
122129
*/
123130
async all(...middleware: HttpMiddleware[]): Promise<void> {
124131
return this.method(
125-
['GET', 'POST', 'PATCH', 'PUT', 'DELETE'],
132+
['GET', 'POST', 'PATCH', 'PUT', 'DELETE', 'OPTIONS'],
126133
...middleware
127134
);
128135
}
@@ -246,6 +253,17 @@ class Api {
246253
const r = this.route(match);
247254
return r.delete(...middleware);
248255
}
256+
257+
/**
258+
* Registers a new route with a OPTIONS handler in a single method.
259+
* @param match the route path matcher e.g. '/home'. Supports path params via colon prefix e.g. '/customers/:customerId'
260+
* @param middleware the middleware/handler to register for calls to DELETE
261+
* @returns {Promise} that resolves when the handler terminates.
262+
*/
263+
async options(match: string, ...middleware: HttpMiddleware[]): Promise<void> {
264+
const r = this.route(match);
265+
return r.options(...middleware);
266+
}
249267
}
250268

251269
/**

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,4 @@ export type WhereQueryOperator =
7878

7979
export type WhereValueExpression = string | number | boolean;
8080

81-
export type HttpMethod = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
81+
export type HttpMethod = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE' | 'OPTIONS';

0 commit comments

Comments
 (0)