diff --git a/packages/frourio/server/api/$values.ts b/packages/frourio/server/api/$values.ts new file mode 100644 index 0000000..5ed7cd2 --- /dev/null +++ b/packages/frourio/server/api/$values.ts @@ -0,0 +1,2 @@ +/* eslint-disable */ +export type Values = {} diff --git a/packages/frourio/server/api/@controller.ts b/packages/frourio/server/api/@controller.ts index bbce02e..ae5d880 100644 --- a/packages/frourio/server/api/@controller.ts +++ b/packages/frourio/server/api/@controller.ts @@ -1,4 +1,5 @@ import { createController, createMiddleware } from 'frourio' +import { Values } from './$values' import { Methods } from './' export const middleware = createMiddleware((req, res, next) => { @@ -6,7 +7,7 @@ export const middleware = createMiddleware((req, res, next) => { next() }) -export default createController({ +export default createController({ get: async v => { return await { status: 200, body: { id: +(v.query?.id || 0) } } }, diff --git a/packages/frourio/server/api/texts/$values.ts b/packages/frourio/server/api/texts/$values.ts new file mode 100644 index 0000000..5ed7cd2 --- /dev/null +++ b/packages/frourio/server/api/texts/$values.ts @@ -0,0 +1,2 @@ +/* eslint-disable */ +export type Values = {} diff --git a/packages/frourio/server/api/texts/@controller.ts b/packages/frourio/server/api/texts/@controller.ts index e805e13..ea3bbd1 100644 --- a/packages/frourio/server/api/texts/@controller.ts +++ b/packages/frourio/server/api/texts/@controller.ts @@ -1,7 +1,8 @@ import { createController } from 'frourio' +import { Values } from './$values' import { Methods } from './' // @ts-expect-error -export default createController({ +export default createController({ get: ({ query }) => ({ status: 200, body: query.val }) }) diff --git a/packages/frourio/server/api/texts/sample/$values.ts b/packages/frourio/server/api/texts/sample/$values.ts new file mode 100644 index 0000000..5ed7cd2 --- /dev/null +++ b/packages/frourio/server/api/texts/sample/$values.ts @@ -0,0 +1,2 @@ +/* eslint-disable */ +export type Values = {} diff --git a/packages/frourio/server/api/texts/sample/@controller.ts b/packages/frourio/server/api/texts/sample/@controller.ts index f69fc8f..90f33a6 100644 --- a/packages/frourio/server/api/texts/sample/@controller.ts +++ b/packages/frourio/server/api/texts/sample/@controller.ts @@ -1,7 +1,8 @@ import { createController } from 'frourio' +import { Values } from './$values' import { Methods } from './' -export default createController({ +export default createController({ // @ts-expect-error get: ({ query }) => ({ status: 200, body: query.val }), put: ({ body }) => ({ status: 200, body }) diff --git a/packages/frourio/server/api/users/@controller.ts b/packages/frourio/server/api/users/@controller.ts index c625a47..18e73c8 100644 --- a/packages/frourio/server/api/users/@controller.ts +++ b/packages/frourio/server/api/users/@controller.ts @@ -1,4 +1,5 @@ import { createController, createMiddleware } from 'frourio' +import { Values } from './$values' import { Methods } from './' const middleware = createMiddleware([ @@ -10,7 +11,7 @@ const middleware = createMiddleware([ export { middleware } -export default createController({ +export default createController({ get: async () => ({ status: 200, body: [{ id: 1, name: 'aa' }] }), post: () => ({ status: 204 }) }) diff --git a/packages/frourio/server/api/users/_userId@number/@controller.ts b/packages/frourio/server/api/users/_userId@number/@controller.ts index 4f79e33..a52bf42 100644 --- a/packages/frourio/server/api/users/_userId@number/@controller.ts +++ b/packages/frourio/server/api/users/_userId@number/@controller.ts @@ -1,6 +1,6 @@ import { createController } from 'frourio' -import { Methods } from './' import { Values } from './$values' +import { Methods } from './' export default createController({ get: ({ params }) => ({ status: 200, body: { id: params.userId, name: 'bbb' } }) diff --git a/packages/frourio/src/createControllersText.ts b/packages/frourio/src/createControllersText.ts index e931d11..646d3f0 100644 --- a/packages/frourio/src/createControllersText.ts +++ b/packages/frourio/src/createControllersText.ts @@ -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') diff --git a/packages/frourio/src/createDefaultFilesIfNotExists.ts b/packages/frourio/src/createDefaultFilesIfNotExists.ts index a3be3b6..326434c 100644 --- a/packages/frourio/src/createDefaultFilesIfNotExists.ts +++ b/packages/frourio/src/createDefaultFilesIfNotExists.ts @@ -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') @@ -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({})\n`, +import { Values } from './$values' +import { Methods } from './' + +export default createController({})\n`, 'utf8' ) } diff --git a/packages/frourio/src/index.ts b/packages/frourio/src/index.ts index 69eb306..9f70199 100644 --- a/packages/frourio/src/index.ts +++ b/packages/frourio/src/index.ts @@ -110,7 +110,7 @@ export type ServerMethods = { ) => ServerResponse | Promise> } -export const createController = ( +export const createController = ( methods: ServerMethods ) => methods