-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make it possible to use our middleware in some unsupported fram…
…eworks
- Loading branch information
Showing
8 changed files
with
475 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.