Skip to content

Commit

Permalink
feat: auto rest ready for use
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Sep 17, 2024
1 parent 76325f7 commit 13b3e14
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/core/src/rest/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import type { UntypedQueryManagerProxy } from '../resource/queryManagerProxy.js'
export async function setupRestApi(mq: MoquerieInstance, expressApp: Application) {
expressApp.use(mq.data.context?.config.rest?.basePath ?? '/rest', async (req, res) => {
try {
const route = req.path.split('/')
const routeType = route[1]
const request = normalizeNodeRequest(req, Request)
const query = Object.fromEntries(new URLSearchParams(request.url.split('?')[1] ?? '').entries())

Expand Down Expand Up @@ -157,6 +155,12 @@ export async function setupRestApi(mq: MoquerieInstance, expressApp: Application
}
}

const route = req.path.split('/')
const routeType = route[1]

// eslint-disable-next-line no-console
console.log(`[Auto REST] ${req.method} ${req.path}`, req.body)

if (!resourceType) {
for (const key in ctx.schema.types) {
const type = ctx.schema.types[key]
Expand Down Expand Up @@ -205,7 +209,7 @@ export async function setupRestApi(mq: MoquerieInstance, expressApp: Application
data = await (ctx.db as UntypedQueryManagerProxy)[resourceType.name].findMany()
}
if (req.method === 'POST') {
data = await (ctx.db as UntypedQueryManagerProxy)[resourceType.name].create({ data: req.body })
data = await (ctx.db as UntypedQueryManagerProxy)[resourceType.name].create(req.body)
}
}
data = await transformResponse(data, {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export async function createServer(mq: MoquerieInstance): Promise<Server> {
const routeInfos: ServerRouteInfo[] = []
const expressApp = express()

expressApp.use(express.json())
expressApp.use(express.urlencoded({ extended: true }))

if (context.config.server?.cors) {
expressApp.use(cors(context.config.server.cors))
}
Expand Down

0 comments on commit 13b3e14

Please sign in to comment.