-
Notifications
You must be signed in to change notification settings - Fork 4
TypeScript in T Koa
Bd999 edited this page Apr 17, 2019
·
2 revisions
In order to use typescript famous type checking, you need to define some interfaces:
import tKoa = require('tkoa');
// defined interface
interface ctx {
response: {
end: Function
}
}
const app = new tKoa();
// response
app.use((ctx: ctx) => {
ctx.response.end('Hello T-koa!');
});
app.listen(3000);
Or you can write like this:
import tKoa = require('tkoa');
const app = new tKoa();
// response and defined interface
app.use((ctx: {response: {end: Function}}) => {
ctx.response.end('Hello T-koa!');
});
app.listen(3000);
If you don't need a type checking system, you can also use the any
type directly (although we don't recommend it):
import tKoa = require('tkoa');
const app = new tKoa();
// The any type is used here
app.use((ctx: any) => {
ctx.response.end('Hello T-koa!');
});
app.listen(3000);
If you need more detailed information, please visit typescript official website
Because t-koa is based on TypeScript, you need to know the types of certain values to work better:
- ctx.req [object]
- ctx.res [object]
res.statusCode [number]
res.writeHead()
res.write()
res.end()
- ctx.request [object]
- ctx.response [object]
- ctx.state [object]
- ctx.app [app(object)]
- ctx.app.emit [function]
- ctx.cookies.get(name,[options])
- ctx.cookies.set(name, value, [options])
- ctx.throw([status], [msg], [properties])
- ctx.assert(value, [status], [msg], [properties])
- ctx.respond [boolean]
The following accessors and alias Request equivalents:
- ctx.header [object]
- ctx.headers [object]
- ctx.method [string]
- ctx.method=
- ctx.url [string]
- ctx.url=
- ctx.originalUrl [string]
- ctx.origin [string]
- ctx.href [string]
- ctx.path [string]
- ctx.path=
- ctx.query [object]
- ctx.query=
- ctx.querystring [string]
- ctx.querystring=
- ctx.host [string]
- ctx.hostname [string]
- ctx.fresh [boolean]
- ctx.stale [boolean]
- ctx.socket [object]
- ctx.protocol [string]
- ctx.secure [boolean]
- ctx.ip [string]
- ctx.ips [object]
- ctx.subdomains [object]
- ctx.is(types...)
- ctx.accepts(types)
- ctx.acceptsEncodings(types)
- ctx.acceptsCharsets(charsets)
- ctx.acceptsLanguages(types)
- ctx.get(field)
The following accessors and alias Response equivalents:
- ctx.body
- ctx.body=
string written
Buffer written
Stream piped
Object || Array json-stringified
null no content response
- ctx.status [number]
- ctx.status=
100 "continue"
101 "switching protocols"
102 "processing"
200 "ok"
201 "created"
202 "accepted"
203 "non-authoritative information"
204 "no content"
205 "reset content"
206 "partial content"
207 "multi-status"
208 "already reported"
226 "im used"
300 "multiple choices"
301 "moved permanently"
302 "found"
303 "see other"
304 "not modified"
305 "use proxy"
307 "temporary redirect"
308 "permanent redirect"
400 "bad request"
401 "unauthorized"
402 "payment required"
403 "forbidden"
404 "not found"
405 "method not allowed"
406 "not acceptable"
407 "proxy authentication required"
408 "request timeout"
409 "conflict"
410 "gone"
411 "length required"
412 "precondition failed"
413 "payload too large"
414 "uri too long"
415 "unsupported media type"
416 "range not satisfiable"
417 "expectation failed"
418 "I'm a teapot"
422 "unprocessable entity"
423 "locked"
424 "failed dependency"
426 "upgrade required"
428 "precondition required"
429 "too many requests"
431 "request header fields too large"
500 "internal server error"
501 "not implemented"
502 "bad gateway"
503 "service unavailable"
504 "gateway timeout"
505 "http version not supported"
506 "variant also negotiates"
507 "insufficient storage"
508 "loop detected"
510 "not extended"
511 "network authentication required"
- ctx.message [string]
- ctx.message=
- ctx.length [number]
- ctx.length=
- ctx.type [string]
- ctx.type=
ctx.type = 'text/plain; charset=utf-8';
ctx.type = 'image/png';
ctx.type = '.png';
ctx.type = 'png';
- ctx.headerSent
- ctx.redirect(url, [alt])
- ctx.attachment([filename], [options])
- ctx.set(field, value)
- ctx.append(field, value)
- ctx.remove(field)
- ctx.lastModified=
- ctx.etag=