Skip to content

Commit

Permalink
chore: types
Browse files Browse the repository at this point in the history
  • Loading branch information
enzonotario committed Feb 16, 2025
1 parent 55a54df commit 6ad6b8c
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 42 deletions.
8 changes: 4 additions & 4 deletions src/composables/useOpenapi.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { OpenAPI } from '@scalar/openapi-types'
import type { OpenApi } from '../lib/OpenApi'
import type { OpenAPIDocument } from '../types'
import type { PartialUseThemeConfig } from './useTheme'
import { createOpenApiInstance } from '../lib/createOpenApiInstance'
import { useTheme } from './useTheme'

export interface OpenAPIData {
spec: OpenAPI.Document
spec: OpenAPIDocument
openapi: ReturnType<typeof createOpenApiInstance>
config?: PartialUseThemeConfig
}
Expand All @@ -20,7 +20,7 @@ export function useOpenapi({
spec,
config,
}: {
spec?: OpenAPI.Document
spec?: OpenAPIDocument
config?: PartialUseThemeConfig
} = {}) {
if (config) {
Expand All @@ -31,7 +31,7 @@ export function useOpenapi({
setupOpenApi({ spec, config })
}

function setupOpenApi({ spec, config }: { spec: OpenAPI.Document, config?: PartialUseThemeConfig }) {
function setupOpenApi({ spec, config }: { spec: OpenAPIDocument, config?: PartialUseThemeConfig }) {
openapi = createOpenApiInstance({
spec,
defaultTag: config?.spec?.defaultTag,
Expand Down
4 changes: 2 additions & 2 deletions src/composables/usePaths.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OpenAPI } from '@scalar/openapi-types'
import type { OpenAPIDocument } from '../types'
import { OpenApi } from '../lib/OpenApi'
import { prepareOpenAPI } from '../lib/prepareOpenAPI/prepareOpenAPI'

Expand All @@ -7,7 +7,7 @@ export function usePaths({
defaultTag = undefined,
defaultTagDescription = undefined,
}: {
spec: OpenAPI.Document
spec: OpenAPIDocument
defaultTag?: string
defaultTagDescription?: string
}) {
Expand Down
7 changes: 4 additions & 3 deletions src/composables/useSidebar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { OpenAPI, OpenAPIV3 } from '@scalar/openapi-types'
import type { OpenAPIV3 } from '@scalar/openapi-types'
import type { DefaultTheme } from 'vitepress'
import type { OpenAPIDocument } from '../types'
import { httpVerbs } from '../index'
import { OpenApi } from '../lib/OpenApi'
import { prepareOpenAPI } from '../lib/prepareOpenAPI/prepareOpenAPI'
Expand All @@ -19,7 +20,7 @@ export type SidebarGroupTemplateFn = (
) => string

export interface SidebarConfig {
spec?: OpenAPI.Document | null
spec?: OpenAPIDocument | null
linkPrefix?: string
tagLinkPrefix?: string
defaultTag?: string
Expand Down Expand Up @@ -240,7 +241,7 @@ export function useSidebar({
linkPrefix: tagsLinkPrefix = tagLinkPrefix,
}: SidebarGroupsConfig = {}): OASidebarItem[] {
if (tags === undefined) {
tags = getOpenApi().getFilteredTags().map((tag: OpenAPIV3.TagObject) => tag.name || '')
tags = getOpenApi().getFilteredTags().map(tag => tag.name || '')
}

if (!getOpenApi().getPaths() || !tags) {
Expand Down
16 changes: 8 additions & 8 deletions src/lib/OpenApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { OpenAPI, OpenAPIV3 } from '@scalar/openapi-types'
import type { ParsedOpenAPI } from '../types'
import type { OpenAPIV3 } from '@scalar/openapi-types'
import type { OpenAPIDocument, ParsedOpenAPI } from '../types'
import { httpVerbs } from '../index'
import { processOpenAPI } from './processOpenAPI/processOpenAPI'

Expand All @@ -16,13 +16,13 @@ export function OpenApi({
transformedSpec: null,
parsedSpec: null,
}) {
let innerSpec: OpenAPI.Document | null = null
let innerSpec: OpenAPIDocument | null = null

function setSpec(spec: any) {
innerSpec = spec
}

function getSpec(): OpenAPI.Document {
function getSpec(): OpenAPIDocument {
if (!innerSpec) {
setSpec(parsedSpec ?? transformedSpec ?? spec ?? {})
}
Expand Down Expand Up @@ -232,22 +232,22 @@ export function OpenApi({

function getTags() {
return (getSpec().tags ?? [])
.map((tag: OpenAPIV3.TagObject) => ({
.map(tag => ({
name: tag.name ?? null,
description: tag.description ?? null,
}))
}

function getFilteredTags(): OpenAPIV3.TagObject[] {
function getFilteredTags() {
const operationsTags = getOperationsTags()

const tags = getTags()
.filter((tag: OpenAPIV3.TagObject) => operationsTags.includes(tag.name ?? ''))
.filter(tag => operationsTags.includes(tag.name ?? ''))

return tags
.concat([
...operationsTags
.filter(tag => !tags.map((tag: OpenAPIV3.TagObject) => tag.name).includes(tag))
.filter(tag => !tags.map(tag => tag.name).includes(tag))
.map(tag => ({
name: tag,
description: null,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/createOpenApiInstance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OpenAPI } from '@scalar/openapi-types'
import type { OpenAPIDocument } from '../types'
import { OpenApi } from './OpenApi'
import { prepareOpenAPI } from './prepareOpenAPI/prepareOpenAPI'
import { processOpenAPI } from './processOpenAPI/processOpenAPI'
Expand All @@ -8,7 +8,7 @@ export function createOpenApiInstance({
defaultTag = undefined,
defaultTagDescription = undefined,
}: {
spec: OpenAPI.Document
spec: OpenAPIDocument
defaultTag?: string
defaultTagDescription?: string
}) {
Expand Down
5 changes: 3 additions & 2 deletions src/lib/prepareOpenAPI/generateMissingOperationIds.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { OpenAPI, OpenAPIV3 } from '@scalar/openapi-types'
import type { OpenAPIV3 } from '@scalar/openapi-types'
import type { OpenAPIDocument } from '../../types'

export function generateMissingOperationIds(spec: OpenAPI.Document): OpenAPI.Document {
export function generateMissingOperationIds(spec: OpenAPIDocument): OpenAPIDocument {
spec.paths = spec.paths || {}

for (const path of Object.keys(spec.paths)) {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/prepareOpenAPI/generateMissingSummary.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { OpenAPI, OpenAPIV3 } from '@scalar/openapi-types'
import type { OperationObject } from '../../types'
import type { OpenAPIV3 } from '@scalar/openapi-types'
import type { OpenAPIDocument, OperationObject } from '../../types'

export function generateMissingSummary(spec: OpenAPI.Document): OpenAPI.Document {
export function generateMissingSummary(spec: OpenAPIDocument): OpenAPIDocument {
spec.paths = spec.paths || {}

for (const path of Object.keys(spec.paths)) {
Expand Down
17 changes: 11 additions & 6 deletions src/lib/prepareOpenAPI/generateMissingTags.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import type { OpenAPI, OpenAPIV3 } from '@scalar/openapi-types'
import type { OpenAPIV3 } from '@scalar/openapi-types'
import type { OpenAPIDocument } from '../../types'

export function generateMissingTags({
spec,
defaultTag = 'Default',
defaultTagDescription = '',
}: {
spec: OpenAPI.Document
spec: OpenAPIDocument
defaultTag?: string
defaultTagDescription?: string
}): OpenAPI.Document {
}): OpenAPIDocument {
const operationTags = new Set<string>()

spec.paths = spec.paths || {}

for (const path of Object.values(spec.paths)) {
for (const verb of Object.keys(path) as OpenAPIV3.HttpMethods[]) {
const operation = path[verb]
for (const [_, pathObject] of Object.entries(spec.paths)) {
for (const verb of Object.keys(pathObject as any) as OpenAPIV3.HttpMethods[]) {
if (!pathObject || !pathObject[verb]) {
continue
}

const operation = pathObject[verb]
const tags = operation.tags || [defaultTag]
operation.tags = tags
tags.forEach((tag: string) => operationTags.add(tag))
Expand Down
8 changes: 4 additions & 4 deletions src/lib/prepareOpenAPI/prepareOpenAPI.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OpenAPI } from '@scalar/openapi-types'
import type { OpenAPIDocument } from '../../types'
import { generateMissingOperationIds } from './generateMissingOperationIds'
import { generateMissingSummary } from './generateMissingSummary'
import { generateMissingTags } from './generateMissingTags'
Expand All @@ -8,10 +8,10 @@ export function prepareOpenAPI({
defaultTag = undefined,
defaultTagDescription = undefined,
}: {
spec: OpenAPI.Document
spec: OpenAPIDocument
defaultTag?: string
defaultTagDescription?: string
}): OpenAPI.Document {
}): OpenAPIDocument {
if (import.meta.env.VITE_DEBUG) {
console.warn('Transforming OpenAPI spec:', spec)
}
Expand All @@ -20,7 +20,7 @@ export function prepareOpenAPI({
return {}
}

if (!spec.openapi || !spec.openapi.startsWith('3.')) {
if (!spec.openapi || !String(spec.openapi).startsWith('3.')) {
console.warn('Only OpenAPI 3.x is supported')
return {}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/processOpenAPI/getCodeSamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function getCodeSamples(spec: ParsedOpenAPI): Promise<ParsedOpenAPI
return spec
}

const baseUrl = resolveBaseUrl(spec.servers?.[0]?.url)
const baseUrl = resolveBaseUrl(spec.servers?.[0]?.url || '')

for (const [path, pathObject] of Object.entries(spec.paths)) {
for (const verb of Object.keys(pathObject) as OpenAPIV3.HttpMethods[]) {
Expand Down
12 changes: 6 additions & 6 deletions src/lib/processOpenAPI/processOpenAPI.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { OpenAPI, OpenAPIV3 } from '@scalar/openapi-types'
import type { OpenAPIV3 } from '@scalar/openapi-types'
import type { JSONSchema } from '@trojs/openapi-dereference'
import type { ParsedContent, ParsedOpenAPI, ParsedOperation } from '../../types'
import type { OpenAPIDocument, ParsedContent, ParsedOpenAPI, ParsedOperation } from '../../types'
import { dereferenceSync } from '@trojs/openapi-dereference'
import { merge } from 'allof-merge'
import { getSchemaExample } from '../examples/getSchemaExample'
import { getSchemaUi } from './getSchemaUi'
import { getSecurityUi } from './getSecurityUi'

function safelyMergeSpec(spec: OpenAPI.Document): ParsedOpenAPI {
function safelyMergeSpec(spec: OpenAPIDocument): ParsedOpenAPI {
try {
return merge(spec) as ParsedOpenAPI
} catch (error: any) {
Expand All @@ -16,7 +16,7 @@ function safelyMergeSpec(spec: OpenAPI.Document): ParsedOpenAPI {
}
}

function safelyDereferenceSpec(spec: OpenAPI.Document): ParsedOpenAPI {
function safelyDereferenceSpec(spec: OpenAPIDocument): ParsedOpenAPI {
try {
return dereferenceSync(spec as JSONSchema) as ParsedOpenAPI
} catch (error: any) {
Expand All @@ -34,7 +34,7 @@ function safelyGenerateSchemaUi(spec: ParsedOpenAPI): ParsedOpenAPI {
}
}

export function processOpenAPI(spec: OpenAPI.Document): ParsedOpenAPI {
export function processOpenAPI(spec: OpenAPIDocument): ParsedOpenAPI {
if (import.meta.env.VITE_DEBUG) {
console.warn('Processing OpenAPI spec:', spec)
}
Expand Down Expand Up @@ -65,7 +65,7 @@ function safelyGenerateSecurityUi(spec: ParsedOpenAPI): ParsedOpenAPI {
continue
}

operation.securityUi = getSecurityUi(operation.security ?? spec.security, spec.components?.securitySchemes || {})
operation.securityUi = getSecurityUi(operation.security ?? spec.security ?? [], spec.components?.securitySchemes || {})
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export type OperationSlot =
// | 'summary'
// | 'servers'

export type ParsedOpenAPI = OpenAPI.Document & {
export type OpenAPIDocument = OpenAPIV3.Document & OpenAPIV3_1.Document

export type ParsedOpenAPI = OpenAPIDocument & {
paths: OpenAPIV3.PathsObject & OpenAPIV3_1.PathsObject & {
[key: string]: OpenAPIV3.PathItemObject & OpenAPIV3_1.PathItemObject & {
[key: string]: ParsedOperation
Expand Down

0 comments on commit 6ad6b8c

Please sign in to comment.