lambder 2.0.12 → 2.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,7 +7,7 @@ interface DieResolverMethods<TOutput> {
7
7
  json: MethodType<LambderResponseBuilder, 'json'>;
8
8
  xml: MethodType<LambderResponseBuilder, 'xml'>;
9
9
  html: MethodType<LambderResponseBuilder, 'html'>;
10
- status301: MethodType<LambderResponseBuilder, 'status301'>;
10
+ redirect: MethodType<LambderResponseBuilder, 'redirect'>;
11
11
  status404: MethodType<LambderResponseBuilder, 'status404'>;
12
12
  cors: MethodType<LambderResponseBuilder, 'cors'>;
13
13
  fileBase64: MethodType<LambderResponseBuilder, 'fileBase64'>;
@@ -12,7 +12,7 @@ export default class LambderResolver extends LambderResponseBuilder {
12
12
  json: this.autoResolve(this.json),
13
13
  xml: this.autoResolve(this.xml),
14
14
  html: this.autoResolve(this.html),
15
- status301: this.autoResolve(this.status301),
15
+ redirect: this.autoResolve(this.redirect),
16
16
  status404: this.autoResolve(this.status404),
17
17
  cors: this.autoResolve(this.cors),
18
18
  fileBase64: this.autoResolve(this.fileBase64),
@@ -1,7 +1,8 @@
1
1
  import LambderUtils from "./LambderUtils.js";
2
2
  import { LambderRenderContext } from "./LambderContext.js";
3
+ export type HttpStatusCode = 200 | 201 | 202 | 204 | 300 | 301 | 302 | 303 | 304 | 307 | 308 | 400 | 401 | 403 | 404 | 405 | 409 | 410 | 413 | 415 | 422 | 429 | 500 | 501 | 502 | 503 | 504;
3
4
  export type LambderResolverResponse = {
4
- statusCode: number;
5
+ statusCode: HttpStatusCode;
5
6
  multiValueHeaders?: Record<string, string[]>;
6
7
  body: string | null;
7
8
  isBase64Encoded?: boolean;
@@ -39,7 +40,7 @@ export default class LambderResponseBuilder<TResponse = any> {
39
40
  json(data: Record<string, any>, headers?: Record<string, string | string[]>): LambderResolverResponse;
40
41
  xml(data: string): LambderResolverResponse;
41
42
  html(data: string, headers?: Record<string, string | string[]>): LambderResolverResponse;
42
- status301(url: string, headers?: Record<string, string | string[]>): LambderResolverResponse;
43
+ redirect(url: string, statusCode?: HttpStatusCode, headers?: Record<string, string | string[]>): LambderResolverResponse;
43
44
  status404(data: string, headers?: Record<string, string | string[]>): LambderResolverResponse;
44
45
  versionExpired(headers?: Record<string, string | string[]>): LambderResolverResponse;
45
46
  cors(): LambderResolverResponse;
@@ -116,9 +116,9 @@ export default class LambderResponseBuilder {
116
116
  });
117
117
  }
118
118
  ;
119
- status301(url, headers) {
119
+ redirect(url, statusCode = 302, headers) {
120
120
  return this.raw({
121
- statusCode: 301,
121
+ statusCode: statusCode,
122
122
  multiValueHeaders: { "Location": [url], ...convertToMultiHeader(headers) },
123
123
  body: null
124
124
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lambder",
3
- "version": "2.0.12",
3
+ "version": "2.0.13",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -9,7 +9,7 @@ interface DieResolverMethods<TOutput> {
9
9
  json: MethodType<LambderResponseBuilder, 'json'>;
10
10
  xml: MethodType<LambderResponseBuilder, 'xml'>;
11
11
  html: MethodType<LambderResponseBuilder, 'html'>;
12
- status301: MethodType<LambderResponseBuilder, 'status301'>;
12
+ redirect: MethodType<LambderResponseBuilder, 'redirect'>;
13
13
  status404: MethodType<LambderResponseBuilder, 'status404'>;
14
14
  cors: MethodType<LambderResponseBuilder, 'cors'>;
15
15
  fileBase64: MethodType<LambderResponseBuilder, 'fileBase64'>;
@@ -49,7 +49,7 @@ export default class LambderResolver<TOutput = any> extends LambderResponseBuild
49
49
  json: this.autoResolve(this.json),
50
50
  xml: this.autoResolve(this.xml),
51
51
  html: this.autoResolve(this.html),
52
- status301: this.autoResolve(this.status301),
52
+ redirect: this.autoResolve(this.redirect),
53
53
  status404: this.autoResolve(this.status404),
54
54
  cors: this.autoResolve(this.cors),
55
55
  fileBase64: this.autoResolve(this.fileBase64),
@@ -15,8 +15,14 @@ const CORS_HEADERS = convertToMultiHeader({
15
15
  "Access-Control-Allow-Methods": "OPTIONS,POST",
16
16
  });
17
17
 
18
+ export type HttpStatusCode =
19
+ | 200 | 201 | 202 | 204
20
+ | 300 | 301 | 302 | 303 | 304 | 307 | 308
21
+ | 400 | 401 | 403 | 404 | 405 | 409 | 410 | 413 | 415 | 422 | 429
22
+ | 500 | 501 | 502 | 503 | 504;
23
+
18
24
  export type LambderResolverResponse = {
19
- statusCode: number,
25
+ statusCode: HttpStatusCode,
20
26
  multiValueHeaders?: Record<string, string[]>,
21
27
  body: string | null,
22
28
  isBase64Encoded?: boolean,
@@ -157,12 +163,13 @@ export default class LambderResponseBuilder<TResponse = any> {
157
163
  });
158
164
  };
159
165
 
160
- status301(
166
+ redirect(
161
167
  url: string,
168
+ statusCode: HttpStatusCode = 302,
162
169
  headers?: Record<string, string|string[]>,
163
170
  ):LambderResolverResponse{
164
171
  return this.raw({
165
- statusCode: 301,
172
+ statusCode: statusCode,
166
173
  multiValueHeaders: { "Location" : [url], ...convertToMultiHeader(headers) },
167
174
  body:null
168
175
  });
@@ -98,7 +98,7 @@ Based on existing test files:
98
98
  - [ ] `res.fileBase64()`
99
99
  - [ ] `res.ejsFile()`
100
100
  - [ ] `res.ejsTemplate()`
101
- - [ ] `res.status301()` (redirects)
101
+ - [ ] `res.redirect()` (redirects)
102
102
  - [ ] `res.status404()`
103
103
  - [ ] `res.cors()`
104
104
  - [ ] `res.die.*` methods (skip afterRender hooks)
@@ -0,0 +1,88 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import Lambder from '../src/Lambder.js';
3
+ import type { APIGatewayProxyEvent, Context } from 'aws-lambda';
4
+
5
+ const createMockEvent = (path: string, method: string = 'GET'): APIGatewayProxyEvent => ({
6
+ body: null,
7
+ headers: {
8
+ Host: 'localhost',
9
+ },
10
+ multiValueHeaders: {},
11
+ httpMethod: method,
12
+ isBase64Encoded: false,
13
+ path,
14
+ pathParameters: null,
15
+ queryStringParameters: null,
16
+ multiValueQueryStringParameters: null,
17
+ stageVariables: null,
18
+ requestContext: {} as any,
19
+ resource: '',
20
+ });
21
+
22
+ const createMockContext = (): Context => ({
23
+ callbackWaitsForEmptyEventLoop: false,
24
+ functionName: 'test',
25
+ functionVersion: '1',
26
+ invokedFunctionArn: 'arn',
27
+ memoryLimitInMB: '128',
28
+ awsRequestId: '123',
29
+ logGroupName: 'group',
30
+ logStreamName: 'stream',
31
+ getRemainingTimeInMillis: () => 1000,
32
+ done: () => {},
33
+ fail: () => {},
34
+ succeed: () => {},
35
+ });
36
+
37
+ describe('Redirect Response', () => {
38
+ it('should redirect with default status code 302', async () => {
39
+ const lambder = new Lambder({
40
+ publicPath: './public',
41
+ apiPath: '/api'
42
+ })
43
+ .addRoute('/old-path', (ctx, res) => {
44
+ return res.redirect('/new-path');
45
+ });
46
+
47
+ const handler = lambder.getHandler();
48
+ const event = createMockEvent('/old-path');
49
+ const result = await handler(event, createMockContext());
50
+
51
+ expect(result.statusCode).toBe(302);
52
+ expect(result.multiValueHeaders?.Location).toEqual(['/new-path']);
53
+ });
54
+
55
+ it('should redirect with custom status code', async () => {
56
+ const lambder = new Lambder({
57
+ publicPath: './public',
58
+ apiPath: '/api'
59
+ })
60
+ .addRoute('/moved-permanently', (ctx, res) => {
61
+ return res.redirect('/new-location', 301);
62
+ });
63
+
64
+ const handler = lambder.getHandler();
65
+ const event = createMockEvent('/moved-permanently');
66
+ const result = await handler(event, createMockContext());
67
+
68
+ expect(result.statusCode).toBe(301);
69
+ expect(result.multiValueHeaders?.Location).toEqual(['/new-location']);
70
+ });
71
+
72
+ it('should redirect using die.redirect', async () => {
73
+ const lambder = new Lambder({
74
+ publicPath: './public',
75
+ apiPath: '/api'
76
+ })
77
+ .addRoute('/die-redirect', (ctx, res) => {
78
+ return res.die.redirect('/somewhere-else');
79
+ });
80
+
81
+ const handler = lambder.getHandler();
82
+ const event = createMockEvent('/die-redirect');
83
+ const result = await handler(event, createMockContext());
84
+
85
+ expect(result.statusCode).toBe(302);
86
+ expect(result.multiValueHeaders?.Location).toEqual(['/somewhere-else']);
87
+ });
88
+ });