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
42 changes: 39 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"node": ">=24.11.0"
},
"dependencies": {
"@asteasolutions/zod-to-openapi": "^8.5.0",
"@emotion/css": "^11.13.5",
"@sentry/cloudflare": "^10.46.0",
"algoliasearch": "^5.50.0",
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { Hono } from 'hono';
import { cors } from 'hono/cors';
import { logger } from 'hono/logger';

import './utils/openapi.ts';

import apiRoutes from './routes/api.ts';
import errorRoutes from './routes/errors.ts';
import indexRoutes from './routes/index.ts';
import librariesRoutes from './routes/libraries.ts';
Expand All @@ -19,6 +22,7 @@ app.use('*', cors(corsOptions));

// Load the routes
indexRoutes(app);
apiRoutes(app);
statsRoutes(app);
whitelistRoutes(app);
libraryRoutes(app);
Expand Down
29 changes: 29 additions & 0 deletions src/routes/api.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as z from 'zod';

export const openApiResponseSchema = z.object({
openapi: z.string(),
info: z.object({
title: z.string(),
description: z.string().optional(),
version: z.string(),
}),
servers: z
.array(
z.object({
url: z.string(),
description: z.string().optional(),
}),
)
.optional(),
paths: z.record(z.string(), z.unknown()).openapi({ type: 'object' }),
components: z
.object({
schemas: z
.record(z.string(), z.unknown())
.openapi({ type: 'object' })
.optional(),
})
.optional(),
});

export type OpenApiResponse = z.infer<typeof openApiResponseSchema>;
43 changes: 43 additions & 0 deletions src/routes/api.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, expect, it } from 'vitest';

import testCors from '../utils/spec/cors.ts';
import { beforeRequest, request } from '../utils/spec/request.ts';

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

describe('/api', () => {
// Fetch the endpoint
const path = '/api';
const response = beforeRequest(path);

// Test the endpoint
testCors(path, response);
it('returns the correct Cache headers', () => {
expect(response.headers.get('Cache-Control')).to.eq(
'public, max-age=21600',
); // 6 hours
});
it('returns the correct status code', () => {
expect(response.status).to.eq(200);
});
it('returns a valid OpenAPI spec in JSON format', async () => {
expect(response.headers.get('Content-Type')).to.match(
/application\/json/,
);
const data = await response.json<OpenApiResponse>();
expect(data.openapi).to.eq('3.0.0');
expect(data.info.title).to.eq('cdnjs API');
expect(data.paths['/libraries']).to.be.an('object');
expect(data.paths['/libraries/{library}']).to.be.an('object');
expect(data.components?.schemas?.Error).to.be.an('object');
});

// Test with a trailing slash
it('responds to requests with a trailing slash', async () => {
const res = await request(path + '/');
expect(res.status).to.eq(200);
const resData = await res.json();
const responseData = await response.json();
expect(resData).to.deep.eq(responseData);
});
});
Loading