cloudflare / chanfana

OpenAPI 3 and 3.1 schema generator and validator for Hono, itty-router and more!
https://chanfana.pages.dev
MIT License
288 stars 38 forks source link

Failed to load API definition. #59

Closed ghost closed 1 year ago

ghost commented 1 year ago

If the main router has base set, going to /<base>/docs renders

failed-to-load-api-def

and /<base>/redocs is broken too. Removal of the base configuration renders the pages.

The openapi.json file does load directly at /<base>/openapi.json though.

Using the workers-sdk openapi example

import { OpenAPIRouter } from '@cloudflare/itty-router-openapi';
import { TaskCreate, TaskDelete, TaskFetch, TaskList } from './tasks';

const router = OpenAPIRouter({
  base: "/tasker",
  openapi_url: '/tasker/openapi.json',
  schema: {
    info: {
      title: 'Worker OpenAPI Example',
      version: '1.0',
    },
  },
});

router.get('/api/tasks/', TaskList);
router.post('/api/tasks/', TaskCreate);
router.get('/api/tasks/:taskSlug/', TaskFetch);
router.delete('/api/tasks/:taskSlug/', TaskDelete);

// Redirect root request to the /docs page
router.original.get('/', request => Response.redirect(`${request.url}/docs`, 302));
    // Added `/` above or it redirects to `/<base>docs` not `/<base>/docs`

// 404 for everything else
router.all('*', () => new Response('Not Found.', { status: 404 }));

export default {
  fetch: router.handle,
};

All other routes continue to work when base is set.

routes-work

Setting docs_url or openapi_url didn't change the above result. Tested with 0.1.0 -> 0.1.6.

G4brym commented 1 year ago

Hey @jasiqli i've published a new version (v0.1.10) that fixes this issue

ghost commented 1 year ago

This works just as I hoped now @G4brym. My (possibly completely inane) demonstration (and reason for this report in the first place) is https://github.com/jasiqli/pages-openapi Any thoughts appreciated.