Skip to content

Commit

Permalink
chore(fastify): clean code
Browse files Browse the repository at this point in the history
Closes: #2445
  • Loading branch information
Romakita committed Jan 4, 2024
1 parent a0b7a4b commit 263b2b8
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("PlatformFastify", () => {
const stub = sandbox.stub().returns("body");

const platform = PlatformFastify.create(Server, {
koa: {
fastify: {
bodyParser: stub
}
});
Expand All @@ -54,7 +54,7 @@ describe("PlatformFastify", () => {
const stub = sandbox.stub().returns("body");

const platform = PlatformFastify.create(Server, {
koa: {
fastify: {
bodyParser: stub
}
});
Expand All @@ -68,7 +68,7 @@ describe("PlatformFastify", () => {
const stub = sandbox.stub().returns("body");

const platform = PlatformFastify.create(Server, {
koa: {
fastify: {
bodyParser: stub
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {Type} from "@tsed/core";
import {PlatformHandlerMetadata, PlatformLayer} from "@tsed/platform-router";
import next from "ajv/dist/vocabularies/next";
import Fastify, {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
import Koa from "koa";
import {PlatformFastifyApplication} from "../services/PlatformFastifyApplication";
import {PlatformFastifyHandler} from "../services/PlatformFastifyHandler";
import {PlatformFastifyRequest} from "../services/PlatformFastifyRequest";
Expand All @@ -32,7 +31,7 @@ declare global {

/**
* @platform
* @koa
* @fastify
*/
export class PlatformFastify implements PlatformAdapter<FastifyInstance> {
readonly providers = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Koa from "koa";
import {PlatformKoaSettings} from "./PlatformFastifySettings";
import {PlatformFastifySettings} from "./PlatformFastifySettings";

export * from "./PlatformFastifySettings";

Expand All @@ -9,9 +8,9 @@ declare global {
/**
* Configuration related to Koa platform application.
*/
koa: PlatformKoaSettings;
fastify: PlatformFastifySettings;
}

export interface NextFunction extends Koa.Next {}
// export interface NextFunction extends Fastify.Next {}
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import {PlatformTest} from "@tsed/common";
import {PlatformKoaRequest} from "@tsed/platform-koa";
import {PlatformFastifyRequest} from "../..";
import {expect} from "chai";

function createRequest() {
const request = PlatformTest.createRequest();
const koaCtx = {
request: {
protocol: "http",
req: request
},
cookie: {
test: "test"
},
cookies: {
test: "test"
},
session: {
test: "test"
}
};
request.ctx = koaCtx;
// const fastifyCtx = {
// request: {
// protocol: "http",
// req: request
// },
// cookie: {
// test: "test"
// },
// cookies: {
// test: "test"
// },
// session: {
// test: "test"
// }
// };
// request.ctx = koaCtx;
const ctx = PlatformTest.createRequestContext({
event: {
request,
ctx: koaCtx
request
// ctx: koaCtx
},
RequestKlass: PlatformKoaRequest
RequestKlass: PlatformFastifyRequest
});

return {req: request, request: ctx.request};
}

describe("PlatformKoaRequest", () => {
describe("PlatformFastifyRequest", () => {
beforeEach(() => PlatformTest.create());
afterEach(() => PlatformTest.reset());
it("should create a PlatformRequest instance", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {IncomingEvent, PlatformContext, PlatformRequest} from "@tsed/common";
import Koa, {FastifyRequest} from "fastify";
import {PlatformContext, PlatformRequest} from "@tsed/common";
import {FastifyRequest} from "fastify";
import {IncomingMessage} from "http";

declare module "fastify" {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
import {PlatformTest} from "@tsed/common";
import {PlatformKoaResponse} from "@tsed/platform-koa";
import {expect} from "chai";
import Sinon from "sinon";
import {PlatformFastifyResponse} from "./PlatformFastifyResponse";

const sandbox = Sinon.createSandbox();

function createResponse() {
const response = PlatformTest.createResponse();
const koaCtx = {
response: {
res: response
}
};

response.ctx = koaCtx;

const ctx = PlatformTest.createRequestContext({
event: {
response,
ctx: koaCtx
response
},
ResponseKlass: PlatformKoaResponse
ResponseKlass: PlatformFastifyResponse
});

return {res: response, response: ctx.response};
}

describe("PlatformKoaResponse", () => {
describe("PlatformFastifyResponse", () => {
beforeEach(() => PlatformTest.create());
afterEach(() => PlatformTest.reset());
it("should create a PlatformResponse instance", () => {
Expand All @@ -46,7 +38,7 @@ describe("PlatformKoaResponse", () => {
});
});
describe("statusCode", () => {
it("return statusCode", async () => {
it("return statusCode", () => {
const {response} = createResponse();

response.status(302);
Expand All @@ -55,7 +47,7 @@ describe("PlatformKoaResponse", () => {
});
});
describe("hasStatus", () => {
it("return hasStatus", async () => {
it("return hasStatus", () => {
const {response} = createResponse();

response.status(404);
Expand All @@ -67,7 +59,7 @@ describe("PlatformKoaResponse", () => {
});
});
describe("contentType()", () => {
it("should set contentType", async () => {
it("should set contentType", () => {
const {res, response} = createResponse();

response.contentType("text/html");
Expand All @@ -76,7 +68,7 @@ describe("PlatformKoaResponse", () => {
});
});
describe("body()", () => {
it("should set body", async () => {
it("should set body", () => {
const {res, response} = createResponse();

response.body("body");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ declare global {

/**
* @platform
* @koa
* @fastify
*/
export class PlatformFastifyResponse extends PlatformResponse<FastifyReply> {
get statusCode() {
Expand All @@ -30,10 +30,9 @@ export class PlatformFastifyResponse extends PlatformResponse<FastifyReply> {
return this.raw.raw;
}

hasStatus() {
// KOA set 404 by default
return this.statusCode !== 404;
}
// hasStatus() {
// return this.statusCode !== 404;
// }

/**
* Sets the HTTP status for the response.
Expand Down
39 changes: 0 additions & 39 deletions packages/platform/platform-fastify/test/app/Server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import "@tsed/ajv";
import {PlatformApplication} from "@tsed/common";
import {Configuration, Inject} from "@tsed/di";
import Application from "koa";
import bodyParser from "koa-bodyparser";
import compress from "koa-compress";
import session from "koa-session";
// @ts-ignore
import methodOverride from "koa-override";

export const rootDir = __dirname;

Expand All @@ -25,37 +19,4 @@ export const rootDir = __dirname;
export class Server {
@Inject()
app: PlatformApplication<Application>;

$beforeRoutesInit() {
this.app.getApp().keys = ["some secret hurr"];
this.app
.use(compress())
.use(methodOverride())
.use(
session(
{
key: "connect.sid" /** (string) cookie key (default is koa.sess) */,
/** (number || 'session') maxAge in ms (default is 1 days) */
/** 'session' will result in a cookie that expires when session/browser is closed */
/** Warning: If a session cookie is stolen, this cookie will never expire */
maxAge: 86400000,
/** (boolean) automatically commit headers (default true) */
overwrite: true,
/** (boolean) can overwrite or not (default true) */
httpOnly: false,
/** (boolean) httpOnly or not (default true) */
signed: false,
/** (boolean) signed or not (default true) */
rolling: false,
/** (boolean) Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge, resetting the expiration countdown. (default is false) */
renew: false,
/** (boolean) renew session when session is nearly expired, so we can always keep user logged in. (default is false)*/
secure: false,
/** (boolean) secure cookie*/
sameSite: undefined /** (string) session cookie sameSite options (default null, don't set it) */
},
this.app.rawApp as any
)
);
}
}

0 comments on commit 263b2b8

Please sign in to comment.