Skip to content

Commit 16fd99b

Browse files
committed
added support for export and init to plugins
1 parent 733e3d6 commit 16fd99b

File tree

8 files changed

+136
-318
lines changed

8 files changed

+136
-318
lines changed

lib/build/genericComponentOverrideContext.js

Lines changed: 46 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/build/superTokens.d.ts

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/build/types.d.ts

Lines changed: 26 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/build/utils.d.ts

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ts/superTokens.tsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
getDefaultCookieScope,
3333
getNormalisedUserContext,
3434
getOriginOfPage,
35+
getPublicPlugin,
3536
getTenantIdFromQueryParams,
3637
isTest,
3738
normaliseCookieScopeOrThrowError,
@@ -56,6 +57,7 @@ import type {
5657
SuperTokensPlugin,
5758
AllRecipeConfigs,
5859
PluginRouteHandler,
60+
SuperTokensPublicPlugin,
5961
} from "./types";
6062

6163
/*
@@ -73,6 +75,8 @@ export default class SuperTokens {
7375
* Instance Attributes.
7476
*/
7577
appInfo: NormalisedAppInfo;
78+
// give access to plugins through the instance
79+
pluginList: SuperTokensPublicPlugin[] = [];
7680
languageTranslations: {
7781
defaultLanguage: string;
7882
userTranslationStore: TranslationStore;
@@ -119,8 +123,28 @@ export default class SuperTokens {
119123
...(this.componentOverrides.authRecipe ?? {}),
120124
...(plugin.generalAuthRecipeComponentOverrides ?? {}),
121125
};
122-
if (plugin.routeHandlers) {
123-
this.pluginRouteHandlers.push(...plugin.routeHandlers);
126+
}
127+
128+
this.pluginList = plugins.map(getPublicPlugin);
129+
130+
// iterated separately so we can pass the instance plugins as reference so they always have access to the latest
131+
for (let pluginIndex = 0; pluginIndex < this.pluginList.length; pluginIndex += 1) {
132+
const pluginInit = plugins[pluginIndex].init;
133+
134+
if (pluginInit && !this.pluginList[pluginIndex].initialized) {
135+
PostSuperTokensInitCallbacks.addPostInitCallback(() => {
136+
pluginInit(config, this.pluginList, package_version);
137+
this.pluginList[pluginIndex].initialized = true;
138+
});
139+
}
140+
141+
const pluginRouteHandlers = plugins[pluginIndex].routeHandlers;
142+
if (pluginRouteHandlers) {
143+
const result = pluginRouteHandlers(config, this.pluginList, package_version);
144+
if (result.status === "ERROR") {
145+
throw new Error(result.message);
146+
}
147+
this.pluginRouteHandlers.push(...result.routeHandlers);
124148
}
125149
}
126150

lib/ts/types.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,13 @@ export type SuperTokensPlugin = {
528528
version?: string;
529529
compatibleAuthReactSDKVersions?: string | string[]; // match the syntax of the engines field in package.json
530530
compatibleWebJSSDKVersions?: string | string[]; // match the syntax of the engines field in package.json
531+
init?: (
532+
config: Omit<SuperTokensConfig, "experimental" | "recipeList">,
533+
allPlugins: Pick<SuperTokensPlugin, "id" | "version" | "exports">[],
534+
sdkVersion: string
535+
) => Promise<void> | void;
531536
dependencies?: (
532-
pluginsAbove: Pick<SuperTokensPlugin, "id" | "version">[],
537+
pluginsAbove: Pick<SuperTokensPlugin, "id" | "version" | "exports">[],
533538
sdkVersion: string
534539
) => { status: "OK"; pluginsToAdd?: SuperTokensPlugin[] } | { status: "ERROR"; message: string };
535540
overrideMap?: {
@@ -538,8 +543,18 @@ export type SuperTokensPlugin = {
538543
};
539544
};
540545
generalAuthRecipeComponentOverrides?: AuthRecipeComponentOverrideMap;
541-
routeHandlers?: PluginRouteHandler[];
546+
routeHandlers?: (
547+
config: Omit<SuperTokensConfig, "experimental" | "recipeList">,
548+
allPlugins: Pick<SuperTokensPlugin, "id" | "version" | "exports">[],
549+
sdkVersion: string
550+
) => { status: "OK"; routeHandlers: PluginRouteHandler[] } | { status: "ERROR"; message: string };
542551
config?: (
543552
config: Omit<SuperTokensConfig, "experimental" | "recipeList">
544553
) => Omit<SuperTokensConfig, "experimental" | "recipeList"> | undefined;
554+
exports?: Record<string, any>;
545555
};
556+
557+
export type SuperTokensPublicPlugin = Pick<
558+
SuperTokensPlugin,
559+
"id" | "version" | "exports" | "compatibleAuthReactSDKVersions" | "compatibleWebJSSDKVersions"
560+
> & { initialized: boolean };

lib/ts/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import type {
3434
NormalisedAppInfo,
3535
NormalisedFormField,
3636
NormalisedGetRedirectionURLContext,
37+
SuperTokensPlugin,
38+
SuperTokensPublicPlugin,
3739
UserContext,
3840
} from "./types";
3941

@@ -553,3 +555,14 @@ export function useRethrowInRender() {
553555

554556
return setError;
555557
}
558+
559+
export function getPublicPlugin(plugin: SuperTokensPlugin): SuperTokensPublicPlugin {
560+
return {
561+
id: plugin.id,
562+
initialized: plugin.init ? false : true, // since the init method is optional, we default to true
563+
version: plugin.version,
564+
exports: plugin.exports,
565+
compatibleAuthReactSDKVersions: plugin.compatibleAuthReactSDKVersions,
566+
compatibleWebJSSDKVersions: plugin.compatibleWebJSSDKVersions,
567+
};
568+
}

0 commit comments

Comments
 (0)