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

feat(h3framework): Added support for h3 framework #297

Closed
wants to merge 13 commits into from
  •  
  •  
  •  
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

## [9.2.1] - 2022-05-05

# Adds

- Support for h3 freamwork.
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved


## [9.2.0] - 2022-04-18

# Adds
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"));
1 change: 0 additions & 1 deletion lib/build/constants.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 9 additions & 14 deletions lib/build/error.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 3 additions & 21 deletions lib/build/framework/awsLambda/framework.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
// @ts-nocheck
import type {
APIGatewayProxyEventV2,
APIGatewayProxyEvent,
APIGatewayProxyResult,
APIGatewayProxyStructuredResultV2,
Handler,
} from "aws-lambda";
import type { APIGatewayProxyEventV2, APIGatewayProxyEvent, APIGatewayProxyResult, APIGatewayProxyStructuredResultV2, Handler } from "aws-lambda";
import { HTTPMethod } from "../../types";
import { BaseRequest } from "../request";
import { BaseResponse } from "../response";
Expand Down Expand Up @@ -57,24 +50,13 @@ export declare class AWSResponse extends BaseResponse {
constructor(event: SupertokensLambdaEvent | SupertokensLambdaEventV2);
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;
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;
sendResponse: (
response?: APIGatewayProxyResult | APIGatewayProxyStructuredResultV2 | undefined
) => APIGatewayProxyResult | APIGatewayProxyStructuredResultV2;
sendResponse: (response?: APIGatewayProxyResult | APIGatewayProxyStructuredResultV2 | undefined) => APIGatewayProxyResult | APIGatewayProxyStructuredResultV2;
}
export interface SessionEventV2 extends SupertokensLambdaEventV2 {
session?: SessionContainerInterface;
Expand Down
193 changes: 78 additions & 115 deletions lib/build/framework/awsLambda/framework.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,13 @@
"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());
});
};
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 utils_1 = require("../../utils");
const request_1 = require("../request");
Expand All @@ -41,20 +19,20 @@ const querystring_1 = require("querystring");
class AWSRequest extends request_1.BaseRequest {
constructor(event) {
super();
this.getFormData = () =>
__awaiter(this, void 0, void 0, function* () {
if (this.parsedUrlEncodedFormData === undefined) {
if (this.event.body === null || this.event.body === undefined) {
this.getFormData = () => __awaiter(this, void 0, void 0, function* () {
if (this.parsedUrlEncodedFormData === undefined) {
if (this.event.body === null || this.event.body === undefined) {
this.parsedUrlEncodedFormData = {};
}
else {
this.parsedUrlEncodedFormData = querystring_1.parse(this.event.body);
if (this.parsedUrlEncodedFormData === undefined) {
this.parsedUrlEncodedFormData = {};
} else {
this.parsedUrlEncodedFormData = querystring_1.parse(this.event.body);
if (this.parsedUrlEncodedFormData === undefined) {
this.parsedUrlEncodedFormData = {};
}
}
}
return this.parsedUrlEncodedFormData;
});
}
return this.parsedUrlEncodedFormData;
});
this.getKeyValueFromQuery = (key) => {
if (this.event.queryStringParameters === undefined || this.event.queryStringParameters === null) {
return undefined;
Expand All @@ -65,20 +43,20 @@ class AWSRequest extends request_1.BaseRequest {
}
return value;
};
this.getJSONBody = () =>
__awaiter(this, void 0, void 0, function* () {
if (this.parsedJSONBody === undefined) {
if (this.event.body === null || this.event.body === undefined) {
this.getJSONBody = () => __awaiter(this, void 0, void 0, function* () {
if (this.parsedJSONBody === undefined) {
if (this.event.body === null || this.event.body === undefined) {
this.parsedJSONBody = {};
}
else {
this.parsedJSONBody = JSON.parse(this.event.body);
if (this.parsedJSONBody === undefined) {
this.parsedJSONBody = {};
} else {
this.parsedJSONBody = JSON.parse(this.event.body);
if (this.parsedJSONBody === undefined) {
this.parsedJSONBody = {};
}
}
}
return this.parsedJSONBody;
});
}
return this.parsedJSONBody;
});
this.getMethod = () => {
let rawMethod = this.event.httpMethod;
if (rawMethod !== undefined) {
Expand All @@ -88,20 +66,15 @@ class AWSRequest extends request_1.BaseRequest {
};
this.getCookieValue = (key) => {
let cookies = this.event.cookies;
if (
(this.event.headers === undefined || this.event.headers === null) &&
(cookies === undefined || cookies === null)
) {
if ((this.event.headers === undefined || this.event.headers === null) &&
(cookies === undefined || cookies === null)) {
return undefined;
}
let value = utils_2.getCookieValueFromHeaders(this.event.headers, key);
if (value === undefined && cookies !== undefined && cookies !== null) {
value = utils_2.getCookieValueFromHeaders(
{
cookie: cookies.join(";"),
},
key
);
value = utils_2.getCookieValueFromHeaders({
cookie: cookies.join(";"),
}, key);
}
return value;
};
Expand Down Expand Up @@ -147,16 +120,7 @@ class AWSResponse extends response_1.BaseResponse {
});
};
this.setCookie = (key, value, domain, secure, httpOnly, expires, path, sameSite) => {
let serialisedCookie = utils_2.serializeCookieValue(
key,
value,
domain,
secure,
httpOnly,
expires,
path,
sameSite
);
let serialisedCookie = utils_2.serializeCookieValue(key, value, domain, secure, httpOnly, expires, path, sameSite);
this.event.supertokens.response.cookies.push(serialisedCookie);
};
/**
Expand Down Expand Up @@ -204,7 +168,8 @@ class AWSResponse extends response_1.BaseResponse {
if (supertokensHeaders[i].allowDuplicateKey && currentValue !== undefined) {
let newValue = `${currentValue}, ${supertokensHeaders[i].value}`;
headers[supertokensHeaders[i].key] = newValue;
} else {
}
else {
headers[supertokensHeaders[i].key] = supertokensHeaders[i].value;
}
}
Expand All @@ -214,28 +179,26 @@ class AWSResponse extends response_1.BaseResponse {
cookies = [];
}
cookies.push(...supertokensCookies);
let result = Object.assign(Object.assign({}, response), { cookies, body, statusCode, headers });
let result = Object.assign(Object.assign({}, response), { cookies,
body,
statusCode,
headers });
return result;
} else {
}
else {
let multiValueHeaders = response.multiValueHeaders;
if (multiValueHeaders === undefined) {
multiValueHeaders = {};
}
let headsersInMultiValueHeaders = Object.keys(multiValueHeaders);
let cookieHeader = headsersInMultiValueHeaders.find(
(h) => h.toLowerCase() === constants_1.COOKIE_HEADER.toLowerCase()
);
let cookieHeader = headsersInMultiValueHeaders.find((h) => h.toLowerCase() === constants_1.COOKIE_HEADER.toLowerCase());
if (cookieHeader === undefined) {
multiValueHeaders[constants_1.COOKIE_HEADER] = supertokensCookies;
} else {
}
else {
multiValueHeaders[cookieHeader].push(...supertokensCookies);
}
let result = Object.assign(Object.assign({}, response), {
multiValueHeaders,
body: body,
statusCode: statusCode,
headers,
});
let result = Object.assign(Object.assign({}, response), { multiValueHeaders, body: body, statusCode: statusCode, headers });
return result;
}
};
Expand All @@ -255,37 +218,37 @@ class AWSResponse extends response_1.BaseResponse {
}
exports.AWSResponse = AWSResponse;
exports.middleware = (handler) => {
return (event, context, callback) =>
__awaiter(void 0, void 0, void 0, function* () {
let supertokens = supertokens_1.default.getInstanceOrThrowError();
let request = new AWSRequest(event);
let response = new AWSResponse(event);
try {
let result = yield supertokens.middleware(request, response);
if (result) {
return response.sendResponse();
}
if (handler !== undefined) {
let handlerResult = yield handler(event, context, callback);
return response.sendResponse(handlerResult);
}
/**
* it reaches this point only if the API route was not exposed by
* the SDK and user didn't provide a handler
*/
response.setStatusCode(404);
response.sendJSONResponse({
error: `The middleware couldn't serve the API path ${request.getOriginalURL()}, method: ${request.getMethod()}. If this is an unexpected behaviour, please create an issue here: https://github.com/supertokens/supertokens-node/issues`,
});
return (event, context, callback) => __awaiter(void 0, void 0, void 0, function* () {
let supertokens = supertokens_1.default.getInstanceOrThrowError();
let request = new AWSRequest(event);
let response = new AWSResponse(event);
try {
let result = yield supertokens.middleware(request, response);
if (result) {
return response.sendResponse();
} catch (err) {
yield supertokens.errorHandler(err, request, response);
if (response.responseSet) {
return response.sendResponse();
}
throw err;
}
});
if (handler !== undefined) {
let handlerResult = yield handler(event, context, callback);
return response.sendResponse(handlerResult);
}
/**
* it reaches this point only if the API route was not exposed by
* the SDK and user didn't provide a handler
*/
response.setStatusCode(404);
response.sendJSONResponse({
error: `The middleware couldn't serve the API path ${request.getOriginalURL()}, method: ${request.getMethod()}. If this is an unexpected behaviour, please create an issue here: https://github.com/supertokens/supertokens-node/issues`,
});
return response.sendResponse();
}
catch (err) {
yield supertokens.errorHandler(err, request, response);
if (response.responseSet) {
return response.sendResponse();
}
throw err;
}
});
};
exports.AWSWrapper = {
middleware: exports.middleware,
Expand Down
5 changes: 1 addition & 4 deletions lib/build/framework/awsLambda/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
// @ts-nocheck
export type { SessionEvent, SessionEventV2 } from "./framework";
export declare const middleware: (
handler?: import("aws-lambda").Handler<any, any> | undefined
) => import("aws-lambda").Handler<any, any>;
export declare const middleware: (handler?: import("aws-lambda").Handler<any, any> | undefined) => import("aws-lambda").Handler<any, any>;
export declare const wrapRequest: (unwrapped: any) => import("..").BaseRequest;
export declare const wrapResponse: (unwrapped: any) => import("..").BaseResponse;
1 change: 0 additions & 1 deletion lib/build/framework/constants.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
// @ts-nocheck
export declare const COOKIE_HEADER = "Set-Cookie";
Loading