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

Accessing openapi.json on prefixed path? #17

Closed Manouchehri closed 1 year ago

Manouchehri commented 1 year ago

So in this example, I have set my wrangler.toml registered to listen on legit.website.example/plans/*, and my router registered on /plans/plans.json.

The issue here is that in production, I cannot access legit.website.example/openapi.json; how can I instead have the schema at like legit.website.example/plans/openapi.json?

name = "tls-breaker-worker-dev"
main = "src/index.js"
compatibility_date = "2022-11-23"
workers_dev = false

[vars]
ENVIRONMENT = "dev"

[env.production]
name = "tls-breaker-worker"
route = { pattern = "legit.website.example/plans/*", zone_name="website.example" }
[env.production.vars]
ENVIRONMENT = "production"
import { OpenAPIRouter, OpenAPIRoute, Query, Int } from '@cloudflare/itty-router-openapi'

const router = OpenAPIRouter()

export default {
    fetch: router.handle
}

export class Plans extends OpenAPIRoute {
    static schema = { 
        tags: ['pricing', 'prices', 'plan', 'plans'],
        summary: "Get the list of products.",
        parameters: {
            product: Query(Int, {
                description: 'Product number',
                default: 1,
                required: false,
              }),
        }
    }

    async handle(request, env, ctx, data) {
        const { product }  = data;
        console.debug('------');
        console.debug(currency);

        return new Response("Hello world", {
            status: 200
        })
    }
}

router.get('/plans/plans.json', Plans);
router.all('*', () => new Response('Not Found.', { status: 404 }))
Manouchehri commented 1 year ago

I'm an idiot, this is documented right in the README.md. I needed to use base.

const router = OpenAPIRouter({
    base: '/plans'
})