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

Docs: add more entity selectors #3620

Merged
merged 6 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export default async function Page({

api.products.list(
{
organizationId: [organization.id],
organizationId: organization.id,
isRecurring: true,
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default async function Layout({
api.products
.list(
{
organizationId: [organization.id],
organizationId: organization.id,
isRecurring: true,
},
cacheConfig,
Expand Down
2 changes: 1 addition & 1 deletion clients/apps/web/src/app/[organization]/(sidebar)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export default async function Page({
),
api.products.list(
{
organizationId: [organization.id],
organizationId: organization.id,
isRecurring: true,
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export default async function Page({

try {
products = await api.products.list(
{ organizationId: [article.organization_id], isRecurring: true },
{ organizationId: article.organization_id, isRecurring: true },
cacheConfig,
)
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export default async function Page({
}

const products = await api.products.list(
{ organizationId: [organization.id], isRecurring: false },
{ organizationId: organization.id, isRecurring: false },
{
...cacheConfig,
next: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export default async function Page({
)
products = await api.products.list(
{
organizationId: [organization.id],
organizationId: organization.id,
isRecurring: true,
limit: 100,
},
Expand Down
2 changes: 1 addition & 1 deletion clients/apps/web/src/app/[organization]/[repo]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default async function Page({
}),
api.products.list(
{
organizationId: [repository.organization.id],
organizationId: repository.organization.id,
isRecurring: true,
},
cacheConfig,
Expand Down
2 changes: 1 addition & 1 deletion clients/apps/web/src/app/[organization]/subscribe/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default async function Page({
)
products = await api.products.list(
{
organizationId: [organization.id],
organizationId: organization.id,
isRecurring: true,
},
cacheConfig,
Expand Down
8 changes: 7 additions & 1 deletion clients/apps/web/src/components/Documentation/BodySchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ export const BodySchema = ({
</AnchoredElement>

<div className="flex flex-col gap-y-4">
<Schema schema={schema} idPrefix={['body']} showRequired showDefault />
<Schema
schema={schema}
idPrefix={['body']}
showRequired
showDefault
showWidgets
/>
</div>
</div>
)
Expand Down
14 changes: 12 additions & 2 deletions clients/apps/web/src/components/Documentation/ParameterWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
getParameterName,
getUnionSchemas,
hasSelectorWidget,
isArraySchema,
isDereferenced,
resolveSchemaMinMax,
} from './openapi'
Expand Down Expand Up @@ -238,7 +239,6 @@ const GenericWidget = ({
}

if (schema.type === 'array' && isDereferenced(schema.items)) {
console.log(schema, schema.items)
return (
<ParameterWidget
schema={schema.items}
Expand Down Expand Up @@ -302,9 +302,19 @@ const UnionParameterWidget = ({
const nullSchema = schemas.find((schema) => schema.type === 'null')
const nonNullSchemas = schemas.filter((schema) => schema.type !== 'null')

// For schemas that are like `uuid | uuid[]`, we don't want the widget twice
// So we remove the array schema if there is a non-array schema with the same type
const deduplicatedArraySchemas = nonNullSchemas.filter(
(schema) =>
!isArraySchema(schema) ||
!nonNullSchemas.some(
(s) => isDereferenced(schema.items) && s.type === schema.items.type,
),
)

return (
<div className="flex flex-row items-center gap-2">
{nonNullSchemas.map((schema, index) => (
{deduplicatedArraySchemas.map((schema, index) => (
<ParameterWidget
key={index}
schema={schema}
Expand Down
158 changes: 103 additions & 55 deletions clients/apps/web/src/components/Documentation/Schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,27 @@ import PropertyDefault from './PropertyDefault'
import PropertyType from './PropertyType'
import ProseWrapper from './ProseWrapper'
import RequiredBadge from './RequiredBadge'
import { getUnionSchemas, isDereferenced } from './openapi'
import {
getUnionSchemas,
isArraySchema,
isDereferenced,
isScalarArraySchema,
} from './openapi'

const UnionSchema = ({
schemas: _schemas,
idPrefix,
parentsProperties,
showRequired,
showDefault,
showWidgets,
}: {
schemas: OpenAPIV3_1.SchemaObject[]
idPrefix: string[]
parentsProperties: string[]
showRequired?: boolean
showDefault?: boolean
showWidgets?: boolean
}) => {
const schemas = _schemas.filter(isDereferenced)
const schemaValues = schemas.map((schema, index) =>
Expand Down Expand Up @@ -58,6 +65,7 @@ const UnionSchema = ({
parentsProperties={parentsProperties}
showRequired={showRequired}
showDefault={showDefault}
showWidgets={showWidgets}
/>
</div>
</TabsContent>
Expand All @@ -66,13 +74,91 @@ const UnionSchema = ({
)
}

const SchemaProperty = ({
name,
property,
required,
idPrefix,
parentsProperties,
showRequired,
showDefault,
showWidgets,
}: {
name: string
property: OpenAPIV3_1.SchemaObject
required: boolean
idPrefix: string[]
parentsProperties: string[]
showRequired?: boolean
showDefault?: boolean
showWidgets?: boolean
}) => {
return (
<ParameterItem>
<AnchoredElement id={[...idPrefix, name]}>
<div className="flex flex-row items-center gap-x-3">
<span className="font-mono font-medium text-black dark:text-white">
{name}
</span>
<PropertyType property={property} />
{showRequired && (
<>{required ? <RequiredBadge /> : <OptionalBadge />}</>
)}
{showDefault && <PropertyDefault property={property} />}
</div>
</AnchoredElement>
{property.description && (
<ProseWrapper className="text-sm">
<Markdown>{property.description}</Markdown>
</ProseWrapper>
)}
{showWidgets &&
property.type != 'object' &&
(!isArraySchema(property) || isScalarArraySchema(property)) && (
<ParameterWidget
schema={property}
parameterName={[...parentsProperties, name].join('.')}
parameterIn="body"
/>
)}
{property.type == 'object' && (
<div className="rounded-md border border-gray-200 p-4 dark:border-gray-800">
<Schema
schema={property}
idPrefix={[...idPrefix, name]}
parentsProperties={[...parentsProperties, name]}
showRequired={showRequired}
showDefault={showDefault}
showWidgets={showWidgets}
/>
</div>
)}
{isArraySchema(property) &&
!isScalarArraySchema(property) &&
isDereferenced(property.items) && (
<div className="rounded-md border border-gray-200 p-4 dark:border-gray-800">
<Schema
schema={property.items}
idPrefix={[...idPrefix, name]}
parentsProperties={[...parentsProperties, name]}
showRequired={showRequired}
showDefault={showDefault}
showWidgets={showWidgets}
/>
</div>
)}
</ParameterItem>
)
}

const SchemaProperties = ({
properties,
required,
idPrefix,
parentsProperties,
showRequired,
showDefault,
showWidgets,
}: {
properties: {
[name: string]: OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.SchemaObject
Expand All @@ -82,6 +168,7 @@ const SchemaProperties = ({
parentsProperties: string[]
showRequired?: boolean
showDefault?: boolean
showWidgets?: boolean
}) => {
return (
<div className="flex flex-col gap-y-4">
Expand All @@ -90,60 +177,17 @@ const SchemaProperties = ({
key: string,
property: OpenAPIV3_1.SchemaObject,
]) => (
<ParameterItem key={key}>
<AnchoredElement id={[...idPrefix, key]}>
<div className="flex flex-row items-center gap-x-3">
<span className="font-mono font-medium text-black dark:text-white">
{key}
</span>
<PropertyType property={property} />
{showRequired && (
<>
{required.includes(key) ? (
<RequiredBadge />
) : (
<OptionalBadge />
)}
</>
)}
{showDefault && <PropertyDefault property={property} />}
</div>
</AnchoredElement>
{property.description && (
<ProseWrapper className="text-sm">
<Markdown>{property.description}</Markdown>
</ProseWrapper>
)}
{property.type != 'object' && property.type != 'array' && (
<ParameterWidget
schema={property}
parameterName={[...parentsProperties, key].join('.')}
parameterIn="body"
/>
)}
{property.type == 'object' && (
<div className="rounded-md border border-gray-200 p-4 dark:border-gray-800">
<Schema
schema={property}
idPrefix={[...idPrefix, key]}
parentsProperties={[...parentsProperties, key]}
showRequired={showRequired}
showDefault={showDefault}
/>
</div>
)}
{property.type == 'array' && (
<div className="rounded-md border border-gray-200 p-4 dark:border-gray-800">
<Schema
schema={property.items}
idPrefix={[...idPrefix, key]}
parentsProperties={[...parentsProperties, key]}
showRequired={showRequired}
showDefault={showDefault}
/>
</div>
)}
</ParameterItem>
<SchemaProperty
key={key}
name={key}
property={property}
required={required.includes(key)}
idPrefix={idPrefix}
parentsProperties={parentsProperties}
showRequired={showRequired}
showDefault={showDefault}
showWidgets={showWidgets}
/>
),
)}
</div>
Expand All @@ -155,12 +199,14 @@ export const Schema = ({
idPrefix,
showRequired,
showDefault,
showWidgets,
parentsProperties,
}: {
schema: OpenAPIV3_1.SchemaObject
idPrefix: string[]
showRequired?: boolean
showDefault?: boolean
showWidgets?: boolean
parentsProperties?: string[]
}) => {
const unionSchemas = getUnionSchemas(schema)
Expand All @@ -171,6 +217,7 @@ export const Schema = ({
idPrefix={idPrefix}
showRequired={showRequired}
showDefault={showDefault}
showWidgets={showWidgets}
parentsProperties={parentsProperties ?? []}
/>
)
Expand All @@ -184,6 +231,7 @@ export const Schema = ({
idPrefix={idPrefix}
showRequired={showRequired}
showDefault={showDefault}
showWidgets={showWidgets}
parentsProperties={parentsProperties ?? []}
/>
)
Expand Down
10 changes: 10 additions & 0 deletions clients/apps/web/src/components/Documentation/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ export const getUnionSchemas = (schema: OpenAPIV3_1.SchemaObject) => {
return null
}

export const isArraySchema = (
s: OpenAPIV3_1.SchemaObject,
): s is OpenAPIV3_1.ArraySchemaObject => s.type === 'array'

export const isScalarArraySchema = (
schema: OpenAPIV3_1.ArraySchemaObject,
): boolean => {
return isDereferenced(schema.items) && schema.items.type !== 'object'
}

export const resolveSchemaMinMax = (
schema: OpenAPIV3_1.SchemaObject,
): [number | undefined, number | undefined] => {
Expand Down
Loading