-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reference): add code infrastructure for bundle feature (#3488)
Refs #692
- Loading branch information
Showing
39 changed files
with
573 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { isEmpty, propEq } from 'ramda'; | ||
import { ParseResultElement } from '@swagger-api/apidom-core'; | ||
|
||
import File from '../util/File'; | ||
import * as plugins from '../util/plugins'; | ||
import UnmatchedBundleStrategyError from '../errors/UnmatchedBundleStrategyError'; | ||
import BundleError from '../errors/BundleError'; | ||
import { ReferenceOptions as IReferenceOptions } from '../types'; | ||
import parse from '../parse'; | ||
import { merge as mergeOptions } from '../options/util'; | ||
import * as url from '../util/url'; | ||
|
||
/** | ||
* Bundle a file with all its external references to a compound document. | ||
*/ | ||
const bundle = async (uri: string, options: IReferenceOptions): Promise<ParseResultElement> => { | ||
const { refSet } = options.bundle; | ||
const sanitizedURI = url.sanitize(uri); | ||
const mergedOptions = mergeOptions(options, { resolve: { baseURI: sanitizedURI } }); | ||
let parseResult; | ||
|
||
// if refSet was provided, use it to avoid unnecessary parsing | ||
if (refSet !== null && refSet.has(sanitizedURI)) { | ||
// @ts-ignore | ||
({ value: parseResult } = refSet.find(propEq(sanitizedURI, 'uri'))); | ||
} else { | ||
parseResult = await parse(uri, mergedOptions); | ||
} | ||
|
||
const file = File({ | ||
uri: mergedOptions.resolve.baseURI, | ||
parseResult, | ||
mediaType: mergedOptions.parse.mediaType, | ||
}); | ||
|
||
const bundleStrategies = await plugins.filter('canBundle', file, mergedOptions.bundle.strategies); | ||
|
||
// we couldn't find any bundle strategy for this File | ||
if (isEmpty(bundleStrategies)) { | ||
throw new UnmatchedBundleStrategyError(file.uri); | ||
} | ||
|
||
try { | ||
const { result } = await plugins.run('bundle', [file, mergedOptions], bundleStrategies); | ||
return result; | ||
} catch (error: any) { | ||
throw new BundleError(`Error while bundling file "${file.uri}"`, { cause: error }); | ||
} | ||
}; | ||
|
||
export default bundle; |
23 changes: 23 additions & 0 deletions
23
packages/apidom-reference/src/bundle/strategies/BundleStrategy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import stampit from 'stampit'; | ||
import { NotImplementedError } from '@swagger-api/apidom-error'; | ||
|
||
import { BundleStrategy as IBundleStrategy } from '../../types'; | ||
|
||
const BundleStrategy: stampit.Stamp<IBundleStrategy> = stampit({ | ||
props: { | ||
name: null, | ||
}, | ||
methods: { | ||
canBundle() { | ||
return false; | ||
}, | ||
|
||
async bundle(): Promise<never> { | ||
throw new NotImplementedError( | ||
'bundle method in BundleStrategy stamp is not yet implemented.', | ||
); | ||
}, | ||
}, | ||
}); | ||
|
||
export default BundleStrategy; |
Oops, something went wrong.