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 9bdb66d
Show file tree
Hide file tree
Showing 8 changed files with 475 additions and 0 deletions.
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"));
76 changes: 76 additions & 0 deletions lib/build/framework/custom/framework.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// @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: () => (
request: BaseRequest,
response: BaseResponse,
next: NextFunction
) => Promise<void>;
export declare const errorHandler: () => (
err: any,
request: BaseRequest,
response: BaseResponse,
next: NextFunction
) => Promise<void>;
export declare const CustomFrameworkWrapper: {
middleware: () => (request: BaseRequest, response: BaseResponse, next: NextFunction) => Promise<void>;
errorHandler: () => (err: any, request: BaseRequest, response: BaseResponse, next: NextFunction) => Promise<void>;
};
export {};
138 changes: 138 additions & 0 deletions lib/build/framework/custom/framework.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
"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 middleware = () => {
return async (request, response, next) => {
let supertokens;
const userContext = utils_1.makeDefaultUserContextFromAPI(request);
try {
supertokens = supertokens_1.default.getInstanceOrThrowError();
const result = await supertokens.middleware(request, response, userContext);
if (!result) {
return next();
}
} catch (err) {
if (supertokens) {
try {
await supertokens.errorHandler(err, request, response);
} catch (_a) {
next(err);
}
} else {
next(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,
};
13 changes: 13 additions & 0 deletions lib/build/framework/custom/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @ts-nocheck
export { PreParsedRequest, CollectingResponse } from "./framework";
export declare const middleware: () => (
request: import("..").BaseRequest,
response: import("..").BaseResponse,
next: import("./framework").NextFunction
) => Promise<void>;
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 9bdb66d

Please sign in to comment.