Skip to content

Commit

Permalink
feat: make it possible to use our middleware in some unsupported fram…
Browse files Browse the repository at this point in the history
…eworks
  • Loading branch information
porcellus committed Sep 28, 2023
1 parent ed34bf4 commit 5aad4fe
Show file tree
Hide file tree
Showing 9 changed files with 597 additions and 1 deletion.
3 changes: 3 additions & 0 deletions framework/custom/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "../../lib/build/framework/custom";
import * as _default from "../../lib/build/framework/custom";
export default _default;
6 changes: 6 additions & 0 deletions framework/custom/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
exports.__esModule = true;
__export(require("../../lib/build/framework/custom"));
104 changes: 104 additions & 0 deletions lib/build/framework/custom/framework.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// @ts-nocheck
import type { HTTPMethod } from "../../types";
import { BaseRequest } from "../request";
import { BaseResponse } from "../response";
declare type RequestInfo = {
url: string;
method: HTTPMethod;
headers: Headers;
cookies: Record<string, string>;
query: Record<string, string>;
getJSONBody: () => Promise<any>;
getFormBody: () => Promise<any>;
};
export declare class PreParsedRequest extends BaseRequest {
private request;
constructor(request: RequestInfo);
getFormData: () => Promise<any>;
getKeyValueFromQuery: (key: string) => string | undefined;
getJSONBody: () => Promise<any>;
getMethod: () => HTTPMethod;
getCookieValue: (key: string) => string | undefined;
getHeaderValue: (key: string) => string | undefined;
getOriginalURL: () => string;
}
export declare type CookieInfo = {
key: string;
value: string;
domain: string | undefined;
secure: boolean;
httpOnly: boolean;
expires: number;
path: string;
sameSite: "strict" | "lax" | "none";
};
export declare class CollectingResponse extends BaseResponse {
statusCode: number;
readonly headers: Headers;
readonly cookies: CookieInfo[];
body?: string;
constructor();
sendHTMLResponse: (html: string) => void;
setHeader: (key: string, value: string, allowDuplicateKey: boolean) => void;
removeHeader: (key: string) => void;
setCookie: (
key: string,
value: string,
domain: string | undefined,
secure: boolean,
httpOnly: boolean,
expires: number,
path: string,
sameSite: "strict" | "lax" | "none"
) => void;
/**
* @param {number} statusCode
*/
setStatusCode: (statusCode: number) => void;
sendJSONResponse: (content: any) => void;
}
export declare type NextFunction = (err?: any) => void;
export declare const middleware: <OrigReqType = BaseRequest, OrigRespType = BaseResponse>(
wrapRequest?: (req: OrigReqType) => BaseRequest,
wrapResponse?: (req: OrigRespType) => BaseResponse
) => (
request: OrigReqType,
response: OrigRespType,
next?: NextFunction | undefined
) => Promise<
| {
handled: boolean;
error?: undefined;
}
| {
error: any;
handled?: undefined;
}
>;
export declare const errorHandler: () => (
err: any,
request: BaseRequest,
response: BaseResponse,
next: NextFunction
) => Promise<void>;
export declare const CustomFrameworkWrapper: {
middleware: <OrigReqType = BaseRequest, OrigRespType = BaseResponse>(
wrapRequest?: (req: OrigReqType) => BaseRequest,
wrapResponse?: (req: OrigRespType) => BaseResponse
) => (
request: OrigReqType,
response: OrigRespType,
next?: NextFunction | undefined
) => Promise<
| {
handled: boolean;
error?: undefined;
}
| {
error: any;
handled?: undefined;
}
>;
errorHandler: () => (err: any, request: BaseRequest, response: BaseResponse, next: NextFunction) => Promise<void>;
};
export {};
152 changes: 152 additions & 0 deletions lib/build/framework/custom/framework.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
"use strict";
/* Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
var __importDefault =
(this && this.__importDefault) ||
function (mod) {
return mod && mod.__esModule ? mod : { default: mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomFrameworkWrapper = exports.errorHandler = exports.middleware = exports.CollectingResponse = exports.PreParsedRequest = void 0;
const utils_1 = require("../../utils");
const request_1 = require("../request");
const response_1 = require("../response");
const supertokens_1 = __importDefault(require("../../supertokens"));
class PreParsedRequest extends request_1.BaseRequest {
constructor(request) {
super();
this.getFormData = async () => {
return this.request.getFormBody();
};
this.getKeyValueFromQuery = (key) => {
if (this.request.query === undefined) {
return undefined;
}
let value = this.request.query[key];
if (value === undefined || typeof value !== "string") {
return undefined;
}
return value;
};
this.getJSONBody = async () => {
return this.request.getJSONBody();
};
this.getMethod = () => {
return utils_1.normaliseHttpMethod(this.request.method);
};
this.getCookieValue = (key) => {
return this.request.cookies[key];
};
this.getHeaderValue = (key) => {
var _a;
return (_a = this.request.headers.get(key)) !== null && _a !== void 0 ? _a : undefined;
};
this.getOriginalURL = () => {
return this.request.url;
};
this.original = request;
this.request = request;
}
}
exports.PreParsedRequest = PreParsedRequest;
class CollectingResponse extends response_1.BaseResponse {
constructor() {
super();
this.sendHTMLResponse = (html) => {
this.headers.set("Content-Type", "text/html");
this.body = html;
};
this.setHeader = (key, value, allowDuplicateKey) => {
if (allowDuplicateKey) {
this.headers.append(key, value);
} else {
this.headers.set(key, value);
}
};
this.removeHeader = (key) => {
this.headers.delete(key);
};
this.setCookie = (key, value, domain, secure, httpOnly, expires, path, sameSite) => {
this.cookies.push({ key, value, domain, secure, httpOnly, expires, path, sameSite });
};
/**
* @param {number} statusCode
*/
this.setStatusCode = (statusCode) => {
this.statusCode = statusCode;
};
this.sendJSONResponse = (content) => {
this.headers.set("Content-Type", "application/json");
this.body = JSON.stringify(content);
};
this.headers = new Headers();
this.statusCode = 200;
this.cookies = [];
}
}
exports.CollectingResponse = CollectingResponse;
const identity = (i) => i;
const middleware = (wrapRequest = identity, wrapResponse = identity) => {
return async (request, response, next) => {
const wrappedReq = wrapRequest(request);
const wrappedResp = wrapResponse(response);
let supertokens;
const userContext = utils_1.makeDefaultUserContextFromAPI(wrappedReq);
try {
supertokens = supertokens_1.default.getInstanceOrThrowError();
const result = await supertokens.middleware(wrappedReq, wrappedResp, userContext);
if (!result) {
if (next) {
next();
}
return { handled: false };
}
return { handled: true };
} catch (err) {
if (supertokens) {
try {
await supertokens.errorHandler(err, wrappedReq, wrappedResp);
return { handled: true };
} catch (_a) {
if (next) {
next(err);
}
return { error: err };
}
} else {
if (next) {
next(err);
}
return { error: err };
}
}
};
};
exports.middleware = middleware;
const errorHandler = () => {
return async (err, request, response, next) => {
let supertokens = supertokens_1.default.getInstanceOrThrowError();
try {
await supertokens.errorHandler(err, request, response);
} catch (err) {
return next(err);
}
};
};
exports.errorHandler = errorHandler;
exports.CustomFrameworkWrapper = {
middleware: exports.middleware,
errorHandler: exports.errorHandler,
};
25 changes: 25 additions & 0 deletions lib/build/framework/custom/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// @ts-nocheck
export { PreParsedRequest, CollectingResponse } from "./framework";
export declare const middleware: <OrigReqType = import("..").BaseRequest, OrigRespType = import("..").BaseResponse>(
wrapRequest?: (req: OrigReqType) => import("..").BaseRequest,
wrapResponse?: (req: OrigRespType) => import("..").BaseResponse
) => (
request: OrigReqType,
response: OrigRespType,
next?: import("./framework").NextFunction | undefined
) => Promise<
| {
handled: boolean;
error?: undefined;
}
| {
error: any;
handled?: undefined;
}
>;
export declare const errorHandler: () => (
err: any,
request: import("..").BaseRequest,
response: import("..").BaseResponse,
next: import("./framework").NextFunction
) => Promise<void>;
33 changes: 33 additions & 0 deletions lib/build/framework/custom/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"use strict";
/* Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.errorHandler = exports.middleware = exports.CollectingResponse = exports.PreParsedRequest = void 0;
const framework_1 = require("./framework");
var framework_2 = require("./framework");
Object.defineProperty(exports, "PreParsedRequest", {
enumerable: true,
get: function () {
return framework_2.PreParsedRequest;
},
});
Object.defineProperty(exports, "CollectingResponse", {
enumerable: true,
get: function () {
return framework_2.CollectingResponse;
},
});
exports.middleware = framework_1.CustomFrameworkWrapper.middleware;
exports.errorHandler = framework_1.CustomFrameworkWrapper.errorHandler;
Loading

0 comments on commit 5aad4fe

Please sign in to comment.