Skip to content
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
4 changes: 4 additions & 0 deletions imports.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '*.css' {
const content: string;
export default content;
}
4,096 changes: 4,049 additions & 47 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"react": "^19.2.5",
"react-dom": "^19.2.5",
"semver": "^7.7.4",
"swagger-ui-react": "^5.32.5",
"zod": "^4.3.6"
},
"devDependencies": {
Expand All @@ -55,6 +56,7 @@
"@types/react": "^19.2.14",
"@types/react-dom": "^19.2.3",
"@types/semver": "^7.7.1",
"@types/swagger-ui-react": "^5.18.0",
"eslint": "^10.2.1",
"eslint-config-prettier": "^10.1.8",
"eslint-plugin-jsdoc": "^62.9.0",
Expand Down
11 changes: 11 additions & 0 deletions src/routes/api.page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Swagger from '../utils/jsx/islands/swagger.tsx';

import type { OpenApiResponse } from './api.schema.ts';

/**
* /api page component.
*
* @param props Page props.
* @param props.data OpenAPI response data.
*/
export default ({ data }: { data: OpenApiResponse }) => <Swagger spec={data} />;
23 changes: 21 additions & 2 deletions src/routes/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as z from 'zod';

import respond, { withCache } from '../utils/respond.ts';

import ApiPage from './api.page.tsx';
import { type OpenApiResponse, openApiResponseSchema } from './api.schema.ts';
import { errorResponseSchema } from './errors.schema.ts';

Expand Down Expand Up @@ -50,7 +51,16 @@ const createHandleGetApi = (registry: OpenAPIRegistry) => {

definitions.sort((a, b) => {
if (a.type === 'route' && b.type === 'route') {
// Sort by method first
// Hoist routes with the libraries tag, as they are the core of cdnjs
const aHasLibrariesTag =
a.route.tags?.includes('libraries');
const bHasLibrariesTag =
b.route.tags?.includes('libraries');
if (aHasLibrariesTag !== bHasLibrariesTag) {
return aHasLibrariesTag ? -1 : 1;
}

// Otherwise, sort by method first
const methodOrder = [
'get',
'post',
Expand Down Expand Up @@ -99,6 +109,15 @@ const createHandleGetApi = (registry: OpenAPIRegistry) => {
) {
delete spec.components.parameters;
}

// Sort the schemas in the components object alphabetically, for easier navigation by humans
if (spec.components?.schemas) {
spec.components.schemas = Object.fromEntries(
Object.entries(spec.components.schemas).sort(([a], [b]) =>
a.localeCompare(b),
),
);
}
}

return spec;
Expand All @@ -113,7 +132,7 @@ const createHandleGetApi = (registry: OpenAPIRegistry) => {
// Set a 6 hour life on this response
withCache(ctx, 6 * 60 * 60);

return respond<OpenApiResponse>(ctx, getOrGenerateSpec());
return respond<OpenApiResponse>(ctx, getOrGenerateSpec(), ApiPage);
};

return handleGetApi;
Expand Down
15 changes: 9 additions & 6 deletions src/routes/libraries.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import { librarySchema } from '../utils/algolia.schema';

export const librariesResponseSchema = z.object({
results: z.array(
librarySchema.partial().and(
z.object({
name: z.string(),
latest: z.string().nullable(),
}),
),
librarySchema
.partial()
.and(
z.object({
name: z.string(),
latest: z.string().nullable(),
}),
)
.openapi('LibraryResult'),
),
total: z.number(),
available: z.number(),
Expand Down
6 changes: 4 additions & 2 deletions src/routes/library.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export const libraryVersionResponseSchema = z
rawFiles: z.array(z.string()),
sri: z.record(z.string(), z.string()),
})
.partial();
.partial()
.openapi('LibraryVersion');

export type LibraryVersionResponse = z.infer<
typeof libraryVersionResponseSchema
Expand All @@ -30,6 +31,7 @@ export const libraryResponseSchema = librarySchema
}),
),
})
.partial();
.partial()
.openapi('Library');

export type LibraryResponse = z.infer<typeof libraryResponseSchema>;
13 changes: 12 additions & 1 deletion src/utils/jsx/island.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { css } from '@emotion/css';
import { env } from 'cloudflare:workers';
import { type ComponentType, createContext, useContext, useId } from 'react';
import * as z from 'zod';
Expand Down Expand Up @@ -46,6 +47,12 @@ const serializeProps = (props: object) =>
.replaceAll('\u2028', '\\u2028')
.replaceAll('\u2029', '\\u2029');

const styles = {
island: css`
display: contents;
`,
};

/**
* Server helper for rendering an island with serialized props and entrypoint script.
*
Expand Down Expand Up @@ -80,7 +87,11 @@ const Island = <T extends object>({

return (
<>
<div data-island={name} data-island-props={propsScriptId}>
<div
className={styles.island}
data-island={name}
data-island-props={propsScriptId}
>
<Component {...props} />
</div>

Expand Down
Loading