Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove fastify with type implementation #932

Merged
merged 13 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added a way to run CI on unmerged PRs
- Added support for FDIs: 3.1 and 4.0. Required by: auth-react >=0.49.0 and web-js>=0.15.0
- The `networkInterceptor` now also gets a new `params` prop in the request config.
- Replicates `fastify` types based on requirement for the SDK instead of using the original module.

### Breaking change

Expand Down
2 changes: 1 addition & 1 deletion examples/fastify/with-emailpassword/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fastify.route({
},
// @ts-ignore
preHandler: verifySession(),
handler: async (req: SessionRequest, _) => {
handler: async (req: SessionRequest, _: any) => {
let session = req.session;
return {
sessionHandle: session!.getHandle(),
Expand Down
652 changes: 6 additions & 646 deletions examples/fastify/with-emailpassword/frontend/package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion examples/fastify/with-emailpassword/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "supertokens-react",
"version": "0.1.0",
"private": true,
"dependencies": {},
"dependencies": {
"react-scripts": "^5.0.1"
},
porcellus marked this conversation as resolved.
Show resolved Hide resolved
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
Expand Down
39,993 changes: 6,816 additions & 33,177 deletions examples/fastify/with-emailpassword/package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions examples/fastify/with-emailpassword/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@
"start:backend": "cd backend && npm run start",
"start:backend-live-demo-app": "cd backend && ./startLiveDemoApp.sh",
"start": "npm-run-all --parallel start:frontend start:backend",
"start-live-demo-app": "npx npm-run-all --parallel start:frontend-live-demo-app start:backend-live-demo-app"
"start-live-demo-app": "npx npm-run-all --parallel start:frontend-live-demo-app start:backend-live-demo-app",
"preinstall": "cd frontend && npm i"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@fastify/cors": "^7.0.0",
"@fastify/formbody": "^6.0.1",
"@fastify/cors": "^10.0.1",
"@fastify/formbody": "^8.0.1",
"axios": "^0.21.0",
"fastify": "3.18.1",
"fastify": "^5.0.0",
"helmet": "^5.1.0",
"morgan": "^1.10.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.2.1",
"react-scripts": "5.0.1",
"supertokens-auth-react": "latest",
"supertokens-node": "file:../../../lib/build",
"typescript": "^4.8.2",
"typescript": "^5.6.2",
"web-vitals": "^2.1.4"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions lib/build/framework/fastify/framework.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-nocheck
import type { FastifyRequest as OriginalFastifyRequest, FastifyReply, FastifyPluginCallback } from "fastify";
import { FastifyRequest as OriginalFastifyRequest, FastifyReply, FastifyPluginCallback } from "./types";
import type { HTTPMethod } from "../../types";
import { BaseRequest } from "../request";
import { BaseResponse } from "../response";
Expand Down Expand Up @@ -45,9 +45,9 @@ export declare class FastifyResponse extends BaseResponse {
export declare type SessionRequest<TRequest extends OriginalFastifyRequest = OriginalFastifyRequest> = TRequest & {
session?: SessionContainerInterface;
};
export interface FasitfyFramework extends Framework {
export interface FastifyFramework extends Framework {
plugin: FastifyPluginCallback;
errorHandler: () => (err: any, req: OriginalFastifyRequest, res: FastifyReply) => Promise<void>;
}
export declare const errorHandler: () => (err: any, req: OriginalFastifyRequest, res: FastifyReply) => Promise<void>;
export declare const FastifyWrapper: FasitfyFramework;
export declare const FastifyWrapper: FastifyFramework;
2 changes: 1 addition & 1 deletion lib/build/framework/fastify/framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class FastifyResponse extends response_1.BaseResponse {
);
let oldHeaders = this.response.getHeader(constants_1.COOKIE_HEADER);
if (oldHeaders === undefined) oldHeaders = [];
else if (!(oldHeaders instanceof Array)) oldHeaders = [oldHeaders];
else if (!(oldHeaders instanceof Array)) oldHeaders = [oldHeaders.toString()];
this.response.removeHeader(constants_1.COOKIE_HEADER);
this.response.header(constants_1.COOKIE_HEADER, [
...oldHeaders.filter((h) => !h.startsWith(key + "=")),
Expand Down
24 changes: 7 additions & 17 deletions lib/build/framework/fastify/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
// @ts-nocheck
/// <reference types="node" />
export type { SessionRequest } from "./framework";
export declare const plugin: import("fastify").FastifyPluginCallback<
Record<never, never>,
import("fastify").RawServerDefault
>;
export declare const plugin: import("./types").FastifyPluginCallback<import("./types").FastifyInstance<
unknown,
import("./types").FastifyRequest,
import("./types").FastifyReply
>>;
export declare const errorHandler: () => (
err: any,
req: import("fastify").FastifyRequest<
import("fastify/types/route").RouteGenericInterface,
import("fastify").RawServerDefault,
import("http").IncomingMessage
>,
res: import("fastify").FastifyReply<
import("fastify").RawServerDefault,
import("http").IncomingMessage,
import("http").ServerResponse<import("http").IncomingMessage>,
import("fastify/types/route").RouteGenericInterface,
unknown
>
req: import("./types").FastifyRequest,
res: import("./types").FastifyReply
) => Promise<void>;
export declare const wrapRequest: (unwrapped: any) => import("..").BaseRequest;
export declare const wrapResponse: (unwrapped: any) => import("..").BaseResponse;
33 changes: 33 additions & 0 deletions lib/build/framework/fastify/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// @ts-nocheck
/**
* Define fastify types based on the parts that are used in the codebase.
*/
export interface FastifyRequest {
body: any;
query: unknown;
headers: Record<string, string | string[] | undefined>;
method: string;
url: string;
}
export interface FastifyReply {
sent: boolean;
statusCode: number;
getHeaders(): Record<string, number | string | string[] | undefined>;
send(payload?: any): FastifyReply;
header(key: any, value: any): FastifyReply;
removeHeader(key: string): void;
getHeader(key: any): number | string | string[] | undefined;
type(contentType: string): FastifyReply;
}
export interface FastifyInstance<
Instance = unknown,
Request extends FastifyRequest = FastifyRequest,
Reply extends FastifyReply = FastifyReply
> {
addHook(this: Instance, name: string, hook: (req: Request, reply: Reply) => void): Instance;
}
export declare type FastifyPluginCallback<Instance = FastifyInstance> = (
instance: Instance,
opts: unknown,
done: (err?: Error) => void
) => void;
5 changes: 5 additions & 0 deletions lib/build/framework/fastify/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";
/**
* Define fastify types based on the parts that are used in the codebase.
*/
Object.defineProperty(exports, "__esModule", { value: true });
2 changes: 1 addition & 1 deletion lib/build/recipe/session/framework/fastify.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-nocheck
import { VerifySessionOptions } from "..";
import { SessionRequest } from "../../../framework/fastify/framework";
import { FastifyReply, FastifyRequest as OriginalFastifyRequest } from "fastify";
import { FastifyReply, FastifyRequest as OriginalFastifyRequest } from "../../../framework/fastify/types";
export declare function verifySession<TRequest extends OriginalFastifyRequest = OriginalFastifyRequest>(
options?: VerifySessionOptions
): (req: SessionRequest<TRequest>, res: FastifyReply) => Promise<void>;
16 changes: 8 additions & 8 deletions lib/ts/framework/fastify/framework.ts
deepjyoti30-st marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
* under the License.
*/

import type {
FastifyInstance,
import {
FastifyRequest as OriginalFastifyRequest,
FastifyReply,
FastifyInstance,
FastifyPluginCallback,
} from "fastify";
} from "./types";
import type { HTTPMethod } from "../../types";
import { getFromObjectCaseInsensitive, makeDefaultUserContextFromAPI, normaliseHttpMethod } from "../../utils";
import { BaseRequest } from "../request";
Expand Down Expand Up @@ -133,9 +133,9 @@ export class FastifyResponse extends BaseResponse {
) => {
let serialisedCookie = serializeCookieValue(key, value, domain, secure, httpOnly, expires, path, sameSite);

let oldHeaders: string | string[] | undefined = this.response.getHeader(COOKIE_HEADER);
let oldHeaders: string | string[] | undefined | number = this.response.getHeader(COOKIE_HEADER);
if (oldHeaders === undefined) oldHeaders = [];
else if (!((oldHeaders as any) instanceof Array)) oldHeaders = [oldHeaders];
else if (!((oldHeaders as any) instanceof Array)) oldHeaders = [oldHeaders.toString()];

this.response.removeHeader(COOKIE_HEADER);
this.response.header(COOKIE_HEADER, [
Expand Down Expand Up @@ -164,7 +164,7 @@ export class FastifyResponse extends BaseResponse {
};
}

function plugin(fastify: FastifyInstance, _: any, done: Function) {
function plugin(fastify: FastifyInstance, _: unknown, done: () => void) {
fastify.addHook("preHandler", async (req: OriginalFastifyRequest, reply: FastifyReply) => {
let supertokens = SuperTokens.getInstanceOrThrowError();
let request = new FastifyRequest(req);
Expand All @@ -185,7 +185,7 @@ export type SessionRequest<TRequest extends OriginalFastifyRequest = OriginalFas
session?: SessionContainerInterface;
};

export interface FasitfyFramework extends Framework {
export interface FastifyFramework extends Framework {
plugin: FastifyPluginCallback;
errorHandler: () => (err: any, req: OriginalFastifyRequest, res: FastifyReply) => Promise<void>;
}
Expand All @@ -200,7 +200,7 @@ export const errorHandler = () => {
};
};

export const FastifyWrapper: FasitfyFramework = {
export const FastifyWrapper: FastifyFramework = {
plugin,
errorHandler,
wrapRequest: (unwrapped) => {
Expand Down
36 changes: 36 additions & 0 deletions lib/ts/framework/fastify/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Define fastify types based on the parts that are used in the codebase.
*/

export interface FastifyRequest {
body: any;
query: unknown;
headers: Record<string, string | string[] | undefined>;
method: string;
url: string;
}

export interface FastifyReply {
sent: boolean;
statusCode: number;
getHeaders(): Record<string, number | string | string[] | undefined>;
porcellus marked this conversation as resolved.
Show resolved Hide resolved
send(payload?: any): FastifyReply;
header(key: any, value: any): FastifyReply;
removeHeader(key: string): void;
getHeader(key: any): number | string | string[] | undefined;
porcellus marked this conversation as resolved.
Show resolved Hide resolved
type(contentType: string): FastifyReply;
}

export interface FastifyInstance<
Instance = unknown,
Request extends FastifyRequest = FastifyRequest,
Reply extends FastifyReply = FastifyReply
> {
addHook(this: Instance, name: string, hook: (req: Request, reply: Reply) => void): Instance;
}

export type FastifyPluginCallback<Instance = FastifyInstance> = (
instance: Instance,
opts: unknown,
done: (err?: Error) => void
) => void;
2 changes: 1 addition & 1 deletion lib/ts/recipe/session/framework/fastify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import Session from "../recipe";
import { VerifySessionOptions } from "..";
import { FastifyRequest, FastifyResponse, SessionRequest } from "../../../framework/fastify/framework";
import { FastifyReply, FastifyRequest as OriginalFastifyRequest } from "fastify";
import { FastifyReply, FastifyRequest as OriginalFastifyRequest } from "../../../framework/fastify/types";
import SuperTokens from "../../../supertokens";
import { makeDefaultUserContextFromAPI } from "../../../utils";

Expand Down
24 changes: 15 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading