Skip to content

Commit

Permalink
feat(rest): add simple rest documentation (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
bahdcoder authored Mar 4, 2021
1 parent 40e35d0 commit 8f30635
Show file tree
Hide file tree
Showing 57 changed files with 3,215 additions and 2,038 deletions.
1 change: 0 additions & 1 deletion examples/blog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ tensei()
])
.databaseConfig({
type: 'sqlite',
debug: true,
dbName: 'tensei'
})
.start()
Expand Down
2 changes: 1 addition & 1 deletion examples/blog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"marked": "^2.0.1"
},
"scripts": {
"example:dev": "nodemon src/index"
"example:dev": "nodemon --watch node_modules/ src/index"
},
"devDependencies": {
"nodemon": "^2.0.7"
Expand Down
300 changes: 113 additions & 187 deletions packages/auth/src/index.ts

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions packages/cms/detail/ID/ID.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import { DetailComponentProps } from '@tensei/components'

import Text from '../Text'

const ID: React.FC<
DetailComponentProps & {
className?: string
onClick?: EventHandler<SyntheticEvent<HTMLParagraphElement>>
}
> = ({ value, values, field, resource, className = '' }) => {
const ID: React.FC<DetailComponentProps & {
className?: string
onClick?: EventHandler<SyntheticEvent<HTMLParagraphElement>>
}> = ({ value, values, field, resource, className = '' }) => {
return (
<>
<Text
Expand Down
8 changes: 3 additions & 5 deletions packages/cms/form/ManyToMany/ManyToMany.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import {
Pulse
} from '@tensei/components'

const ManyToMany: React.FC<
FormComponentProps & {
customQuery?: (parameters: AbstractData) => AbstractData
}
> = ({
const ManyToMany: React.FC<FormComponentProps & {
customQuery?: (parameters: AbstractData) => AbstractData
}> = ({
field,
name,
id,
Expand Down
8 changes: 3 additions & 5 deletions packages/cms/index/ID/ID.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import IDDetail from '../../detail/ID'

import { IndexComponentProps } from '@tensei/components'

const ID: React.FC<
IndexComponentProps & {
noLink?: boolean
}
> = ({ value, values, field, resource, noLink }) => {
const ID: React.FC<IndexComponentProps & {
noLink?: boolean
}> = ({ value, values, field, resource, noLink }) => {
const detail = (
<IDDetail
value={value}
Expand Down
10 changes: 4 additions & 6 deletions packages/cms/pages/ResourceDetail/ResourceDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ import PageWrapper from '../../components/PageWrapper'

export interface ResourceDetailProps {}

const ResourceDetail: React.FC<
ResourceDetailProps &
RouteComponentProps<{
id: string
}>
> = () => {
const ResourceDetail: React.FC<ResourceDetailProps &
RouteComponentProps<{
id: string
}>> = () => {
const history = useHistory()
const [data, setData] = useState<any>({})
const [loading, setLoading] = useState(true)
Expand Down
9 changes: 6 additions & 3 deletions packages/cms/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,9 @@ class CmsPlugin {
private permissionResource() {
return resource(this.config.permissionResource)
.fields([
text('Name').searchable().rules('required'),
text('Name')
.searchable()
.rules('required'),
text('Slug')
.rules('required')
.unique()
Expand Down Expand Up @@ -388,7 +390,9 @@ class CmsPlugin {
.sortable()
.searchable()
.rules('required'),
text('Description').nullable().rules('max:255'),
text('Description')
.nullable()
.rules('max:255'),
belongsToMany(this.config.userResource),
belongsToMany(this.config.permissionResource).owner()
])
Expand Down Expand Up @@ -573,7 +577,6 @@ class CmsPlugin {
const path = route.config.path.startsWith('/')
? route.config.path
: `/${route.config.path}`

;(this.router as any)[route.config.type.toLowerCase()](
path,

Expand Down
76 changes: 75 additions & 1 deletion packages/common/src/api/Route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
RouteConfig,
AuthorizeFunction,
RouteContract,
RouteParameter,
ResourceContract,
RouteExtendContract
} from '@tensei/common'
Expand All @@ -17,13 +18,31 @@ export class Route implements RouteContract {
name: '',
cms: false,
type: 'GET',
group: 'Misc',
groupSlug: 'misc',
internal: false,
middleware: [],
parameters: [],
snakeCaseName: '',
paramCaseName: '',
authorize: [],
description: '',
handler: async () => {},
extend: {}
extend: {},
sampleRequest: '',
sampleResponse: ''
}

sampleRequest(sample: string) {
this.config.sampleRequest = sample

return this
}

sampleResponse(sample: string) {
this.config.sampleResponse = sample

return this
}

constructor(name?: string) {
Expand All @@ -36,6 +55,25 @@ export class Route implements RouteContract {
this.config.snakeCaseName = snakeCase(name)
}

group(name: string) {
this.config.group = name
this.config.groupSlug = paramCase(name)

return this
}

parameters(parameters: RouteParameter[]) {
this.config.parameters = [...this.config.parameters, ...parameters]

return this
}

description(description: string) {
this.config.description = description

return this
}

path(path: string) {
this.config.path = path.startsWith('/') ? path.substring(1) : path

Expand Down Expand Up @@ -119,6 +157,42 @@ export class Route implements RouteContract {

return this
}

serialize() {
const {
id,
path,
name,
type,
snakeCaseName,
middleware,
paramCaseName,
authorize,
group,
groupSlug,
parameters,
description,
sampleRequest,
sampleResponse
} = this.config

return {
id,
path,
name,
type,
group,
groupSlug,
parameters,
description,
snakeCaseName,
paramCaseName,
sampleRequest,
sampleResponse,
middleware: middleware.length,
authorize: authorize.length
}
}
}

export const route = (name?: string) => new Route(name)
1 change: 1 addition & 0 deletions packages/common/src/fields/Number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class NumberField extends Text {
this.htmlAttributes({
type: 'number'
})
this.rules('number')
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/common/src/fields/Select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export class Select extends Field {
this.defaultFormValue('')
this.property.enum = true
this.property.items = []

this.rules('string')
}

/**
Expand All @@ -41,6 +43,8 @@ export class Select extends Field {

this.property.items = this.selectOptions.map(option => option.value)

this.rules(`in:${this.property.items.join(',')}`)

return this
}

Expand Down
1 change: 1 addition & 0 deletions packages/common/src/fields/Text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class Text extends Field {
super(name, databaseField)

this.defaultFormValue('')
this.rules('string')
this.property.columnTypes = ['varchar(255)']
}

Expand Down
1 change: 1 addition & 0 deletions packages/common/src/fields/Textarea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class Textarea extends Text {
public constructor(name: string, databaseField?: string) {
super(name, databaseField)

this.rules('string')
this.property.columnTypes = ['text']
this.hideOnIndex()
}
Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/fields/Timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export class Timestamp extends DateField {
this.property.columnTypes = ['timestamp']
this.property.reference = ReferenceType.SCALAR

this.rules('date')

this.defaultFormValue(Dayjs().format(this.config.dateFormat))
}

Expand Down
20 changes: 20 additions & 0 deletions packages/common/typings/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,18 @@ declare module '@tensei/common/config' {
delete(): this
internal(): this
id(id: string): this
group(name: string): this
description(description: string): this
sampleRequest(sample: string): this
sampleResponse(sample: string): this
parameters(parameters: RouteParameter[]): this
extend(extend: RouteExtendContract): this
resource(resource: ResourceContract): this
middleware(middleware: RequestHandler[]): this
resource(resource: ResourceContract): this
authorize(authorize: AuthorizeFunction): this
handle(handler: RouteConfig['handler']): this
serialize(): DataPayload
}

interface UtilsContract {
Expand Down Expand Up @@ -128,15 +134,29 @@ declare module '@tensei/common/config' {
middleware(...middleware: GraphQlMiddleware[]): this
}

interface RouteParameter {
name: string
type: string | number
description: string
validation?: string[]
in: 'header' | 'body' | 'query' | 'path'
}

interface RouteConfig {
path: string
name: string
cms: boolean
internal: boolean
id: string
group: string
groupSlug: string
sampleRequest: string
sampleResponse: string
parameters: RouteParameter[]
type: EndpointTypes
snakeCaseName: string
paramCaseName: string
description: string
resource?: ResourceContract
middleware: RequestHandler[]
authorize: AuthorizeFunction[]
Expand Down
25 changes: 10 additions & 15 deletions packages/components/src/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,33 +62,28 @@ const Button: React.FC<ButtonProps> = ({
}

if (clear) {
props.className = `font-bold h-9 flex items-center ${
props.className || ''
} ${classes.clear}`
props.className = `font-bold h-9 flex items-center ${props.className ||
''} ${classes.clear}`
}

if (primary) {
props.className = `font-bold h-9 flex items-center ${
props.className || ''
} ${classes.primary}`
props.className = `font-bold h-9 flex items-center ${props.className ||
''} ${classes.primary}`
}

if (danger) {
props.className = `font-bold h-9 flex items-center ${
props.className || ''
} ${classes.danger}`
props.className = `font-bold h-9 flex items-center ${props.className ||
''} ${classes.danger}`
}

if (success) {
props.className = `font-bold h-9 flex items-center ${
props.className || ''
} ${classes.success}`
props.className = `font-bold h-9 flex items-center ${props.className ||
''} ${classes.success}`
}

if (secondary) {
props.className = `font-bold h-9 flex items-center ${
props.className || ''
} ${classes.secondary}`
props.className = `font-bold h-9 flex items-center ${props.className ||
''} ${classes.secondary}`
}

return (
Expand Down
5 changes: 2 additions & 3 deletions packages/components/src/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ const Checkbox: React.FC<CheckboxProps> = ({
<input
{...props}
disabled={disabled}
className={`rounded-sm border border-tensei-gray-500 ${
checkboxClassName || ''
} ${disabled ? 'opacity-80 cursor-not-allowed' : ''}`}
className={`rounded-sm border border-tensei-gray-500 ${checkboxClassName ||
''} ${disabled ? 'opacity-80 cursor-not-allowed' : ''}`}
/>

{error ? (
Expand Down
5 changes: 2 additions & 3 deletions packages/components/src/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ const Modal: React.FC<ModalProps> = ({
aria-labelledby="modal-headline"
className={`inline-block bg-white rounded-lg ${
noPadding ? '' : 'px-4 pt-5 pb-4 sm:p-6'
} text-left overflow-visible shadow-xl transform transition-all w-full ${
className || ''
}`}
} text-left overflow-visible shadow-xl transform transition-all w-full ${className ||
''}`}
>
{title ? (
<div className="w-full flex py-5 px-8 justify-between border-b border-tensei-gray-500">
Expand Down
1 change: 0 additions & 1 deletion packages/core/Tensei.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ export class Tensei implements TenseiContract {
const path = route.config.path.startsWith('/')
? route.config.path
: `/${route.config.path}`

;(this.app as any)[route.config.type.toLowerCase()](
path,

Expand Down
Loading

0 comments on commit 8f30635

Please sign in to comment.