import type {
ApolloServerPlugin,
BaseContext,
} from '../../../externalTypes/index.js';
import type { ImplicitlyInstallablePlugin } from '../../../ApolloServer.js';
import type {
ApolloServerPluginLandingPageLocalDefaultOptions,
ApolloServerPluginLandingPageProductionDefaultOptions,
LandingPageConfig,
} from './types.js';
import {
getEmbeddedExplorerHTML,
getEmbeddedSandboxHTML,
} from './getEmbeddedHTML.js';
import { packageVersion } from '../../../generated/packageVersion.js';
import { createHash } from '@apollo/utils.createhash';
import { v4 as uuidv4 } from 'uuid';
export type {
ApolloServerPluginLandingPageLocalDefaultOptions,
ApolloServerPluginLandingPageProductionDefaultOptions,
};
export function ApolloServerPluginLandingPageLocalDefault(
options: ApolloServerPluginLandingPageLocalDefaultOptions = {},
): ApolloServerPlugin {
const { version, __internal_apolloStudioEnv__, ...rest } = {
// we default to Sandbox unless embed is specified as false
embed: true as const,
...options,
};
return ApolloServerPluginLandingPageDefault(version, {
isProd: false,
apolloStudioEnv: __internal_apolloStudioEnv__,
...rest,
});
}
export function ApolloServerPluginLandingPageProductionDefault(
options: ApolloServerPluginLandingPageProductionDefaultOptions = {},
): ApolloServerPlugin {
const { version, __internal_apolloStudioEnv__, ...rest } = options;
return ApolloServerPluginLandingPageDefault(version, {
isProd: true,
apolloStudioEnv: __internal_apolloStudioEnv__,
...rest,
});
}
// A triple encoding! Wow! First we use JSON.stringify to turn our object into a
// string. Then we encodeURIComponent so we don't have to stress about what
// would happen if the config contained ``. Finally, we JSON.stringify
// it again, which in practice just wraps it in a pair of double quotes (since
// there shouldn't be any backslashes left after encodeURIComponent). The
// consumer of this needs to decodeURIComponent and then JSON.parse; there's
// only one JSON.parse because the outermost JSON string is parsed by the JS
// parser itself.
function encodeConfig(config: LandingPageConfig): string {
return JSON.stringify(encodeURIComponent(JSON.stringify(config)));
}
const getNonEmbeddedLandingPageHTML = (
cdnVersion: string,
config: LandingPageConfig,
apolloServerVersion: string,
nonce: string,
) => {
const encodedConfig = encodeConfig(config);
return `
Welcome to Apollo Server
The full landing page cannot be loaded; it appears that you might be offline.