-
Notifications
You must be signed in to change notification settings - Fork 4k
Implement OpenAPI models blocks #2908
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
47cabdf
v1 models
nolannbiron 6343c94
Preserver original order in traverse
nolannbiron f64d125
Simplify getOpenAPIComponents
nolannbiron 83e70ba
Fix format
nolannbiron e672724
Fix example and description missing
nolannbiron d6bbe40
wip
nolannbiron ed27fa5
fix styles
nolannbiron 7c26dfa
Remove OpenAPIModelsSchema + align to main
nolannbiron 2bc18ee
Update models
nolannbiron f69cd9e
revert wip
nolannbiron 16d7653
Add biome rule to fix on save
nolannbiron a054457
format
nolannbiron f40e9c2
Fixes
nolannbiron f054610
add todo
nolannbiron 8bf1c10
Enforce typing weakmap
nolannbiron 7fed0e2
Fix typing
nolannbiron 61899e8
Revert changes in Disclosure group
nolannbiron 9334cb8
Refactor resolveOpenAPIModels
nolannbiron 40d5f10
Refactor before review
nolannbiron deddaf3
Fix after review
nolannbiron 08a8ba0
Generic ResolveOpenAPIBlockResult type
nolannbiron 1cd4790
Changeset
nolannbiron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,7 @@ | ||
--- | ||
'@gitbook/openapi-parser': minor | ||
'@gitbook/react-openapi': minor | ||
'gitbook': minor | ||
--- | ||
|
||
Implement OpenAPI models blocks |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
55 changes: 55 additions & 0 deletions
55
packages/gitbook/src/lib/openapi/resolveOpenAPIModelsBlock.ts
This file contains hidden or 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,55 @@ | ||
import { fetchOpenAPIFilesystem } from '@/lib/openapi/fetch'; | ||
import type { ResolveOpenAPIBlockResult } from '@/lib/openapi/types'; | ||
import { OpenAPIParseError } from '@gitbook/openapi-parser'; | ||
import { type OpenAPIModelsData, resolveOpenAPIModels } from '@gitbook/react-openapi'; | ||
import type { AnyOpenAPIBlock, ResolveOpenAPIBlockArgs } from './types'; | ||
|
||
type ResolveOpenAPIModelsBlockResult = ResolveOpenAPIBlockResult<OpenAPIModelsData>; | ||
|
||
const weakmap = new WeakMap<AnyOpenAPIBlock, Promise<ResolveOpenAPIModelsBlockResult>>(); | ||
|
||
/** | ||
* Cache the result of resolving an OpenAPI block. | ||
* It is important because the resolve is called in sections and in the block itself. | ||
*/ | ||
export function resolveOpenAPIModelsBlock( | ||
args: ResolveOpenAPIBlockArgs | ||
): Promise<ResolveOpenAPIModelsBlockResult> { | ||
if (weakmap.has(args.block)) { | ||
return weakmap.get(args.block)!; | ||
} | ||
|
||
const result = baseResolveOpenAPIModelsBlock(args); | ||
weakmap.set(args.block, result); | ||
return result; | ||
} | ||
|
||
/** | ||
* Resolve OpenAPI models block. | ||
*/ | ||
async function baseResolveOpenAPIModelsBlock( | ||
args: ResolveOpenAPIBlockArgs | ||
): Promise<ResolveOpenAPIModelsBlockResult> { | ||
const { context, block } = args; | ||
if (!block.data.path || !block.data.method) { | ||
return { data: null, specUrl: null }; | ||
} | ||
|
||
try { | ||
const { filesystem, specUrl } = await fetchOpenAPIFilesystem({ block, context }); | ||
|
||
if (!filesystem || !specUrl) { | ||
return { data: null, specUrl: null }; | ||
} | ||
|
||
const data = await resolveOpenAPIModels(filesystem); | ||
|
||
return { data, specUrl }; | ||
} catch (error) { | ||
if (error instanceof OpenAPIParseError) { | ||
return { error }; | ||
} | ||
|
||
throw error; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
packages/gitbook/src/lib/openapi/resolveOpenAPIOperationBlock.ts
This file contains hidden or 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,57 @@ | ||
import { fetchOpenAPIFilesystem } from '@/lib/openapi/fetch'; | ||
import { OpenAPIParseError } from '@gitbook/openapi-parser'; | ||
import { type OpenAPIOperationData, resolveOpenAPIOperation } from '@gitbook/react-openapi'; | ||
import type { AnyOpenAPIBlock, ResolveOpenAPIBlockArgs, ResolveOpenAPIBlockResult } from './types'; | ||
|
||
type ResolveOpenAPIOperationBlockResult = ResolveOpenAPIBlockResult<OpenAPIOperationData>; | ||
|
||
const weakmap = new WeakMap<AnyOpenAPIBlock, Promise<ResolveOpenAPIOperationBlockResult>>(); | ||
|
||
/** | ||
* Cache the result of resolving an OpenAPI block. | ||
* It is important because the resolve is called in sections and in the block itself. | ||
*/ | ||
export function resolveOpenAPIOperationBlock( | ||
args: ResolveOpenAPIBlockArgs | ||
): Promise<ResolveOpenAPIOperationBlockResult> { | ||
if (weakmap.has(args.block)) { | ||
return weakmap.get(args.block)!; | ||
} | ||
|
||
const result = baseResolveOpenAPIOperationBlock(args); | ||
weakmap.set(args.block, result); | ||
return result; | ||
} | ||
|
||
/** | ||
* Resolve OpenAPI operation block. | ||
*/ | ||
async function baseResolveOpenAPIOperationBlock( | ||
args: ResolveOpenAPIBlockArgs | ||
): Promise<ResolveOpenAPIOperationBlockResult> { | ||
const { context, block } = args; | ||
if (!block.data.path || !block.data.method) { | ||
return { data: null, specUrl: null }; | ||
} | ||
|
||
try { | ||
const { filesystem, specUrl } = await fetchOpenAPIFilesystem({ block, context }); | ||
|
||
if (!filesystem) { | ||
return { data: null, specUrl: null }; | ||
} | ||
|
||
const data = await resolveOpenAPIOperation(filesystem, { | ||
path: block.data.path, | ||
method: block.data.method, | ||
}); | ||
|
||
return { data, specUrl }; | ||
} catch (error) { | ||
if (error instanceof OpenAPIParseError) { | ||
return { error }; | ||
} | ||
|
||
throw error; | ||
} | ||
} |
This file contains hidden or 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,50 @@ | ||
import type { DocumentBlockOpenAPI, DocumentBlockOpenAPIOperation } from '@gitbook/api'; | ||
import type { Filesystem, OpenAPIParseError, OpenAPIV3xDocument } from '@gitbook/openapi-parser'; | ||
import type { GitBookAnyContext } from '@v2/lib/context'; | ||
|
||
//!!TODO: Add DocumentBlockOpenAPIModels when available in @gitbook/api | ||
export type AnyOpenAPIBlock = DocumentBlockOpenAPI | DocumentBlockOpenAPIOperation; | ||
|
||
/** | ||
* Arguments for resolving OpenAPI block. | ||
*/ | ||
export type ResolveOpenAPIBlockArgs = { | ||
block: AnyOpenAPIBlock; | ||
context: GitBookAnyContext; | ||
}; | ||
|
||
/** | ||
* Fetch OpenAPI filesystem result. | ||
*/ | ||
export type FetchOpenAPIFilesystemResult = | ||
| { | ||
error?: undefined; | ||
filesystem: Filesystem<OpenAPIV3xDocument> | null; | ||
specUrl: string | null; | ||
} | ||
| FetchOpenAPIFilesystemError; | ||
|
||
/** | ||
* Fetch OpenAPI filesystem error. | ||
*/ | ||
type FetchOpenAPIFilesystemError = { | ||
error: OpenAPIParseError; | ||
filesystem?: undefined; | ||
specUrl?: undefined; | ||
}; | ||
|
||
/** | ||
* Resolved OpenAPI block result. | ||
*/ | ||
export type ResolveOpenAPIBlockResult<T> = | ||
| { error?: undefined; data: T | null; specUrl: string | null } | ||
| ResolveOpenAPIBlockError; | ||
|
||
/** | ||
* Resolved OpenAPI block error. | ||
*/ | ||
type ResolveOpenAPIBlockError = { | ||
error: OpenAPIParseError; | ||
data?: undefined; | ||
specUrl?: undefined; | ||
}; |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.