mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-12 05:48:57 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d9b5a898ab | |||
| f127d28675 |
@@ -1,5 +1,13 @@
|
||||
# gitbook
|
||||
|
||||
## 0.7.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- f127d28: Rename OpenAPIModels to OpenAPISchemas
|
||||
- Updated dependencies [f127d28]
|
||||
- @gitbook/react-openapi@1.1.2
|
||||
|
||||
## 0.7.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "gitbook",
|
||||
"version": "0.7.1",
|
||||
"version": "0.7.2",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "env-cmd --silent -f ../../.env.local next dev",
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
@apply flex-1 flex flex-col gap-4 mb-14;
|
||||
}
|
||||
|
||||
.openapi-models {
|
||||
.openapi-schemas {
|
||||
@apply flex flex-col mb-14 flex-1;
|
||||
}
|
||||
|
||||
@@ -620,10 +620,10 @@
|
||||
@apply space-y-2.5;
|
||||
}
|
||||
|
||||
.openapi-section-models {
|
||||
.openapi-section-schemas {
|
||||
@apply border border-tint-subtle rounded-lg;
|
||||
}
|
||||
|
||||
.openapi-section-models > .openapi-section-body > .openapi-schema-properties > .openapi-schema {
|
||||
.openapi-section-schemas > .openapi-section-body > .openapi-schema-properties > .openapi-schema {
|
||||
@apply p-2.5;
|
||||
}
|
||||
|
||||
+10
-10
@@ -1,35 +1,35 @@
|
||||
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 OpenAPISchemasData, resolveOpenAPISchemas } from '@gitbook/react-openapi';
|
||||
import type { AnyOpenAPIBlock, ResolveOpenAPIBlockArgs } from './types';
|
||||
|
||||
type ResolveOpenAPIModelsBlockResult = ResolveOpenAPIBlockResult<OpenAPIModelsData>;
|
||||
type ResolveOpenAPISchemasBlockResult = ResolveOpenAPIBlockResult<OpenAPISchemasData>;
|
||||
|
||||
const weakmap = new WeakMap<AnyOpenAPIBlock, Promise<ResolveOpenAPIModelsBlockResult>>();
|
||||
const weakmap = new WeakMap<AnyOpenAPIBlock, Promise<ResolveOpenAPISchemasBlockResult>>();
|
||||
|
||||
/**
|
||||
* 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(
|
||||
export function resolveOpenAPISchemasBlock(
|
||||
args: ResolveOpenAPIBlockArgs
|
||||
): Promise<ResolveOpenAPIModelsBlockResult> {
|
||||
): Promise<ResolveOpenAPISchemasBlockResult> {
|
||||
if (weakmap.has(args.block)) {
|
||||
return weakmap.get(args.block)!;
|
||||
}
|
||||
|
||||
const result = baseResolveOpenAPIModelsBlock(args);
|
||||
const result = baseResolveOpenAPISchemasBlock(args);
|
||||
weakmap.set(args.block, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve OpenAPI models block.
|
||||
* Resolve OpenAPI schemas block.
|
||||
*/
|
||||
async function baseResolveOpenAPIModelsBlock(
|
||||
async function baseResolveOpenAPISchemasBlock(
|
||||
args: ResolveOpenAPIBlockArgs
|
||||
): Promise<ResolveOpenAPIModelsBlockResult> {
|
||||
): Promise<ResolveOpenAPISchemasBlockResult> {
|
||||
const { context, block } = args;
|
||||
if (!block.data.path || !block.data.method) {
|
||||
return { data: null, specUrl: null };
|
||||
@@ -42,7 +42,7 @@ async function baseResolveOpenAPIModelsBlock(
|
||||
return { data: null, specUrl: null };
|
||||
}
|
||||
|
||||
const data = await resolveOpenAPIModels(filesystem);
|
||||
const data = await resolveOpenAPISchemas(filesystem);
|
||||
|
||||
return { data, specUrl };
|
||||
} catch (error) {
|
||||
@@ -2,7 +2,7 @@ import type { DocumentBlockOpenAPI, DocumentBlockOpenAPIOperation } from '@gitbo
|
||||
import type { Filesystem, OpenAPIParseError, OpenAPIV3xDocument } from '@gitbook/openapi-parser';
|
||||
import type { GitBookAnyContext } from '@v2/lib/context';
|
||||
|
||||
//!!TODO: Add DocumentBlockOpenAPIModels when available in @gitbook/api
|
||||
//!!TODO: Add DocumentBlockOpenAPISchemas when available in @gitbook/api
|
||||
export type AnyOpenAPIBlock = DocumentBlockOpenAPI | DocumentBlockOpenAPIOperation;
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
# @gitbook/react-openapi
|
||||
|
||||
## 1.1.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- f127d28: Rename OpenAPIModels to OpenAPISchemas
|
||||
|
||||
## 1.1.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
"default": "./dist/index.js"
|
||||
}
|
||||
},
|
||||
"version": "1.1.1",
|
||||
"version": "1.1.2",
|
||||
"sideEffects": false,
|
||||
"dependencies": {
|
||||
"@gitbook/openapi-parser": "workspace:*",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export * from './models';
|
||||
export * from './schemas';
|
||||
export * from './OpenAPIOperation';
|
||||
export * from './OpenAPIOperationContext';
|
||||
export * from './resolveOpenAPIOperation';
|
||||
export type { OpenAPIModelsData, OpenAPIOperationData } from './types';
|
||||
export type { OpenAPISchemasData, OpenAPIOperationData } from './types';
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
export * from './OpenAPIModels';
|
||||
export * from './resolveOpenAPIModels';
|
||||
+18
-18
@@ -2,18 +2,18 @@ import clsx from 'clsx';
|
||||
import { OpenAPIDisclosureGroup } from '../OpenAPIDisclosureGroup';
|
||||
import { OpenAPIRootSchema } from '../OpenAPISchema';
|
||||
import { Section, SectionBody } from '../StaticSection';
|
||||
import type { OpenAPIClientContext, OpenAPIContextProps, OpenAPIModelsData } from '../types';
|
||||
import type { OpenAPIClientContext, OpenAPIContextProps, OpenAPISchemasData } from '../types';
|
||||
|
||||
/**
|
||||
* Display OpenAPI Models.
|
||||
* Display OpenAPI Schemas.
|
||||
*/
|
||||
export function OpenAPIModels(props: {
|
||||
export function OpenAPISchemas(props: {
|
||||
className?: string;
|
||||
data: OpenAPIModelsData;
|
||||
data: OpenAPISchemasData;
|
||||
context: OpenAPIContextProps;
|
||||
}) {
|
||||
const { className, data, context } = props;
|
||||
const { models } = data;
|
||||
const { schemas } = data;
|
||||
|
||||
const clientContext: OpenAPIClientContext = {
|
||||
defaultInteractiveOpened: context.defaultInteractiveOpened,
|
||||
@@ -21,30 +21,30 @@ export function OpenAPIModels(props: {
|
||||
blockKey: context.blockKey,
|
||||
};
|
||||
|
||||
if (!models.length) {
|
||||
if (!schemas.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={clsx('openapi-models', className)}>
|
||||
<OpenAPIRootModelsSchema models={models} context={clientContext} />
|
||||
<div className={clsx('openapi-schemas', className)}>
|
||||
<OpenAPIRootSchemasSchema schemas={schemas} context={clientContext} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Root schema for OpenAPI models.
|
||||
* It displays a single model or a disclosure group for multiple models.
|
||||
* Root schema for OpenAPI schemas.
|
||||
* It displays a single model or a disclosure group for multiple schemas.
|
||||
*/
|
||||
function OpenAPIRootModelsSchema(props: {
|
||||
models: OpenAPIModelsData['models'];
|
||||
function OpenAPIRootSchemasSchema(props: {
|
||||
schemas: OpenAPISchemasData['schemas'];
|
||||
context: OpenAPIClientContext;
|
||||
}) {
|
||||
const { models, context } = props;
|
||||
const { schemas, context } = props;
|
||||
|
||||
// If there is only one model, we show it directly.
|
||||
if (models.length === 1) {
|
||||
const schema = models?.[0]?.schema;
|
||||
if (schemas.length === 1) {
|
||||
const schema = schemas?.[0]?.schema;
|
||||
|
||||
if (!schema) {
|
||||
return null;
|
||||
@@ -59,12 +59,12 @@ function OpenAPIRootModelsSchema(props: {
|
||||
);
|
||||
}
|
||||
|
||||
// If there are multiple models, we use a disclosure group to show them all.
|
||||
// If there are multiple schemas, we use a disclosure group to show them all.
|
||||
return (
|
||||
<OpenAPIDisclosureGroup
|
||||
allowsMultipleExpanded
|
||||
icon={context.icons.chevronRight}
|
||||
groups={models.map(({ name, schema }) => ({
|
||||
groups={schemas.map(({ name, schema }) => ({
|
||||
id: name,
|
||||
label: (
|
||||
<div className="openapi-response-tab-content" key={`model-${name}`}>
|
||||
@@ -75,7 +75,7 @@ function OpenAPIRootModelsSchema(props: {
|
||||
{
|
||||
id: 'model',
|
||||
body: (
|
||||
<Section className="openapi-section-models">
|
||||
<Section className="openapi-section-schemas">
|
||||
<SectionBody>
|
||||
<OpenAPIRootSchema schema={schema} context={context} />
|
||||
</SectionBody>
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from './OpenAPISchemas';
|
||||
export * from './resolveOpenAPISchemas';
|
||||
+9
-9
@@ -6,28 +6,28 @@ import {
|
||||
shouldIgnoreEntity,
|
||||
} from '@gitbook/openapi-parser';
|
||||
import { dereferenceFilesystem } from '../dereference';
|
||||
import type { OpenAPIModel, OpenAPIModelsData } from '../types';
|
||||
import type { OpenAPISchema, OpenAPISchemasData } from '../types';
|
||||
|
||||
//!!TODO: We should return only the models that are used in the block. Still a WIP awaiting future work.
|
||||
//!!TODO: We should return only the schemas that are used in the block. Still a WIP awaiting future work.
|
||||
|
||||
/**
|
||||
* Resolve an OpenAPI models from a file and compile it to a more usable format.
|
||||
* Models are extracted from the OpenAPI components.schemas
|
||||
* Resolve an OpenAPI schemas from a file and compile it to a more usable format.
|
||||
* Schemas are extracted from the OpenAPI components.schemas
|
||||
*/
|
||||
export async function resolveOpenAPIModels(
|
||||
export async function resolveOpenAPISchemas(
|
||||
filesystem: Filesystem<OpenAPIV3xDocument>
|
||||
): Promise<OpenAPIModelsData | null> {
|
||||
): Promise<OpenAPISchemasData | null> {
|
||||
const schema = await dereferenceFilesystem(filesystem);
|
||||
|
||||
const models = getOpenAPIComponents(schema);
|
||||
const schemas = getOpenAPIComponents(schema);
|
||||
|
||||
return { models };
|
||||
return { schemas };
|
||||
}
|
||||
|
||||
/**
|
||||
* Get OpenAPI components.schemas that are not ignored.
|
||||
*/
|
||||
function getOpenAPIComponents(schema: OpenAPIV3.Document | OpenAPIV3_1.Document): OpenAPIModel[] {
|
||||
function getOpenAPIComponents(schema: OpenAPIV3.Document | OpenAPIV3_1.Document): OpenAPISchema[] {
|
||||
const schemas = schema.components?.schemas ?? {};
|
||||
return Object.entries(schemas)
|
||||
.filter(([, schema]) => !shouldIgnoreEntity(schema))
|
||||
@@ -56,12 +56,12 @@ export interface OpenAPIOperationData extends OpenAPICustomSpecProperties {
|
||||
securities: [string, OpenAPIV3.SecuritySchemeObject][];
|
||||
}
|
||||
|
||||
export type OpenAPIModel = {
|
||||
export type OpenAPISchema = {
|
||||
name: string;
|
||||
schema: OpenAPIV3.SchemaObject;
|
||||
};
|
||||
|
||||
export interface OpenAPIModelsData {
|
||||
/** Components schemas to be used for models */
|
||||
models: OpenAPIModel[];
|
||||
export interface OpenAPISchemasData {
|
||||
/** Components schemas to be used for schemas */
|
||||
schemas: OpenAPISchema[];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user