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

H3 support #351

Open
wants to merge 16 commits into
base: 11.0
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

# Adds

- Support for h3 framework.

## [11.0.0] - 2022-07-05

### Breaking change:
Expand Down Expand Up @@ -159,6 +163,10 @@ SuperTokens.init({
})
```

### Adds

- Support for h3 freamwork.

## [9.2.3] - 2022-06-03

- Changes `getUserMetadata` to return `any` type for metadata so that it's easier to use.
Expand Down
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
/// <reference types="node" />
import { H3Event } from "h3";
import type { IncomingMessage, ServerResponse } from "http";
import { SessionContainerInterface } from "../../recipe/session/types";
import { BaseRequest } from "../request";
import { BaseResponse } from "../response";
import { Framework } from "../types";
export declare class H3Request extends BaseRequest {
private request;
constructor(request: IncomingMessage);
getCookieValue: (key: string) => string | undefined;
getFormData: () => Promise<any>;
getMethod: () => import("../../types").HTTPMethod;
getHeaderValue: (key: string) => string | undefined;
getOriginalURL: () => string;
getKeyValueFromQuery: (key: string) => string | undefined;
getJSONBody: () => Promise<any>;
}
export declare class H3ResponseTokens extends BaseResponse {
private response;
private statusCode;
constructor(response: ServerResponse);
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;
setStatusCode: (statusCode: number) => void;
sendJSONResponse: (content: any) => void;
}
export interface SessionRequest extends IncomingMessage {
session?: SessionContainerInterface;
}
export declare const middlware: () => (
req: IncomingMessage,
res: ServerResponse,
next: (err?: Error | undefined) => any
) => Promise<any>;
export declare const errorHandler: () => (event: H3Event, errorPlain: Error, statusCode: number) => Promise<void>;
export interface H3Framework extends Framework {
middlware: () => (req: IncomingMessage, res: ServerResponse, next: (err?: Error) => any) => Promise<void>;
errorHandler: () => (event: H3Event, errorPlain: Error, statusCode: number) => Promise<void>;
}
export declare const H3Wrapper: H3Framework;
185 changes: 185 additions & 0 deletions lib/build/framework/h3/framework.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
"use strict";
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 supertokens_1 = require("../../supertokens");
const utils_1 = require("../../utils");
const request_1 = require("../request");
const response_1 = require("../response");
const utils_2 = require("../utils");
class H3Request extends request_1.BaseRequest {
constructor(request) {
super();
this.getCookieValue = (key) => {
return utils_2.getCookieValueFromIncomingMessage(this.request, key);
};
this.getFormData = () =>
__awaiter(this, void 0, void 0, function* () {
return utils_2.useRawBody(this.request);
});
this.getMethod = () => {
return utils_1.normaliseHttpMethod(this.request.method);
};
this.getHeaderValue = (key) => {
return utils_2.getHeaderValueFromIncomingMessage(this.request, key);
};
this.getOriginalURL = () => {
return this.request.url;
};
this.getKeyValueFromQuery = (key) => {
let path = this.request.url || "/";
const queryIndex = path.lastIndexOf("?");
if (queryIndex > -1) {
const queryArray = path.substring(queryIndex + 1, path.length).split("&");
const index = queryArray.findIndex((el) => el.includes(key));
if (index === -1) return undefined;
const value = queryArray[index].split("=")[1];
if (value === undefined || typeof value !== "string") {
return undefined;
}
return value;
} else {
return undefined;
}
};
this.getJSONBody = () =>
__awaiter(this, void 0, void 0, function* () {
return yield utils_2.useBody(this.request);
});
this.original = request;
this.request = request;
}
}
exports.H3Request = H3Request;
class H3ResponseTokens extends response_1.BaseResponse {
constructor(response) {
super();
this.sendHTMLResponse = (html) => {
if (this.response.writable) {
this.response.setHeader("Content-Type", "text/html");
this.response.statusCode = this.statusCode;
this.response.end(html);
}
};
this.setHeader = (key, value, allowDuplicateKey) => {
try {
const allheaders = this.response.getHeaders();
let existingValue = allheaders[key];
// we have the this.response.header for compatibility with nextJS
if (existingValue === undefined) {
this.response.setHeader(key, value);
} else if (allowDuplicateKey) {
this.response.setHeader(key, existingValue + ", " + value);
} else {
// we overwrite the current one with the new one
this.response.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) => {
utils_2.setCookieForServerResponse(
this.response,
key,
value,
domain,
secure,
httpOnly,
expires,
path,
sameSite
);
};
this.setStatusCode = (statusCode) => {
if (this.response.writable) {
this.statusCode = statusCode;
}
};
this.sendJSONResponse = (content) => {
if (this.response.writable) {
content = JSON.stringify(content);
this.response.setHeader("Content-Type", "application/json");
this.response.statusCode = this.statusCode;
this.response.end(content, "utf-8");
}
};
this.original = response;
this.response = response;
this.statusCode = 200;
}
}
exports.H3ResponseTokens = H3ResponseTokens;
exports.middlware = () => {
return (req, res, next) =>
__awaiter(void 0, void 0, void 0, function* () {
let supertokens;
const request = new H3Request(req);
const response = new H3ResponseTokens(res);
try {
supertokens = supertokens_1.default.getInstanceOrThrowError();
const result = yield supertokens.middleware(request, response);
if (!result) {
return next();
}
} catch (err) {
if (supertokens) {
try {
yield supertokens.errorHandler(err, request, response);
} catch (_a) {
next(err);
}
} else {
next(err);
}
}
});
};
exports.errorHandler = () => {
return (event, errorPlain, statusCode) =>
__awaiter(void 0, void 0, void 0, function* () {
const error = h3_1.createError(errorPlain);
error.statusCode = statusCode;
h3_1.sendError(event, error);
});
};
exports.H3Wrapper = {
middlware: exports.middlware,
errorHandler: exports.errorHandler,
wrapRequest: (unwrapped) => {
return new H3Request(unwrapped.req);
},
wrapResponse: (unwrapped) => {
return new H3ResponseTokens(unwrapped.res);
},
};
15 changes: 15 additions & 0 deletions lib/build/framework/h3/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @ts-nocheck
/// <reference types="node" />
export type { SessionRequest } from "./framework";
export declare const middleware: () => (
req: import("http").IncomingMessage,
res: import("http").ServerResponse,
next: (err?: Error | undefined) => any
) => Promise<void>;
export declare const errorHandler: () => (
event: import("h3").H3Event,
errorPlain: Error,
statusCode: number
) => Promise<void>;
export declare const wrapRequest: (unwrapped: any) => import("..").BaseRequest;
export declare const wrapResponse: (unwrapped: any) => import("..").BaseResponse;
7 changes: 7 additions & 0 deletions lib/build/framework/h3/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const framework_1 = require("./framework");
exports.middleware = framework_1.H3Wrapper.middlware;
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"],
};
2 changes: 2 additions & 0 deletions lib/build/framework/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export declare function setHeaderForExpressLikeResponse(
value: string,
allowDuplicateKey: boolean
): void;
export declare function useRawBody(req: IncomingMessage): Promise<string>;
export declare function useBody<T = any>(req: IncomingMessage): Promise<T>;
/**
*
* @param res
Expand Down
Loading