-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(types): typescript support (#28)
- Loading branch information
1 parent
6be344f
commit 33c6b52
Showing
10 changed files
with
275 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import 'egg'; | ||
import { Readable } from 'stream'; | ||
interface EggFile { | ||
field: string; | ||
filename: string; | ||
encoding: string; | ||
mime: string; | ||
filepath: string; | ||
} | ||
|
||
interface MultipartOptions { | ||
requireFile?: boolean; // required file submit, default is true | ||
defCharset?: string; | ||
limits?: { | ||
fieldNameSize?: number; | ||
fieldSize?: number; | ||
fields?: number; | ||
fileSize?: number; | ||
files?: number; | ||
parts?: number; | ||
headerPairs?: number; | ||
}; | ||
checkFile?( | ||
fieldname: string, | ||
file: any, | ||
filename: string, | ||
encoding: string, | ||
mimetype: string | ||
): void | Error; | ||
} | ||
|
||
interface MultipartFileStream extends Readable { | ||
fields: any; | ||
|
||
filename: string; | ||
|
||
fieldname: string; | ||
|
||
mime: string; | ||
|
||
mimeType: string; | ||
|
||
transferEncoding: string; | ||
|
||
encoding: string; | ||
|
||
truncated: boolean; | ||
} | ||
|
||
interface ScheduleOptions { | ||
type?: string; | ||
cron?: string; | ||
cronOptions?: { | ||
tz?: string; | ||
utc?: boolean; | ||
iterator?: boolean; | ||
currentDate?: string|number|Date; | ||
endDate?: string|number|Date; | ||
}; | ||
interval?: number|string; | ||
immediate?: boolean; | ||
disable?: boolean; | ||
env?: string[]; | ||
} | ||
|
||
declare module 'egg' { | ||
interface Context { | ||
/** | ||
* clean up request tmp files helper | ||
* @param {EggFile[]} files file paths need to clenup, default is `ctx.request.files`. | ||
* @return {Promise<void>} | ||
*/ | ||
cleanupRequestFiles(files?: EggFile[]): Promise<void>; | ||
|
||
/** | ||
* create multipart.parts instance, to get separated files. | ||
* @param {MultipartOptions} options | ||
* @return {Function} return a function which return a Promise | ||
*/ | ||
multipart(options?: MultipartOptions): (fn?: Function) => Promise<any>; | ||
|
||
/** | ||
* get upload file stream | ||
* @param {MultipartOptions} options | ||
* @return {Promise<MultipartFileStream>} | ||
*/ | ||
getFileStream(options?: MultipartOptions): Promise<MultipartFileStream> | ||
|
||
} | ||
|
||
interface Request { | ||
/** | ||
* Files Object Array | ||
*/ | ||
files: EggFile[]; | ||
} | ||
|
||
interface EggAppConfig { | ||
multipart: { | ||
mode?: string; | ||
autoFields?: boolean; | ||
defaultCharset?: string; | ||
fieldNameSize?: number; | ||
fieldSize?: string|number; | ||
fields?: number; | ||
fileSize?: string|number; | ||
files?: number; | ||
whitelist?: () => string[]|string[]; | ||
fileExtensions?: string[]; | ||
tmpdir?: string; | ||
cleanSchedule?: ScheduleOptions; | ||
} | ||
} | ||
} |
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 @@ | ||
*.js |
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,13 @@ | ||
import { Controller } from 'egg'; | ||
|
||
class HomeController extends Controller { | ||
async index() { | ||
const { ctx } = this; | ||
ctx.body = { | ||
body: ctx.request.body, | ||
files: ctx.request.files, | ||
}; | ||
} | ||
} | ||
|
||
export default HomeController; |
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 @@ | ||
import { Application } from 'egg'; | ||
|
||
export default (app: Application) => { | ||
const { controller } = app; | ||
app.post('/', controller.home.index); | ||
} |
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,15 @@ | ||
import { EggAppInfo, EggAppConfig, PowerPartial } from 'egg'; | ||
|
||
export default (appInfo: EggAppInfo) => { | ||
const config = {} as PowerPartial<EggAppConfig>; | ||
|
||
config.keys = 'multipart-ts-test'; | ||
|
||
config.appInfo = appInfo; | ||
|
||
config.multipart = { | ||
mode: 'file', | ||
}; | ||
|
||
return config; | ||
} |
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 @@ | ||
{ | ||
"name": "multipart-typescript-demo" | ||
} |
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,24 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true, | ||
"target": "es2017", | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"noImplicitAny": false, | ||
"allowSyntheticDefaultImports": true, | ||
"experimentalDecorators": true, | ||
"emitDecoratorMetadata": true, | ||
"pretty": true, | ||
"noEmitOnError": false, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"allowUnreachableCode": false, | ||
"allowUnusedLabels": false, | ||
"strictPropertyInitialization": false, | ||
"noFallthroughCasesInSwitch": true, | ||
"baseUrl": "./", | ||
"paths": { | ||
"egg-multipart": ["../../../../"] | ||
} | ||
} | ||
} |
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,8 @@ | ||
import 'egg'; | ||
import HomeController from '../app/controller/home'; | ||
|
||
declare module 'egg' { | ||
interface IController { | ||
home: HomeController; | ||
} | ||
} |
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,85 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const formstream = require('formstream'); | ||
const urllib = require('urllib'); | ||
const path = require('path'); | ||
const mock = require('egg-mock'); | ||
const rimraf = require('mz-modules/rimraf'); | ||
const coffee = require('coffee'); | ||
|
||
describe('test/ts.test.js', () => { | ||
it('should compile ts without err', () => { | ||
return coffee.fork( | ||
require.resolve('typescript/bin/tsc'), | ||
[ '-p', path.resolve(__dirname, './fixtures/apps/ts/tsconfig.json') ] | ||
) | ||
.debug() | ||
.expect('code', 0) | ||
.end(); | ||
}); | ||
}); | ||
|
||
describe('test/ts.test.js', () => { | ||
let app; | ||
let server; | ||
let host; | ||
before(() => { | ||
app = mock.app({ | ||
baseDir: 'apps/ts', | ||
}); | ||
return app.ready(); | ||
}); | ||
before(() => { | ||
server = app.listen(); | ||
host = 'http://127.0.0.1:' + server.address().port; | ||
}); | ||
after(() => { | ||
return rimraf(app.config.multipart.tmpdir); | ||
}); | ||
after(() => app.close()); | ||
after(() => server.close()); | ||
beforeEach(() => app.mockCsrf()); | ||
afterEach(mock.restore); | ||
|
||
it('ts should run without err', async () => { | ||
const form = formstream(); | ||
form.field('foo', 'bar').field('luckyscript', 'egg'); | ||
form.file('file1', __filename, 'foooooooo.js'); | ||
form.file('file2', __filename); | ||
// will ignore empty file | ||
form.buffer('file3', Buffer.from(''), '', 'application/octet-stream'); | ||
form.file('bigfile', path.join(__dirname, 'fixtures', 'bigfile.js')); | ||
// other form fields | ||
form.field('work', 'with Node.js'); | ||
|
||
const headers = form.headers(); | ||
const res = await urllib.request(host + '/', { | ||
method: 'POST', | ||
headers, | ||
stream: form, | ||
}); | ||
|
||
assert(res.status === 200); | ||
const data = JSON.parse(res.data); | ||
assert.deepStrictEqual(data.body, { foo: 'bar', luckyscript: 'egg', work: 'with Node.js' }); | ||
assert(data.files.length === 3); | ||
assert(data.files[0].field === 'file1'); | ||
assert(data.files[0].filename === 'foooooooo.js'); | ||
assert(data.files[0].encoding === '7bit'); | ||
assert(data.files[0].mime === 'application/javascript'); | ||
assert(data.files[0].filepath.startsWith(app.config.multipart.tmpdir)); | ||
|
||
assert(data.files[1].field === 'file2'); | ||
assert(data.files[1].filename === 'ts.test.js'); | ||
assert(data.files[1].encoding === '7bit'); | ||
assert(data.files[1].mime === 'application/javascript'); | ||
assert(data.files[1].filepath.startsWith(app.config.multipart.tmpdir)); | ||
|
||
assert(data.files[2].field === 'bigfile'); | ||
assert(data.files[2].filename === 'bigfile.js'); | ||
assert(data.files[2].encoding === '7bit'); | ||
assert(data.files[2].mime === 'application/javascript'); | ||
assert(data.files[2].filepath.startsWith(app.config.multipart.tmpdir)); | ||
}); | ||
}); |