Skip to content

Commit

Permalink
fix(platform-serverless): fix mime import to resolve mime.getType() u…
Browse files Browse the repository at this point in the history
…ndefined function error
  • Loading branch information
Romakita committed Jun 22, 2024
1 parent a1f7b11 commit 39ec58a
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 21 deletions.
5 changes: 3 additions & 2 deletions packages/platform/platform-serverless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@
"@types/encodeurl": "^1.0.2",
"@types/mime": "^3.0.4",
"eslint": "^8.57.0",
"find-my-way": "^7.7.0",
"find-my-way": "^8.2.0",
"jest": "^29.7.0"
},
"peerDependencies": {
"@tsed/di": "7.73.2",
"@tsed/logger": ">=6.7.5"
"@tsed/logger": ">=6.7.5",
"find-my-way": ">=7.0.0"
},
"peerDependenciesMeta": {
"@tsed/di": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Controller, Inject, Injectable} from "@tsed/di";
import {BodyParams, PathParams, QueryParams} from "@tsed/platform-params";
import {PlatformServerlessTest} from "@tsed/platform-serverless-testing";
import {PlatformServerlessTest, createFakeEvent, createFakeContext} from "@tsed/platform-serverless-testing";
import {Delete, Get, Patch, Post, Put} from "@tsed/schema";
import {PlatformServerless} from "./PlatformServerless.js";

Expand Down Expand Up @@ -194,6 +194,17 @@ describe("PlatformServerless", () => {
label: "label"
});
});
it("should call lambda from handler() but returns a 404 if controller doesn't match and endpoint", async () => {
const event = createFakeEvent();
event.httpMethod = "GET";
event.path = "/test/other";
const context = createFakeContext();
const response = await PlatformServerlessTest.instance.handler()(event, context);

expect(response.statusCode).toEqual(404);
expect(response.headers).toEqual({"x-request-id": "requestId"});
expect(response.body).toEqual("Not found");
});
});
describe("callback()", () => {
it("should create handler from given parameters", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ export class PlatformServerless {
if (!this._router) {
const {default: FindMyMay} = await import("find-my-way");

this._router = FindMyMay({caseSensitive: false, ignoreTrailingSlash: true});
this._router = FindMyMay({
caseSensitive: false,
ignoreTrailingSlash: true
});

this.callbacks();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ describe("ServerlessRequest", () => {

expect(context.request.params).toEqual({});
});

it("should return expected value (undefined params)", () => {
const context = createServerlessContext({
endpoint: {} as any
});

delete context.event.pathParameters;

expect(context.request.params).toEqual({});
});
});

describe("query()", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,21 @@ describe("ServerlessResponse", () => {
});
});
describe("contentLength()", () => {
it("should return the locals", () => {
it("should return the content length", () => {
const context = createServerlessContext({
endpoint: {} as any
});
context.response.contentLength(300);
expect(context.response.getContentLength()).toEqual(300);
});

it("should return the default content length", () => {
const context = createServerlessContext({
endpoint: {} as any
});
context.response.contentLength(" " as any);
expect(context.response.getContentLength()).toEqual(0);
});
});
describe("location()", () => {
it("should set location", () => {
Expand Down Expand Up @@ -103,4 +111,28 @@ describe("ServerlessResponse", () => {
expect(context.response.getBody()).toEqual("Moved Permanently. Redirecting to https://referrer.com");
});
});
describe("setHeader()", () => {
it("should set location", () => {
const context = createServerlessContext({
endpoint: {} as any
});

context.event.headers["Referrer"] = "https://referrer.com";
context.response.set("location", "back");
expect(context.response.getHeaders()).toEqual({
location: "https://referrer.com"
});
});
it("should set location back with default value", () => {
const context = createServerlessContext({
endpoint: {} as any
});

context.event.headers["Referrer"] = "";
context.response.set("location", "back");
expect(context.response.getHeaders()).toEqual({
location: "/"
});
});
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {getValue} from "@tsed/core";
import {getStatusMessage} from "@tsed/schema";
import encodeUrl from "encodeurl";
import * as mime from "mime";
import mime from "mime";
import {ServerlessContext} from "./ServerlessContext.js";

export type HeaderValue = boolean | number | string;
Expand Down
31 changes: 16 additions & 15 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7654,14 +7654,15 @@ __metadata:
"@types/mime": "npm:^3.0.4"
encodeurl: "npm:^2.0.0"
eslint: "npm:^8.57.0"
find-my-way: "npm:^7.7.0"
find-my-way: "npm:^8.2.0"
jest: "npm:^29.7.0"
mime: "npm:^3.0.0"
tslib: "npm:2.6.1"
uuid: "npm:^9.0.1"
peerDependencies:
"@tsed/di": 7.73.2
"@tsed/logger": ">=6.7.5"
find-my-way: ">=7.0.0"
peerDependenciesMeta:
"@tsed/di":
optional: false
Expand Down Expand Up @@ -15695,14 +15696,14 @@ __metadata:
languageName: node
linkType: hard

"find-my-way@npm:^7.7.0":
version: 7.7.0
resolution: "find-my-way@npm:7.7.0"
"find-my-way@npm:^8.2.0":
version: 8.2.0
resolution: "find-my-way@npm:8.2.0"
dependencies:
fast-deep-equal: "npm:^3.1.3"
fast-querystring: "npm:^1.0.0"
safe-regex2: "npm:^2.0.0"
checksum: 10/0b634bce3374de9f3ae660231a963f858cc271bd95c914b941ba1a5fb53d3f978cf71708de6d454ead72c99745a147dd621616645b3d4bf39e4395efe6b42d91
safe-regex2: "npm:^3.1.0"
checksum: 10/07e46c94c594f7f3f3bf3acb5839753c4d02dca83ecd0d3e966b11effc741d212d15050c365ab104e5ea3312f44d08a68fa7939bb759b851d6c874e0cf106523
languageName: node
linkType: hard

Expand Down Expand Up @@ -25802,10 +25803,10 @@ __metadata:
languageName: node
linkType: hard

"ret@npm:~0.2.0":
version: 0.2.2
resolution: "ret@npm:0.2.2"
checksum: 10/9f16517f77a3b508c529bc22187c132cd7907cd9270601d6794e1c8a58f6990872b4697b4edfdebb4f87017f9f0a285007b740a9ffb8236805b923fd1bc84eb1
"ret@npm:~0.4.0":
version: 0.4.3
resolution: "ret@npm:0.4.3"
checksum: 10/d6a00f0920400b78b6aa96ce1c953d2f783f4fd5d56b5e842a744c40e33545e7955fb132386ada406361881353292fe7282f4e6e82b2c1e61f6c96a6ea4bb2d7
languageName: node
linkType: hard

Expand Down Expand Up @@ -26018,12 +26019,12 @@ __metadata:
languageName: node
linkType: hard

"safe-regex2@npm:^2.0.0":
version: 2.0.0
resolution: "safe-regex2@npm:2.0.0"
"safe-regex2@npm:^3.1.0":
version: 3.1.0
resolution: "safe-regex2@npm:3.1.0"
dependencies:
ret: "npm:~0.2.0"
checksum: 10/af1f0b367d0c769eccca7a5aa93d222e542fb494940849c7bbbbe8942c0026cf207f15ba3aacdd4f3e4f6b5a31fa7a775f7cdd8e6670b893fd16e96247fdbd02
ret: "npm:~0.4.0"
checksum: 10/4f9f7172662763619052a45599e515efc5dd10a932690f610c8ab808a4baa41be3feafefa444f7532651d721d12871a1c9a85330626cdd013b804e8f4240dff1
languageName: node
linkType: hard

Expand Down

0 comments on commit 39ec58a

Please sign in to comment.