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

Add h3 framework #477

Open
wants to merge 6 commits into
base: 13.1
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion add-ts-no-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ glob(__dirname + "/lib/**/*.d.ts", (err, files) => {
let lines = contents.split("\n");
let newContents = `// @ts-nocheck`;
for (line of lines) {
if (line.match(/\/\/\/\ \<reference\ types\=\"(express|koa|loopback|hapi|aws|fastify)/g) === null) {
if (line.match(/\/\/\/\ \<reference\ types\=\"(express|koa|loopback|hapi|aws|fastify|h3)/g) === null) {
newContents += `\n${line}`;
}
}
Expand Down
3 changes: 3 additions & 0 deletions framework/h3/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "../../lib/build/framework/h3";
import * as _default from "../../lib/build/framework/h3";
export default _default;
6 changes: 6 additions & 0 deletions framework/h3/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/h3"));
52 changes: 52 additions & 0 deletions lib/build/framework/h3/framework.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// @ts-nocheck
import { EventHandler, H3Event } from "h3";
import type { HTTPMethod } from "../../types";
import { BaseRequest } from "../request";
import { BaseResponse } from "../response";
import type { Framework } from "../types";
import type { SessionContainerInterface } from "../../recipe/session/types";
export declare class H3Request extends BaseRequest {
private event;
constructor(event: H3Event);
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 class H3Response extends BaseResponse {
private event;
private statusCode;
constructor(event: H3Event);
sendHTMLResponse: (html: string) => void;
setHeader: (key: string, value: string, allowDuplicateKey: boolean) => 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 interface SessionEvent extends H3Event {
context: {
session?: SessionContainerInterface;
};
}
export declare const middleware: EventHandler<void>;
export declare const errorHandler: EventHandler<void>;
export interface H3Framework extends Framework {
middleware: EventHandler<any>;
errorHandler: EventHandler<any>;
}
export declare const H3Wrapper: H3Framework;
180 changes: 180 additions & 0 deletions lib/build/framework/h3/framework.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
"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 __awaiter =
(this && this.__awaiter) ||
function (thisArg, _arguments, P, generator) {
function adopt(value) {
return value instanceof P
? value
: new P(function (resolve) {
resolve(value);
});
}
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
}
function rejected(value) {
try {
step(generator["throw"](value));
} catch (e) {
reject(e);
}
}
function step(result) {
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
}
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const h3_1 = require("h3");
const utils_1 = require("../../utils");
const request_1 = require("../request");
const response_1 = require("../response");
const supertokens_1 = require("../../supertokens");
class H3Request extends request_1.BaseRequest {
constructor(event) {
super();
this.getFormData = () =>
__awaiter(this, void 0, void 0, function* () {
return h3_1.readBody(this.event);
});
this.getKeyValueFromQuery = (key) => {
const query = h3_1.getQuery(this.event);
if (query === undefined) {
return undefined;
}
let value = query[key];
if (value === undefined || typeof value !== "string") {
return undefined;
}
return value;
};
this.getJSONBody = () =>
__awaiter(this, void 0, void 0, function* () {
return h3_1.readBody(this.event);
});
this.getMethod = () => {
return utils_1.normaliseHttpMethod(h3_1.getMethod(this.event));
};
this.getCookieValue = (key) => {
return h3_1.getCookie(this.event, key);
};
this.getHeaderValue = (key) => {
return h3_1.getHeader(this.event, key);
};
this.getOriginalURL = () => {
let path = this.event.path;
if (!path) {
throw new Error("Error while trying to get original url");
}
return path;
};
this.original = event;
this.event = event;
}
}
exports.H3Request = H3Request;
class H3Response extends response_1.BaseResponse {
constructor(event) {
super();
this.sendHTMLResponse = (html) => {
if (this.event.node.res.writable) {
this.event.node.res.setHeader("Content-Type", "text/html");
this.event.node.res.statusCode = this.statusCode;
h3_1.send(this.event, html);
// this.response.status(this.statusCode).send(Buffer.from(html));
}
};
this.setHeader = (key, value, allowDuplicateKey) => {
try {
let existingHeaders = this.event.node.res.getHeaders();
let existingValue = existingHeaders[key.toLowerCase()];
// we have the this.response.header for compatibility with nextJS
if (existingValue === undefined) {
this.event.node.res.setHeader(key, value);
} else if (allowDuplicateKey) {
this.event.node.res.setHeader(key, existingValue + ", " + value);
} else {
// we overwrite the current one with the new one
this.event.node.res.setHeader(key, value);
}
} catch (err) {
throw new Error("Error while setting header with key: " + key + " and value: " + value);
}
};
this.setCookie = (key, value, domain, secure, httpOnly, expires, path, sameSite) => {
h3_1.setCookie(this.event, key, value, {
secure,
sameSite,
httpOnly,
expires: new Date(expires),
domain,
path,
});
};
/**
* @param {number} statusCode
*/
this.setStatusCode = (statusCode) => {
if (this.event.node.res.writable) {
this.statusCode = statusCode;
}
};
this.sendJSONResponse = (content) => {
if (this.event.node.res.writable) {
this.event.node.res.setHeader("Content-Type", "application/json");
this.event.node.res.statusCode = this.statusCode;
h3_1.send(this.event, content);
}
};
this.original = event;
this.event = event;
this.statusCode = 200;
}
}
exports.H3Response = H3Response;
exports.middleware = h3_1.eventHandler((event) =>
__awaiter(void 0, void 0, void 0, function* () {
const supertokens = supertokens_1.default.getInstanceOrThrowError();
const request = new H3Request(event);
const response = new H3Response(event);
try {
yield supertokens.middleware(request, response);
} catch (err) {
yield supertokens.errorHandler(err, request, response);
}
})
);
exports.errorHandler = h3_1.eventHandler((event) => {
const error = h3_1.createError({});
h3_1.sendError(event, error);
});
exports.H3Wrapper = {
middleware: exports.middleware,
errorHandler: exports.errorHandler,
wrapRequest: (unwrapped) => {
return new H3Request(unwrapped);
},
wrapResponse: (unwrapped) => {
return new H3Response(unwrapped);
},
};
6 changes: 6 additions & 0 deletions lib/build/framework/h3/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @ts-nocheck
export type { SessionEvent } from "./framework";
export declare const middleware: import("h3").EventHandler<any>;
export declare const errorHandler: import("h3").EventHandler<any>;
export declare const wrapRequest: (unwrapped: any) => import("..").BaseRequest;
export declare const wrapResponse: (unwrapped: any) => import("..").BaseResponse;
21 changes: 21 additions & 0 deletions lib/build/framework/h3/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"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 });
const framework_1 = require("./framework");
exports.middleware = framework_1.H3Wrapper.middleware;
exports.errorHandler = framework_1.H3Wrapper.errorHandler;
exports.wrapRequest = framework_1.H3Wrapper.wrapRequest;
exports.wrapResponse = framework_1.H3Wrapper.wrapResponse;
3 changes: 3 additions & 0 deletions lib/build/framework/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import * as hapiFramework from "./hapi";
import * as loopbackFramework from "./loopback";
import * as koaFramework from "./koa";
import * as awsLambdaFramework from "./awsLambda";
import * as h3Framework from "./h3";
declare const _default: {
express: typeof expressFramework;
fastify: typeof fastifyFramework;
hapi: typeof hapiFramework;
loopback: typeof loopbackFramework;
koa: typeof koaFramework;
awsLambda: typeof awsLambdaFramework;
h3: typeof h3Framework;
};
export default _default;
export declare let express: typeof expressFramework;
Expand All @@ -22,3 +24,4 @@ export declare let hapi: typeof hapiFramework;
export declare let loopback: typeof loopbackFramework;
export declare let koa: typeof koaFramework;
export declare let awsLambda: typeof awsLambdaFramework;
export declare let h3: typeof h3Framework;
3 changes: 3 additions & 0 deletions lib/build/framework/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@ const hapiFramework = require("./hapi");
const loopbackFramework = require("./loopback");
const koaFramework = require("./koa");
const awsLambdaFramework = require("./awsLambda");
const h3Framework = require("./h3");
exports.default = {
express: expressFramework,
fastify: fastifyFramework,
hapi: hapiFramework,
loopback: loopbackFramework,
koa: koaFramework,
awsLambda: awsLambdaFramework,
h3: h3Framework,
};
exports.express = expressFramework;
exports.fastify = fastifyFramework;
exports.hapi = hapiFramework;
exports.loopback = loopbackFramework;
exports.koa = koaFramework;
exports.awsLambda = awsLambdaFramework;
exports.h3 = h3Framework;
2 changes: 1 addition & 1 deletion lib/build/framework/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-nocheck
export declare type TypeFramework = "express" | "fastify" | "hapi" | "loopback" | "koa" | "awsLambda";
export declare type TypeFramework = "express" | "fastify" | "hapi" | "loopback" | "koa" | "awsLambda" | "h3";
import { BaseRequest, BaseResponse } from ".";
export declare let SchemaFramework: {
type: string;
Expand Down
2 changes: 1 addition & 1 deletion lib/build/framework/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchemaFramework = {
type: "string",
enum: ["express", "fastify", "hapi", "loopback", "koa", "awsLambda"],
enum: ["express", "fastify", "hapi", "loopback", "koa", "awsLambda", "h3"],
};
3 changes: 3 additions & 0 deletions lib/build/recipe/session/framework/h3.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @ts-nocheck
import type { VerifySessionOptions } from "..";
export declare function verifySession(options?: VerifySessionOptions): import("h3").EventHandler<void>;
Loading