Skip to content

Commit

Permalink
add externalrefresolver to loadconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
tatomyr committed Dec 13, 2023
1 parent 3eafa4b commit 53450f8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
6 changes: 2 additions & 4 deletions packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ import {
stringifyYaml,
isAbsoluteUrl,
loadConfig,
Document,
Region,
Config,
Oas3Definition,
Oas2Definition,
RedoclyClient,
} from '@redocly/openapi-core';
import { ConfigValidationError } from '@redocly/openapi-core/lib/config';
import {
Totals,
outputExtensions,
Expand All @@ -37,7 +35,7 @@ import { isEmptyObject } from '@redocly/openapi-core/lib/utils';
import { Arguments } from 'yargs';
import { version } from './update-version-notifier';
import { DESTINATION_REGEX } from './commands/push';
import type { ResolvedRefMap } from '@redocly/openapi-core/lib/resolve';
import type { ConfigValidationError, RawConfigProcessor } from '@redocly/openapi-core/lib/config';

export async function getFallbackApisOrExit(
argsApis: string[] | undefined,
Expand Down Expand Up @@ -426,7 +424,7 @@ export async function loadConfigAndHandleErrors(
options: {
configPath?: string;
customExtends?: string[];
processRawConfig?: (rawConfig: Document, resolvedRefMap: ResolvedRefMap) => void | Promise<void>;
processRawConfig?: RawConfigProcessor;
files?: string[];
region?: Region;
} = {}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function resolveConfigFile(opts: {
} & BundleOptions) {
const {
ref,
externalRefResolver = new BaseResolver(/* opts.config.resolve */),
externalRefResolver = new BaseResolver(),
base = null,
} = opts;

Expand Down
29 changes: 17 additions & 12 deletions packages/core/src/config/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { RawConfig, RawUniversalConfig, Region } from './types';
import { RegionalTokenWithValidity } from '../redocly/redocly-client-types';
import {bundleConfig, resolveConfigFile} from '../bundle';
import { Document } from '../resolve';
import type { ResolvedRefMap } from '../resolve';
import type { BaseResolver, ResolvedRefMap } from '../resolve';

async function addConfigMetadata({
rawConfig,
Expand Down Expand Up @@ -69,20 +69,23 @@ async function addConfigMetadata({
);
}

export type RawConfigProcessor = (
rawConfig: Document,
resolvedRefMap: ResolvedRefMap
) => void | Promise<void>

export async function loadConfig(
options: {
configPath?: string;
customExtends?: string[];
processRawConfig?: (
rawConfig: Document,
resolvedRefMap: ResolvedRefMap
) => void | Promise<void>;
processRawConfig?: RawConfigProcessor;
externalRefResolver?: BaseResolver;
files?: string[];
region?: Region;
} = {}
): Promise<Config> {
const { configPath = findConfig(), customExtends, processRawConfig, files, region } = options;
const rawConfig = await getConfig(configPath, processRawConfig);
const { configPath = findConfig(), customExtends, processRawConfig, files, region , externalRefResolver} = options;
const rawConfig = await getConfig({configPath, processRawConfig, externalRefResolver});

const redoclyClient = new RedoclyClient();
const tokens = await redoclyClient.getTokens();
Expand Down Expand Up @@ -114,15 +117,17 @@ export function findConfig(dir?: string): string | undefined {
return existingConfigFiles[0];
}

export async function getConfig(
configPath: string | undefined = findConfig(),
processRawConfig?: (rawConfig: Document, resolvedRefMap: ResolvedRefMap) => void | Promise<void>
): Promise<RawConfig> {
export async function getConfig(options: {
configPath: string | undefined ;
processRawConfig?: RawConfigProcessor;
externalRefResolver?: BaseResolver;
}): Promise<RawConfig> {
const { configPath = findConfig(), processRawConfig, externalRefResolver } = options;
if (!configPath || !doesYamlFileExist(configPath)) return {};
try {
// const rawConfig =
// (await loadYaml<RawConfig & DeprecatedInRawConfig & FlatRawConfig>(configPath)) || {};
const { document, resolvedRefMap } = await resolveConfigFile({ ref: configPath, config: {} as any });
const { document, resolvedRefMap } = await resolveConfigFile({ ref: configPath, config: {} as any, externalRefResolver });
if (typeof processRawConfig === 'function') {
await processRawConfig(document, resolvedRefMap);
}
Expand Down

0 comments on commit 53450f8

Please sign in to comment.