Skip to content

Commit

Permalink
feat: generate $values.ts into all dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Jun 13, 2020
1 parent 1e97a44 commit f5ed392
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 24 deletions.
2 changes: 2 additions & 0 deletions packages/frourio/server/api/$values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable */
export type Values = {}
3 changes: 2 additions & 1 deletion packages/frourio/server/api/@controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { createController, createMiddleware } from 'frourio'
import { Values } from './$values'
import { Methods } from './'

export const middleware = createMiddleware((req, res, next) => {
console.log('Controller level middleware:', req.path)
next()
})

export default createController<Methods>({
export default createController<Methods, Values>({
get: async v => {
return await { status: 200, body: { id: +(v.query?.id || 0) } }
},
Expand Down
2 changes: 2 additions & 0 deletions packages/frourio/server/api/texts/$values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable */
export type Values = {}
3 changes: 2 additions & 1 deletion packages/frourio/server/api/texts/@controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createController } from 'frourio'
import { Values } from './$values'
import { Methods } from './'

// @ts-expect-error
export default createController<Methods>({
export default createController<Methods, Values>({
get: ({ query }) => ({ status: 200, body: query.val })
})
2 changes: 2 additions & 0 deletions packages/frourio/server/api/texts/sample/$values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable */
export type Values = {}
3 changes: 2 additions & 1 deletion packages/frourio/server/api/texts/sample/@controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createController } from 'frourio'
import { Values } from './$values'
import { Methods } from './'

export default createController<Methods>({
export default createController<Methods, Values>({
// @ts-expect-error
get: ({ query }) => ({ status: 200, body: query.val }),
put: ({ body }) => ({ status: 200, body })
Expand Down
3 changes: 2 additions & 1 deletion packages/frourio/server/api/users/@controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createController, createMiddleware } from 'frourio'
import { Values } from './$values'
import { Methods } from './'

const middleware = createMiddleware([
Expand All @@ -10,7 +11,7 @@ const middleware = createMiddleware([

export { middleware }

export default createController<Methods>({
export default createController<Methods, Values>({
get: async () => ({ status: 200, body: [{ id: 1, name: 'aa' }] }),
post: () => ({ status: 204 })
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createController } from 'frourio'
import { Methods } from './'
import { Values } from './$values'
import { Methods } from './'

export default createController<Methods, Values>({
get: ({ params }) => ({ status: 200, body: { id: params.userId, name: 'bbb' } })
Expand Down
28 changes: 13 additions & 15 deletions packages/frourio/src/createControllersText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,21 @@ export default (inputDir: string) => {
const valuesPath = path.join(input, '$values.ts')
const hasValuesFile = !!(params.length || userPath)

if (hasValuesFile) {
const text = `/* eslint-disable */\n${
userPath ? `import { User } from '${userPath}'\n\n` : ''
}export type Values = {${
params.length
? `\n params: {\n${params.map(v => ` ${v[0]}: ${v[1]}`).join('\n')}\n }`
: ''
}${userPath ? '\n user: User' : ''}\n}\n`

if (!fs.existsSync(valuesPath) || fs.readFileSync(valuesPath, 'utf8') !== text) {
fs.writeFileSync(valuesPath, text, 'utf8')
}
} else if (fs.existsSync(valuesPath)) {
fs.unlinkSync(valuesPath)
const text = hasValuesFile
? `/* eslint-disable */\n${
userPath ? `import { User } from '${userPath}'\n\n` : ''
}export type Values = {${
params.length
? `\n params: {\n${params.map(v => ` ${v[0]}: ${v[1]}`).join('\n')}\n }`
: ''
}${userPath ? '\n user: User' : ''}\n}\n`
: '/* eslint-disable */\nexport type Values = {}\n'

if (!fs.existsSync(valuesPath) || fs.readFileSync(valuesPath, 'utf8') !== text) {
fs.writeFileSync(valuesPath, text, 'utf8')
}

createDefaultFiles(input, hasValuesFile)
createDefaultFiles(input)

const validatorPrefix = 'Valid'
const methods = parseInterface(fs.readFileSync(path.join(input, 'index.ts'), 'utf8'), 'Methods')
Expand Down
8 changes: 5 additions & 3 deletions packages/frourio/src/createDefaultFilesIfNotExists.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs'
import path from 'path'

export default (dir: string, hasValuesFile: boolean) => {
export default (dir: string) => {
const indexFilePath = path.join(dir, 'index.ts')
if (!fs.existsSync(indexFilePath)) {
fs.writeFileSync(indexFilePath, 'export type Methods = {}\n', 'utf8')
Expand All @@ -12,8 +12,10 @@ export default (dir: string, hasValuesFile: boolean) => {
fs.writeFileSync(
controllerFilePath,
`import { createController } from 'frourio'
${hasValuesFile ? "import { Values } from './$values'\n" : ''}import { Methods } from './'\n
export default createController<Methods${hasValuesFile ? ', Values' : ''}>({})\n`,
import { Values } from './$values'
import { Methods } from './'
export default createController<Methods, Values>({})\n`,
'utf8'
)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/frourio/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export type ServerMethods<T extends AspidaMethods, U extends ServerValues> = {
) => ServerResponse<T[K]> | Promise<ServerResponse<T[K]>>
}

export const createController = <T extends AspidaMethods, U extends ServerValues = {}>(
export const createController = <T extends AspidaMethods, U extends ServerValues>(
methods: ServerMethods<T, U>
) => methods

Expand Down

0 comments on commit f5ed392

Please sign in to comment.