From f156a883cbafbaa42100abf80b6aa5719943db35 Mon Sep 17 00:00:00 2001 From: "P.O Marec" Date: Thu, 12 Jun 2025 19:15:16 +0200 Subject: [PATCH] Group apiOnly() --- src/router/group.ts | 17 ++++++++++++++--- tests/router/group.spec.ts | 29 ++++++++++++++++++++++++++--- tests/router/resource.spec.ts | 2 +- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/router/group.ts b/src/router/group.ts index fc1526d..f13b072 100644 --- a/src/router/group.ts +++ b/src/router/group.ts @@ -8,13 +8,13 @@ */ import Macroable from '@poppinss/macroable' -import type { RouteMatcher, StoreRouteMiddleware } from '../types/route.js' import type { MiddlewareFn, ParsedNamedMiddleware } from '../types/middleware.js' +import type { RouteMatcher, StoreRouteMiddleware } from '../types/route.js' -import { Route } from './route.js' +import { OneOrMore } from '../types/base.js' import { BriskRoute } from './brisk.js' import { RouteResource } from './resource.js' -import { OneOrMore } from '../types/base.js' +import { Route } from './route.js' /** * Group class exposes the API to take action on a group of routes. @@ -240,4 +240,15 @@ export class RouteGroup extends Macroable { middleware(middleware: OneOrMore): this { return this.use(middleware) } + + /** + * Register api only for all route resources. + * The `create` and `edit` routes, which are meant to show forms will not be registered + */ + apiOnly(): this { + this.routes + .filter((route) => route instanceof RouteResource) + .forEach((route) => route.apiOnly()) + return this + } } diff --git a/tests/router/group.spec.ts b/tests/router/group.spec.ts index b545d5f..d1ac515 100644 --- a/tests/router/group.spec.ts +++ b/tests/router/group.spec.ts @@ -7,15 +7,15 @@ * file that was distributed with this source code. */ -import { test } from '@japa/runner' import { AppFactory } from '@adonisjs/application/factories' +import { test } from '@japa/runner' -import { Route } from '../../src/router/route.js' +import { defineNamedMiddleware } from '../../src/define_middleware.js' import { toRoutesJSON } from '../../src/helpers.js' import { BriskRoute } from '../../src/router/brisk.js' import { RouteGroup } from '../../src/router/group.js' import { RouteResource } from '../../src/router/resource.js' -import { defineNamedMiddleware } from '../../src/define_middleware.js' +import { Route } from '../../src/router/route.js' const BASE_URL = new URL('./app/', import.meta.url) @@ -1301,3 +1301,26 @@ test.group('Route group | matchers', () => { ]) }) }) + +test.group('Route group | api only', () => { + test('mark non-api routes deleted', ({ assert, expectTypeOf }) => { + const app = new AppFactory().create(BASE_URL, () => {}) + const resource = new RouteResource(app, [], { + resource: 'photos', + controller: '#controllers/photos', + globalMatchers: {}, + shallow: true, + }) + + const group = new RouteGroup([resource]) + + group.apiOnly() + + expectTypeOf(resource.apiOnly().only) + .parameter(0) + .toEqualTypeOf<('index' | 'store' | 'show' | 'update' | 'destroy')[]>() + + assert.isTrue(resource.routes.find((route) => route.getName() === 'photos.create')!.isDeleted()) + assert.isTrue(resource.routes.find((route) => route.getName() === 'photos.edit')!.isDeleted()) + }) +}) diff --git a/tests/router/resource.spec.ts b/tests/router/resource.spec.ts index 168c9ef..959cea5 100644 --- a/tests/router/resource.spec.ts +++ b/tests/router/resource.spec.ts @@ -7,8 +7,8 @@ * file that was distributed with this source code. */ -import { test } from '@japa/runner' import { AppFactory } from '@adonisjs/application/factories' +import { test } from '@japa/runner' import { RouteResource } from '../../src/router/resource.js'