Skip to content

Commit 408f6aa

Browse files
author
Frantz Kati
committed
fix(rest): fix rest API endpoints
Fix rest api package to correctly generate API routes based on developer configuration
1 parent 0908618 commit 408f6aa

File tree

24 files changed

+974
-1412
lines changed

24 files changed

+974
-1412
lines changed

examples/blog/app.js

+3-16
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,14 @@ const Post = require('./resources/Post')
1111
const User = require('./resources/User')
1212
const Editor = require('./resources/Editor')
1313
const Comment = require('./resources/Comment')
14+
const Reaction = require('./resources/Reaction')
1415

1516
module.exports = tensei()
1617
.dashboardPath('tensei')
17-
.resources([Tag, Post, User, Comment, Editor])
18+
.resources([Tag, Post, User, Comment, Editor, Reaction])
1819
.clientUrl('https://google.com')
1920
.serverUrl('http://localhost:5000')
2021
.defaultStorageDriver('local')
21-
.graphQlTypeDefs([
22-
`
23-
type aggregate_comments {
24-
count: Int!
25-
average: Int!
26-
}
27-
28-
extend type Query {
29-
aggregate_comments: aggregate_comments
30-
}
31-
`,
32-
])
3322
.graphQlQueries([])
3423
.routes([
3524
route('Get products')
@@ -73,17 +62,15 @@ module.exports = tensei()
7362
secret: process.env.LINKEDIN_SECRET,
7463
})
7564
.plugin(),
65+
media().plugin(),
7666
graphql()
77-
.subscriptions(new RedisPubSub())
7867
.middlewareOptions({
7968
cors: {
8069
credentials: true,
8170
origin: ['http://localhost:3001'],
8271
},
8372
})
8473
.plugin(),
85-
media().plugin(),
86-
graphql().plugin(),
8774
rest().plugin(),
8875
docs().plugin(),
8976
])

examples/blog/resources/Comment.js

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
belongsTo,
66
filter,
77
hasOne,
8+
hasMany,
89
} = require('@tensei/core')
910

1011
module.exports = resource('Comment')
@@ -14,6 +15,7 @@ module.exports = resource('Comment')
1415
textarea('Reply').rules('required', 'max:255').hideOnIndex(),
1516
belongsTo('Post'),
1617
hasOne('Editor'),
18+
hasMany('Reaction'),
1719
])
1820
.filters([
1921
filter('Comments on Post')

examples/blog/resources/Reaction.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const { select, resource, belongsTo } = require('@tensei/core')
2+
3+
module.exports = resource('Reaction').fields([
4+
select('Type')
5+
.options([
6+
{ label: 'Heart', value: 'heart' },
7+
{ label: 'Downvote', value: 'downvote' },
8+
])
9+
.nullable(),
10+
belongsTo('Comment'),
11+
])

packages/auth/src/index.ts

-22
Original file line numberDiff line numberDiff line change
@@ -635,37 +635,15 @@ class Auth {
635635
async (request, response, next) => {
636636
await this.getAuthUserFromContext(request as any)
637637

638-
return next()
639-
},
640-
async (request, response, next) => {
641638
await this.setAuthUserForPublicRoutes(
642639
request as any
643640
)
644641

645-
return next()
646-
},
647-
async (request, response, next) => {
648642
await this.ensureAuthUserIsNotBlocked(
649643
request as any
650644
)
651645

652646
return next()
653-
},
654-
async (request, response, next) => {
655-
const authorizers = await Promise.all(
656-
route.config.authorize.map(fn =>
657-
fn(request as any)
658-
)
659-
)
660-
661-
if (
662-
authorizers.filter(authorized => authorized)
663-
.length !== route.config.authorize.length
664-
) {
665-
throw request.forbiddenError('Unauthorized.')
666-
}
667-
668-
next()
669647
}
670648
])
671649
if (route.config.resource) {

packages/common/src/api/Route.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class Route implements RouteContract {
3737
}
3838

3939
path(path: string) {
40-
this.config.path = path
40+
this.config.path = path.startsWith('/') ? path.substring(1) : path
4141

4242
return this
4343
}

packages/common/src/utils/graphql.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { parseResolveInfo } from 'graphql-parse-resolve-info'
2-
import { ResourceContract, FilterOperators, oneToOne } from '@tensei/common'
2+
import { ResourceContract, FilterOperators } from '@tensei/common'
33
import { EntityManager, ReferenceType, Configuration } from '@mikro-orm/core'
44

55
export const filterOperators: FilterOperators[] = [

0 commit comments

Comments
 (0)