Skip to content

Commit 33cf199

Browse files
committed
feat: enhance public config handling with normalized app info and improve types
1 parent 56dfed5 commit 33cf199

File tree

6 files changed

+53
-40
lines changed

6 files changed

+53
-40
lines changed

lib/build/genericComponentOverrideContext.js

Lines changed: 13 additions & 7 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: 9 additions & 1 deletion
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: 2 additions & 6 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: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export default class SuperTokens {
101101
*/
102102
constructor(config: SuperTokensConfig, plugins: SuperTokensPlugin[]) {
103103
this.appInfo = normaliseInputAppInfoOrThrowError(config.appInfo);
104+
104105
if (config.recipeList === undefined || config.recipeList.length === 0) {
105106
throw new Error(
106107
"Please provide at least one recipe to the supertokens.init function call. See https://supertokens.io/docs/emailpassword/quick-setup/frontend"
@@ -127,13 +128,16 @@ export default class SuperTokens {
127128

128129
this.pluginList = plugins.map(getPublicPlugin);
129130

131+
const publicConfig = getPublicConfig({
132+
...config,
133+
appInfo: this.appInfo,
134+
});
130135
// iterated separately so we can pass the instance plugins as reference so they always have access to the latest
131136
for (let pluginIndex = 0; pluginIndex < this.pluginList.length; pluginIndex += 1) {
132137
const pluginInit = plugins[pluginIndex].init;
133-
134138
if (pluginInit && !this.pluginList[pluginIndex].initialized) {
135139
PostSuperTokensInitCallbacks.addPostInitCallback(() => {
136-
pluginInit(getPublicConfig(config), this.pluginList, package_version);
140+
pluginInit(publicConfig, this.pluginList, package_version);
137141
this.pluginList[pluginIndex].initialized = true;
138142
});
139143
}
@@ -142,7 +146,7 @@ export default class SuperTokens {
142146
if (pluginRouteHandlers) {
143147
let handlers: PluginRouteHandler[] = [];
144148
if (typeof pluginRouteHandlers === "function") {
145-
const result = pluginRouteHandlers(getPublicConfig(config), this.pluginList, package_version);
149+
const result = pluginRouteHandlers(publicConfig, this.pluginList, package_version);
146150
if (result.status === "ERROR") {
147151
throw new Error(result.message);
148152
}
@@ -204,6 +208,12 @@ export default class SuperTokens {
204208
? config.recipeList
205209
: config.recipeList.concat(Multitenancy.init({}));
206210

211+
const normalisedAppInfo = normaliseInputAppInfoOrThrowError(config.appInfo);
212+
const publicConfig = getPublicConfig({
213+
...config,
214+
appInfo: normalisedAppInfo,
215+
});
216+
207217
const finalPluginList: SuperTokensPlugin[] = [];
208218
if (config.experimental?.plugins) {
209219
for (const plugin of config.experimental.plugins) {
@@ -220,9 +230,10 @@ export default class SuperTokens {
220230
);
221231
}
222232
}
233+
223234
if (plugin.dependencies) {
224235
const result = plugin.dependencies(
225-
getPublicConfig(config),
236+
publicConfig,
226237
finalPluginList.map(getPublicPlugin),
227238
package_version
228239
);
@@ -233,6 +244,7 @@ export default class SuperTokens {
233244
finalPluginList.push(...result.pluginsToAdd);
234245
}
235246
}
247+
236248
finalPluginList.push(plugin);
237249
}
238250
}
@@ -246,10 +258,9 @@ export default class SuperTokens {
246258

247259
for (const plugin of finalPluginList) {
248260
if (plugin.config) {
249-
// prevent override of appInfo
250-
// doing it like this because we don't want to override the appInfo and we can't make sure the plugin won't return it
251-
// @ts-ignore
252-
const { appInfo, ...pluginConfig } = plugin.config(getPublicConfig(config)) || {};
261+
// @ts-ignore we don't want to override the appInfo and we can't make sure the plugin won't return it
262+
const { appInfo, ...pluginConfig } =
263+
plugin.config(getPublicConfig({ ...config, appInfo: normalisedAppInfo })) || {};
253264

254265
config = { ...config, ...pluginConfig };
255266
}

lib/ts/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ export type SuperTokensPlugin = {
547547
sdkVersion: string
548548
) => { status: "OK"; routeHandlers: PluginRouteHandler[] } | { status: "ERROR"; message: string })
549549
| PluginRouteHandler[];
550+
550551
config?: (config: SuperTokensPublicConfig) => Omit<SuperTokensPublicConfig, "appInfo"> | undefined;
551552
exports?: Record<string, any>;
552553
};
@@ -555,9 +556,14 @@ export const nonPublicConfigProperties = ["experimental"] as const;
555556

556557
export type NonPublicConfigPropertiesType = (typeof nonPublicConfigProperties)[number];
557558

559+
export type SuperTokensConfigWithNormalisedAppInfo = Omit<SuperTokensConfig, "appInfo"> & {
560+
appInfo: NormalisedAppInfo;
561+
};
558562
export type SuperTokensPublicPlugin = Pick<
559563
SuperTokensPlugin,
560564
"id" | "version" | "exports" | "compatibleAuthReactSDKVersions" | "compatibleWebJSSDKVersions"
561565
> & { initialized: boolean };
562566

563-
export type SuperTokensPublicConfig = Omit<SuperTokensConfig, NonPublicConfigPropertiesType>;
567+
export type SuperTokensPublicConfig = Omit<Omit<SuperTokensConfig, NonPublicConfigPropertiesType>, "appInfo"> & {
568+
appInfo: NormalisedAppInfo;
569+
};

lib/ts/utils.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ import type {
3434
NormalisedAppInfo,
3535
NormalisedFormField,
3636
NormalisedGetRedirectionURLContext,
37-
SuperTokensConfig,
3837
SuperTokensPlugin,
3938
SuperTokensPublicConfig,
4039
SuperTokensPublicPlugin,
4140
UserContext,
4241
NonPublicConfigPropertiesType,
42+
SuperTokensConfigWithNormalisedAppInfo,
4343
} from "./types";
4444
import { nonPublicConfigProperties } from "./types";
4545

@@ -571,8 +571,8 @@ export function getPublicPlugin(plugin: SuperTokensPlugin): SuperTokensPublicPlu
571571
};
572572
}
573573

574-
export function getPublicConfig(config: SuperTokensConfig): SuperTokensPublicConfig {
575-
const configKeys = Object.keys(config) as (keyof SuperTokensConfig)[];
574+
export function getPublicConfig(config: SuperTokensConfigWithNormalisedAppInfo): SuperTokensPublicConfig {
575+
const configKeys = Object.keys(config) as (keyof SuperTokensConfigWithNormalisedAppInfo)[];
576576

577577
const publicConfig = configKeys.reduce((acc, key) => {
578578
if (nonPublicConfigProperties.includes(key as NonPublicConfigPropertiesType)) {
@@ -584,17 +584,3 @@ export function getPublicConfig(config: SuperTokensConfig): SuperTokensPublicCon
584584

585585
return publicConfig;
586586
}
587-
588-
export function getNonPublicConfig(config: SuperTokensConfig): Pick<SuperTokensConfig, NonPublicConfigPropertiesType> {
589-
const configKeys = Object.keys(config) as (keyof SuperTokensConfig)[];
590-
591-
const publicConfig = configKeys.reduce((acc, key) => {
592-
if (nonPublicConfigProperties.includes(key as NonPublicConfigPropertiesType)) {
593-
return { ...acc, [key]: config[key] };
594-
} else {
595-
return acc;
596-
}
597-
}, {} as Pick<SuperTokensConfig, NonPublicConfigPropertiesType>);
598-
599-
return publicConfig;
600-
}

0 commit comments

Comments
 (0)