-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Graft over shared libs from jy95 to add support for resolving $ref
Most of these TS files are grafted over verbatim from the jy95/docusaurus-json-schema-plugin repository as it provides a succinct way of acquiring and then rendering nested JSON schemas Signed-off-by: Jeremy Ho <jujaga@gmail.com>
- Loading branch information
Showing
6 changed files
with
93 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Example of basePath = '/schemas/examples/refs' | ||
export default function generatePath(path: string, basePath: string = '') { | ||
const parts = path.toString().split('/'); | ||
let finalPath = basePath; | ||
|
||
for (let i = 0; i < parts.length; i++) { | ||
if (parts[i] === '..') { | ||
// Move up one directory in the base path | ||
finalPath = finalPath.split('/').slice(0, -1).join('/'); | ||
} else if (parts[i] === '.') { | ||
// Ignore current directory notation | ||
continue; | ||
} else { | ||
// Append the current directory to the base path | ||
finalPath = `${finalPath}/${parts[i]}`; | ||
} | ||
} | ||
|
||
return finalPath; | ||
} |
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,34 @@ | ||
import LocalFileResolver from '@site/src/components/shared/localFileResolver'; | ||
import RemoteResolver from '@site/src/components/shared/remoteResolver'; | ||
|
||
type Params = { | ||
basePath?: string; | ||
jsonPointer?: string; | ||
remote?: boolean; | ||
}; | ||
|
||
export default function generateResolverOptions(params: Params) { | ||
const { basePath, jsonPointer, remote } = params; | ||
|
||
let config = {}; | ||
|
||
if (basePath) { | ||
config['resolvers'] = { | ||
file: LocalFileResolver(basePath) | ||
}; | ||
} | ||
|
||
if (remote) { | ||
if (config['resolvers'] === undefined) { | ||
config['resolvers'] = {}; | ||
} | ||
config['resolvers']['http'] = RemoteResolver('http'); | ||
config['resolvers']['https'] = RemoteResolver('https'); | ||
} | ||
|
||
if (jsonPointer) { | ||
config['jsonPointer'] = jsonPointer; | ||
} | ||
|
||
return config; | ||
} |
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,4 @@ | ||
export { default as generatePath } from './generatePath'; | ||
export { default as generateResolverOptions } from './generateResolverOptions'; | ||
export { default as localFileResolver } from './localFileResolver'; | ||
export { default as remoteResolver } from './remoteResolver'; |
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,16 @@ | ||
import generatePath from '@site/src/components/shared/generatePath' | ||
|
||
// Here a workaround for Docusaurus, as your assets are public at the end, require them | ||
export default function LocalFileResolver(basePath: string = '') { | ||
return { | ||
resolve: (ref: string) => { | ||
return new Promise((resolve, reject) => { | ||
const temp_url = generatePath(ref, basePath) | ||
//import("@site/static/schemas/examples/array/additionalItems1.json") | ||
import(`@site/static/${temp_url.substring(1)}`) | ||
.then((result) => resolve(result.default)) | ||
.catch((err) => reject(err)) | ||
}) | ||
}, | ||
} | ||
} |
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,18 @@ | ||
type Param = 'http' | 'https'; | ||
|
||
export default function LocalFileResolver(_type: Param = 'http') { | ||
return { | ||
resolve: (ref: string) => { | ||
return new Promise((resolve, reject) => { | ||
fetch(ref.toString(), { | ||
headers: { | ||
Accept: 'application/json' | ||
} | ||
}) | ||
.then((response) => response.json()) | ||
.then((result) => resolve(result)) | ||
.catch((err) => reject(err)); | ||
}); | ||
} | ||
}; | ||
} |