History

openapi works not bad

Rhys Sullivan committed 7 months ago

Commit a9739d61 parent c45a85b20 files · +4934 −159Browse files

Showing the changes to plugins/openapi/shared (1 of 20 files). Show all

Unified · Split

plugins/openapi/shared/index.ts+97−1
@@ -23,6 +23,13 @@ export const OpenApiConnectInputSchema = Schema.Struct({ · 22 unmodified lines
2323 auth: OpenApiConnectionAuthSchema,
2424 });
2525
26+export const OpenApiSourceConfigPayloadSchema = OpenApiConnectInputSchema;
27+
28+export const OpenApiUpdateSourceInputSchema = Schema.Struct({
29+ sourceId: Schema.String,
30+ config: OpenApiSourceConfigPayloadSchema,
31+});
32+
2633 export const OpenApiSourceConfigSchema = Schema.Struct({
2734 specUrl: Schema.String,
2835 baseUrl: Schema.NullOr(Schema.String),
@@ -43,20 +50,35 @@ export const OpenApiPreviewRequestSchema = Schema.Struct({ · 14 unmodified lines
4350 specUrl: Schema.String,
4451 });
4552
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+
4660 export const OpenApiPreviewResponseSchema = Schema.Struct({
4761 title: Schema.NullOr(Schema.String),
4862 version: Schema.NullOr(Schema.String),
4963 baseUrl: Schema.NullOr(Schema.String),
64+ namespace: Schema.NullOr(Schema.String),
5065 operationCount: Schema.Number,
66+ securitySchemes: Schema.Array(OpenApiPreviewSecuritySchemeSchema),
5167 warnings: Schema.Array(Schema.String),
5268 });
5369
5470 export type OpenApiConnectionAuth = typeof OpenApiConnectionAuthSchema.Type;
5571 export type OpenApiConnectInput = typeof OpenApiConnectInputSchema.Type;
72+export type OpenApiSourceConfigPayload =
73+ typeof OpenApiSourceConfigPayloadSchema.Type;
5674 export type OpenApiSourceConfig = typeof OpenApiSourceConfigSchema.Type;
5775 export type OpenApiStoredSourceData = typeof OpenApiStoredSourceDataSchema.Type;
5876 export type OpenApiPreviewRequest = typeof OpenApiPreviewRequestSchema.Type;
77+export type OpenApiPreviewSecurityScheme =
78+ typeof OpenApiPreviewSecuritySchemeSchema.Type;
5979 export type OpenApiPreviewResponse = typeof OpenApiPreviewResponseSchema.Type;
80+export type OpenApiUpdateSourceInput =
81+ typeof OpenApiUpdateSourceInputSchema.Type;
6082
6183 type JsonRecord = Record<string, unknown>;
6284
@@ -68,6 +90,45 @@ const asRecord = (value: unknown): JsonRecord | null => · 5 unmodified lines
6890 const asString = (value: unknown): string | null =>
6991 typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
7092
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+
71132 const parseDocument = (text: string): unknown => {
72133 const trimmed = text.trim();
73134 if (!trimmed) {
@@ -96,6 +157,35 @@ const deriveBaseUrl = (document: JsonRecord, specUrl: string): string | null => · 22 unmodified lines
96157 }
97158 };
98159
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+
99189 const countOperations = (document: JsonRecord): number => {
100190 const paths = asRecord(document.paths);
101191 if (paths === null) {
@@ -147,6 +237,7 @@ export const previewOpenApiDocument = async ( · 45 unmodified lines
147237
148238 const info = asRecord(document.info);
149239 const warnings: string[] = [];
240+ const title = asString(info?.title);
150241
151242 const openapiVersion = asString(document.openapi) ?? asString(document.swagger);
152243 if (openapiVersion === null) {
@@ -159,10 +250,15 @@ export const previewOpenApiDocument = async ( · 6 unmodified lines
159250 }
160251
161252 return {
162− title: asString(info?.title),
253+ title,
163254 version: asString(info?.version),
164255 baseUrl,
256+ namespace: deriveOpenApiNamespace({
257+ specUrl: input.specUrl,
258+ title,
259+ }),
165260 operationCount: countOperations(document),
261+ securitySchemes: previewSecuritySchemes(document),
166262 warnings,
167263 };
168264 };

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t