Skip to content

Commit

Permalink
Merge pull request #11 from frouriojs/develop
Browse files Browse the repository at this point in the history
chore(release): 0.5.0
  • Loading branch information
solufa authored Jun 12, 2020
2 parents 5b180cf + 73a8be6 commit 443ff0d
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 103 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [0.5.0](https://github.com/frouriojs/frourio/compare/v0.4.0...v0.5.0) (2020-06-12)


### Features

* support migration direcroty ([1a3da5e](https://github.com/frouriojs/frourio/commit/1a3da5eb594a3824a902ad8fa190db22d9d91354))

## [0.4.0](https://github.com/frouriojs/frourio/compare/v0.3.1...v0.4.0) (2020-06-11)


Expand Down
9 changes: 4 additions & 5 deletions packages/frourio/__test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import FormData from 'form-data'
import axios from 'axios'
import aspida from '@aspida/axios'
import api from '../server/api/$api'
import { app } from '../server/$app'
import config from '../server/frourio.config'
import { run } from '../server/$app'

const port = 11111
const baseURL = `http://localhost:${port}${config.basePath ?? ''}`
const baseURL = `http://localhost:${port}`
const client = api(aspida(undefined, { baseURL }))
let server: Server

beforeEach(fn => {
server = app.listen(port, fn)
beforeEach(async () => {
server = (await run({ port, staticDir: 'packages/frourio/public' })).server
})

afterEach(fn => server.close(fn))
Expand Down
2 changes: 1 addition & 1 deletion packages/frourio/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "frourio",
"version": "0.4.0",
"version": "0.5.0",
"description": "Perfectly type-checkable REST framework for TypeScript",
"author": "m-mitsuhide <[email protected]>",
"license": "MIT",
Expand Down
85 changes: 46 additions & 39 deletions packages/frourio/server/$app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
import 'reflect-metadata'
import { tmpdir } from 'os'
import { Server } from 'http'
import express from 'express'
import express, { Express } from 'express'
import multer from 'multer'
import helmet from 'helmet'
import cors from 'cors'
import { createConnection } from 'typeorm'
import { createRouter } from 'frourio'
import config from './frourio.config'
import { createConnection, Connection } from 'typeorm'
import { createRouter, Config } from 'frourio'
import { Task as Entity0 } from './entity/Task'
import { TaskSubscriber as Subscriber0 } from './subscriber/TaskSubscriber'
import * as Types from './types'
Expand Down Expand Up @@ -70,53 +69,61 @@ export const controllers = {
}
}

export const router = createRouter(
controllers,
multer({
dest: config.uploader?.dest ?? tmpdir(),
limits: { fileSize: config.uploader?.size ?? 1024 ** 3 }
}).any()
)

export const app = express()
export const entities = [Entity0]
export const migrations = []
export const subscribers = [Subscriber0]
export const run = async (config: Config) => {
const app = express()
const router = createRouter(
controllers,
multer({
dest: config.uploader?.dest ?? tmpdir(),
limits: { fileSize: config.uploader?.size ?? 1024 ** 3 }
}).any()
)

if (config.helmet) app.use(helmet())
if (config.cors) app.use(cors())
if (config.helmet) app.use(helmet())
if (config.cors) app.use(cors())

app.use((req, res, next) => {
express.json()(req, res, err => {
if (err) return res.sendStatus(400)
app.use((req, res, next) => {
express.json()(req, res, err => {
if (err) return res.sendStatus(400)

next()
next()
})
})
})

if (config.basePath && config.basePath !== '/') {
app.use(config.basePath.startsWith('/') ? config.basePath : `/${config.basePath}`, router)
} else {
app.use(router)
}
if (config.basePath && config.basePath !== '/') {
app.use(config.basePath.startsWith('/') ? config.basePath : `/${config.basePath}`, router)
} else {
app.use(router)
}

if (config.staticDir) {
;(Array.isArray(config.staticDir) ? config.staticDir : [config.staticDir]).forEach(dir =>
app.use(express.static(dir))
)
}
if (config.staticDir) {
;(Array.isArray(config.staticDir) ? config.staticDir : [config.staticDir]).forEach(dir =>
app.use(express.static(dir))
)
}

let connection: Connection | null = null

export const run = async (port: number | string = config.port ?? 8080) => {
if (config.typeorm) {
await createConnection({
entities: [Entity0],
subscribers: [Subscriber0],
migrations: ['server/migration/*.js'],
connection = await createConnection({
entities,
migrations,
subscribers,
...config.typeorm
})
}

return new Promise<Server>(resolve => {
const server = app.listen(port, () => {
console.log(`Frourio is running on http://localhost:${port}`)
resolve(server)
return new Promise<{
app: Express
server: Server
connection: typeof connection
}>(resolve => {
const server = app.listen(config.port, () => {
console.log(`Frourio is running on http://localhost:${config.port}`)
resolve({ app, server, connection })
})
})
}
10 changes: 0 additions & 10 deletions packages/frourio/server/frourio.config.ts

This file was deleted.

7 changes: 6 additions & 1 deletion packages/frourio/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { run } from './$app'

run()
run({
port: 10000,
basePath: '/api',
staticDir: 'packages/frourio/public',
cors: true
})
86 changes: 46 additions & 40 deletions packages/frourio/src/buildServerFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,69 @@ export default (input: string): Template => {
import 'reflect-metadata'
import { tmpdir } from 'os'
import { Server } from 'http'
import express from 'express'
import express, { Express } from 'express'
import multer from 'multer'
import helmet from 'helmet'
import cors from 'cors'
import { createConnection } from 'typeorm'
import { createRouter } from 'frourio'
import config from './frourio.config'
${typeormText.imports}
import { createConnection, Connection } from 'typeorm'
import { createRouter, Config } from 'frourio'${typeormText.imports}
${createControllersText(`${input}/api`)}
export const router = createRouter(
controllers,
multer({
dest: config.uploader?.dest ?? tmpdir(),
limits: { fileSize: config.uploader?.size ?? 1024 ** 3 }
}).any()
)
export const app = express()
export const entities = [${typeormText.entities}]
export const migrations = [${typeormText.migrations}]
export const subscribers = [${typeormText.subscribers}]
export const run = async (config: Config) => {
const app = express()
const router = createRouter(
controllers,
multer({
dest: config.uploader?.dest ?? tmpdir(),
limits: { fileSize: config.uploader?.size ?? 1024 ** 3 }
}).any()
)
if (config.helmet) app.use(helmet())
if (config.cors) app.use(cors())
if (config.helmet) app.use(helmet())
if (config.cors) app.use(cors())
app.use((req, res, next) => {
express.json()(req, res, err => {
if (err) return res.sendStatus(400)
app.use((req, res, next) => {
express.json()(req, res, err => {
if (err) return res.sendStatus(400)
next()
next()
})
})
})
if (config.basePath && config.basePath !== '/') {
app.use(config.basePath.startsWith('/') ? config.basePath : \`/\${config.basePath}\`, router)
} else {
app.use(router)
}
if (config.basePath && config.basePath !== '/') {
app.use(config.basePath.startsWith('/') ? config.basePath : \`/\${config.basePath}\`, router)
} else {
app.use(router)
}
if (config.staticDir) {
;(Array.isArray(config.staticDir) ? config.staticDir : [config.staticDir]).forEach(dir =>
app.use(express.static(dir))
)
}
if (config.staticDir) {
;(Array.isArray(config.staticDir) ? config.staticDir : [config.staticDir]).forEach(dir =>
app.use(express.static(dir))
)
}
let connection: Connection | null = null
export const run = async (port: number | string = config.port ?? 8080) => {
if (config.typeorm) {
await createConnection({
entities: ${typeormText.entities},
subscribers: ${typeormText.subscribers},
migrations: ['${input}/migration/*.js'],
connection = await createConnection({
entities,
migrations,
subscribers,
...config.typeorm
})
}
return new Promise<Server>(resolve => {
const server = app.listen(port, () => {
console.log(\`Frourio is running on http://localhost:\${port}\`)
resolve(server)
return new Promise<{
app: Express
server: Server
connection: typeof connection
}>(resolve => {
const server = app.listen(config.port, () => {
console.log(\`Frourio is running on http://localhost:\${config.port}\`)
resolve({ app, server, connection })
})
})
}
Expand Down
23 changes: 17 additions & 6 deletions packages/frourio/src/createTypeormText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,40 @@ import listFiles from './listFiles'

export default (inputDir: string) => {
const entities = fs.existsSync(`${inputDir}/entity`) ? listFiles(`${inputDir}/entity`) : []
const migrations = fs.existsSync(`${inputDir}/migration`)
? listFiles(`${inputDir}/migration`)
: []
const subscribers = fs.existsSync(`${inputDir}/subscriber`)
? listFiles(`${inputDir}/subscriber`)
: []

const imports = `${entities
.map(
(e, i) =>
`import { ${path.basename(e, path.extname(e))} as Entity${i} } from '${e
`\nimport { ${path.basename(e, path.extname(e))} as Entity${i} } from '${e
.replace(inputDir, '.')
.replace('.ts', '')}'`
)
.join('\n')}${entities.length && subscribers.length ? '\n' : ''}${subscribers
.join('')}${migrations
.map((m, i) => {
const names = path.basename(m, path.extname(m)).split('-')
return `\nimport { ${names[1]}${names[0]} as Migration${i} } from '${m
.replace(inputDir, '.')
.replace('.ts', '')}'`
})
.join('')}${subscribers
.map(
(s, i) =>
`import { ${path.basename(s, path.extname(s))} as Subscriber${i} } from '${s
`\nimport { ${path.basename(s, path.extname(s))} as Subscriber${i} } from '${s
.replace(inputDir, '.')
.replace('.ts', '')}'`
)
.join('\n')}`
.join('')}`

return {
imports,
entities: `[${entities.map((_, i) => `Entity${i}`).join(', ')}]`,
subscribers: `[${subscribers.map((_, i) => `Subscriber${i}`).join(', ')}]`
entities: entities.map((_, i) => `Entity${i}`).join(', '),
migrations: migrations.map((_, i) => `Migration${i}`).join(', '),
subscribers: subscribers.map((_, i) => `Subscriber${i}`).join(', ')
}
}
2 changes: 1 addition & 1 deletion packages/frourio/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ConnectionOptions } from 'typeorm'
export * as Validator from 'class-validator'

export type Config = {
port?: number
port: number
basePath?: string
staticDir?: string | string[]
helmet?: boolean
Expand Down

0 comments on commit 443ff0d

Please sign in to comment.