UNPKG

@graphql-markdown/core

Version:

GraphQL-Markdown core package for generating Markdown documentation from a GraphQL schema.

176 lines (175 loc) 5.85 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.loadConfiguration = exports.setLoaderOptions = exports.graphQLConfigExtension = exports.EXTENSION_NAME = void 0; const logger_1 = require("@graphql-markdown/logger"); /** * The name of the GraphQL Markdown extension. * Used to identify the extension in graphql-config. */ exports.EXTENSION_NAME = "graphql-markdown"; /** * GraphQL extension declaration for graphql-config. * * @returns The extension configuration object with name property. * * @example * ```typescript * // In graphql-config setup * const config = await loadConfig({ * extensions: [graphQLConfigExtension], * }); * ``` */ const graphQLConfigExtension = () => { return { name: exports.EXTENSION_NAME }; }; exports.graphQLConfigExtension = graphQLConfigExtension; /** * Sets loader options for GraphQL Markdown loaders. * * This function takes a LoaderOption object and merges the provided options * with any existing options for each loader. * * @param loaders - The loader configuration object. * @param options - The package options to apply to loaders. * @returns The updated loader configuration. * * @example * ```typescript * const loaders = { * TypeScriptLoader: { * module: "@graphql-markdown/typescript-loader", * options: { baseDir: "./src" } * } * }; * const options = { outputDir: "./docs" }; * const updatedLoaders = setLoaderOptions(loaders, options); * // Result: loaders with { baseDir: "./src", outputDir: "./docs" } * ``` */ const setLoaderOptions = (loaders, options) => { for (const loader in loaders) { if (typeof loaders[loader] === "string") { loaders[loader] = { module: loaders[loader], options, }; } else { loaders[loader].options = { ...options, ...loaders[loader].options, }; } } return loaders; }; exports.setLoaderOptions = setLoaderOptions; /** * Loads the GraphQL Markdown configuration from graphql-config. * * This function attempts to load the GraphQL config and extract the * GraphQL Markdown extension configuration for the specified project ID. * It also normalizes schema configurations. * * @param id - The project ID to load configuration for. * @param options - Optional package options to apply. * @param throwOptions - Options for controlling throw behavior. * @returns The extension project configuration if found, otherwise undefined. * * @throws Will throw an error if throwOnMissing or throwOnEmpty is true and * the corresponding condition is met. * * @example * ```typescript * // Basic usage * const config = await loadConfiguration("my-project"); * * // With options and throw behavior * const config = await loadConfiguration( * "my-project", * { baseDir: "./src" }, * { throwOnMissing: true, throwOnEmpty: false } * ); * ``` */ const loadConfiguration = async (id, options, { throwOnMissing, throwOnEmpty } = { throwOnMissing: false, throwOnEmpty: false, }) => { let graphQLConfig; if (typeof id !== "string") { return undefined; } try { graphQLConfig = await Promise.resolve().then(() => __importStar(require("graphql-config"))); } catch { (0, logger_1.log)("Cannot find module 'graphql-config'!"); return undefined; } const config = await graphQLConfig.loadConfig({ ...options, extensions: [exports.graphQLConfigExtension], throwOnMissing, throwOnEmpty, }); if (!config) { return undefined; } try { const projectConfig = config .getProject(id) .extension(exports.EXTENSION_NAME); if (Array.isArray(projectConfig.schema)) { const schema = projectConfig.schema[0]; if (typeof schema === "string") { projectConfig.schema = schema; } if (typeof projectConfig.schema === "object") { projectConfig.schema = Object.keys(schema)[0]; if (typeof projectConfig.loaders !== "undefined") { projectConfig.loaders = (0, exports.setLoaderOptions)(projectConfig.loaders ?? {}, Object.values(schema)[0]); } } } return projectConfig; } catch { return undefined; } }; exports.loadConfiguration = loadConfiguration;