-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
1,137 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { createMiddleware } from 'aspida-server' | ||
|
||
export default createMiddleware([]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }] }) | ||
} | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type User = { | ||
id: number | ||
name: string | ||
role: 'admin' | 'user' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/aspida-server/apis/user/_userId@number/@controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' } }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { User } from '..' | ||
|
||
export type Methods = { | ||
get: { | ||
resBody: User | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = { server: { port: 10000 } } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
Oops, something went wrong.