Skip to content
Closed
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
17 changes: 14 additions & 3 deletions src/router/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -240,4 +240,15 @@ export class RouteGroup extends Macroable {
middleware(middleware: OneOrMore<MiddlewareFn | ParsedNamedMiddleware>): 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
}
}
29 changes: 26 additions & 3 deletions tests/router/group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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())
})
})
2 changes: 1 addition & 1 deletion tests/router/resource.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down