Showing the changes to plugins/openapi/shared (1 of 20 files). Show all
Unified · Split
| @@ -23,6 +23,13 @@ export const OpenApiConnectInputSchema = Schema.Struct({ · 22 unmodified lines | |||
| 23 | auth: OpenApiConnectionAuthSchema, | 23 | auth: OpenApiConnectionAuthSchema, |
| 24 | }); | 24 | }); |
| 25 | 25 | ||
| 26 | export const OpenApiSourceConfigPayloadSchema = OpenApiConnectInputSchema; | ||
| 27 | |||
| 28 | export const OpenApiUpdateSourceInputSchema = Schema.Struct({ | ||
| 29 | sourceId: Schema.String, | ||
| 30 | config: OpenApiSourceConfigPayloadSchema, | ||
| 31 | }); | ||
| 32 | |||
| 26 | export const OpenApiSourceConfigSchema = Schema.Struct({ | 33 | export const OpenApiSourceConfigSchema = Schema.Struct({ |
| 27 | specUrl: Schema.String, | 34 | specUrl: Schema.String, |
| 28 | baseUrl: Schema.NullOr(Schema.String), | 35 | baseUrl: Schema.NullOr(Schema.String), |
| @@ -43,20 +50,35 @@ export const OpenApiPreviewRequestSchema = Schema.Struct({ · 14 unmodified lines | |||
| 43 | specUrl: Schema.String, | 50 | specUrl: Schema.String, |
| 44 | }); | 51 | }); |
| 45 | 52 | ||
| 53 | export const OpenApiPreviewSecuritySchemeSchema = Schema.Struct({ | ||
| 54 | name: Schema.String, | ||
| 55 | kind: Schema.Literal("apiKey", "http", "oauth2", "openIdConnect", "unknown"), | ||
| 56 | placement: Schema.NullOr(Schema.String), | ||
| 57 | scheme: Schema.NullOr(Schema.String), | ||
| 58 | }); | ||
| 59 | |||
| 46 | export const OpenApiPreviewResponseSchema = Schema.Struct({ | 60 | export const OpenApiPreviewResponseSchema = Schema.Struct({ |
| 47 | title: Schema.NullOr(Schema.String), | 61 | title: Schema.NullOr(Schema.String), |
| 48 | version: Schema.NullOr(Schema.String), | 62 | version: Schema.NullOr(Schema.String), |
| 49 | baseUrl: Schema.NullOr(Schema.String), | 63 | baseUrl: Schema.NullOr(Schema.String), |
| 64 | namespace: Schema.NullOr(Schema.String), | ||
| 50 | operationCount: Schema.Number, | 65 | operationCount: Schema.Number, |
| 66 | securitySchemes: Schema.Array(OpenApiPreviewSecuritySchemeSchema), | ||
| 51 | warnings: Schema.Array(Schema.String), | 67 | warnings: Schema.Array(Schema.String), |
| 52 | }); | 68 | }); |
| 53 | 69 | ||
| 54 | export type OpenApiConnectionAuth = typeof OpenApiConnectionAuthSchema.Type; | 70 | export type OpenApiConnectionAuth = typeof OpenApiConnectionAuthSchema.Type; |
| 55 | export type OpenApiConnectInput = typeof OpenApiConnectInputSchema.Type; | 71 | export type OpenApiConnectInput = typeof OpenApiConnectInputSchema.Type; |
| 72 | export type OpenApiSourceConfigPayload = | ||
| 73 | typeof OpenApiSourceConfigPayloadSchema.Type; | ||
| 56 | export type OpenApiSourceConfig = typeof OpenApiSourceConfigSchema.Type; | 74 | export type OpenApiSourceConfig = typeof OpenApiSourceConfigSchema.Type; |
| 57 | export type OpenApiStoredSourceData = typeof OpenApiStoredSourceDataSchema.Type; | 75 | export type OpenApiStoredSourceData = typeof OpenApiStoredSourceDataSchema.Type; |
| 58 | export type OpenApiPreviewRequest = typeof OpenApiPreviewRequestSchema.Type; | 76 | export type OpenApiPreviewRequest = typeof OpenApiPreviewRequestSchema.Type; |
| 77 | export type OpenApiPreviewSecurityScheme = | ||
| 78 | typeof OpenApiPreviewSecuritySchemeSchema.Type; | ||
| 59 | export type OpenApiPreviewResponse = typeof OpenApiPreviewResponseSchema.Type; | 79 | export type OpenApiPreviewResponse = typeof OpenApiPreviewResponseSchema.Type; |
| 80 | export type OpenApiUpdateSourceInput = | ||
| 81 | typeof OpenApiUpdateSourceInputSchema.Type; | ||
| 60 | 82 | ||
| 61 | type JsonRecord = Record<string, unknown>; | 83 | type JsonRecord = Record<string, unknown>; |
| 62 | 84 | ||
| @@ -68,6 +90,45 @@ const asRecord = (value: unknown): JsonRecord | null => · 5 unmodified lines | |||
| 68 | const asString = (value: unknown): string | null => | 90 | const asString = (value: unknown): string | null => |
| 69 | typeof value === "string" && value.trim().length > 0 ? value.trim() : null; | 91 | typeof value === "string" && value.trim().length > 0 ? value.trim() : null; |
| 70 | 92 | ||
| 93 | const namespaceFromUrl = (value: string): string | null => { | ||
| 94 | try { | ||
| 95 | const url = new URL(value); | ||
| 96 | const hostname = url.hostname.trim().toLowerCase(); | ||
| 97 | if (!hostname) { | ||
| 98 | return null; | ||
| 99 | } | ||
| 100 | |||
| 101 | const parts = hostname.split(".").filter(Boolean); | ||
| 102 | if (parts.length === 0) { | ||
| 103 | return null; | ||
| 104 | } | ||
| 105 | |||
| 106 | const candidate = (parts.length >= 2 ? parts[parts.length - 2] : parts[0])?.trim(); | ||
| 107 | return candidate && candidate.length > 0 ? candidate : null; | ||
| 108 | } catch { | ||
| 109 | return null; | ||
| 110 | } | ||
| 111 | }; | ||
| 112 | |||
| 113 | const slugifyName = (value: string | null): string | null => { | ||
| 114 | if (!value) { | ||
| 115 | return null; | ||
| 116 | } | ||
| 117 | |||
| 118 | const slug = value | ||
| 119 | .trim() | ||
| 120 | .toLowerCase() | ||
| 121 | .replace(/[^a-z0-9]+/g, "-") | ||
| 122 | .replace(/^-+|-+$/g, ""); | ||
| 123 | |||
| 124 | return slug.length > 0 ? slug : null; | ||
| 125 | }; | ||
| 126 | |||
| 127 | export const deriveOpenApiNamespace = (input: { | ||
| 128 | specUrl: string; | ||
| 129 | title?: string | null; | ||
| 130 | }): string | null => slugifyName(input.title ?? null) ?? namespaceFromUrl(input.specUrl); | ||
| 131 | |||
| 71 | const parseDocument = (text: string): unknown => { | 132 | const parseDocument = (text: string): unknown => { |
| 72 | const trimmed = text.trim(); | 133 | const trimmed = text.trim(); |
| 73 | if (!trimmed) { | 134 | if (!trimmed) { |
| @@ -96,6 +157,35 @@ const deriveBaseUrl = (document: JsonRecord, specUrl: string): string | null => · 22 unmodified lines | |||
| 96 | } | 157 | } |
| 97 | }; | 158 | }; |
| 98 | 159 | ||
| 160 | const previewSecuritySchemes = (document: JsonRecord): Array<OpenApiPreviewSecurityScheme> => { | ||
| 161 | const components = asRecord(document.components); | ||
| 162 | const securitySchemes = asRecord(components?.securitySchemes); | ||
| 163 | if (!securitySchemes) { | ||
| 164 | return []; | ||
| 165 | } | ||
| 166 | |||
| 167 | return Object.entries(securitySchemes) | ||
| 168 | .sort(([left], [right]) => left.localeCompare(right)) | ||
| 169 | .map(([name, rawScheme]) => { | ||
| 170 | const scheme = asRecord(rawScheme); | ||
| 171 | const rawKind = asString(scheme?.type); | ||
| 172 | const kind = | ||
| 173 | rawKind === "apiKey" || | ||
| 174 | rawKind === "http" || | ||
| 175 | rawKind === "oauth2" || | ||
| 176 | rawKind === "openIdConnect" | ||
| 177 | ? rawKind | ||
| 178 | : "unknown"; | ||
| 179 | |||
| 180 | return { | ||
| 181 | name, | ||
| 182 | kind, | ||
| 183 | placement: asString(scheme?.in), | ||
| 184 | scheme: asString(scheme?.scheme), | ||
| 185 | } satisfies OpenApiPreviewSecurityScheme; | ||
| 186 | }); | ||
| 187 | }; | ||
| 188 | |||
| 99 | const countOperations = (document: JsonRecord): number => { | 189 | const countOperations = (document: JsonRecord): number => { |
| 100 | const paths = asRecord(document.paths); | 190 | const paths = asRecord(document.paths); |
| 101 | if (paths === null) { | 191 | if (paths === null) { |
| @@ -147,6 +237,7 @@ export const previewOpenApiDocument = async ( · 45 unmodified lines | |||
| 147 | 237 | ||
| 148 | const info = asRecord(document.info); | 238 | const info = asRecord(document.info); |
| 149 | const warnings: string[] = []; | 239 | const warnings: string[] = []; |
| 240 | const title = asString(info?.title); | ||
| 150 | 241 | ||
| 151 | const openapiVersion = asString(document.openapi) ?? asString(document.swagger); | 242 | const openapiVersion = asString(document.openapi) ?? asString(document.swagger); |
| 152 | if (openapiVersion === null) { | 243 | if (openapiVersion === null) { |
| @@ -159,10 +250,15 @@ export const previewOpenApiDocument = async ( · 6 unmodified lines | |||
| 159 | } | 250 | } |
| 160 | 251 | ||
| 161 | return { | 252 | return { |
| 162 | title: asString(info?.title), | 253 | title, |
| 163 | version: asString(info?.version), | 254 | version: asString(info?.version), |
| 164 | baseUrl, | 255 | baseUrl, |
| 256 | namespace: deriveOpenApiNamespace({ | ||
| 257 | specUrl: input.specUrl, | ||
| 258 | title, | ||
| 259 | }), | ||
| 165 | operationCount: countOperations(document), | 260 | operationCount: countOperations(document), |
| 261 | securitySchemes: previewSecuritySchemes(document), | ||
| 166 | warnings, | 262 | warnings, |
| 167 | }; | 263 | }; |
| 168 | }; | 264 | }; |