Skip to content

Commit

Permalink
feat: update based on initial feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
porcellus committed Jan 15, 2025
1 parent ffca670 commit b1f72d2
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 19 deletions.
23 changes: 19 additions & 4 deletions lib/build/supertokens.js

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

19 changes: 16 additions & 3 deletions lib/build/types.d.ts

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

2 changes: 1 addition & 1 deletion lib/build/utils.d.ts

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

37 changes: 32 additions & 5 deletions lib/ts/supertokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@
* under the License.
*/

import { TypeInput, NormalisedAppinfo, HTTPMethod, SuperTokensInfo, UserContext, PluginRouteHandler } from "./types";
import {
TypeInput,
NormalisedAppinfo,
HTTPMethod,
SuperTokensInfo,
UserContext,
PluginRouteHandler,
SuperTokensPlugin,
} from "./types";
import {
normaliseInputAppInfoOrThrowError,
maxVersion,
Expand Down Expand Up @@ -53,18 +61,35 @@ export default class SuperTokens {
telemetryEnabled: boolean;

constructor(config: TypeInput) {
const pluginList = config.plugins ?? [];
const inputPluginList = config.plugins ?? [];
this.pluginRouteHandlers = [];
for (const plugin of pluginList) {
const versionContraints = Array.isArray(plugin.sdkVersion) ? plugin.sdkVersion : [plugin.sdkVersion];
const finalPluginList: SuperTokensPlugin[] = [];
for (const plugin of inputPluginList) {
const versionContraints = Array.isArray(plugin.compatibleSDKVersions)
? plugin.compatibleSDKVersions
: [plugin.compatibleSDKVersions];
if (!versionContraints.includes(version)) {
// TODO: better checks
throw new Error("Plugin version mismatch");
}
if (plugin.dependencies) {
const result = plugin.dependencies(finalPluginList, version);
if (result.status === "ERROR") {
throw new Error(result.message);
}
if (result.pluginsToAdd) {
finalPluginList.push(...result.pluginsToAdd);
}
finalPluginList.push(plugin);
}
}

for (const plugin of finalPluginList) {
if (plugin.routeHandlers) {
this.pluginRouteHandlers.push(...plugin.routeHandlers);
}
}

if (config.debug === true) {
enableDebugLogs();
}
Expand Down Expand Up @@ -138,7 +163,9 @@ export default class SuperTokens {
const recipeModule = func(
this.appInfo,
this.isInServerlessEnv,
pluginList.map((p) => p.overrideMap)
finalPluginList.filter((p) => p.overrideMap !== undefined).map((p) => p.overrideMap) as NonNullable<
SuperTokensPlugin["overrideMap"]
>[]
);
if (recipeModule.getRecipeId() === MultitenancyRecipe.RECIPE_ID) {
multitenancyFound = true;
Expand Down
13 changes: 9 additions & 4 deletions lib/ts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,14 @@ export type PluginRouteHandler = {
};

export type SuperTokensPlugin = {
id: string;
sdkVersion: string | string[]; // match the syntax of the engines field in package.json
overrideMap: {
id: string; // TODO: validate that no two plugins have the same id
version?: string;
compatibleSDKVersions?: string | string[]; // match the syntax of the engines field in package.json
dependencies?: (
pluginsAbove: SuperTokensPlugin[],
sdkVersion: string
) => { status: "OK"; pluginsToAdd?: SuperTokensPlugin[] } | { status: "ERROR"; message: string };
overrideMap?: {
[recipeId in keyof AllRecipeConfigs]?: RecipePluginOverride<recipeId> & {
recipeInitRequired?: boolean | ((sdkVersion: string) => boolean);
};
Expand Down Expand Up @@ -151,7 +156,7 @@ export interface HttpRequest {
export type RecipeListFunction = (
appInfo: NormalisedAppinfo,
isInServerlessEnv: boolean,
overrideMaps?: SuperTokensPlugin["overrideMap"][]
overrideMaps?: NonNullable<SuperTokensPlugin["overrideMap"]>[]
) => RecipeModule;

export type APIHandled = {
Expand Down
2 changes: 1 addition & 1 deletion lib/ts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ export const isBuffer = (obj: any): boolean => {
export function applyPlugins<T extends keyof AllRecipeConfigs>(
recipeId: T,
config: AllRecipeConfigs[T] | undefined,
plugins: SuperTokensPlugin["overrideMap"][]
plugins: NonNullable<SuperTokensPlugin["overrideMap"]>[]
): AllRecipeConfigs[T] {
config = config ?? ({} as AllRecipeConfigs[T]);
let functionLayers = [config.override?.functions];
Expand Down
2 changes: 1 addition & 1 deletion test/with-typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2317,7 +2317,7 @@ Supertokens.init({
plugins: [
{
id: "asdf",
sdkVersion: "1.2.3",
compatibleSDKVersions: "1.2.3",
overrideMap: {
emailpassword: {
id: "emailpassword",
Expand Down

0 comments on commit b1f72d2

Please sign in to comment.