Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom loader to be provided #161

Open
jammi opened this issue May 23, 2019 · 1 comment
Open

Allow custom loader to be provided #161

jammi opened this issue May 23, 2019 · 1 comment

Comments

@jammi
Copy link

jammi commented May 23, 2019

My use-case is that I have an object of objects, for which there are no real correspending files (let's say they come from a database), let's say:

const oaData = {
  'index.foo': {openapi: '3.0.2', ...},
  'schemas.foo': {...}
};

and my refs are like:

{ $ref: 'schemas.foo#/components/schemas/foofoo' }

When json-refs is used with it like this, there's no real way to fetch such references with a processContent function because PathLoader will try to resolve to process.cwd() and of course won't find such files.

What I suggest is providing an option for a simple function that gets an unmangled uri and then either returns an object (or error) as-is (synchronous), or returns a promise (asynchronous). You could name it customLoader or somesuch.

It could even have some matcher function to provide a way to validate whether to use the customLoader or fall back to PathLoader ending up with a simplest use-case (using the oaData from above) such as:

resolveRefs(oaData['index.foo'], {
  useCustomLoader: uri => !!oaData[uri],
  customLoader: uri => oaData[uri]
}).then(buildAPI);

or let's say from an async database loader such as:

resolveRefs(oaIndex, {
  overrideLoader: uri.startsWith('mydb://'),
  customLoader: uri => dbLoader(uri).then(res => JSON.parse(res.oa_data))
}).then(buildAPI);
@whitlockjc
Copy link
Owner

I guess it's time I get this supported: whitlockjc/path-loader#1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants