Skip to content

Commit a2706a5

Browse files
committed
chore: Merge branch 'master' of github.com:muratcorlu/lambda-expressless
2 parents 02c56c9 + 98addfe commit a2706a5

File tree

5 files changed

+242
-0
lines changed

5 files changed

+242
-0
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"semantic-release": "^15.13.21"
2626
},
2727
"dependencies": {
28+
"@types/aws-lambda": "^8.10.33",
2829
"accepts": "^1.3.7",
2930
"type-is": "^1.6.18"
3031
}

src/lambda-wrapper.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Request } from './request';
2+
import { Response } from './response';
3+
import { APIGatewayProxyHandler } from 'aws-lambda';
4+
5+
export interface Middleware {
6+
(request: Request, response: Response, next: Function): void
7+
}
8+
9+
export function use(...handlers: Middleware[]): ApiGatewayProxyHandler;
10+
11+
export { Request } from './request';
12+
export { Response } from './response';

src/request.d.ts

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
2+
import { APIGatewayProxyEvent } from 'aws-lambda';
3+
import { Accepts } from 'accepts';
4+
import { Readable } from 'stream';
5+
6+
export class Request extends Readable {
7+
constructor(event: APIGatewayProxyEvent);
8+
headers: { [name: string]: string };
9+
hostname: string | null;
10+
method: string;
11+
query: { [name: string]: string } | null;
12+
path: string;
13+
params: { [name: string]: string } | null;
14+
protocol: 'http' | 'https';
15+
secure: boolean;
16+
ips: string[];
17+
ip: string;
18+
host: string | null;
19+
xhr: boolean;
20+
event: APIGatewayProxyEvent;
21+
accept: Accepts;
22+
/**
23+
* Check if the given `type(s)` is acceptable, returning
24+
* the best match when true, otherwise `undefined`, in which
25+
* case you should respond with 406 "Not Acceptable".
26+
*
27+
* The `type` value may be a single MIME type string
28+
* such as "application/json", an extension name
29+
* such as "json", a comma-delimited list such as "json, html, text/plain",
30+
* an argument list such as `"json", "html", "text/plain"`,
31+
* or an array `["json", "html", "text/plain"]`. When a list
32+
* or array is given, the _best_ match, if any is returned.
33+
*
34+
* Examples:
35+
*
36+
* // Accept: text/html
37+
* req.accepts('html');
38+
* // => "html"
39+
*
40+
* // Accept: text/*, application/json
41+
* req.accepts('html');
42+
* // => "html"
43+
* req.accepts('text/html');
44+
* // => "text/html"
45+
* req.accepts('json, text');
46+
* // => "json"
47+
* req.accepts('application/json');
48+
* // => "application/json"
49+
*
50+
* // Accept: text/*, application/json
51+
* req.accepts('image/png');
52+
* req.accepts('png');
53+
* // => undefined
54+
*
55+
* // Accept: text/*;q=.5, application/json
56+
* req.accepts(['html', 'json']);
57+
* req.accepts('html', 'json');
58+
* req.accepts('html, json');
59+
* // => "json"
60+
*
61+
* @param {String|Array} type(s)
62+
* @return {String|Array|Boolean}
63+
*/
64+
accepts(args: string | string[]): string | boolean | string[];
65+
/**
66+
* Check if the given `encoding`s are accepted.
67+
* @param {String|Array} ...encoding
68+
* @return {String|Array}
69+
*/
70+
acceptsLanguages(args: string | string[]): string | string[];
71+
/**
72+
* Return request header.
73+
*
74+
* The `Referrer` header field is special-cased,
75+
* both `Referrer` and `Referer` are interchangeable.
76+
*
77+
* Examples:
78+
*
79+
* req.get('Content-Type');
80+
* // => "text/plain"
81+
*
82+
* req.get('content-type');
83+
* // => "text/plain"
84+
*
85+
* req.get('Something');
86+
* // => undefined
87+
*
88+
* Aliased as `req.header()`.
89+
*
90+
* @param {String} key
91+
* @return {String}
92+
*/
93+
get(key: string): string | undefined;
94+
95+
header(name: string): string | undefined;
96+
97+
/**
98+
* Check if the incoming request contains the "Content-Type"
99+
* header field, and it contains the give mime `type`.
100+
*
101+
* Examples:
102+
*
103+
* // With Content-Type: text/html; charset=utf-8
104+
* req.is('html');
105+
* req.is('text/html');
106+
* req.is('text/*');
107+
* // => true
108+
*
109+
* // When Content-Type is application/json
110+
* req.is('json');
111+
* req.is('application/json');
112+
* req.is('application/*');
113+
* // => true
114+
*
115+
* req.is('html');
116+
* // => false
117+
*
118+
* @param {String|Array} types...
119+
* @return {String|false|null}
120+
*/
121+
is(types: string | string[]): string | boolean | null;
122+
}

src/response.d.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { Request } from "./request";
2+
import { APIGatewayProxyCallback, APIGatewayProxyResult } from 'aws-lambda';
3+
4+
export class Response {
5+
/**
6+
* Response object constructor
7+
*
8+
* @param {Request} req Request object for this Response
9+
* @param {APIGatewayProxyCallback} callback AWS Lambda callback function
10+
*/
11+
constructor(req: Request, callback: APIGatewayProxyCallback);
12+
req: Request;
13+
callback: APIGatewayProxyCallback;
14+
responseObj: APIGatewayProxyResult;
15+
/**
16+
* Ends the response process.
17+
*/
18+
end: () => void;
19+
/**
20+
* Get response header value for given key
21+
* @param {string} key Header key to get
22+
* @return {string}
23+
*/
24+
get: (key: string) => string;
25+
/**
26+
* Performs content-negotiation on the Accept HTTP header
27+
* on the request object, when present. It uses `req.accepts()`
28+
* to select a handler for the request, based on the acceptable
29+
* types ordered by their quality values. If the header is not
30+
* specified, the first callback is invoked. When no match is
31+
* found, the server responds with 406 “Not Acceptable”, or invokes
32+
* the `default` callback.
33+
*
34+
* The `Content-Type` response header is set when a callback is
35+
* selected. However, you may alter this within the callback using
36+
* methods such as `res.set()` or `res.type()`.
37+
*
38+
* The following example would respond with `{ "message": "hey" }`
39+
* when the `Accept` header field is set to “application/json”
40+
* or “*\/json” (however if it is “*\/*”, then the response will
41+
* be “hey”).
42+
*
43+
* res.format({
44+
* 'text/plain': function(){
45+
* res.send('hey');
46+
* },
47+
*
48+
* 'text/html': function(){
49+
* res.send('<p>hey</p>');
50+
* },
51+
*
52+
* 'appliation/json': function(){
53+
* res.send({ message: 'hey' });
54+
* }
55+
* });
56+
*
57+
* By default it passes an `Error`
58+
* with a `.status` of 406 to `next(err)`
59+
* if a match is not made. If you provide
60+
* a `.default` callback it will be invoked
61+
* instead.
62+
*
63+
* @param {Object} obj
64+
* @return {Response} for chaining
65+
*/
66+
format: (obj: Object) => Response;
67+
/**
68+
* Sends a JSON response. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string using JSON.stringify().
69+
*
70+
* The parameter can be any JSON type, including object, array, string, Boolean, number, or null, and you can also use it to convert other values to JSON.
71+
*
72+
* ```js
73+
* res.json(null)
74+
* res.json({ user: 'tobi' })
75+
* res.status(500).json({ error: 'message' })
76+
* ```
77+
* @param {Object} body Any type of oject
78+
*/
79+
json: (body: Object) => void;
80+
/**
81+
* Sends the HTTP response.
82+
* @param {Object} body
83+
*/
84+
send: (body: Object) => void;
85+
/**
86+
* Set response header
87+
* @param {string} key Header key
88+
* @param {string} value Header value
89+
*/
90+
set: (key: string, value: string) => Response;
91+
/**
92+
* Set status code for response
93+
* @param {number} status Status code. Ex: 200, 201, 400, 404, 500 etc.
94+
*/
95+
status: (status: number) => Response;
96+
/**
97+
* Sets the Content-Type HTTP header
98+
*
99+
* @param {string} type Mime type will be set as Content-Type response header
100+
*/
101+
type: (type: string) => Response;
102+
}

0 commit comments

Comments
 (0)