Skip to content

Commit

Permalink
feat(server): add prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed May 6, 2020
1 parent e9b79d7 commit 9c4240b
Show file tree
Hide file tree
Showing 26 changed files with 1,137 additions and 15 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
],
"scripts": {
"dev:aspida": "npm run build:aspida && cd packages/aspida && node bin/index.js --build",
"dev:server": "npm run build:server && cd packages/aspida-server && node bin/index.js --build",
"dev:openapi": "npm run build:openapi && cd packages/openapi2aspida && node samples/rimraf.js && node bin/index.js --build",
"dev:mock": "npm run build:mock && cd packages/aspida-mock && aspida -b && node bin/index.js --build",
"dev:path": "npm run build:path && cd packages/pathpida && node bin/index.js --build",
"build:aspida": "npm run rimraf -- aspida && cd packages/aspida && tsc --project tsconfig.build.json",
"build:server": "npm run rimraf -- aspida-server && cd packages/aspida-server && tsc --project tsconfig.build.json",
"build:axios": "npm run rimraf -- aspida-axios && cd packages/aspida-axios && tsc --project tsconfig.build.json",
"build:fetch": "npm run rimraf -- aspida-fetch && cd packages/aspida-fetch && tsc --project tsconfig.build.json",
"build:node-fetch": "npm run rimraf -- aspida-node-fetch && cd packages/aspida-node-fetch && tsc --project tsconfig.build.json",
Expand All @@ -25,7 +27,7 @@
"lint:fix": "npm run lint -- --fix",
"test": "jest",
"test:watch": "npm test -- --watch",
"typecheck": "npm run build:aspida && npm run build:mock && npm run build:axios && tsc --noEmit"
"typecheck": "npm run build:aspida && npm run build:mock && npm run build:axios && npm run build:server && tsc --noEmit"
},
"devDependencies": {
"@commitlint/cli": "^8.3.5",
Expand Down
21 changes: 21 additions & 0 deletions packages/aspida-server/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 m-mitsuhide <m.mitsuhide@amatelus.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
25 changes: 25 additions & 0 deletions packages/aspida-server/apis/$controllers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-disable */
import controller0 from './@controller'
import controller1 from './user/@controller'
import controller2 from './user/_userId@number/@controller'
import middleware0 from './@middleware'

export default {
name: '/',
controller: controller0,
middleware: middleware0,
children: {
names: [
{
name: '/user',
controller: controller1,
children: {
value: {
name: '/_userId@number',
controller: controller2
}
}
}
]
}
}
14 changes: 14 additions & 0 deletions packages/aspida-server/apis/$server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-disable */
import express from 'express'
import helmet from 'helmet'
import cors from 'cors'
import { createRouter } from 'aspida-server'
import controllers from './$controllers'

express()
.use(helmet())
.use(cors())
.use(express.json())
.use(express.urlencoded({ extended: true }))
.use(createRouter(controllers))
.listen(10000)
9 changes: 9 additions & 0 deletions packages/aspida-server/apis/@controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createController } from 'aspida-server'
import { Methods } from '.'

export default createController<Methods>({
get: v => {
return new Promise(resolve => resolve({ status: 200, resBody: v.query }))
},
post: async () => ({ status: 200, resBody: { id: 1 } })
})
3 changes: 3 additions & 0 deletions packages/aspida-server/apis/@middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createMiddleware } from 'aspida-server'

export default createMiddleware([])
26 changes: 26 additions & 0 deletions packages/aspida-server/apis/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export type Methods = {
get: {
query: {
id: number
}

resBody: { id: number }
}

post: {
query: {
id: number
}

reqFormat: FormData

reqBody: {
name: string
file: File
}

resBody: {
id: number
}
}
}
6 changes: 6 additions & 0 deletions packages/aspida-server/apis/user/$values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* eslint-disable */
import { User } from './@user'

export type Values = {
user: User
}
11 changes: 11 additions & 0 deletions packages/aspida-server/apis/user/@controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { createController } from 'aspida-server'
import { Methods } from '.'

export default createController<Methods>([
(req, res, next) => {
next()
},
{
get: async () => ({ status: 200, resBody: [{ id: 1, name: 'aa' }] })
}
])
5 changes: 5 additions & 0 deletions packages/aspida-server/apis/user/@user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type User = {
id: number
name: string
role: 'admin' | 'user'
}
9 changes: 9 additions & 0 deletions packages/aspida-server/apis/user/_userId@number/$values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable */
import { User } from './../@user'

export type Values = {
params: {
userId: number
}
user: User
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createController } from 'aspida-server'
import { Methods } from '.'
import { Values } from './$values'

export default createController<Methods, Values>({
get: async ({ params }) => ({ status: 200, resBody: { id: params.userId, name: 'bbb' } })
})
7 changes: 7 additions & 0 deletions packages/aspida-server/apis/user/_userId@number/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { User } from '..'

export type Methods = {
get: {
resBody: User
}
}
10 changes: 10 additions & 0 deletions packages/aspida-server/apis/user/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type User = {
id: number
name: string
}

export type Methods = {
get: {
resBody: User[]
}
}
1 change: 1 addition & 0 deletions packages/aspida-server/aspida.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { server: { port: 10000 } }
2 changes: 2 additions & 0 deletions packages/aspida-server/bin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
require('../dist/cli').run(process.argv.slice(2))
32 changes: 32 additions & 0 deletions packages/aspida-server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "aspida-server",
"version": "0.0.0",
"description": "TypeScript friendly RESTful API dedicated server",
"author": "m-mitsuhide <m.mitsuhide@amatelus.com>",
"license": "MIT",
"main": "dist/index.js",
"bin": "bin/index.js",
"homepage": "https://github.com/aspidajs/aspida/tree/master/packages/aspida-server#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/aspidajs/aspida.git"
},
"bugs": {
"url": "https://github.com/aspidajs/aspida/issues"
},
"files": [
"dist"
],
"keywords": [
"aspida",
"server"
],
"dependencies": {
"@types/cors": "^2.8.6",
"@types/express": "^4.17.6",
"@types/helmet": "^0.0.46",
"cors": "^2.8.5",
"express": "^4.17.1",
"helmet": "^3.22.0"
}
}
53 changes: 53 additions & 0 deletions packages/aspida-server/src/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Config } from './getConfig'
import build, { Template } from './buildRouteFile'
import { Command } from 'aspida/dist/cli/command'

export class CommandToBuild implements Command {
static getFactory(configs: Config[], io: BuildIO) {
return {
create(command: BuildCommand): Command {
return new CommandToBuild(command, configs, io)
}
}
}

// eslint-disable-next-line no-useless-constructor
private constructor(
private readonly command: BuildCommand,
private readonly configs: Config[],
private readonly io: BuildIO
) {}

exec() {
this.configs.forEach(config => {
this.command.run(config, this.io)
})
}
}

type BuildCommand = {
run(config: Config, io: BuildIO): void
}

export type BuildIO = {
write(template: Template): void
watch(input: string, callback: () => void): void
}

export class Build implements BuildCommand {
run(config: Config, io: BuildIO) {
build(config).forEach(t => {
io.write(t)
})
}
}

export class Watch implements BuildCommand {
// eslint-disable-next-line no-useless-constructor
constructor(private readonly build = new Build()) {}

run(config: Config, io: BuildIO) {
this.build.run(config, io)
io.watch(config.input, () => this.build.run(config, io))
}
}
33 changes: 33 additions & 0 deletions packages/aspida-server/src/buildRouteFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import path from 'path'
import { Config } from './getConfig'
import createRouteString from './createRouteString'

export type Template = {
filePath: string
text: string
}

export default ({ input, port }: Config): Template[] => [
{
text: createRouteString(input),
filePath: path.posix.join(input, `$controllers.ts`)
},
{
text: `/* eslint-disable */
import express from 'express'
import helmet from 'helmet'
import cors from 'cors'
import { createRouter } from 'aspida-server'
import controllers from './$controllers'
express()
.use(helmet())
.use(cors())
.use(express.json())
.use(express.urlencoded({ extended: true }))
.use(createRouter(controllers))
.listen(${port})
`,
filePath: path.posix.join(input, `$server.ts`)
}
]
26 changes: 26 additions & 0 deletions packages/aspida-server/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import minimist from 'minimist'
import getConfig, { Config } from './getConfig'
import write from './writeRouteFile'
import watch from 'aspida/dist/cli/watchInputDir'
import { options } from 'aspida/dist/cli'
import { Build, Watch, CommandToBuild } from './build'
import { Command, nullCommand } from 'aspida/dist/cli/command'
import { version as versionCommand } from 'aspida/dist/cli/version'

const getBuildCommandFactory = (configs: Config[]) =>
CommandToBuild.getFactory(configs, { write, watch })

export const run = (args: string[]) => {
const argv = minimist(args, options)

const commands: Command[] = [
argv.version !== undefined ? versionCommand : nullCommand,
argv.build !== undefined || argv.watch !== undefined
? getBuildCommandFactory(getConfig(argv.config)).create(
argv.watch !== undefined ? new Watch() : new Build()
)
: nullCommand
]

commands.forEach(c => c.exec())
}

0 comments on commit 9c4240b

Please sign in to comment.